]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
367a4130cf59bff942acefea01a812799626e09f
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.organization;
25
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.DELETE;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.PUT;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.QueryParam;
35 import javax.ws.rs.WebApplicationException;
36 import javax.ws.rs.core.Context;
37 import javax.ws.rs.core.MultivaluedMap;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.UriBuilder;
40 import javax.ws.rs.core.UriInfo;
41
42 import org.collectionspace.services.OrgAuthorityJAXBSchema;
43 import org.collectionspace.services.OrganizationJAXBSchema;
44 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
45 import org.collectionspace.services.common.ClientType;
46 import org.collectionspace.services.common.ServiceMain;
47 import org.collectionspace.services.common.context.MultipartServiceContext;
48 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
49 import org.collectionspace.services.common.context.ServiceContext;
50 import org.collectionspace.services.common.document.BadRequestException;
51 import org.collectionspace.services.common.document.DocumentFilter;
52 import org.collectionspace.services.common.document.DocumentHandler;
53 import org.collectionspace.services.common.document.DocumentNotFoundException;
54 import org.collectionspace.services.common.query.IQueryManager;
55 import org.collectionspace.services.contact.ContactResource;
56 import org.collectionspace.services.contact.ContactsCommon;
57 import org.collectionspace.services.contact.ContactsCommonList;
58 import org.collectionspace.services.contact.ContactJAXBSchema;
59 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
60 import org.collectionspace.services.common.security.UnauthorizedException;
61 import org.collectionspace.services.organization.nuxeo.OrganizationDocumentModelHandler;
62 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
63 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
64 import org.jboss.resteasy.util.HttpResponseCodes;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
67
68 @Path("/orgauthorities")
69 @Consumes("multipart/mixed")
70 @Produces("multipart/mixed")
71 public class OrgAuthorityResource extends AbstractCollectionSpaceResourceImpl {
72
73     private final static String orgAuthorityServiceName = "orgauthorities";
74     private final static String organizationServiceName = "organizations";
75     final Logger logger = LoggerFactory.getLogger(OrgAuthorityResource.class);
76     //FIXME retrieve client type from configuration
77     final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
78     private ContactResource contactResource = new ContactResource();
79
80     public OrgAuthorityResource() {
81         // do nothing
82     }
83
84     @Override
85     protected String getVersionString() {
86         /** The last change revision. */
87         final String lastChangeRevision = "$LastChangedRevision$";
88         return lastChangeRevision;
89     }
90     
91     @Override
92     public String getServiceName() {
93         return orgAuthorityServiceName;
94     }
95
96     public String getItemServiceName() {
97         return organizationServiceName;
98     }
99
100     public String getContactServiceName() {
101         return contactResource.getServiceName();
102     }
103
104     /*
105     public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception {
106     RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName());
107     ctx.setInput(input);
108     return ctx;
109     }
110      */
111     @Override
112     public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
113         DocumentHandler docHandler =ctx.getDocumentHandler();
114         if (ctx.getInput() != null) {
115             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), OrgauthoritiesCommon.class);
116             if (obj != null) {
117                 docHandler.setCommonPart((OrgauthoritiesCommon) obj);
118             }
119         }
120         return docHandler;
121     }
122
123     private DocumentHandler createItemDocumentHandler(
124             ServiceContext ctx,
125             String inAuthority) throws Exception {
126         DocumentHandler docHandler = ctx.getDocumentHandler();
127         ((OrganizationDocumentModelHandler) docHandler).setInAuthority(inAuthority);
128         if (ctx.getInput() != null) {
129             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(getItemServiceName()),
130                     OrganizationsCommon.class);
131             if (obj != null) {
132                 docHandler.setCommonPart((OrganizationsCommon) obj);
133             }
134         }
135         return docHandler;
136     }
137
138     private DocumentHandler createContactDocumentHandler(
139             ServiceContext ctx, String inAuthority,
140             String inItem) throws Exception {
141         DocumentHandler docHandler = ctx.getDocumentHandler();
142         // Set the inAuthority and inItem values, which specify the
143         // parent authority (e.g. PersonAuthority, OrgAuthority) and the item
144         // (e.g. Person, Organization) with which the Contact is associated.
145         ((ContactDocumentModelHandler) docHandler).setInAuthority(inAuthority);
146         ((ContactDocumentModelHandler) docHandler).setInItem(inItem);
147         if (ctx.getInput() != null) {
148             Object obj = ((MultipartServiceContext) ctx)
149                 .getInputPart(ctx.getCommonPartLabel(getContactServiceName()),
150                 ContactsCommon.class);
151             if (obj != null) {
152                 docHandler.setCommonPart((ContactsCommon) obj);
153             }
154         }
155         return docHandler;
156     }
157
158     @POST
159     public Response createOrgAuthority(MultipartInput input) {
160         try {
161             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
162             DocumentHandler handler = createDocumentHandler(ctx);
163             String csid = getRepositoryClient(ctx).create(ctx, handler);
164             //orgAuthorityObject.setCsid(csid);
165             UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
166             path.path("" + csid);
167             Response response = Response.created(path.build()).build();
168             return response;
169         } catch (BadRequestException bre) {
170             Response response = Response.status(
171                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
172             throw new WebApplicationException(response);
173         } catch (UnauthorizedException ue) {
174             Response response = Response.status(
175                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
176             throw new WebApplicationException(response);
177         } catch (Exception e) {
178             if (logger.isDebugEnabled()) {
179                 logger.debug("Caught exception in createOrgAuthority", e);
180             }
181             Response response = Response.status(
182                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
183             throw new WebApplicationException(response);
184         }
185     }
186
187
188     @GET
189     @Path("urn:cspace:name({specifier})")
190     public MultipartOutput getOrgAuthorityByName(@PathParam("specifier") String specifier) {
191         String idValue = null;
192         if (specifier == null) {
193             logger.error("getOrgAuthority: missing name!");
194             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
195                     "get failed on OrgAuthority (missing specifier)").type(
196                     "text/plain").build();
197             throw new WebApplicationException(response);
198         }
199         String whereClause =
200                 OrgAuthorityJAXBSchema.ORGAUTHORITIES_COMMON+
201                 ":"+OrgAuthorityJAXBSchema.DISPLAY_NAME+
202                 "='"+specifier+"'";
203         // We only get a single doc - if there are multiple,
204         // it is an error in use.
205
206         if (logger.isDebugEnabled()) {
207             logger.debug("getOrgAuthority with name=" + specifier);
208         } 
209         MultipartOutput result = null;
210         try {
211             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
212             DocumentHandler handler = createDocumentHandler(ctx);
213             DocumentFilter myFilter = new DocumentFilter(whereClause, 0, 1);
214             handler.setDocumentFilter(myFilter);
215             getRepositoryClient(ctx).get(ctx, handler);
216             result = (MultipartOutput) ctx.getOutput();
217         } catch (UnauthorizedException ue) {
218             Response response = Response.status(
219                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
220             throw new WebApplicationException(response);
221         } catch (DocumentNotFoundException dnfe) {
222             if (logger.isDebugEnabled()) {
223                 logger.debug("getOrgAuthority", dnfe);
224             }
225             Response response = Response.status(Response.Status.NOT_FOUND).entity(
226                     "Get failed on OrgAuthority spec=" + specifier).type(
227                     "text/plain").build();
228             throw new WebApplicationException(response);
229         } catch (Exception e) {
230             if (logger.isDebugEnabled()) {
231                 logger.debug("getOrgAuthority", e);
232             }
233             Response response = Response.status(
234                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
235             throw new WebApplicationException(response);
236         }
237         if (result == null) {
238             Response response = Response.status(Response.Status.NOT_FOUND).entity(
239                     "Get failed, the requested OrgAuthority spec:" + specifier + ": was not found.").type(
240                     "text/plain").build();
241             throw new WebApplicationException(response);
242         }
243         return result;
244     }
245
246     @GET
247     @Path("{csid}")
248     public MultipartOutput getOrgAuthority(@PathParam("csid") String csid) {
249         String idValue = null;
250         if (csid == null) {
251             logger.error("getOrgAuthority: missing csid!");
252             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
253                     "get failed on OrgAuthority csid=" + csid).type(
254                     "text/plain").build();
255             throw new WebApplicationException(response);
256         }
257         if (logger.isDebugEnabled()) {
258             logger.debug("getOrgAuthority with path(id)=" + csid);
259         }
260         MultipartOutput result = null;
261         try {
262             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
263             DocumentHandler handler = createDocumentHandler(ctx);
264             getRepositoryClient(ctx).get(ctx, csid, handler);
265             result = (MultipartOutput) ctx.getOutput();
266         } catch (UnauthorizedException ue) {
267             Response response = Response.status(
268                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
269             throw new WebApplicationException(response);
270         } catch (DocumentNotFoundException dnfe) {
271             if (logger.isDebugEnabled()) {
272                 logger.debug("getOrgAuthority", dnfe);
273             }
274             Response response = Response.status(Response.Status.NOT_FOUND).entity(
275                     "Get failed on OrgAuthority csid=" + csid).type(
276                     "text/plain").build();
277             throw new WebApplicationException(response);
278         } catch (Exception e) {
279             if (logger.isDebugEnabled()) {
280                 logger.debug("getOrgAuthority", e);
281             }
282             Response response = Response.status(
283                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
284             throw new WebApplicationException(response);
285         }
286         if (result == null) {
287             Response response = Response.status(Response.Status.NOT_FOUND).entity(
288                     "Get failed, the requested OrgAuthority CSID:" + csid + ": was not found.").type(
289                     "text/plain").build();
290             throw new WebApplicationException(response);
291         }
292         return result;
293     }
294
295     @GET
296     @Produces("application/xml")
297     public OrgauthoritiesCommonList getOrgAuthorityList(@Context UriInfo ui) {
298         OrgauthoritiesCommonList orgAuthorityObjectList = new OrgauthoritiesCommonList();
299         try {
300             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
301             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
302             DocumentHandler handler = createDocumentHandler(ctx);
303             DocumentFilter myFilter = handler.createDocumentFilter(ctx); //new DocumentFilter();
304             myFilter.setPagination(queryParams);
305             String nameQ = queryParams.getFirst("refName");
306             if (nameQ != null) {
307                 myFilter.setWhereClause("orgauthorities_common:refName='" + nameQ + "'");
308             }
309             handler.setDocumentFilter(myFilter);
310             getRepositoryClient(ctx).getFiltered(ctx, handler);
311             orgAuthorityObjectList = (OrgauthoritiesCommonList) handler.getCommonPartList();
312         } catch (UnauthorizedException ue) {
313             Response response = Response.status(
314                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
315             throw new WebApplicationException(response);
316         } catch (Exception e) {
317             if (logger.isDebugEnabled()) {
318                 logger.debug("Caught exception in getOrgAuthorityList", e);
319             }
320             Response response = Response.status(
321                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
322             throw new WebApplicationException(response);
323         }
324         return orgAuthorityObjectList;
325     }
326
327     @PUT
328     @Path("{csid}")
329     public MultipartOutput updateOrgAuthority(
330             @PathParam("csid") String csid,
331             MultipartInput theUpdate) {
332         if (logger.isDebugEnabled()) {
333             logger.debug("updateOrgAuthority with csid=" + csid);
334         }
335         if (csid == null || "".equals(csid)) {
336             logger.error("updateOrgAuthority: missing csid!");
337             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
338                     "update failed on OrgAuthority csid=" + csid).type(
339                     "text/plain").build();
340             throw new WebApplicationException(response);
341         }
342         MultipartOutput result = null;
343         try {
344             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
345             DocumentHandler handler = createDocumentHandler(ctx);
346             getRepositoryClient(ctx).update(ctx, csid, handler);
347             result = (MultipartOutput) ctx.getOutput();
348         } catch (BadRequestException bre) {
349             Response response = Response.status(
350                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
351             throw new WebApplicationException(response);
352         } catch (UnauthorizedException ue) {
353             Response response = Response.status(
354                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
355             throw new WebApplicationException(response);
356         } catch (DocumentNotFoundException dnfe) {
357             if (logger.isDebugEnabled()) {
358                 logger.debug("caught exception in updateOrgAuthority", dnfe);
359             }
360             Response response = Response.status(Response.Status.NOT_FOUND).entity(
361                     "Update failed on OrgAuthority csid=" + csid).type(
362                     "text/plain").build();
363             throw new WebApplicationException(response);
364         } catch (Exception e) {
365             Response response = Response.status(
366                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
367             throw new WebApplicationException(response);
368         }
369         return result;
370     }
371
372     @DELETE
373     @Path("{csid}")
374     public Response deleteOrgAuthority(@PathParam("csid") String csid) {
375
376         if (logger.isDebugEnabled()) {
377             logger.debug("deleteOrgAuthority with csid=" + csid);
378         }
379         if (csid == null || "".equals(csid)) {
380             logger.error("deleteOrgAuthority: missing csid!");
381             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
382                     "delete failed on OrgAuthority csid=" + csid).type(
383                     "text/plain").build();
384             throw new WebApplicationException(response);
385         }
386         try {
387             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
388             getRepositoryClient(ctx).delete(ctx, csid);
389             return Response.status(HttpResponseCodes.SC_OK).build();
390         } catch (UnauthorizedException ue) {
391             Response response = Response.status(
392                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
393             throw new WebApplicationException(response);
394         } catch (DocumentNotFoundException dnfe) {
395             if (logger.isDebugEnabled()) {
396                 logger.debug("caught exception in deleteOrgAuthority", dnfe);
397             }
398             Response response = Response.status(Response.Status.NOT_FOUND).entity(
399                     "Delete failed on OrgAuthority csid=" + csid).type(
400                     "text/plain").build();
401             throw new WebApplicationException(response);
402         } catch (Exception e) {
403             Response response = Response.status(
404                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
405             throw new WebApplicationException(response);
406         }
407
408     }
409
410     /*************************************************************************
411      * Organization parts - this is a sub-resource of OrgAuthority
412      *************************************************************************/
413     @POST
414     @Path("{csid}/items")
415     public Response createOrganization(@PathParam("csid") String parentcsid, MultipartInput input) {
416         try {
417             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getItemServiceName());
418             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
419             String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
420             UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
421             path.path(parentcsid + "/items/" + itemcsid);
422             Response response = Response.created(path.build()).build();
423             return response;
424         } catch (BadRequestException bre) {
425             Response response = Response.status(
426                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
427             throw new WebApplicationException(response);
428         } catch (UnauthorizedException ue) {
429             Response response = Response.status(
430                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
431             throw new WebApplicationException(response);
432         } catch (Exception e) {
433             if (logger.isDebugEnabled()) {
434                 logger.debug("Caught exception in createOrganization", e);
435             }
436             Response response = Response.status(
437                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
438             throw new WebApplicationException(response);
439         }
440     }
441
442     @GET
443     @Path("{csid}/items/{itemcsid}")
444     public MultipartOutput getOrganization(
445             @PathParam("csid") String parentcsid,
446             @PathParam("itemcsid") String itemcsid) {
447         if (logger.isDebugEnabled()) {
448             logger.debug("getOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
449         }
450         if (parentcsid == null || "".equals(parentcsid)) {
451             logger.error("getOrganization: missing csid!");
452             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
453                     "get failed on Organization csid=" + parentcsid).type(
454                     "text/plain").build();
455             throw new WebApplicationException(response);
456         }
457         if (itemcsid == null || "".equals(itemcsid)) {
458             logger.error("getOrganization: missing itemcsid!");
459             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
460                     "get failed on Organization itemcsid=" + itemcsid).type(
461                     "text/plain").build();
462             throw new WebApplicationException(response);
463         }
464         MultipartOutput result = null;
465         try {
466             // Note that we have to create the service context for the Items, not the main service
467             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
468             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
469             getRepositoryClient(ctx).get(ctx, itemcsid, handler);
470             // TODO should we assert that the item is in the passed orgAuthority?
471             result = (MultipartOutput) ctx.getOutput();
472         } catch (UnauthorizedException ue) {
473             Response response = Response.status(
474                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
475             throw new WebApplicationException(response);
476         } catch (DocumentNotFoundException dnfe) {
477             if (logger.isDebugEnabled()) {
478                 logger.debug("getOrganization", dnfe);
479             }
480             Response response = Response.status(Response.Status.NOT_FOUND).entity(
481                     "Get failed on Organization csid=" + itemcsid).type(
482                     "text/plain").build();
483             throw new WebApplicationException(response);
484         } catch (Exception e) {
485             if (logger.isDebugEnabled()) {
486                 logger.debug("getOrganization", e);
487             }
488             Response response = Response.status(
489                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
490             throw new WebApplicationException(response);
491         }
492         if (result == null) {
493             Response response = Response.status(Response.Status.NOT_FOUND).entity(
494                     "Get failed, the requested Organization CSID:" + itemcsid + ": was not found.").type(
495                     "text/plain").build();
496             throw new WebApplicationException(response);
497         }
498         return result;
499     }
500
501     @GET
502     @Path("{csid}/items")
503     @Produces("application/xml")
504     public OrganizationsCommonList getOrganizationList(
505             @PathParam("csid") String parentcsid,
506             @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
507             @Context UriInfo ui) {
508         OrganizationsCommonList organizationObjectList = new OrganizationsCommonList();
509         try {
510             // Note that docType defaults to the ServiceName, so we're fine with that.
511             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
512             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
513             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
514             DocumentFilter myFilter = handler.createDocumentFilter(ctx); //new DocumentFilter();
515             myFilter.setPagination(queryParams);
516             myFilter.setWhereClause(OrganizationJAXBSchema.ORGANIZATIONS_COMMON +
517                         ":" + OrganizationJAXBSchema.IN_AUTHORITY + "=" +
518                         "'" + parentcsid + "'");
519             
520             // AND organizations_common:displayName LIKE '%partialTerm%'
521             if (partialTerm != null && !partialTerm.isEmpty()) {
522                 String ptClause = "AND " + OrganizationJAXBSchema.ORGANIZATIONS_COMMON +
523                         ":" + OrganizationJAXBSchema.DISPLAY_NAME +
524                         " LIKE " + "'%" + partialTerm + "%'";
525                 myFilter.appendWhereClause(ptClause);
526             }            
527             handler.setDocumentFilter(myFilter);
528             getRepositoryClient(ctx).getFiltered(ctx, handler);
529             organizationObjectList = (OrganizationsCommonList) handler.getCommonPartList();
530         } catch (UnauthorizedException ue) {
531             Response response = Response.status(
532                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
533             throw new WebApplicationException(response);
534         } catch (Exception e) {
535             if (logger.isDebugEnabled()) {
536                 logger.debug("Caught exception in getOrganizationList", e);
537             }
538             Response response = Response.status(
539                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
540             throw new WebApplicationException(response);
541         }
542         return organizationObjectList;
543     }
544
545     @GET
546     @Path("urn:cspace:name({specifier})/items")
547     @Produces("application/xml")
548     public OrganizationsCommonList getOrganizationListByAuthName(
549                 @PathParam("specifier") String parentSpecifier,
550             @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,            
551             @Context UriInfo ui) {
552         OrganizationsCommonList personObjectList = new OrganizationsCommonList();
553         try {
554             String whereClause =
555                 OrgAuthorityJAXBSchema.ORGAUTHORITIES_COMMON+
556                 ":"+OrgAuthorityJAXBSchema.DISPLAY_NAME+
557                 "='"+parentSpecifier+"'";
558             // Need to get an Authority by name
559             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
560             String parentcsid = 
561                 getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
562
563             ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
564             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
565             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
566             DocumentFilter myFilter = handler.createDocumentFilter(ctx);// new DocumentFilter();
567             myFilter.setPagination(queryParams);
568
569             // Add the where clause "organizations_common:inAuthority='" + parentcsid + "'"
570             myFilter.setWhereClause(OrganizationJAXBSchema.ORGANIZATIONS_COMMON + ":" +
571                         OrganizationJAXBSchema.IN_AUTHORITY + "='" + parentcsid + "'");
572             
573             // AND organizations_common:displayName LIKE '%partialTerm%'
574             if (partialTerm != null && !partialTerm.isEmpty()) {
575                 String ptClause = "AND " +
576                 OrganizationJAXBSchema.ORGANIZATIONS_COMMON + ":" +
577                 OrganizationJAXBSchema.DISPLAY_NAME +
578                         " LIKE " +
579                         "'%" + partialTerm + "%'";
580                 myFilter.appendWhereClause(ptClause);
581             }
582             
583             handler.setDocumentFilter(myFilter);
584             getRepositoryClient(ctx).getFiltered(ctx, handler);
585             personObjectList = (OrganizationsCommonList) handler.getCommonPartList();
586         } catch (UnauthorizedException ue) {
587             Response response = Response.status(
588                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
589             throw new WebApplicationException(response);
590         } catch (Exception e) {
591             if (logger.isDebugEnabled()) {
592                 logger.debug("Caught exception in getOrganizationListByAuthName", e);
593             }
594             Response response = Response.status(
595                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
596             throw new WebApplicationException(response);
597         }
598         return personObjectList;
599     }
600
601     @PUT
602     @Path("{csid}/items/{itemcsid}")
603     public MultipartOutput updateOrganization(
604             @PathParam("csid") String parentcsid,
605             @PathParam("itemcsid") String itemcsid,
606             MultipartInput theUpdate) {
607         if (logger.isDebugEnabled()) {
608             logger.debug("updateOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
609         }
610         if (parentcsid == null || "".equals(parentcsid)) {
611             logger.error("updateOrganization: missing csid!");
612             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
613                     "update failed on Organization parentcsid=" + parentcsid).type(
614                     "text/plain").build();
615             throw new WebApplicationException(response);
616         }
617         if (itemcsid == null || "".equals(itemcsid)) {
618             logger.error("updateOrganization: missing itemcsid!");
619             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
620                     "update failed on Organization=" + itemcsid).type(
621                     "text/plain").build();
622             throw new WebApplicationException(response);
623         }
624         MultipartOutput result = null;
625         try {
626             // Note that we have to create the service context for the Items, not the main service
627             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getItemServiceName());
628             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
629             getRepositoryClient(ctx).update(ctx, itemcsid, handler);
630             result = (MultipartOutput) ctx.getOutput();
631         } catch (BadRequestException bre) {
632             Response response = Response.status(
633                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
634             throw new WebApplicationException(response);
635         } catch (UnauthorizedException ue) {
636             Response response = Response.status(
637                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
638             throw new WebApplicationException(response);
639         } catch (DocumentNotFoundException dnfe) {
640             if (logger.isDebugEnabled()) {
641                 logger.debug("caught exception in updateOrganization", dnfe);
642             }
643             Response response = Response.status(Response.Status.NOT_FOUND).entity(
644                     "Update failed on Organization csid=" + itemcsid).type(
645                     "text/plain").build();
646             throw new WebApplicationException(response);
647         } catch (Exception e) {
648             Response response = Response.status(
649                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
650             throw new WebApplicationException(response);
651         }
652         return result;
653     }
654
655     @DELETE
656     @Path("{csid}/items/{itemcsid}")
657     public Response deleteOrganization(
658             @PathParam("csid") String parentcsid,
659             @PathParam("itemcsid") String itemcsid) {
660         if (logger.isDebugEnabled()) {
661             logger.debug("deleteOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
662         }
663         if (parentcsid == null || "".equals(parentcsid)) {
664             logger.error("deleteOrganization: missing csid!");
665             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
666                     "delete failed on Organization parentcsid=" + parentcsid).type(
667                     "text/plain").build();
668             throw new WebApplicationException(response);
669         }
670         if (itemcsid == null || "".equals(itemcsid)) {
671             logger.error("deleteOrganization: missing itemcsid!");
672             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
673                     "delete failed on Organization=" + itemcsid).type(
674                     "text/plain").build();
675             throw new WebApplicationException(response);
676         }
677         try {
678             // Note that we have to create the service context for the Items, not the main service
679             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
680             getRepositoryClient(ctx).delete(ctx, itemcsid);
681             return Response.status(HttpResponseCodes.SC_OK).build();
682         } catch (UnauthorizedException ue) {
683             Response response = Response.status(
684                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
685             throw new WebApplicationException(response);
686         } catch (DocumentNotFoundException dnfe) {
687             if (logger.isDebugEnabled()) {
688                 logger.debug("caught exception in deleteOrganization", dnfe);
689             }
690             Response response = Response.status(Response.Status.NOT_FOUND).entity(
691                     "Delete failed on Organization itemcsid=" + itemcsid).type(
692                     "text/plain").build();
693             throw new WebApplicationException(response);
694         } catch (Exception e) {
695             Response response = Response.status(
696                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
697             throw new WebApplicationException(response);
698         }
699
700     }
701
702     /*************************************************************************
703      * Contact parts - this is a sub-resource of Organization (or "item")
704      *************************************************************************/
705     @POST
706     @Path("{parentcsid}/items/{itemcsid}/contacts")
707     public Response createContact(
708             @PathParam("parentcsid") String parentcsid,
709             @PathParam("itemcsid") String itemcsid,
710             MultipartInput input) {
711         try {
712             // Note that we have to create the service context and document
713             // handler for the Contact service, not the main service.
714             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getContactServiceName());
715             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
716             String csid = getRepositoryClient(ctx).create(ctx, handler);
717             UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
718             path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
719             Response response = Response.created(path.build()).build();
720             return response;
721         } catch (BadRequestException bre) {
722             Response response = Response.status(
723                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
724             throw new WebApplicationException(response);
725         } catch (UnauthorizedException ue) {
726             Response response = Response.status(
727                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
728             throw new WebApplicationException(response);
729         } catch (Exception e) {
730             if (logger.isDebugEnabled()) {
731                 logger.debug("Caught exception in createContact", e);
732             }
733             Response response = Response.status(
734                 Response.Status.INTERNAL_SERVER_ERROR)
735                 .entity("Attempt to create Contact failed.")
736                 .type("text/plain").build();
737             throw new WebApplicationException(response);
738         }
739
740     }
741
742     @GET
743     @Produces({"application/xml"})
744     @Path("{parentcsid}/items/{itemcsid}/contacts/")
745     public ContactsCommonList getContactList(
746             @PathParam("parentcsid") String parentcsid,
747             @PathParam("itemcsid") String itemcsid,
748             @Context UriInfo ui) {
749         ContactsCommonList contactObjectList = new ContactsCommonList();
750         try {
751             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getContactServiceName());
752             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
753             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
754             DocumentFilter myFilter = handler.createDocumentFilter(ctx); //new DocumentFilter();
755             myFilter.setPagination(queryParams);
756             myFilter.setWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
757                 ContactJAXBSchema.IN_AUTHORITY +
758                 "='" + parentcsid + "'" +
759                 " AND " +
760                 ContactJAXBSchema.CONTACTS_COMMON + ":" +
761                 ContactJAXBSchema.IN_ITEM +
762                 "='" + itemcsid + "'" +
763                 " AND ecm:isProxy = 0");
764             handler.setDocumentFilter(myFilter);
765             getRepositoryClient(ctx).getFiltered(ctx, handler);
766             contactObjectList = (ContactsCommonList) handler.getCommonPartList();
767         } catch (UnauthorizedException ue) {
768             Response response = Response.status(
769                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
770             throw new WebApplicationException(response);
771         } catch (Exception e) {
772             if (logger.isDebugEnabled()) {
773                 logger.debug("Caught exception in getContactsList", e);
774             }
775             Response response = Response.status(
776                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
777             throw new WebApplicationException(response);
778         }
779         return contactObjectList;
780     }
781
782     @GET
783     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
784     public MultipartOutput getContact(
785             @PathParam("parentcsid") String parentcsid,
786             @PathParam("itemcsid") String itemcsid,
787             @PathParam("csid") String csid) {
788         MultipartOutput result = null;
789        if (logger.isDebugEnabled()) {
790             logger.debug("getContact with parentCsid=" + parentcsid +
791             " itemcsid=" + itemcsid + " csid=" + csid);
792         }
793         try {
794             // Note that we have to create the service context and document
795             // handler for the Contact service, not the main service.
796             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getContactServiceName());
797             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
798             getRepositoryClient(ctx).get(ctx, csid, handler);
799             result = (MultipartOutput) ctx.getOutput();
800         } catch (UnauthorizedException ue) {
801             Response response = Response.status(
802                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
803             throw new WebApplicationException(response);
804         } catch (DocumentNotFoundException dnfe) {
805             if (logger.isDebugEnabled()) {
806                 logger.debug("getContact", dnfe);
807             }
808             Response response = Response.status(Response.Status.NOT_FOUND)
809                 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
810                 .type("text/plain").build();
811             throw new WebApplicationException(response);
812         } catch (Exception e) {
813             if (logger.isDebugEnabled()) {
814                 logger.debug("getContact", e);
815             }
816             Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
817                 .entity("Get contact failed")
818                 .type("text/plain").build();
819             throw new WebApplicationException(response);
820         }
821         if (result == null) {
822             Response response = Response.status(Response.Status.NOT_FOUND)
823                 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
824                 .type("text/plain").build();
825             throw new WebApplicationException(response);
826         }
827         return result;
828
829     }
830
831     @PUT
832     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
833     public MultipartOutput updateContact(
834             @PathParam("parentcsid") String parentcsid,
835             @PathParam("itemcsid") String itemcsid,
836             @PathParam("csid") String csid,
837             MultipartInput theUpdate) {
838        if (logger.isDebugEnabled()) {
839             logger.debug("updateContact with parentcsid=" + parentcsid +
840             " itemcsid=" + itemcsid + " csid=" + csid);
841         }
842        if (parentcsid == null || parentcsid.trim().isEmpty()) {
843             logger.error("updateContact: missing csid!");
844             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
845                     "update failed on Contact parentcsid=" + parentcsid).type(
846                     "text/plain").build();
847             throw new WebApplicationException(response);
848         }
849         if (itemcsid == null || itemcsid.trim().isEmpty()) {
850             logger.error("updateContact: missing itemcsid!");
851             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
852                     "update failed on Contact=" + itemcsid).type(
853                     "text/plain").build();
854             throw new WebApplicationException(response);
855         }
856         if (csid == null || csid.trim().isEmpty()) {
857             logger.error("updateContact: missing csid!");
858             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
859                     "update failed on Contact=" + csid).type(
860                     "text/plain").build();
861             throw new WebApplicationException(response);
862         }
863         MultipartOutput result = null;
864         try {
865             // Note that we have to create the service context and document
866             // handler for the Contact service, not the main service.
867             ServiceContext ctx = MultipartServiceContextFactory.get()
868                 .createServiceContext(theUpdate, getContactServiceName());
869             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
870             getRepositoryClient(ctx).update(ctx, csid, handler);
871             result = (MultipartOutput) ctx.getOutput();
872         } catch (BadRequestException bre) {
873             Response response = Response.status(
874                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
875             throw new WebApplicationException(response);
876         } catch (UnauthorizedException ue) {
877             Response response = Response.status(
878                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
879             throw new WebApplicationException(response);
880         } catch (DocumentNotFoundException dnfe) {
881             if (logger.isDebugEnabled()) {
882                 logger.debug("caught exception in updateContact", dnfe);
883             }
884             Response response = Response.status(Response.Status.NOT_FOUND).entity(
885                     "Update failed on Contact csid=" + itemcsid).type(
886                     "text/plain").build();
887             throw new WebApplicationException(response);
888         } catch (Exception e) {
889             Response response = Response.status(
890                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
891             throw new WebApplicationException(response);
892         }
893         return result;
894     }
895
896     @DELETE
897     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
898     public Response deleteContact(
899             @PathParam("parentcsid") String parentcsid,
900             @PathParam("itemcsid") String itemcsid,
901             @PathParam("csid") String csid) {
902         if (logger.isDebugEnabled()) {
903             logger.debug("deleteContact with parentCsid=" + parentcsid +
904             " itemcsid=" + itemcsid + " csid=" + csid);
905         }
906         if (parentcsid == null || parentcsid.trim().isEmpty()) {
907             logger.error("deleteContact: missing parentcsid!");
908             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
909                     "delete contact failed on parentcsid=" + parentcsid).type(
910                     "text/plain").build();
911             throw new WebApplicationException(response);
912         }
913         if (itemcsid == null || itemcsid.trim().isEmpty()) {
914             logger.error("deleteContact: missing itemcsid!");
915             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
916                     "delete contact failed on itemcsid=" + itemcsid).type(
917                     "text/plain").build();
918             throw new WebApplicationException(response);
919         }
920         if (csid == null || csid.trim().isEmpty()) {
921             logger.error("deleteContact: missing csid!");
922             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
923                     "delete contact failed on csid=" + csid).type(
924                     "text/plain").build();
925             throw new WebApplicationException(response);
926         }
927         try {
928             // Note that we have to create the service context for the
929             // Contact service, not the main service.
930             ServiceContext ctx =
931                 MultipartServiceContextFactory.get().createServiceContext(null, getContactServiceName());
932             getRepositoryClient(ctx).delete(ctx, csid);
933             return Response.status(HttpResponseCodes.SC_OK).build();
934          } catch (UnauthorizedException ue) {
935             Response response = Response.status(
936                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
937             throw new WebApplicationException(response);
938          } catch (DocumentNotFoundException dnfe) {
939             if (logger.isDebugEnabled()) {
940                 logger.debug("Caught exception in deleteContact", dnfe);
941             }
942             Response response = Response.status(Response.Status.NOT_FOUND)
943                 .entity("Delete failed, the requested Contact CSID:" + csid + ": was not found.")
944                 .type("text/plain").build();
945             throw new WebApplicationException(response);
946        } catch (Exception e) {
947             Response response = Response.status(
948                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
949             throw new WebApplicationException(response);
950         }
951     }
952
953 }