Ignore:
Timestamp:
02/20/14 15:52:16 (10 years ago)
Author:
olhsha
Message:

refactoring repetitive code for different sort of resources. Replaced with one code using enum type Resource

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

    r4532 r4539  
    1919
    2020import eu.dasish.annotation.backend.BackendConstants;
     21import eu.dasish.annotation.backend.Resource;
    2122import eu.dasish.annotation.backend.dao.DBIntegrityService;
    2223import eu.dasish.annotation.schema.Annotation;
    23 import eu.dasish.annotation.schema.AnnotationActionName;
     24import eu.dasish.annotation.schema.AnnotationBody;
    2425import eu.dasish.annotation.schema.AnnotationInfoList;
    25 import eu.dasish.annotation.schema.Action;
    26 import eu.dasish.annotation.schema.ActionList;
    27 import eu.dasish.annotation.schema.AnnotationBody;
    2826import eu.dasish.annotation.schema.ObjectFactory;
    2927import eu.dasish.annotation.schema.Permission;
    30 import eu.dasish.annotation.schema.PermissionActionName;
    3128import eu.dasish.annotation.schema.UserWithPermissionList;
    3229import eu.dasish.annotation.schema.ReferenceList;
    3330import eu.dasish.annotation.schema.ResponseBody;
    34 import eu.dasish.annotation.schema.UserWithPermission;
    3531import java.io.IOException;
    36 import java.net.URI;
    37 import java.sql.Timestamp;
    38 import java.util.ArrayList;
    39 import java.util.List;
    4032import java.util.UUID;
    4133import javax.servlet.ServletContext;
     
    113105        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    114106        try {
    115             final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(externalIdentifier));
     107            final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.ANNOTATION);
    116108            if (annotationID != null) {
    117109                String remoteUser = httpServletRequest.getRemoteUser();
     
    144136        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    145137        try {
    146             final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(externalIdentifier));
     138            final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.ANNOTATION);
    147139            if (annotationID != null) {
    148140                String remoteUser = httpServletRequest.getRemoteUser();
     
    157149                } else {
    158150                    verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
    159 
    160151                }
    161152            } else {
     
    209200        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    210201        try {
    211             final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(externalIdentifier));
     202            final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.ANNOTATION);
    212203            String remoteUser = httpServletRequest.getRemoteUser();
    213204            Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser);
     
    215206                if (annotationID != null) {
    216207                    if (dbIntegrityService.canRead(userID, annotationID)) {
    217                         final UserWithPermissionList permissionList = dbIntegrityService.getPermissionsForAnnotation(annotationID);
     208                        final UserWithPermissionList permissionList = dbIntegrityService.getPermissions(annotationID, Resource.ANNOTATION);
    218209                        return new ObjectFactory().createPermissionList(permissionList);
    219210                    } else {
     
    240231        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    241232        try {
    242             final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(externalIdentifier));
     233            final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.ANNOTATION);
    243234            String remoteUser = httpServletRequest.getRemoteUser();
    244235            Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser);
     
    280271        if (userID != null) {
    281272            Number annotationID = dbIntegrityService.addUsersAnnotation(userID, annotation);
    282             return new ObjectFactory().createResponseBody(makeAnnotationResponseEnvelope(annotationID));
     273            return new ObjectFactory().createResponseBody(dbIntegrityService.makeAnnotationResponseEnvelope(annotationID));
    283274        } else {
    284275            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     
    306297
    307298        try {
    308             final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(externalIdentifier));
     299            final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.ANNOTATION);
    309300            if (annotationID != null) {
    310301                String remoteUser = httpServletRequest.getRemoteUser();
     
    313304                    if (userID.equals(dbIntegrityService.getAnnotationOwner(annotationID)) || dbIntegrityService.getTypeOfUserAccount(userID).equals(admin)) {
    314305                        int updatedRows = dbIntegrityService.updateAnnotation(annotation);
    315                         return new ObjectFactory().createResponseBody(makeAnnotationResponseEnvelope(annotationID));
     306                        return new ObjectFactory().createResponseBody(dbIntegrityService.makeAnnotationResponseEnvelope(annotationID));
    316307                    } else {
    317308                        verboseOutput.FORBIDDEN_PERMISSION_CHANGING(externalIdentifier);
     
    338329        dbIntegrityService.setServiceURI(path);
    339330        try {
    340             final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(externalIdentifier));
     331            final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.ANNOTATION);
    341332            String remoteUser = httpServletRequest.getRemoteUser();
    342333            Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser);
     
    345336                    if (dbIntegrityService.canWrite(userID, annotationID)) {
    346337                        int updatedRows = dbIntegrityService.updateAnnotationBody(annotationID, annotationBody);
    347                         return new ObjectFactory().createResponseBody(makeAnnotationResponseEnvelope(annotationID));
     338                        return new ObjectFactory().createResponseBody(dbIntegrityService.makeAnnotationResponseEnvelope(annotationID));
    348339                    } else {
    349340                        verboseOutput.FORBIDDEN_ANNOTATION_WRITING(externalIdentifier);
     
    373364        if (remoteUserID != null) {
    374365            try {
    375                 final Number userID = dbIntegrityService.getUserInternalIdentifier(UUID.fromString(userExternalId));
     366                final Number userID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(userExternalId), Resource.PRINCIPAL);
    376367                if (userID != null) {
    377368                    try {
    378                         final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(annotationExternalId));
     369                        final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(annotationExternalId), Resource.ANNOTATION);
    379370                        if (annotationID != null) {
    380371                            if (remoteUserID.equals(dbIntegrityService.getAnnotationOwner(annotationID)) || dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) {
     
    414405        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    415406        try {
    416             final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(annotationExternalId));
     407            final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(annotationExternalId), Resource.ANNOTATION);
    417408            String remoteUser = httpServletRequest.getRemoteUser();
    418409            Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser);
     
    421412                    if (remoteUserID.equals(dbIntegrityService.getAnnotationOwner(annotationID)) || dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) {
    422413                        int updatedRows = dbIntegrityService.updatePermissions(annotationID, permissions);
    423                         return new ObjectFactory().createResponseBody(makePermissionResponseEnvelope(annotationID));
     414                        return new ObjectFactory().createResponseBody(dbIntegrityService.makePermissionResponseEnvelope(annotationID, Resource.ANNOTATION));
    424415                    } else {
    425416                        verboseOutput.FORBIDDEN_PERMISSION_CHANGING(annotationExternalId);
     
    448439        int deletedRows = 0;
    449440        try {
    450             final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(annotationId));
     441            final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(annotationId), Resource.ANNOTATION);
    451442            String remoteUser = httpServletRequest.getRemoteUser();
    452443            Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser);
     
    454445                if (annotationID != null) {
    455446                    if (remoteUserID.equals(dbIntegrityService.getAnnotationOwner(annotationID)) || dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) {
    456                         Number userID = dbIntegrityService.getUserInternalIdentifier(UUID.fromString(userId));
     447                        Number userID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(userId), Resource.PRINCIPAL);
    457448                        if (userID != null) {
    458449                            deletedRows = dbIntegrityService.updateAnnotationPrincipalPermission(annotationID, userID, null);
     
    477468        return (deletedRows + " is deleted.");
    478469    }
    479 /////////////////////////////////////////
    480 
    481     private ResponseBody makeAnnotationResponseEnvelope(Number annotationID) {
    482         ResponseBody result = new ResponseBody();
    483         result.setPermissions(null);
    484         Annotation annotation = dbIntegrityService.getAnnotation(annotationID);
    485         result.setAnnotation(annotation);
    486         List<String> targetsNoCached = dbIntegrityService.getTargetsWithNoCachedRepresentation(annotationID);
    487         ActionList actionList = new ActionList();
    488         result.setActionList(actionList);
    489         actionList.getAction().addAll(makeActionList(targetsNoCached, AnnotationActionName.CREATE_CACHED_REPRESENTATION.value()));
    490         return result;
    491     }
    492 
    493     /////////////////////////////////////////
    494     private ResponseBody makePermissionResponseEnvelope(Number annotationID) {
    495         ResponseBody result = new ResponseBody();
    496         result.setAnnotation(null);
    497         UserWithPermissionList permissions = dbIntegrityService.getPermissionsForAnnotation(annotationID);
    498         result.setPermissions(permissions);
    499         List<String> usersWithNoInfo = dbIntegrityService.getUsersWithNoInfo(annotationID);
    500         ActionList actionList = new ActionList();
    501         result.setActionList(actionList);
    502         actionList.getAction().addAll(makeActionList(usersWithNoInfo, PermissionActionName.PROVIDE_USER_INFO.value()));
    503         return result;
    504     }
    505 
    506     // REFACTOR : move to the integrity service all te methods below 
    507     private List<Action> makeActionList(List<String> resourceURIs, String message) {
    508         if (resourceURIs != null) {
    509             if (resourceURIs.isEmpty()) {
    510                 return (new ArrayList<Action>());
    511             } else {
    512                 List<Action> result = new ArrayList<Action>();
    513                 for (String resourceURI : resourceURIs) {
    514                     Action action = new Action();
    515                     result.add(action);
    516                     action.setMessage(message);
    517                     action.setObject(resourceURI);
    518                 }
    519                 return result;
    520             }
    521         } else {
    522             return null;
    523         }
    524     }
     470
    525471}
Note: See TracChangeset for help on using the changeset viewer.