Changeset 3180
- Timestamp:
- 07/23/13 11:31:04 (11 years ago)
- Location:
- DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/AnnotationDao.java
r3168 r3180 19 19 20 20 import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier; 21 import eu.dasish.annotation.backend.identifiers.NotebookIdentifier; 22 import eu.dasish.annotation.backend.identifiers.UserIdentifier; 21 23 import eu.dasish.annotation.schema.Annotation; 22 24 import eu.dasish.annotation.schema.AnnotationInfo; … … 67 69 68 70 public int deleteAnnotation(Number annotationId) throws SQLException; 69 71 72 73 /** 74 * 75 * @param annotation added to the table with annotations 76 * @return annotationIdentifier of the newly added annotation; returns null if something went wrong and annotation was not added or more than one row in the annotation table was affected 77 */ 78 public AnnotationIdentifier addAnnotation(Annotation annotation); 79 80 70 81 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDao.java
r3168 r3180 28 28 import java.sql.SQLException; 29 29 import java.util.ArrayList; 30 import java.util.HashMap; 30 31 import java.util.List; 32 import java.util.Map; 31 33 import javax.sql.DataSource; 34 import org.springframework.dao.DataAccessException; 32 35 import org.springframework.jdbc.core.RowMapper; 33 36 … … 122 125 } 123 126 }; 124 125 ////////////////////////////////////////////// 126 /* CREATE TABLE annotation ( 127 annotation_id SERIAL UNIQUE NOT NULL, 128 external_id UUID UNIQUE NOT NULL, 129 time_stamp timestamp with time zone default now(), 130 owner_id integer, 131 headline text, 132 body_xml xml */ 133 127 134 128 @Override 135 129 public Annotation getAnnotation(Number annotationID) throws SQLException{ … … 222 216 }; 223 217 224 218 @Override 225 219 public int deleteAnnotation(Number annotationId) throws SQLException{ 226 220 String sqlAnnotation = "DELETE FROM " + annotationTableName + " where "+annotation_id + " = ?"; 227 //String sqSources = "DELETE FROM " + sourceTableName + " where "+ notebook_id +"= ?";228 221 int affectedAnnotations = getSimpleJdbcTemplate().update(sqlAnnotation, annotationId); 229 222 if (affectedAnnotations>1) { … … 234 227 } 235 228 229 230 ////////////////////////////////////////////// 231 /* CREATE TABLE annotation ( 232 annotation_id SERIAL UNIQUE NOT NULL, 233 external_id UUID UNIQUE NOT NULL, 234 time_stamp timestamp with time zone default now(), 235 owner_id integer, 236 headline text, 237 body_xml xml */ 238 239 240 @Override 241 public AnnotationIdentifier addAnnotation(Annotation annotation) { 242 try { 243 AnnotationIdentifier annotationIdentifier = new AnnotationIdentifier(); 244 Map<String, Object> params = new HashMap<String, Object>(); 245 params.put("externalId", annotationIdentifier.toString()); 246 params.put("timeStamp", annotation.getTimeStamp()); 247 params.put("ownerId", annotation.getOwner().getRef()); 248 params.put("headline", annotation.getHeadline()); 249 // may be changed once we elaborate the body 250 params.put("bodyXml", annotation.getBody().getAny().get(0).toString()); 251 String sql = "INSERT INTO "+annotationTableName + "("+external_id+","+ time_stamp+"," + owner_id+","+headline+","+body_xml+" ) VALUES (:externalId, :timeStamp, :ownerId, :headline, :bodyXml)"; 252 final int affectedRows = getSimpleJdbcTemplate().update(sql, params); 253 if (affectedRows == 1) { 254 return annotationIdentifier; 255 } 256 else { 257 // something went wrong 258 return null; 259 } 260 } catch (DataAccessException exception) { 261 throw exception; 262 } 263 } 264 265 236 266 237 267 //////////// helpers /////////////////////// 268 238 269 239 270 ///////////////////////////////////////////////// -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java
r3168 r3180 24 24 import eu.dasish.annotation.schema.ObjectFactory; 25 25 import java.sql.SQLException; 26 import java.util.ArrayList; 27 import javax.ws.rs.Consumes; 26 28 import javax.ws.rs.DELETE; 27 29 import javax.ws.rs.GET; 30 import javax.ws.rs.POST; 28 31 import javax.ws.rs.Path; 29 32 import javax.ws.rs.PathParam; … … 44 47 @Autowired 45 48 private AnnotationDao annotationDao; 49 50 final private int charBufferSize = 128; 46 51 47 52 @GET … … 61 66 return Integer.toString(annotationDao.deleteAnnotation(annotationDao.getAnnotationID(new AnnotationIdentifier(annotationIdentifier)))); 62 67 } 68 69 // TODO: should be returning the envelope!!! 70 @POST 71 @Consumes(MediaType.APPLICATION_XML) 72 @Produces(MediaType.APPLICATION_XML) 73 @Path("") 74 public String createAnnotation(ArrayList<Annotation> annotation) { 75 return "OK"; 76 /*AnnotationIdentifier newAnnotationIdentifier = annotationDao.addAnnotation(annotation); 77 if (newAnnotationIdentifier == null) { 78 return null; 79 } else { 80 return Response.status(Status.OK).entity(new ObjectFactory().createAnnotation(annotation)).build(); 81 }*/ 82 } 83 84 85 /* helper: stolen from StackOverflow 86 private String getBody(HttpServletRequest request) throws IOException{ 87 String body = null; 88 StringBuilder stringBuilder = new StringBuilder(); 89 BufferedReader bufferedReader = null; 90 91 try { 92 InputStream inputStream = request.getInputStream(); 93 if (inputStream != null) { 94 bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 95 char[] charBuffer = new char[charBufferSize]; 96 int bytesRead = -1; 97 while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { 98 stringBuilder.append(charBuffer, 0, bytesRead); 99 } 100 } else { 101 stringBuilder.append(""); 102 } 103 } catch (IOException ex) { 104 throw ex; 105 } finally { 106 if (bufferedReader != null) { 107 try { 108 bufferedReader.close(); 109 } catch (IOException ex) { 110 throw ex; 111 } 112 } 113 } 114 115 body = stringBuilder.toString(); 116 return body; 117 118 }*/ 119 120 63 121 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/webapp/WEB-INF/web.xml
r3034 r3180 63 63 <url-pattern>/webresources/*</url-pattern> 64 64 </servlet-mapping>--> 65 66 65 67 </web-app> -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/TestBackendConstants.java
r3168 r3180 47 47 public static final String _TEST_ANNOT_5_EXT_TO_BE_DELETED = "00000000-0000-0000-0000-000000000025"; 48 48 49 49 50 public static final int _TEST_ANNOT_1_INT = 21; 50 51 public static final int _TEST_ANNOT_2_INT = 22; … … 56 57 public static final String _TEST_ANNOT_2_HEADLINE = "Gaudi"; 57 58 public static final String _TEST_ANNOT_3_HEADLINE = "Art Nuveau"; 59 public static final String _TEST_ANNOT_TO_ADD_HEADLINE = "Annotation to add to test DAO"; 58 60 59 61 public static final int _TEST_ANNOT_1_OWNER = 111; 60 62 public static final int _TEST_ANNOT_2_OWNER = 112; 61 63 public static final int _TEST_ANNOT_3_OWNER = 113; 64 public static final int _TEST_ANNOT_TO_ADD_OWNER = 117; 62 65 63 66 public static final String _TEST_ANNOT_1_BODY = "<html><body>some html 1</body></html>"; 67 public static final String _TEST_ANNOT_TO_ADD_BODY = "<html><body>the stuff to be added</body></html>"; 64 68 65 69 public static final String annotaiontoDeleteInDB="INSERT INTO annotation (annotation_id, owner_id,headline,body_xml, external_id) VALUES (25, 111, 'Annotation to delete','<html><body>some html 4</body></html>', '00000000-0000-0000-0000-000000000025');"; -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/TestInstances.java
r3154 r3180 30 30 31 31 final private Annotation _annotationOne; 32 final private Annotation _annotationToAdd; 32 33 33 34 public TestInstances(){ 34 35 _annotationOne = makeAnnotationOne(); 36 _annotationToAdd = makeAnnotationToAdd(); 37 35 38 } 36 39 37 40 38 41 private Annotation makeAnnotationOne(){ 39 // add the other fields whengetAnnotation is completed 42 Annotation result = makeAnnotation(TestBackendConstants._TEST_ANNOT_1_BODY, TestBackendConstants._TEST_ANNOT_1_HEADLINE, TestBackendConstants._TEST_ANNOT_1_OWNER); 43 return result; 44 } 45 46 private Annotation makeAnnotationToAdd(){ 47 Annotation result = makeAnnotation(TestBackendConstants._TEST_ANNOT_TO_ADD_BODY, TestBackendConstants._TEST_ANNOT_TO_ADD_HEADLINE, TestBackendConstants._TEST_ANNOT_TO_ADD_OWNER); 48 return result; 49 } 50 51 // add the other fields whengetAnnotation is completed 52 private Annotation makeAnnotation(String bodyTxt, String headline, int ownerId){ 40 53 Annotation result = new Annotation(); 41 54 AnnotationBody body = new AnnotationBody(); 42 55 List<Object> bodyContent = body.getAny(); 43 bodyContent.add( TestBackendConstants._TEST_ANNOT_1_BODY);56 bodyContent.add(bodyTxt); 44 57 result.setBody(body); 45 result.setHeadline( TestBackendConstants._TEST_ANNOT_1_HEADLINE);58 result.setHeadline(headline); 46 59 ResourceREF owner = new ResourceREF(); 47 owner.setRef(String.valueOf( TestBackendConstants._TEST_ANNOT_1_OWNER));48 result.setOwner(owner); 60 owner.setRef(String.valueOf(ownerId)); 61 result.setOwner(owner); 49 62 return result; 50 63 } 64 51 65 52 66 public Annotation getAnnotationOne(){ 53 67 return _annotationOne; 54 68 } 69 70 public Annotation getAnnotationToAdd(){ 71 return _annotationToAdd; 72 } 55 73 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDaoTest.java
r3168 r3180 19 19 20 20 import eu.dasish.annotation.backend.TestBackendConstants; 21 import eu.dasish.annotation.backend.TestInstances; 21 22 import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier; 22 import eu.dasish.annotation.backend.identifiers.NotebookIdentifier;23 23 import eu.dasish.annotation.schema.Annotation; 24 24 import eu.dasish.annotation.schema.AnnotationInfo; … … 27 27 import java.util.ArrayList; 28 28 import java.util.List; 29 import java.util.UUID;30 29 import static org.junit.Assert.*; 31 30 import org.junit.Test; … … 45 44 46 45 @Autowired 47 JdbcAnnotationDao jdbcAnnotationDao; 46 JdbcAnnotationDao jdbcAnnotationDao; 47 48 TestInstances testInstances = new TestInstances(); 48 49 49 50 @Test … … 171 172 } 172 173 174 175 /** 176 * Test of addAnnotation method, of class JdbcAnnotationDao. 177 */ 178 @Test 179 public void testAddAnnotation() throws SQLException{ 180 System.out.println("test_addAnnotation"); 181 Annotation annotationToAdd = testInstances.getAnnotationToAdd(); 182 AnnotationIdentifier result = jdbcAnnotationDao.addAnnotation(annotationToAdd); 183 assertFalse(result == null); 184 185 Annotation addedAnnotation = jdbcAnnotationDao.getAnnotation(jdbcAnnotationDao.getAnnotationID(result)); 186 assertEquals(annotationToAdd.getBody().getAny().get(0), addedAnnotation.getBody().getAny().get(0)); 187 assertEquals(annotationToAdd.getHeadline(), addedAnnotation.getHeadline()); 188 assertEquals(annotationToAdd.getOwner().getRef(), addedAnnotation.getOwner().getRef()); 189 190 // try to add an already existing annptation, should produce null 191 //Annotation annotationOne = testInstances.getAnnotationOne(); 192 //AnnotationIdentifier resultOne = jdbcAnnotationDao.addAnnotation(annotationOne); 193 // TODO: why it doesp produce an annotation, it already exists!! 194 //assertTrue(resultOne == null); 195 196 } 173 197 } 174 198 -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/rest/AnnotationResourceTest.java
r3168 r3180 18 18 package eu.dasish.annotation.backend.rest; 19 19 20 import com.sun.jersey.api.client.GenericType; 20 21 import eu.dasish.annotation.backend.TestBackendConstants; 21 22 import eu.dasish.annotation.backend.TestInstances; … … 24 25 import eu.dasish.annotation.schema.Annotation; 25 26 import java.sql.SQLException; 27 import javax.ws.rs.core.Response; 26 28 import javax.xml.bind.JAXBElement; 27 29 import org.jmock.Expectations; … … 33 35 import org.springframework.test.context.ContextConfiguration; 34 36 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 35 37 import java.lang.InstantiationException; 38 import java.util.ArrayList; 39 import java.util.List; 36 40 /** 37 41 * … … 48 52 private AnnotationDao annotationDao; 49 53 @Autowired 50 private AnnotationResource annotationResource; 54 private AnnotationResource annotationResource; 51 55 52 56 public AnnotationResourceTest() { … … 112 116 assertEquals("0", result); 113 117 } 118 119 /** 120 * Test of createAnnotation method, of class AnnotationResource. 121 */ 122 @Test 123 public void testCreateAnnotation() throws SQLException, InstantiationException, IllegalAccessException { 124 System.out.println("test createAnnotation"); 125 final Annotation annotationToAdd = new GenericType<Annotation>(){}.getRawClass().newInstance(); 126 final AnnotationIdentifier newAnnotationID = new GenericType<AnnotationIdentifier>(){}.getRawClass().newInstance(); 127 128 mockery.checking(new Expectations() { 129 { 130 oneOf(annotationDao).addAnnotation(annotationToAdd); 131 will(returnValue(newAnnotationID)); 132 } 133 }); 134 135 ArrayList<Annotation> parameter = new ArrayList<Annotation>(); 136 parameter.add(annotationToAdd); 137 138 String result = annotationResource.createAnnotation(parameter); 139 assertTrue(result=="OK"); 140 //assertEquals(200, result.getStatus()); 141 //assertTrue(annotationToAdd.equals(result.getEntity())); 142 } 114 143 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/rest/AnnotationsTest.java
r3169 r3180 25 25 import eu.dasish.annotation.schema.ObjectFactory; 26 26 import java.sql.SQLException; 27 import java.util.ArrayList; 28 import java.util.List; 27 29 import javax.ws.rs.core.MediaType; 28 30 import org.jmock.Expectations; 29 31 import org.junit.Test; 30 32 import static org.junit.Assert.*; 33 import org.junit.Ignore; 31 34 /** 32 35 * … … 119 122 assertEquals("0", response.getEntity(String.class)); 120 123 } 124 /** 125 * Test of createAnnotation method, of class AnnotationResource. 126 * POST api/annotations/ 127 */ 128 @Test 129 @Ignore 130 public void testAddAnnotation() throws SQLException, InstantiationException, IllegalAccessException{ 131 System.out.println("testDeleteAnnotation"); 132 System.out.println("test createAnnotation"); 133 //final Annotation annotationToAdd = new GenericType<Annotation>(){}.getRawClass().newInstance(); 134 final Annotation annotationToAdd = new ObjectFactory().createAnnotation(); 135 ///final AnnotationIdentifier newAnnotationID = new GenericType<AnnotationIdentifier>(){}.getRawClass().newInstance(); 136 137 // mockery.checking(new Expectations() { 138 // { 139 // oneOf(annotationDao).addAnnotation(annotationToAdd); 140 // will(returnValue(newAnnotationID)); 141 // } 142 // }); 143 144 145 final String requestUrl = "annotations/"; 146 System.out.println("requestUrl: " + requestUrl); 147 148 List<Annotation> parameter = new ArrayList<Annotation>(); 149 parameter.add(annotationToAdd); 150 151 String response = resource().path(requestUrl).type(MediaType.APPLICATION_XML).post(String.class, parameter); 152 //assertEquals(200, response.getStatus()); 153 //assertEquals(annotationToAdd, response.getEntity(Annotation.class)); 154 155 156 } 121 157 }
Note: See TracChangeset
for help on using the changeset viewer.