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 private final static String orgAuthorityServiceName = "orgauthorities";
84 private final static String ORGAUTHORITIES_COMMON = "orgauthorities_common";
86 private final static String organizationServiceName = "organizations";
87 private final static String ORGANIZATIONS_COMMON = "organizations_common";
89 final Logger logger = LoggerFactory.getLogger(OrgAuthorityResource.class);
91 /** The contact resource. */
92 private ContactResource contactResource = new ContactResource();
95 * Instantiates a new org authority resource.
97 public OrgAuthorityResource() {
98 super(OrgauthoritiesCommon.class, OrgAuthorityResource.class,
99 ORGAUTHORITIES_COMMON, ORGANIZATIONS_COMMON);
103 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
106 public String getServiceName() {
107 return orgAuthorityServiceName;
111 * Gets the item service name.
113 * @return the item service name
115 public String getItemServiceName() {
116 return organizationServiceName;
120 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
123 public Class<OrgauthoritiesCommon> getCommonPartClass() {
124 return OrgauthoritiesCommon.class;
128 * Gets the contact service name.
130 * @return the contact service name
132 public String getContactServiceName() {
133 return contactResource.getServiceName();
137 * Creates the contact document handler.
140 * @param inAuthority the in authority
141 * @param inItem the in item
143 * @return the document handler
145 * @throws Exception the exception
147 private DocumentHandler createContactDocumentHandler(
148 ServiceContext<MultipartInput, MultipartOutput> ctx, String inAuthority,
149 String inItem) throws Exception {
151 ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler)createDocumentHandler(
153 ctx.getCommonPartLabel(getContactServiceName()),
154 ContactsCommon.class);
155 docHandler.setInAuthority(inAuthority);
156 docHandler.setInItem(inItem);
161 /*************************************************************************
162 * Contact parts - this is a sub-resource of Organization (or "item")
167 *************************************************************************/
169 @Path("{parentcsid}/items/{itemcsid}/contacts")
170 public Response createContact(
171 @PathParam("parentcsid") String parentcsid,
172 @PathParam("itemcsid") String itemcsid,
173 MultipartInput input) {
175 // Note that we have to create the service context and document
176 // handler for the Contact service, not the main service.
177 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(), input);
178 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
179 String csid = getRepositoryClient(ctx).create(ctx, handler);
180 UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
181 path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
182 Response response = Response.created(path.build()).build();
184 } catch (BadRequestException bre) {
185 Response response = Response.status(
186 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
187 throw new WebApplicationException(response);
188 } catch (UnauthorizedException ue) {
189 Response response = Response.status(
190 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
191 throw new WebApplicationException(response);
192 } catch (Exception e) {
193 if (logger.isDebugEnabled()) {
194 logger.debug("Caught exception in createContact", e);
196 Response response = Response.status(
197 Response.Status.INTERNAL_SERVER_ERROR)
198 .entity("Attempt to create Contact failed.")
199 .type("text/plain").build();
200 throw new WebApplicationException(response);
206 * Gets the contact list.
208 * @param parentcsid the parentcsid
209 * @param itemcsid the itemcsid
212 * @return the contact list
215 @Produces({"application/xml"})
216 @Path("{parentcsid}/items/{itemcsid}/contacts/")
217 public ContactsCommonList getContactList(
218 @PathParam("parentcsid") String parentcsid,
219 @PathParam("itemcsid") String itemcsid,
220 @Context UriInfo ui) {
221 ContactsCommonList contactObjectList = new ContactsCommonList();
223 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
224 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(),
226 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
227 DocumentFilter myFilter = handler.getDocumentFilter(); //new DocumentFilter();
228 myFilter.setWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
229 ContactJAXBSchema.IN_AUTHORITY +
230 "='" + parentcsid + "'" +
231 IQueryManager.SEARCH_QUALIFIER_AND +
232 ContactJAXBSchema.CONTACTS_COMMON + ":" +
233 ContactJAXBSchema.IN_ITEM +
234 "='" + itemcsid + "'" );
235 getRepositoryClient(ctx).getFiltered(ctx, handler);
236 contactObjectList = (ContactsCommonList) handler.getCommonPartList();
237 } catch (UnauthorizedException ue) {
238 Response response = Response.status(
239 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
240 throw new WebApplicationException(response);
241 } catch (Exception e) {
242 if (logger.isDebugEnabled()) {
243 logger.debug("Caught exception in getContactsList", e);
245 Response response = Response.status(
246 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
247 throw new WebApplicationException(response);
249 return contactObjectList;
255 * @param parentcsid the parentcsid
256 * @param itemcsid the itemcsid
257 * @param csid the csid
259 * @return the contact
262 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
263 public MultipartOutput getContact(
264 @PathParam("parentcsid") String parentcsid,
265 @PathParam("itemcsid") String itemcsid,
266 @PathParam("csid") String csid) {
267 MultipartOutput result = null;
268 if (logger.isDebugEnabled()) {
269 logger.debug("getContact with parentCsid=" + parentcsid +
270 " itemcsid=" + itemcsid + " csid=" + csid);
273 // Note that we have to create the service context and document
274 // handler for the Contact service, not the main service.
275 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
276 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
277 getRepositoryClient(ctx).get(ctx, csid, handler);
278 result = (MultipartOutput) ctx.getOutput();
279 } catch (UnauthorizedException ue) {
280 Response response = Response.status(
281 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
282 throw new WebApplicationException(response);
283 } catch (DocumentNotFoundException dnfe) {
284 if (logger.isDebugEnabled()) {
285 logger.debug("getContact", dnfe);
287 Response response = Response.status(Response.Status.NOT_FOUND)
288 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
289 .type("text/plain").build();
290 throw new WebApplicationException(response);
291 } catch (Exception e) {
292 if (logger.isDebugEnabled()) {
293 logger.debug("getContact", e);
295 Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
296 .entity("Get contact failed")
297 .type("text/plain").build();
298 throw new WebApplicationException(response);
300 if (result == null) {
301 Response response = Response.status(Response.Status.NOT_FOUND)
302 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
303 .type("text/plain").build();
304 throw new WebApplicationException(response);
313 * @param parentcsid the parentcsid
314 * @param itemcsid the itemcsid
315 * @param csid the csid
316 * @param theUpdate the the update
318 * @return the multipart output
321 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
322 public MultipartOutput updateContact(
323 @PathParam("parentcsid") String parentcsid,
324 @PathParam("itemcsid") String itemcsid,
325 @PathParam("csid") String csid,
326 MultipartInput theUpdate) {
327 if (logger.isDebugEnabled()) {
328 logger.debug("updateContact with parentcsid=" + parentcsid +
329 " itemcsid=" + itemcsid + " csid=" + csid);
331 if (parentcsid == null || parentcsid.trim().isEmpty()) {
332 logger.error("updateContact: missing csid!");
333 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
334 "update failed on Contact parentcsid=" + parentcsid).type(
335 "text/plain").build();
336 throw new WebApplicationException(response);
338 if (itemcsid == null || itemcsid.trim().isEmpty()) {
339 logger.error("updateContact: missing itemcsid!");
340 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
341 "update failed on Contact=" + itemcsid).type(
342 "text/plain").build();
343 throw new WebApplicationException(response);
345 if (csid == null || csid.trim().isEmpty()) {
346 logger.error("updateContact: missing csid!");
347 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
348 "update failed on Contact=" + csid).type(
349 "text/plain").build();
350 throw new WebApplicationException(response);
352 MultipartOutput result = null;
354 // Note that we have to create the service context and document
355 // handler for the Contact service, not the main service.
356 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(), theUpdate);
357 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
358 getRepositoryClient(ctx).update(ctx, csid, handler);
359 result = (MultipartOutput) ctx.getOutput();
360 } catch (BadRequestException bre) {
361 Response response = Response.status(
362 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
363 throw new WebApplicationException(response);
364 } catch (UnauthorizedException ue) {
365 Response response = Response.status(
366 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
367 throw new WebApplicationException(response);
368 } catch (DocumentNotFoundException dnfe) {
369 if (logger.isDebugEnabled()) {
370 logger.debug("caught exception in updateContact", dnfe);
372 Response response = Response.status(Response.Status.NOT_FOUND).entity(
373 "Update failed on Contact csid=" + itemcsid).type(
374 "text/plain").build();
375 throw new WebApplicationException(response);
376 } catch (Exception e) {
377 Response response = Response.status(
378 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
379 throw new WebApplicationException(response);
387 * @param parentcsid the parentcsid
388 * @param itemcsid the itemcsid
389 * @param csid the csid
391 * @return the response
394 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
395 public Response deleteContact(
396 @PathParam("parentcsid") String parentcsid,
397 @PathParam("itemcsid") String itemcsid,
398 @PathParam("csid") String csid) {
399 if (logger.isDebugEnabled()) {
400 logger.debug("deleteContact with parentCsid=" + parentcsid +
401 " itemcsid=" + itemcsid + " csid=" + csid);
403 if (parentcsid == null || parentcsid.trim().isEmpty()) {
404 logger.error("deleteContact: missing parentcsid!");
405 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
406 "delete contact failed on parentcsid=" + parentcsid).type(
407 "text/plain").build();
408 throw new WebApplicationException(response);
410 if (itemcsid == null || itemcsid.trim().isEmpty()) {
411 logger.error("deleteContact: missing itemcsid!");
412 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
413 "delete contact failed on itemcsid=" + itemcsid).type(
414 "text/plain").build();
415 throw new WebApplicationException(response);
417 if (csid == null || csid.trim().isEmpty()) {
418 logger.error("deleteContact: missing csid!");
419 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
420 "delete contact failed on csid=" + csid).type(
421 "text/plain").build();
422 throw new WebApplicationException(response);
425 // Note that we have to create the service context for the
426 // Contact service, not the main service.
427 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
428 getRepositoryClient(ctx).delete(ctx, csid);
429 return Response.status(HttpResponseCodes.SC_OK).build();
430 } catch (UnauthorizedException ue) {
431 Response response = Response.status(
432 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
433 throw new WebApplicationException(response);
434 } catch (DocumentNotFoundException dnfe) {
435 if (logger.isDebugEnabled()) {
436 logger.debug("Caught exception in deleteContact", dnfe);
438 Response response = Response.status(Response.Status.NOT_FOUND)
439 .entity("Delete failed, the requested Contact CSID:" + csid + ": was not found.")
440 .type("text/plain").build();
441 throw new WebApplicationException(response);
442 } catch (Exception e) {
443 Response response = Response.status(
444 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
445 throw new WebApplicationException(response);