Changeset 3609


Ignore:
Timestamp:
09/26/13 09:09:53 (11 years ago)
Author:
olhsha
Message:

fixing ticket 373: add, delete USER fro the DBintegrityService. Extending userIsInUse in the dao.

Lots of other stuff since power crashes, including GET and POST method for annotationResource

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  
    2121import eu.dasish.annotation.backend.dao.DBIntegrityService;
    2222import eu.dasish.annotation.schema.Annotation;
     23import eu.dasish.annotation.schema.AnnotationAction;
     24import eu.dasish.annotation.schema.AnnotationActionList;
     25import eu.dasish.annotation.schema.AnnotationActionName;
     26import eu.dasish.annotation.schema.AnnotationInfoList;
     27import eu.dasish.annotation.schema.AnnotationResponseBody;
     28import eu.dasish.annotation.schema.AnnotationResponseContent;
    2329import eu.dasish.annotation.schema.ObjectFactory;
     30import eu.dasish.annotation.schema.PermissionList;
     31import eu.dasish.annotation.schema.ResponseBody;
     32import eu.dasish.annotation.schema.SourceList;
    2433import java.sql.SQLException;
     34import java.sql.Timestamp;
     35import java.util.List;
    2536import java.util.UUID;
    2637import javax.servlet.http.HttpServletRequest;
     
    2940import javax.ws.rs.GET;
    3041import javax.ws.rs.POST;
     42import javax.ws.rs.PUT;
    3143import javax.ws.rs.Path;
    3244import javax.ws.rs.PathParam;
    3345import javax.ws.rs.Produces;
     46import javax.ws.rs.QueryParam;
    3447import javax.ws.rs.core.Context;
    3548import javax.ws.rs.core.MediaType;
     
    4558@Path("/annotations")
    4659public class AnnotationResource {
    47    
     60
    4861    @Autowired
    4962    private DBIntegrityService dbIntegrityService;
     
    6881    }
    6982
     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
    70125    ///////////////////////////////////////////////////////
    71     // TODO: return envelope: deleted or not deleted
     126    // TODO: how to return the status code?
    72127    @DELETE
    73128    @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}")
     
    76131        int[] resultDelete = dbIntegrityService.deleteAnnotation(annotationID);
    77132        String result = Integer.toString(resultDelete[0]);
    78         return result;
     133        return result + " annotation(s) deleted.";
    79134    }
    80135
    81136    ///////////////////////////////////////////////////////
    82     // TODO: should be returning the envelope!!!
    83137    @POST
    84138    @Consumes(MediaType.APPLICATION_XML)
    85139    @Produces(MediaType.APPLICATION_XML)
    86140    @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);
    88172       
     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 {
    89198        String remoteUser = httpServletRequest.getRemoteUser();
    90199        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);
    98204    }
    99205}
Note: See TracChangeset for help on using the changeset viewer.