Changeset 3609
- Timestamp:
- 09/26/13 09:09:53 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java
r3477 r3609 21 21 import eu.dasish.annotation.backend.dao.DBIntegrityService; 22 22 import eu.dasish.annotation.schema.Annotation; 23 import eu.dasish.annotation.schema.AnnotationAction; 24 import eu.dasish.annotation.schema.AnnotationActionList; 25 import eu.dasish.annotation.schema.AnnotationActionName; 26 import eu.dasish.annotation.schema.AnnotationInfoList; 27 import eu.dasish.annotation.schema.AnnotationResponseBody; 28 import eu.dasish.annotation.schema.AnnotationResponseContent; 23 29 import eu.dasish.annotation.schema.ObjectFactory; 30 import eu.dasish.annotation.schema.PermissionList; 31 import eu.dasish.annotation.schema.ResponseBody; 32 import eu.dasish.annotation.schema.SourceList; 24 33 import java.sql.SQLException; 34 import java.sql.Timestamp; 35 import java.util.List; 25 36 import java.util.UUID; 26 37 import javax.servlet.http.HttpServletRequest; … … 29 40 import javax.ws.rs.GET; 30 41 import javax.ws.rs.POST; 42 import javax.ws.rs.PUT; 31 43 import javax.ws.rs.Path; 32 44 import javax.ws.rs.PathParam; 33 45 import javax.ws.rs.Produces; 46 import javax.ws.rs.QueryParam; 34 47 import javax.ws.rs.core.Context; 35 48 import javax.ws.rs.core.MediaType; … … 45 58 @Path("/annotations") 46 59 public class AnnotationResource { 47 60 48 61 @Autowired 49 62 private DBIntegrityService dbIntegrityService; … … 68 81 } 69 82 83 //TODO: unit test 84 @GET 85 @Produces(MediaType.TEXT_XML) 86 @Path("{annotationid: " + BackendConstants.regExpIdentifier + "/sources}") 87 public JAXBElement<SourceList> getAnnotationSources(@PathParam("annotationid") String ExternalIdentifier) throws SQLException { 88 final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(ExternalIdentifier)); 89 dbIntegrityService.setServiceURI(httpServletRequest.getServletPath()); 90 final SourceList sourceList = dbIntegrityService.getAnnotationSources(annotationID); 91 return new ObjectFactory().createSourceList(sourceList); 92 } 93 94 // TODO Unit test 95 @GET 96 @Produces(MediaType.TEXT_XML) 97 @Path("") 98 public JAXBElement<AnnotationInfoList> getFilteredAnnotations(@QueryParam("link") String link, 99 @QueryParam("text") String text, 100 @QueryParam("access") String permission, 101 @QueryParam("access") String namespace, 102 @QueryParam("after") Timestamp after, 103 @QueryParam("before") Timestamp before 104 ) throws SQLException { 105 dbIntegrityService.setServiceURI(httpServletRequest.getServletPath()); 106 107 String remoteUser = httpServletRequest.getRemoteUser(); 108 UUID userExternalID = (remoteUser != null) ? UUID.fromString(remoteUser) : null; 109 110 final AnnotationInfoList annotationInfoList = dbIntegrityService.getFilteredAnnotationInfos(link, text, text, namespace, userExternalID, after, before); 111 return new ObjectFactory().createAnnotationInfoList(annotationInfoList); 112 } 113 114 // TODO Unit test 115 @GET 116 @Produces(MediaType.TEXT_XML) 117 @Path("{annotationid: " + BackendConstants.regExpIdentifier + "/permissions}") 118 public JAXBElement<PermissionList> getAnnotationPermissions(@PathParam("annotationid") String ExternalIdentifier) throws SQLException { 119 final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(ExternalIdentifier)); 120 dbIntegrityService.setServiceURI(httpServletRequest.getServletPath()); 121 final PermissionList permissionList = dbIntegrityService.getPermissionsForAnnotation(annotationID); 122 return new ObjectFactory().createPermissionList(permissionList); 123 } 124 70 125 /////////////////////////////////////////////////////// 71 // TODO: return envelope: deleted or not deleted126 // TODO: how to return the status code? 72 127 @DELETE 73 128 @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}") … … 76 131 int[] resultDelete = dbIntegrityService.deleteAnnotation(annotationID); 77 132 String result = Integer.toString(resultDelete[0]); 78 return result ;133 return result + " annotation(s) deleted."; 79 134 } 80 135 81 136 /////////////////////////////////////////////////////// 82 // TODO: should be returning the envelope!!!83 137 @POST 84 138 @Consumes(MediaType.APPLICATION_XML) 85 139 @Produces(MediaType.APPLICATION_XML) 86 140 @Path("") 87 public JAXBElement<Annotation> createAnnotation(Annotation annotation) throws SQLException { 141 public JAXBElement<ResponseBody> createAnnotation(Annotation annotation) throws SQLException { 142 return new ObjectFactory().createResponseBody(addORupdateAnnotation(annotation, true)); 143 } 144 145 146 /////////////////////////////////////////////////////// 147 // TODO: unit test 148 @PUT 149 @Consumes(MediaType.APPLICATION_XML) 150 @Produces(MediaType.APPLICATION_XML) 151 @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}") 152 public JAXBElement<ResponseBody> updateAnnotation(@PathParam("annotationid") String externalIdentifier, Annotation annotation) throws SQLException, Exception { 153 if (!(httpServletRequest.getServletPath()+externalIdentifier).equals(annotation.getURI())){ 154 throw new Exception("External annotation id and the annotation id from the request body do not match"); 155 } 156 return new ObjectFactory().createResponseBody(addORupdateAnnotation(annotation, false)); 157 } 158 159 160 161 private ResponseBody makeResponseEnvelope(Number annotationID) throws SQLException{ 162 ResponseBody result = new ResponseBody(); 163 result.setPermissionResponse(null); 164 AnnotationResponseBody subresult = new AnnotationResponseBody(); 165 result.setAnnotationResponse(subresult); 166 AnnotationActionList actions = new AnnotationActionList(); 167 AnnotationResponseContent content = new AnnotationResponseContent(); 168 AnnotationAction action = new AnnotationAction(); 169 actions.setAction(action); 170 subresult.setActions(actions); 171 subresult.setContent(content); 88 172 173 if (annotationID != null) { 174 Annotation annotation = dbIntegrityService.getAnnotation(annotationID); 175 content.setAnnotation(annotation); 176 List<Number> cached = dbIntegrityService.getSourcesWithNoCachedRepresentation(annotationID); 177 if (cached == null) { 178 subresult.setActions(null); 179 } else { 180 if (cached.isEmpty()) { 181 action.setAction(null); 182 } 183 else { 184 action.setAction(AnnotationActionName.CREATE_CACHED_REPRESENTATION); 185 } 186 } 187 188 } else { 189 content.setAnnotation(null); 190 action.setAction(null); 191 } 192 return result; 193 } 194 195 196 197 private ResponseBody addORupdateAnnotation(Annotation annotation, boolean newAnnotation) throws SQLException { 89 198 String remoteUser = httpServletRequest.getRemoteUser(); 90 199 UUID userExternalID = (remoteUser != null) ? UUID.fromString(remoteUser) : null; 91 Number userID = dbIntegrityService.getUserInternalIdentifier(userExternalID); 92 93 dbIntegrityService.setServiceURI(httpServletRequest.getServletPath()); 94 95 Number newAnnotationID = dbIntegrityService.addUsersAnnotation(annotation, userID); 96 Annotation newAnnotation = dbIntegrityService.getAnnotation(newAnnotationID); 97 return (new ObjectFactory().createAnnotation(newAnnotation)); 200 Number userID = dbIntegrityService.getUserInternalIdentifier(userExternalID); 201 dbIntegrityService.setServiceURI(httpServletRequest.getServletPath()); 202 Number newAnnotationID = newAnnotation ? dbIntegrityService.addUsersAnnotation(userID, annotation) : dbIntegrityService.updateUsersAnnotation(userID, annotation); 203 return makeResponseEnvelope(newAnnotationID); 98 204 } 99 205 }
Note: See TracChangeset
for help on using the changeset viewer.