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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.organization;
26 import java.util.List;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.PathParam;
35 import javax.ws.rs.Produces;
36 import javax.ws.rs.WebApplicationException;
37 import javax.ws.rs.core.Context;
38 import javax.ws.rs.core.MultivaluedMap;
39 import javax.ws.rs.core.Response;
40 import javax.ws.rs.core.UriBuilder;
41 import javax.ws.rs.core.UriInfo;
43 import org.collectionspace.services.common.vocabulary.AuthorityResource;
44 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
45 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
46 import org.collectionspace.services.common.authorityref.AuthorityRefList;
47 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
48 import org.collectionspace.services.common.context.ServiceBindingUtils;
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.document.DocumentWrapper;
55 import org.collectionspace.services.common.query.IQueryManager;
56 import org.collectionspace.services.common.repository.RepositoryClient;
57 import org.collectionspace.services.contact.ContactResource;
58 import org.collectionspace.services.contact.ContactsCommon;
59 import org.collectionspace.services.contact.ContactsCommonList;
60 import org.collectionspace.services.contact.ContactJAXBSchema;
61 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
62 import org.collectionspace.services.common.security.UnauthorizedException;
63 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
64 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
65 import org.collectionspace.services.organization.nuxeo.OrganizationDocumentModelHandler;
66 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
67 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
68 import org.jboss.resteasy.util.HttpResponseCodes;
69 import org.nuxeo.ecm.core.api.DocumentModel;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
74 * The Class OrgAuthorityResource.
76 @Path("/orgauthorities")
77 @Consumes("multipart/mixed")
78 @Produces("multipart/mixed")
79 public class OrgAuthorityResource extends
80 AuthorityResource<OrgauthoritiesCommon, OrgauthoritiesCommonList, OrganizationsCommon,
81 OrganizationDocumentModelHandler> {
83 /** The Constant orgAuthorityServiceName. */
84 private final static String orgAuthorityServiceName = "orgauthorities";
85 private final static String ORGAUTHORITIES_COMMON = "orgauthorities_common";
87 /** The Constant organizationServiceName. */
88 private final static String organizationServiceName = "organizations";
89 private final static String ORGANIZATIONS_COMMON = "organizations_common";
92 final Logger logger = LoggerFactory.getLogger(OrgAuthorityResource.class);
93 //FIXME retrieve client type from configuration
94 /** The Constant CLIENT_TYPE. */
95 //final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
97 /** The contact resource. */
98 private ContactResource contactResource = new ContactResource();
101 * Instantiates a new org authority resource.
103 public OrgAuthorityResource() {
104 super(OrgauthoritiesCommon.class, OrgAuthorityResource.class,
105 ORGAUTHORITIES_COMMON, ORGANIZATIONS_COMMON);
109 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
112 public String getServiceName() {
113 return orgAuthorityServiceName;
117 * Gets the item service name.
119 * @return the item service name
121 public String getItemServiceName() {
122 return organizationServiceName;
126 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
129 public Class<OrgauthoritiesCommon> getCommonPartClass() {
130 return OrgauthoritiesCommon.class;
134 * Gets the contact service name.
136 * @return the contact service name
138 public String getContactServiceName() {
139 return contactResource.getServiceName();
143 * Creates the contact document handler.
146 * @param inAuthority the in authority
147 * @param inItem the in item
149 * @return the document handler
151 * @throws Exception the exception
153 private DocumentHandler createContactDocumentHandler(
154 ServiceContext<MultipartInput, MultipartOutput> ctx, String inAuthority,
155 String inItem) throws Exception {
157 ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler)createDocumentHandler(
159 ctx.getCommonPartLabel(getContactServiceName()),
160 ContactsCommon.class);
161 docHandler.setInAuthority(inAuthority);
162 docHandler.setInItem(inItem);
167 /*************************************************************************
168 * Contact parts - this is a sub-resource of Organization (or "item")
173 *************************************************************************/
175 @Path("{parentcsid}/items/{itemcsid}/contacts")
176 public Response createContact(
177 @PathParam("parentcsid") String parentcsid,
178 @PathParam("itemcsid") String itemcsid,
179 MultipartInput input) {
181 // Note that we have to create the service context and document
182 // handler for the Contact service, not the main service.
183 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(), input);
184 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
185 String csid = getRepositoryClient(ctx).create(ctx, handler);
186 UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
187 path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
188 Response response = Response.created(path.build()).build();
190 } catch (BadRequestException bre) {
191 Response response = Response.status(
192 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
193 throw new WebApplicationException(response);
194 } catch (UnauthorizedException ue) {
195 Response response = Response.status(
196 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
197 throw new WebApplicationException(response);
198 } catch (Exception e) {
199 if (logger.isDebugEnabled()) {
200 logger.debug("Caught exception in createContact", e);
202 Response response = Response.status(
203 Response.Status.INTERNAL_SERVER_ERROR)
204 .entity("Attempt to create Contact failed.")
205 .type("text/plain").build();
206 throw new WebApplicationException(response);
212 * Gets the contact list.
214 * @param parentcsid the parentcsid
215 * @param itemcsid the itemcsid
218 * @return the contact list
221 @Produces({"application/xml"})
222 @Path("{parentcsid}/items/{itemcsid}/contacts/")
223 public ContactsCommonList getContactList(
224 @PathParam("parentcsid") String parentcsid,
225 @PathParam("itemcsid") String itemcsid,
226 @Context UriInfo ui) {
227 ContactsCommonList contactObjectList = new ContactsCommonList();
229 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
230 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(),
232 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
233 DocumentFilter myFilter = handler.getDocumentFilter(); //new DocumentFilter();
234 myFilter.setWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
235 ContactJAXBSchema.IN_AUTHORITY +
236 "='" + parentcsid + "'" +
237 IQueryManager.SEARCH_QUALIFIER_AND +
238 ContactJAXBSchema.CONTACTS_COMMON + ":" +
239 ContactJAXBSchema.IN_ITEM +
240 "='" + itemcsid + "'" +
241 IQueryManager.SEARCH_QUALIFIER_AND +
243 getRepositoryClient(ctx).getFiltered(ctx, handler);
244 contactObjectList = (ContactsCommonList) handler.getCommonPartList();
245 } catch (UnauthorizedException ue) {
246 Response response = Response.status(
247 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
248 throw new WebApplicationException(response);
249 } catch (Exception e) {
250 if (logger.isDebugEnabled()) {
251 logger.debug("Caught exception in getContactsList", e);
253 Response response = Response.status(
254 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
255 throw new WebApplicationException(response);
257 return contactObjectList;
263 * @param parentcsid the parentcsid
264 * @param itemcsid the itemcsid
265 * @param csid the csid
267 * @return the contact
270 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
271 public MultipartOutput getContact(
272 @PathParam("parentcsid") String parentcsid,
273 @PathParam("itemcsid") String itemcsid,
274 @PathParam("csid") String csid) {
275 MultipartOutput result = null;
276 if (logger.isDebugEnabled()) {
277 logger.debug("getContact with parentCsid=" + parentcsid +
278 " itemcsid=" + itemcsid + " csid=" + csid);
281 // Note that we have to create the service context and document
282 // handler for the Contact service, not the main service.
283 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
284 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
285 getRepositoryClient(ctx).get(ctx, csid, handler);
286 result = (MultipartOutput) ctx.getOutput();
287 } catch (UnauthorizedException ue) {
288 Response response = Response.status(
289 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
290 throw new WebApplicationException(response);
291 } catch (DocumentNotFoundException dnfe) {
292 if (logger.isDebugEnabled()) {
293 logger.debug("getContact", dnfe);
295 Response response = Response.status(Response.Status.NOT_FOUND)
296 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
297 .type("text/plain").build();
298 throw new WebApplicationException(response);
299 } catch (Exception e) {
300 if (logger.isDebugEnabled()) {
301 logger.debug("getContact", e);
303 Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
304 .entity("Get contact failed")
305 .type("text/plain").build();
306 throw new WebApplicationException(response);
308 if (result == null) {
309 Response response = Response.status(Response.Status.NOT_FOUND)
310 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
311 .type("text/plain").build();
312 throw new WebApplicationException(response);
321 * @param parentcsid the parentcsid
322 * @param itemcsid the itemcsid
323 * @param csid the csid
324 * @param theUpdate the the update
326 * @return the multipart output
329 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
330 public MultipartOutput updateContact(
331 @PathParam("parentcsid") String parentcsid,
332 @PathParam("itemcsid") String itemcsid,
333 @PathParam("csid") String csid,
334 MultipartInput theUpdate) {
335 if (logger.isDebugEnabled()) {
336 logger.debug("updateContact with parentcsid=" + parentcsid +
337 " itemcsid=" + itemcsid + " csid=" + csid);
339 if (parentcsid == null || parentcsid.trim().isEmpty()) {
340 logger.error("updateContact: missing csid!");
341 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
342 "update failed on Contact parentcsid=" + parentcsid).type(
343 "text/plain").build();
344 throw new WebApplicationException(response);
346 if (itemcsid == null || itemcsid.trim().isEmpty()) {
347 logger.error("updateContact: missing itemcsid!");
348 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
349 "update failed on Contact=" + itemcsid).type(
350 "text/plain").build();
351 throw new WebApplicationException(response);
353 if (csid == null || csid.trim().isEmpty()) {
354 logger.error("updateContact: missing csid!");
355 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
356 "update failed on Contact=" + csid).type(
357 "text/plain").build();
358 throw new WebApplicationException(response);
360 MultipartOutput result = null;
362 // Note that we have to create the service context and document
363 // handler for the Contact service, not the main service.
364 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(), theUpdate);
365 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
366 getRepositoryClient(ctx).update(ctx, csid, handler);
367 result = (MultipartOutput) ctx.getOutput();
368 } catch (BadRequestException bre) {
369 Response response = Response.status(
370 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
371 throw new WebApplicationException(response);
372 } catch (UnauthorizedException ue) {
373 Response response = Response.status(
374 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
375 throw new WebApplicationException(response);
376 } catch (DocumentNotFoundException dnfe) {
377 if (logger.isDebugEnabled()) {
378 logger.debug("caught exception in updateContact", dnfe);
380 Response response = Response.status(Response.Status.NOT_FOUND).entity(
381 "Update failed on Contact csid=" + itemcsid).type(
382 "text/plain").build();
383 throw new WebApplicationException(response);
384 } catch (Exception e) {
385 Response response = Response.status(
386 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
387 throw new WebApplicationException(response);
395 * @param parentcsid the parentcsid
396 * @param itemcsid the itemcsid
397 * @param csid the csid
399 * @return the response
402 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
403 public Response deleteContact(
404 @PathParam("parentcsid") String parentcsid,
405 @PathParam("itemcsid") String itemcsid,
406 @PathParam("csid") String csid) {
407 if (logger.isDebugEnabled()) {
408 logger.debug("deleteContact with parentCsid=" + parentcsid +
409 " itemcsid=" + itemcsid + " csid=" + csid);
411 if (parentcsid == null || parentcsid.trim().isEmpty()) {
412 logger.error("deleteContact: missing parentcsid!");
413 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
414 "delete contact failed on parentcsid=" + parentcsid).type(
415 "text/plain").build();
416 throw new WebApplicationException(response);
418 if (itemcsid == null || itemcsid.trim().isEmpty()) {
419 logger.error("deleteContact: missing itemcsid!");
420 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
421 "delete contact failed on itemcsid=" + itemcsid).type(
422 "text/plain").build();
423 throw new WebApplicationException(response);
425 if (csid == null || csid.trim().isEmpty()) {
426 logger.error("deleteContact: missing csid!");
427 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
428 "delete contact failed on csid=" + csid).type(
429 "text/plain").build();
430 throw new WebApplicationException(response);
433 // Note that we have to create the service context for the
434 // Contact service, not the main service.
435 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
436 getRepositoryClient(ctx).delete(ctx, csid);
437 return Response.status(HttpResponseCodes.SC_OK).build();
438 } catch (UnauthorizedException ue) {
439 Response response = Response.status(
440 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
441 throw new WebApplicationException(response);
442 } catch (DocumentNotFoundException dnfe) {
443 if (logger.isDebugEnabled()) {
444 logger.debug("Caught exception in deleteContact", dnfe);
446 Response response = Response.status(Response.Status.NOT_FOUND)
447 .entity("Delete failed, the requested Contact CSID:" + csid + ": was not found.")
448 .type("text/plain").build();
449 throw new WebApplicationException(response);
450 } catch (Exception e) {
451 Response response = Response.status(
452 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
453 throw new WebApplicationException(response);