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 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;
42 import org.collectionspace.services.OrganizationJAXBSchema;
43 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
44 import org.collectionspace.services.common.ClientType;
45 import org.collectionspace.services.common.ServiceMain;
46 import org.collectionspace.services.common.context.MultipartServiceContext;
47 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
48 import org.collectionspace.services.common.context.ServiceContext;
49 import org.collectionspace.services.common.document.DocumentFilter;
50 import org.collectionspace.services.common.document.DocumentHandler;
51 import org.collectionspace.services.common.document.DocumentHandlerFactory;
52 import org.collectionspace.services.common.document.DocumentNotFoundException;
53 import org.collectionspace.services.common.security.UnauthorizedException;
54 import org.collectionspace.services.common.query.IQueryManager;
55 import org.collectionspace.services.organization.nuxeo.OrganizationDocumentModelHandler;
56 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
57 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
58 import org.jboss.resteasy.util.HttpResponseCodes;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
62 @Path("/orgauthorities")
63 @Consumes("multipart/mixed")
64 @Produces("multipart/mixed")
65 public class OrgAuthorityResource extends AbstractCollectionSpaceResource {
67 private final static String orgAuthorityServiceName = "orgauthorities";
68 private final static String organizationServiceName = "organizations";
69 final Logger logger = LoggerFactory.getLogger(OrgAuthorityResource.class);
70 //FIXME retrieve client type from configuration
71 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
73 public OrgAuthorityResource() {
78 protected String getVersionString() {
79 /** The last change revision. */
80 final String lastChangeRevision = "$LastChangedRevision$";
81 return lastChangeRevision;
85 public String getServiceName() {
86 return orgAuthorityServiceName;
89 public String getItemServiceName() {
90 return organizationServiceName;
94 public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception {
95 RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName());
101 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
102 DocumentHandler docHandler = DocumentHandlerFactory.getInstance().getHandler(
103 ctx.getDocumentHandlerClass());
104 docHandler.setServiceContext(ctx);
105 if (ctx.getInput() != null) {
106 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), OrgauthoritiesCommon.class);
108 docHandler.setCommonPart((OrgauthoritiesCommon) obj);
114 private DocumentHandler createItemDocumentHandler(
116 String inAuthority) throws Exception {
117 DocumentHandler docHandler = DocumentHandlerFactory.getInstance().getHandler(
118 ctx.getDocumentHandlerClass());
119 docHandler.setServiceContext(ctx);
120 ((OrganizationDocumentModelHandler) docHandler).setInAuthority(inAuthority);
121 if (ctx.getInput() != null) {
122 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(getItemServiceName()),
123 OrganizationsCommon.class);
125 docHandler.setCommonPart((OrganizationsCommon) obj);
132 public Response createOrgAuthority(MultipartInput input) {
134 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
135 DocumentHandler handler = createDocumentHandler(ctx);
136 String csid = getRepositoryClient(ctx).create(ctx, handler);
137 //orgAuthorityObject.setCsid(csid);
138 UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
139 path.path("" + csid);
140 Response response = Response.created(path.build()).build();
142 } catch (UnauthorizedException ue) {
143 Response response = Response.status(
144 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
145 throw new WebApplicationException(response);
146 } catch (Exception e) {
147 if (logger.isDebugEnabled()) {
148 logger.debug("Caught exception in createOrgAuthority", e);
150 Response response = Response.status(
151 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
152 throw new WebApplicationException(response);
158 public MultipartOutput getOrgAuthority(@PathParam("csid") String csid) {
159 String idValue = null;
161 logger.error("getOrgAuthority: missing csid!");
162 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
163 "get failed on OrgAuthority csid=" + csid).type(
164 "text/plain").build();
165 throw new WebApplicationException(response);
167 if (logger.isDebugEnabled()) {
168 logger.debug("getOrgAuthority with path(id)=" + csid);
170 MultipartOutput result = null;
172 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
173 DocumentHandler handler = createDocumentHandler(ctx);
174 getRepositoryClient(ctx).get(ctx, csid, handler);
175 result = (MultipartOutput) ctx.getOutput();
176 } catch (UnauthorizedException ue) {
177 Response response = Response.status(
178 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
179 throw new WebApplicationException(response);
180 } catch (DocumentNotFoundException dnfe) {
181 if (logger.isDebugEnabled()) {
182 logger.debug("getOrgAuthority", dnfe);
184 Response response = Response.status(Response.Status.NOT_FOUND).entity(
185 "Get failed on OrgAuthority csid=" + csid).type(
186 "text/plain").build();
187 throw new WebApplicationException(response);
188 } catch (Exception e) {
189 if (logger.isDebugEnabled()) {
190 logger.debug("getOrgAuthority", e);
192 Response response = Response.status(
193 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
194 throw new WebApplicationException(response);
196 if (result == null) {
197 Response response = Response.status(Response.Status.NOT_FOUND).entity(
198 "Get failed, the requested OrgAuthority CSID:" + csid + ": was not found.").type(
199 "text/plain").build();
200 throw new WebApplicationException(response);
206 @Produces("application/xml")
207 public OrgauthoritiesCommonList getOrgAuthorityList(@Context UriInfo ui) {
208 OrgauthoritiesCommonList orgAuthorityObjectList = new OrgauthoritiesCommonList();
210 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
211 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
212 DocumentHandler handler = createDocumentHandler(ctx);
213 DocumentFilter myFilter = new DocumentFilter();
214 myFilter.setPagination(queryParams);
215 String nameQ = queryParams.getFirst("refName");
217 myFilter.setWhereClause("orgauthorities_common:refName='" + nameQ + "'");
219 handler.setDocumentFilter(myFilter);
220 getRepositoryClient(ctx).getFiltered(ctx, handler);
221 orgAuthorityObjectList = (OrgauthoritiesCommonList) handler.getCommonPartList();
222 } catch (UnauthorizedException ue) {
223 Response response = Response.status(
224 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
225 throw new WebApplicationException(response);
226 } catch (Exception e) {
227 if (logger.isDebugEnabled()) {
228 logger.debug("Caught exception in getOrgAuthorityList", e);
230 Response response = Response.status(
231 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
232 throw new WebApplicationException(response);
234 return orgAuthorityObjectList;
239 public MultipartOutput updateOrgAuthority(
240 @PathParam("csid") String csid,
241 MultipartInput theUpdate) {
242 if (logger.isDebugEnabled()) {
243 logger.debug("updateOrgAuthority with csid=" + csid);
245 if (csid == null || "".equals(csid)) {
246 logger.error("updateOrgAuthority: missing csid!");
247 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
248 "update failed on OrgAuthority csid=" + csid).type(
249 "text/plain").build();
250 throw new WebApplicationException(response);
252 MultipartOutput result = null;
254 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
255 DocumentHandler handler = createDocumentHandler(ctx);
256 getRepositoryClient(ctx).update(ctx, csid, handler);
257 result = (MultipartOutput) ctx.getOutput();
258 } catch (UnauthorizedException ue) {
259 Response response = Response.status(
260 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
261 throw new WebApplicationException(response);
262 } catch (DocumentNotFoundException dnfe) {
263 if (logger.isDebugEnabled()) {
264 logger.debug("caugth exception in updateOrgAuthority", dnfe);
266 Response response = Response.status(Response.Status.NOT_FOUND).entity(
267 "Update failed on OrgAuthority csid=" + csid).type(
268 "text/plain").build();
269 throw new WebApplicationException(response);
270 } catch (Exception e) {
271 Response response = Response.status(
272 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
273 throw new WebApplicationException(response);
280 public Response deleteOrgAuthority(@PathParam("csid") String csid) {
282 if (logger.isDebugEnabled()) {
283 logger.debug("deleteOrgAuthority with csid=" + csid);
285 if (csid == null || "".equals(csid)) {
286 logger.error("deleteOrgAuthority: missing csid!");
287 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
288 "delete failed on OrgAuthority csid=" + csid).type(
289 "text/plain").build();
290 throw new WebApplicationException(response);
293 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
294 getRepositoryClient(ctx).delete(ctx, csid);
295 return Response.status(HttpResponseCodes.SC_OK).build();
296 } catch (UnauthorizedException ue) {
297 Response response = Response.status(
298 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
299 throw new WebApplicationException(response);
300 } catch (DocumentNotFoundException dnfe) {
301 if (logger.isDebugEnabled()) {
302 logger.debug("caught exception in deleteOrgAuthority", dnfe);
304 Response response = Response.status(Response.Status.NOT_FOUND).entity(
305 "Delete failed on OrgAuthority csid=" + csid).type(
306 "text/plain").build();
307 throw new WebApplicationException(response);
308 } catch (Exception e) {
309 Response response = Response.status(
310 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
311 throw new WebApplicationException(response);
316 /*************************************************************************
317 * Organization parts - this is a sub-resource of OrgAuthority
318 *************************************************************************/
320 @Path("{csid}/items")
321 public Response createOrganization(@PathParam("csid") String parentcsid, MultipartInput input) {
323 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getItemServiceName());
324 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
325 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
326 UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
327 path.path(parentcsid + "/items/" + itemcsid);
328 Response response = Response.created(path.build()).build();
330 } catch (UnauthorizedException ue) {
331 Response response = Response.status(
332 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
333 throw new WebApplicationException(response);
334 } catch (Exception e) {
335 if (logger.isDebugEnabled()) {
336 logger.debug("Caught exception in createOrganization", e);
338 Response response = Response.status(
339 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
340 throw new WebApplicationException(response);
345 @Path("{csid}/items/{itemcsid}")
346 public MultipartOutput getOrganization(
347 @PathParam("csid") String parentcsid,
348 @PathParam("itemcsid") String itemcsid) {
349 if (logger.isDebugEnabled()) {
350 logger.debug("getOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
352 if (parentcsid == null || "".equals(parentcsid)) {
353 logger.error("getOrganization: missing csid!");
354 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
355 "get failed on Organization csid=" + parentcsid).type(
356 "text/plain").build();
357 throw new WebApplicationException(response);
359 if (itemcsid == null || "".equals(itemcsid)) {
360 logger.error("getOrganization: missing itemcsid!");
361 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
362 "get failed on Organization itemcsid=" + itemcsid).type(
363 "text/plain").build();
364 throw new WebApplicationException(response);
366 MultipartOutput result = null;
368 // Note that we have to create the service context for the Items, not the main service
369 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
370 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
371 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
372 // TODO should we assert that the item is in the passed orgAuthority?
373 result = (MultipartOutput) ctx.getOutput();
374 } catch (UnauthorizedException ue) {
375 Response response = Response.status(
376 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
377 throw new WebApplicationException(response);
378 } catch (DocumentNotFoundException dnfe) {
379 if (logger.isDebugEnabled()) {
380 logger.debug("getOrganization", dnfe);
382 Response response = Response.status(Response.Status.NOT_FOUND).entity(
383 "Get failed on Organization csid=" + itemcsid).type(
384 "text/plain").build();
385 throw new WebApplicationException(response);
386 } catch (Exception e) {
387 if (logger.isDebugEnabled()) {
388 logger.debug("getOrganization", e);
390 Response response = Response.status(
391 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
392 throw new WebApplicationException(response);
394 if (result == null) {
395 Response response = Response.status(Response.Status.NOT_FOUND).entity(
396 "Get failed, the requested Organization CSID:" + itemcsid + ": was not found.").type(
397 "text/plain").build();
398 throw new WebApplicationException(response);
404 @Path("{csid}/items")
405 @Produces("application/xml")
406 public OrganizationsCommonList getOrganizationList(
407 @PathParam("csid") String parentcsid,
408 @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
409 @Context UriInfo ui) {
410 OrganizationsCommonList organizationObjectList = new OrganizationsCommonList();
412 // Note that docType defaults to the ServiceName, so we're fine with that.
413 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
414 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
415 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
416 DocumentFilter myFilter = new DocumentFilter();
417 myFilter.setPagination(queryParams);
418 myFilter.setWhereClause(OrganizationJAXBSchema.ORGANIZATIONS_COMMON +
419 ":" + OrganizationJAXBSchema.IN_AUTHORITY + "=" +
420 "'" + parentcsid + "'");
422 // AND organizations_common:displayName LIKE '%partialTerm%'
423 if (partialTerm != null && !partialTerm.isEmpty()) {
424 String ptClause = "AND " + OrganizationJAXBSchema.ORGANIZATIONS_COMMON +
425 ":" + OrganizationJAXBSchema.DISPLAY_NAME +
426 " LIKE " + "'%" + partialTerm + "%'";
427 myFilter.appendWhereClause(ptClause);
429 handler.setDocumentFilter(myFilter);
430 getRepositoryClient(ctx).getFiltered(ctx, handler);
431 organizationObjectList = (OrganizationsCommonList) handler.getCommonPartList();
432 } catch (UnauthorizedException ue) {
433 Response response = Response.status(
434 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
435 throw new WebApplicationException(response);
436 } catch (Exception e) {
437 if (logger.isDebugEnabled()) {
438 logger.debug("Caught exception in getOrganizationList", e);
440 Response response = Response.status(
441 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
442 throw new WebApplicationException(response);
444 return organizationObjectList;
448 @Path("{csid}/items/{itemcsid}")
449 public MultipartOutput updateOrganization(
450 @PathParam("csid") String parentcsid,
451 @PathParam("itemcsid") String itemcsid,
452 MultipartInput theUpdate) {
453 if (logger.isDebugEnabled()) {
454 logger.debug("updateOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
456 if (parentcsid == null || "".equals(parentcsid)) {
457 logger.error("updateOrganization: missing csid!");
458 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
459 "update failed on Organization parentcsid=" + parentcsid).type(
460 "text/plain").build();
461 throw new WebApplicationException(response);
463 if (itemcsid == null || "".equals(itemcsid)) {
464 logger.error("updateOrganization: missing itemcsid!");
465 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
466 "update failed on Organization=" + itemcsid).type(
467 "text/plain").build();
468 throw new WebApplicationException(response);
470 MultipartOutput result = null;
472 // Note that we have to create the service context for the Items, not the main service
473 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getItemServiceName());
474 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
475 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
476 result = (MultipartOutput) ctx.getOutput();
477 } catch (UnauthorizedException ue) {
478 Response response = Response.status(
479 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
480 throw new WebApplicationException(response);
481 } catch (DocumentNotFoundException dnfe) {
482 if (logger.isDebugEnabled()) {
483 logger.debug("caugth exception in updateOrganization", dnfe);
485 Response response = Response.status(Response.Status.NOT_FOUND).entity(
486 "Update failed on Organization csid=" + itemcsid).type(
487 "text/plain").build();
488 throw new WebApplicationException(response);
489 } catch (Exception e) {
490 Response response = Response.status(
491 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
492 throw new WebApplicationException(response);
498 @Path("{csid}/items/{itemcsid}")
499 public Response deleteOrganization(
500 @PathParam("csid") String parentcsid,
501 @PathParam("itemcsid") String itemcsid) {
502 if (logger.isDebugEnabled()) {
503 logger.debug("deleteOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
505 if (parentcsid == null || "".equals(parentcsid)) {
506 logger.error("deleteOrganization: missing csid!");
507 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
508 "delete failed on Organization parentcsid=" + parentcsid).type(
509 "text/plain").build();
510 throw new WebApplicationException(response);
512 if (itemcsid == null || "".equals(itemcsid)) {
513 logger.error("deleteOrganization: missing itemcsid!");
514 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
515 "delete failed on Organization=" + itemcsid).type(
516 "text/plain").build();
517 throw new WebApplicationException(response);
520 // Note that we have to create the service context for the Items, not the main service
521 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
522 getRepositoryClient(ctx).delete(ctx, itemcsid);
523 return Response.status(HttpResponseCodes.SC_OK).build();
524 } catch (UnauthorizedException ue) {
525 Response response = Response.status(
526 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
527 throw new WebApplicationException(response);
528 } catch (DocumentNotFoundException dnfe) {
529 if (logger.isDebugEnabled()) {
530 logger.debug("caught exception in deleteOrganization", dnfe);
532 Response response = Response.status(Response.Status.NOT_FOUND).entity(
533 "Delete failed on Organization itemcsid=" + itemcsid).type(
534 "text/plain").build();
535 throw new WebApplicationException(response);
536 } catch (Exception e) {
537 Response response = Response.status(
538 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
539 throw new WebApplicationException(response);