]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-398: I created an abstract base class for service resources. It helps consolid...
authorRichard Millet <richard.millet@berkeley.edu>
Fri, 28 Aug 2009 17:12:02 +0000 (17:12 +0000)
committerRichard Millet <richard.millet@berkeley.edu>
Fri, 28 Aug 2009 17:12:02 +0000 (17:12 +0000)
services/common/src/main/java/org/collectionspace/services/common/CollectionSpaceHandlerFactory.java [new file with mode: 0644]
services/common/src/main/java/org/collectionspace/services/common/CollectionSpaceResource.java [new file with mode: 0644]
services/relation/service/src/main/java/org/collectionspace/services/relation/NewRelationResource.java
services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationHandlerFactory.java

diff --git a/services/common/src/main/java/org/collectionspace/services/common/CollectionSpaceHandlerFactory.java b/services/common/src/main/java/org/collectionspace/services/common/CollectionSpaceHandlerFactory.java
new file mode 100644 (file)
index 0000000..cd3368e
--- /dev/null
@@ -0,0 +1,8 @@
+package org.collectionspace.services.common;\r
+\r
+import org.collectionspace.services.common.repository.DocumentHandler;\r
+import org.collectionspace.services.common.NuxeoClientType;\r
+\r
+public interface CollectionSpaceHandlerFactory {\r
+    public DocumentHandler getHandler(String clientType) throws IllegalArgumentException;\r
+}\r
diff --git a/services/common/src/main/java/org/collectionspace/services/common/CollectionSpaceResource.java b/services/common/src/main/java/org/collectionspace/services/common/CollectionSpaceResource.java
new file mode 100644 (file)
index 0000000..e4b33b7
--- /dev/null
@@ -0,0 +1,38 @@
+package org.collectionspace.services.common;\r
+\r
+import org.collectionspace.services.common.repository.DocumentHandler;\r
+import org.collectionspace.services.common.repository.RepositoryClient;\r
+import org.collectionspace.services.common.repository.RepositoryClientFactory;\r
+import org.collectionspace.services.common.CollectionSpaceHandlerFactory;\r
+//import org.collectionspace.services.relation.nuxeo.RelationHandlerFactory;\r
+\r
+public abstract class CollectionSpaceResource {\r
+\r
+       // Fields for default client factory and client\r
+       private RepositoryClientFactory defaultClientFactory;\r
+       private RepositoryClient defaultClient;\r
+       \r
+       // Fields for default document handler factory and handler\r
+       private CollectionSpaceHandlerFactory defaultHandlerFactory;\r
+       private DocumentHandler defaultHandler;\r
+       \r
+       // Methods that subclasses must implement\r
+       abstract protected String getClientType();\r
+       abstract protected RepositoryClientFactory getDefaultClientFactory();\r
+       abstract protected CollectionSpaceHandlerFactory getDefaultHandlerFactory();\r
+       \r
+       protected RepositoryClient getDefaultClient() {\r
+               return this.defaultClient;\r
+       }\r
+       \r
+       protected DocumentHandler getDefaultHandler() {\r
+               return this.defaultHandler;\r
+       }\r
+       \r
+       public CollectionSpaceResource() {\r
+               defaultClientFactory = getDefaultClientFactory(); //implemented by subclasses\r
+               defaultClient = defaultClientFactory.getClient(getClientType());\r
+               defaultHandlerFactory = getDefaultHandlerFactory(); //implemented by subclasses\r
+               defaultHandler = defaultHandlerFactory.getHandler(getClientType());\r
+       }       \r
+}\r
index c4874470ab091502320f5dc80956a625b06d0699..2f4f0096243229de39ca7a7b2e5b847a8f5ca885 100644 (file)
@@ -46,8 +46,10 @@ import javax.xml.bind.Marshaller;
 
 import org.collectionspace.services.relation.RelationList.RelationListItem;
 
+import org.collectionspace.services.common.CollectionSpaceResource;
 import org.collectionspace.services.relation.nuxeo.RelationNuxeoConstants;
 import org.collectionspace.services.relation.nuxeo.RelationHandlerFactory;
