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.person;
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.context.ServiceBindingUtils;
47 import org.collectionspace.services.common.context.ServiceContext;
48 import org.collectionspace.services.common.document.BadRequestException;
49 import org.collectionspace.services.common.document.DocumentFilter;
50 import org.collectionspace.services.common.document.DocumentHandler;
51 import org.collectionspace.services.common.document.DocumentNotFoundException;
52 import org.collectionspace.services.common.document.DocumentWrapper;
53 import org.collectionspace.services.common.repository.RepositoryClient;
54 import org.collectionspace.services.common.security.UnauthorizedException;
55 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
56 import org.collectionspace.services.contact.ContactResource;
57 import org.collectionspace.services.contact.ContactsCommon;
58 import org.collectionspace.services.contact.ContactsCommonList;
59 import org.collectionspace.services.contact.ContactJAXBSchema;
60 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
61 import org.collectionspace.services.person.nuxeo.PersonDocumentModelHandler;
63 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
64 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
65 import org.jboss.resteasy.util.HttpResponseCodes;
67 import org.nuxeo.ecm.core.api.DocumentModel;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
73 * The Class PersonAuthorityResource.
75 @Path("/personauthorities")
76 @Consumes("multipart/mixed")
77 @Produces("multipart/mixed")
78 public class PersonAuthorityResource extends
79 AuthorityResource<PersonauthoritiesCommon, PersonauthoritiesCommonList, PersonsCommon,
80 PersonDocumentModelHandler> {
82 private final static String personAuthorityServiceName = "personauthorities";
83 private final static String PERSONAUTHORITIES_COMMON = "personauthorities_common";
85 private final static String personServiceName = "persons";
86 private final static String PERSONS_COMMON = "persons_common";
88 final Logger logger = LoggerFactory.getLogger(PersonAuthorityResource.class);
89 //FIXME retrieve client type from configuration
90 /** The Constant CLIENT_TYPE. */
91 //final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
93 /** The contact resource. */
94 private ContactResource contactResource = new ContactResource();
97 * Instantiates a new person authority resource.
99 public PersonAuthorityResource() {
100 super(PersonauthoritiesCommon.class, PersonAuthorityResource.class,
101 PERSONAUTHORITIES_COMMON, PERSONS_COMMON);
105 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
108 public String getServiceName() {
109 return personAuthorityServiceName;
113 * Gets the item service name.
115 * @return the item service name
118 public String getItemServiceName() {
119 return personServiceName;
123 public Class<PersonauthoritiesCommon> getCommonPartClass() {
124 return PersonauthoritiesCommon.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 {
150 ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler)createDocumentHandler(ctx,
151 ctx.getCommonPartLabel(getContactServiceName()),
152 ContactsCommon.class);
153 docHandler.setInAuthority(inAuthority);
154 docHandler.setInItem(inItem);
160 * ***********************************************************************
161 * Contact parts - this is a sub-resource of Person (or "item")
162 * ***********************************************************************.
164 * @param parentcsid the parentcsid
165 * @param itemcsid the itemcsid
166 * @param input the input
167 * @return the response
170 @Path("{parentcsid}/items/{itemcsid}/contacts")
171 public Response createContact(
172 @PathParam("parentcsid") String parentcsid,
173 @PathParam("itemcsid") String itemcsid,
174 MultipartInput input) {
176 // Note that we have to create the service context and document
177 // handler for the Contact service, not the main service.
178 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(), input);
179 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
180 String csid = getRepositoryClient(ctx).create(ctx, handler);
181 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
182 path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
183 Response response = Response.created(path.build()).build();
185 } catch (BadRequestException bre) {
186 Response response = Response.status(
187 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
188 throw new WebApplicationException(response);
189 } catch (UnauthorizedException ue) {
190 Response response = Response.status(
191 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
192 throw new WebApplicationException(response);
193 } catch (Exception e) {
194 if (logger.isDebugEnabled()) {
195 logger.debug("Caught exception in createContact", e);
197 Response response = Response.status(
198 Response.Status.INTERNAL_SERVER_ERROR)
199 .entity("Attempt to create Contact failed.")
200 .type("text/plain").build();
201 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 handler.getDocumentFilter().setWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
228 ContactJAXBSchema.IN_AUTHORITY +
229 "='" + parentcsid + "'" +
231 ContactJAXBSchema.CONTACTS_COMMON + ":" +
232 ContactJAXBSchema.IN_ITEM +
233 "='" + itemcsid + "'" );
234 getRepositoryClient(ctx).getFiltered(ctx, handler);
235 contactObjectList = (ContactsCommonList) handler.getCommonPartList();
236 } catch (UnauthorizedException ue) {
237 Response response = Response.status(
238 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
239 throw new WebApplicationException(response);
240 } catch (Exception e) {
241 if (logger.isDebugEnabled()) {
242 logger.debug("Caught exception in getContactsList", e);
244 Response response = Response.status(
245 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
246 throw new WebApplicationException(response);
248 return contactObjectList;
254 * @param parentcsid the parentcsid
255 * @param itemcsid the itemcsid
256 * @param csid the csid
258 * @return the contact
261 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
262 public MultipartOutput getContact(
263 @PathParam("parentcsid") String parentcsid,
264 @PathParam("itemcsid") String itemcsid,
265 @PathParam("csid") String csid) {
266 MultipartOutput result = null;
267 if (logger.isDebugEnabled()) {
268 logger.debug("getContact with parentCsid=" + parentcsid +
269 " itemcsid=" + itemcsid + " csid=" + csid);
272 // Note that we have to create the service context and document
273 // handler for the Contact service, not the main service.
274 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
275 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
276 getRepositoryClient(ctx).get(ctx, csid, handler);
277 result = (MultipartOutput) ctx.getOutput();
278 } catch (UnauthorizedException ue) {
279 Response response = Response.status(
280 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
281 throw new WebApplicationException(response);
282 } catch (DocumentNotFoundException dnfe) {
283 if (logger.isDebugEnabled()) {
284 logger.debug("getContact", dnfe);
286 Response response = Response.status(Response.Status.NOT_FOUND)
287 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
288 .type("text/plain").build();
289 throw new WebApplicationException(response);
290 } catch (Exception e) {
291 if (logger.isDebugEnabled()) {
292 logger.debug("getContact", e);
294 Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
295 .entity("Get contact failed")
296 .type("text/plain").build();
297 throw new WebApplicationException(response);
299 if (result == null) {
300 Response response = Response.status(Response.Status.NOT_FOUND)
301 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
302 .type("text/plain").build();
303 throw new WebApplicationException(response);
312 * @param parentcsid the parentcsid
313 * @param itemcsid the itemcsid
314 * @param csid the csid
315 * @param theUpdate the the update
317 * @return the multipart output
320 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
321 public MultipartOutput updateContact(
322 @PathParam("parentcsid") String parentcsid,
323 @PathParam("itemcsid") String itemcsid,
324 @PathParam("csid") String csid,
325 MultipartInput theUpdate) {
326 if (logger.isDebugEnabled()) {
327 logger.debug("updateContact with parentcsid=" + parentcsid +
328 " itemcsid=" + itemcsid + " csid=" + csid);
330 if (parentcsid == null || parentcsid.trim().isEmpty()) {
331 logger.error("updateContact: missing csid!");
332 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
333 "update failed on Contact parentcsid=" + parentcsid).type(
334 "text/plain").build();
335 throw new WebApplicationException(response);
337 if (itemcsid == null || itemcsid.trim().isEmpty()) {
338 logger.error("updateContact: missing itemcsid!");
339 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
340 "update failed on Contact=" + itemcsid).type(
341 "text/plain").build();
342 throw new WebApplicationException(response);
344 if (csid == null || csid.trim().isEmpty()) {
345 logger.error("updateContact: missing csid!");
346 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
347 "update failed on Contact=" + csid).type(
348 "text/plain").build();
349 throw new WebApplicationException(response);
351 MultipartOutput result = null;
353 // Note that we have to create the service context and document
354 // handler for the Contact service, not the main service.
355 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(),
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);