+import org.collectionspace.services.common.CollectionSpaceHandlerFactory;
 import org.collectionspace.services.common.NuxeoClientType;
 import org.collectionspace.services.common.ServiceMain;
 import org.collectionspace.services.common.relation.RelationsManager;
@@ -62,33 +64,36 @@ import org.slf4j.LoggerFactory;
 @Path("/relations")
 @Consumes("application/xml")
 @Produces("application/xml")
-public class NewRelationResource {
+public class NewRelationResource extends CollectionSpaceResource {
 
        public final static String SERVICE_NAME = "relations";
        final Logger logger = LoggerFactory.getLogger(NewRelationResource.class);
        // FIXME retrieve client type from configuration
-       final static NuxeoClientType CLIENT_TYPE = ServiceMain.getInstance()
-                       .getNuxeoClientType();
-
-       public NewRelationResource() {
-               // do nothing
+//     final static NuxeoClientType CLIENT_TYPE = ServiceMain.getInstance()
+//                     .getNuxeoClientType();
+       
+       protected String getClientType() {
+               return ServiceMain.getInstance().getNuxeoClientType().toString();
+       }
+       
+       protected RepositoryClientFactory getDefaultClientFactory() {
+               return RepositoryClientFactory.getInstance();
+       }
+       
+       protected CollectionSpaceHandlerFactory getDefaultHandlerFactory() {
+               return RelationHandlerFactory.getInstance();
        }
+       
+//     public NewRelationResource() {
+//     }
 
        @POST
        public Response createRelation(Relation relation) {
 
                String csid = null;
                try {
-                       RepositoryClientFactory clientFactory = RepositoryClientFactory
-                                       .getInstance();
-                       RepositoryClient client = clientFactory.getClient(CLIENT_TYPE
-                                       .toString());
-                       RelationHandlerFactory handlerFactory = RelationHandlerFactory
-                                       .getInstance();
-                       DocumentHandler handler = (DocumentHandler) handlerFactory
-                                       .getHandler(CLIENT_TYPE.toString());
-                       handler.setCommonObject(relation);
-                       csid = client.create(SERVICE_NAME, handler);
+                       getDefaultHandler().setCommonObject(relation);
+                       csid = getDefaultClient().create(SERVICE_NAME, getDefaultHandler());
                        relation.setCsid(csid);
                        if (logger.isDebugEnabled()) {
                                verbose("createRelation: ", relation);
@@ -107,6 +112,23 @@ public class NewRelationResource {
                        throw new WebApplicationException(response);
                }
        }
+       
+       @GET
+       @Path("query/{queryValue}")
+       public Response getQuery(@PathParam("queryValue") String queryString) {
+               
+               Response result = null;
+               
+               if (logger.isDebugEnabled() == true) {
+                       logger.debug("Query string is: " + queryString);
+               }
+                               
+               result = Response.status(Response.Status.ACCEPTED).entity(
+                                               "Query performed. Look in $JBOSS_HOME/server/cspace/log/" +
+                                               "directory for results ").type("text/plain").build();
+               
+               return result;
+       }
 
        @GET
        @Path("{csid}")
@@ -123,16 +145,8 @@ public class NewRelationResource {
                }
                Relation relation = null;
                try {
-                       RepositoryClientFactory clientFactory = RepositoryClientFactory
-                                       .getInstance();
-                       RepositoryClient client = clientFactory.getClient(CLIENT_TYPE
-                                       .toString());
-                       RelationHandlerFactory handlerFactory = RelationHandlerFactory
-                                       .getInstance();
-                       DocumentHandler handler = (DocumentHandler) handlerFactory
-                                       .getHandler(CLIENT_TYPE.toString());
-                       client.get(csid, handler);
-                       relation = (Relation) handler.getCommonObject();
+                       getDefaultClient().get(csid, getDefaultHandler());
+                       relation = (Relation) getDefaultHandler().getCommonObject();
                } catch (DocumentNotFoundException dnfe) {
                        if (logger.isDebugEnabled()) {
                                logger.debug("getRelation", dnfe);
@@ -267,16 +281,8 @@ public class NewRelationResource {
                        verbose("updateRelation with input: ", theUpdate);
                }
                try {
-                       RepositoryClientFactory clientFactory = RepositoryClientFactory
-                                       .getInstance();
-                       RepositoryClient client = clientFactory.getClient(CLIENT_TYPE
-                                       .toString());
-                       RelationHandlerFactory handlerFactory = RelationHandlerFactory
-                                       .getInstance();
-                       DocumentHandler handler = (DocumentHandler) handlerFactory
-                                       .getHandler(CLIENT_TYPE.toString());
-                       handler.setCommonObject(theUpdate);
-                       client.update(csid, handler);
+                       getDefaultHandler().setCommonObject(theUpdate);
+                       getDefaultClient().update(csid, getDefaultHandler());
                } catch (DocumentNotFoundException dnfe) {
                        if (logger.isDebugEnabled()) {
                                logger.debug("caugth exception in updateRelation", dnfe);
@@ -309,11 +315,7 @@ public class NewRelationResource {
                        throw new WebApplicationException(response);
                }
                try {
-                       RepositoryClientFactory clientFactory = RepositoryClientFactory
-                                       .getInstance();
-                       RepositoryClient client = clientFactory.getClient(CLIENT_TYPE
-                                       .toString());
-                       client.delete(csid);
+                       getDefaultClient().delete(csid);
                        return Response.status(HttpResponseCodes.SC_OK).build();
                } catch (DocumentNotFoundException dnfe) {
                        if (logger.isDebugEnabled()) {
@@ -349,22 +351,13 @@ public class NewRelationResource {
                                throws WebApplicationException {
                RelationList relationList = new RelationList();
                try {
-                       RepositoryClientFactory clientFactory = RepositoryClientFactory
-                                       .getInstance();
-                       RepositoryClient client = clientFactory.getClient(CLIENT_TYPE
-                                       .toString());
-                       RelationHandlerFactory handlerFactory = RelationHandlerFactory
-                                       .getInstance();
-                       DocumentHandler handler = (DocumentHandler) handlerFactory
-                                       .getHandler(CLIENT_TYPE.toString());
-
-                       Map propsFromPath = handler.getProperties();
+                       Map propsFromPath = getDefaultHandler().getProperties();
                        propsFromPath.put(RelationsManager.SUBJECT, subjectCsid);
                        propsFromPath.put(RelationsManager.PREDICATE, predicate);
                        propsFromPath.put(RelationsManager.OBJECT, objectCsid);
 
-                       client.getAll(SERVICE_NAME, handler);
-                       relationList = (RelationList) handler.getCommonObjectList();
+                       getDefaultClient().getAll(SERVICE_NAME, getDefaultHandler());
+                       relationList = (RelationList) getDefaultHandler().getCommonObjectList();
                } catch (Exception e) {
                        if (logger.isDebugEnabled()) {
                                logger.debug("Caught exception in getRelationList", e);
index b2914b8fbc18843ef828cd066bb544af87400932..b2f1a5b6065e2b10c7279919735a677900162168 100644 (file)
@@ -25,6 +25,7 @@ package org.collectionspace.services.relation.nuxeo;
 
 import org.collectionspace.services.common.NuxeoClientType;
 import org.collectionspace.services.common.repository.DocumentHandler;
+import org.collectionspace.services.common.CollectionSpaceHandlerFactory;
 
 /**
  * CollectionObjectHandlerFactory creates handlers for collectionobject based
@@ -33,7 +34,7 @@ import org.collectionspace.services.common.repository.DocumentHandler;
  * $LastChangedRevision: $
  * $LastChangedDate: $
  */
-public class RelationHandlerFactory {
+public class RelationHandlerFactory implements CollectionSpaceHandlerFactory {
 
     private static final RelationHandlerFactory self = new RelationHandlerFactory();