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.DocumentNotFoundException;
52 import org.collectionspace.services.common.security.UnauthorizedException;
53 import org.collectionspace.services.common.query.IQueryManager;
54 import org.collectionspace.services.organization.nuxeo.OrgAuthorityHandlerFactory;
55 import org.collectionspace.services.organization.nuxeo.OrganizationDocumentModelHandler;
56 import org.collectionspace.services.organization.nuxeo.OrganizationHandlerFactory;
57 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
58 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
59 import org.jboss.resteasy.util.HttpResponseCodes;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
63 @Path("/orgauthorities")
64 @Consumes("multipart/mixed")
65 @Produces("multipart/mixed")
66 public class OrgAuthorityResource extends AbstractCollectionSpaceResource {
68 private final static String orgAuthorityServiceName = "orgauthorities";
69 private final static String organizationServiceName = "organizations";
70 final Logger logger = LoggerFactory.getLogger(OrgAuthorityResource.class);
71 //FIXME retrieve client type from configuration
72 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
74 public OrgAuthorityResource() {
79 protected String getVersionString() {
80 /** The last change revision. */
81 final String lastChangeRevision = "$LastChangedRevision$";
82 return lastChangeRevision;
86 public String getServiceName() {
87 return orgAuthorityServiceName;
90 public String getItemServiceName() {
91 return organizationServiceName;
95 public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception {
96 RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName());
102 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
103 DocumentHandler docHandler = OrgAuthorityHandlerFactory.getInstance().getHandler(
104 ctx.getRepositoryClientType().toString());
105 docHandler.setServiceContext(ctx);
106 if (ctx.getInput() != null) {
107 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), OrgauthoritiesCommon.class);
109 docHandler.setCommonPart((OrgauthoritiesCommon) obj);
115 private DocumentHandler createItemDocumentHandler(
117 String inAuthority) throws Exception {
118 DocumentHandler docHandler = OrganizationHandlerFactory.getInstance().getHandler(
119 ctx.getRepositoryClientType().toString());
120 docHandler.setServiceContext(ctx);
121 ((OrganizationDocumentModelHandler) docHandler).setInAuthority(inAuthority);
122 if (ctx.getInput() != null) {
123 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(getItemServiceName()),
124 OrganizationsCommon.class);
126 docHandler.setCommonPart((OrganizationsCommon) obj);
133 public Response createOrgAuthority(MultipartInput input) {
135 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
136 DocumentHandler handler = createDocumentHandler(ctx);
137 String csid = getRepositoryClient(ctx).create(ctx, handler);
138 //orgAuthorityObject.setCsid(csid);
139 UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
140 path.path("" + csid);
141 Response response = Response.created(path.build()).build();
143 } catch (UnauthorizedException ue) {
144 Response response = Response.status(
145 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
146 throw new WebApplicationException(response);
147 } catch (Exception e) {
148 if (logger.isDebugEnabled()) {
149 logger.debug("Caught exception in createOrgAuthority", e);
151 Response response = Response.status(
152 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
153 throw new WebApplicationException(response);
159 public MultipartOutput getOrgAuthority(@PathParam("csid") String csid) {
160 String idValue = null;
162 logger.error("getOrgAuthority: missing csid!");
163 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
164 "get failed on OrgAuthority csid=" + csid).type(
165 "text/plain").build();
166 throw new WebApplicationException(response);
168 if (logger.isDebugEnabled()) {
169 logger.debug("getOrgAuthority with path(id)=" + csid);
171 MultipartOutput result = null;
173 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
174 DocumentHandler handler = createDocumentHandler(ctx);
175 getRepositoryClient(ctx).get(ctx, csid, handler);
176 result = (MultipartOutput) ctx.getOutput();
177 } catch (UnauthorizedException ue) {
178 Response response = Response.status(
179 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
180 throw new WebApplicationException(response);
181 } catch (DocumentNotFoundException dnfe) {
182 if (logger.isDebugEnabled()) {
183 logger.debug("getOrgAuthority", dnfe);
185 Response response = Response.status(Response.Status.NOT_FOUND).entity(
186 "Get failed on OrgAuthority csid=" + csid).type(
187 "text/plain").build();
188 throw new WebApplicationException(response);
189 } catch (Exception e) {
190 if (logger.isDebugEnabled()) {
191 logger.debug("getOrgAuthority", e);
193 Response response = Response.status(
194 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
195 throw new WebApplicationException(response);
197 if (result == null) {
198 Response response = Response.status(Response.Status.NOT_FOUND).entity(
199 "Get failed, the requested OrgAuthority CSID:" + csid + ": was not found.").type(
200 "text/plain").build();
201 throw new WebApplicationException(response);
207 @Produces("application/xml")
208 public OrgauthoritiesCommonList getOrgAuthorityList(@Context UriInfo ui) {
209 OrgauthoritiesCommonList orgAuthorityObjectList = new OrgauthoritiesCommonList();
211 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
212 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
213 DocumentHandler handler = createDocumentHandler(ctx);
214 DocumentFilter myFilter = new DocumentFilter();
215 myFilter.setPagination(queryParams);
216 String nameQ = queryParams.getFirst("refName");
218 myFilter.setWhereClause("orgauthorities_common:refName='" + nameQ + "'");
220 handler.setDocumentFilter(myFilter);
221 getRepositoryClient(ctx).getFiltered(ctx, handler);
222 orgAuthorityObjectList = (OrgauthoritiesCommonList) handler.getCommonPartList();
223 } catch (UnauthorizedException ue) {
224 Response response = Response.status(
225 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
226 throw new WebApplicationException(response);
227 } catch (Exception e) {
228 if (logger.isDebugEnabled()) {
229 logger.debug("Caught exception in getOrgAuthorityList", e);
231 Response response = Response.status(
232 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
233 throw new WebApplicationException(response);
235 return orgAuthorityObjectList;
240 public MultipartOutput updateOrgAuthority(
241 @PathParam("csid") String csid,
242 MultipartInput theUpdate) {
243 if (logger.isDebugEnabled()) {
244 logger.debug("updateOrgAuthority with csid=" + csid);
246 if (csid == null || "".equals(csid)) {
247 logger.error("updateOrgAuthority: missing csid!");
248 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
249 "update failed on OrgAuthority csid=" + csid).type(
250 "text/plain").build();
251 throw new WebApplicationException(response);
253 MultipartOutput result = null;
255 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
256 DocumentHandler handler = createDocumentHandler(ctx);
257 getRepositoryClient(ctx).update(ctx, csid, handler);
258 result = (MultipartOutput) ctx.getOutput();
259 } catch (UnauthorizedException ue) {
260 Response response = Response.status(
261 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
262 throw new WebApplicationException(response);
263 } catch (DocumentNotFoundException dnfe) {
264 if (logger.isDebugEnabled()) {
265 logger.debug("caugth exception in updateOrgAuthority", dnfe);
267 Response response = Response.status(Response.Status.NOT_FOUND).entity(
268 "Update failed on OrgAuthority csid=" + csid).type(
269 "text/plain").build();
270 throw new WebApplicationException(response);
271 } catch (Exception e) {
272 Response response = Response.status(
273 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
274 throw new WebApplicationException(response);
281 public Response deleteOrgAuthority(@PathParam("csid") String csid) {
283 if (logger.isDebugEnabled()) {
284 logger.debug("deleteOrgAuthority with csid=" + csid);
286 if (csid == null || "".equals(csid)) {
287 logger.error("deleteOrgAuthority: missing csid!");
288 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
289 "delete failed on OrgAuthority csid=" + csid).type(
290 "text/plain").build();
291 throw new WebApplicationException(response);
294 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
295 getRepositoryClient(ctx).delete(ctx, csid);
296 return Response.status(HttpResponseCodes.SC_OK).build();
297 } catch (UnauthorizedException ue) {
298 Response response = Response.status(
299 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
300 throw new WebApplicationException(response);
301 } catch (DocumentNotFoundException dnfe) {
302 if (logger.isDebugEnabled()) {
303 logger.debug("caught exception in deleteOrgAuthority", dnfe);
305 Response response = Response.status(Response.Status.NOT_FOUND).entity(
306 "Delete failed on OrgAuthority csid=" + csid).type(
307 "text/plain").build();
308 throw new WebApplicationException(response);
309 } catch (Exception e) {
310 Response response = Response.status(
311 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
312 throw new WebApplicationException(response);
317 /*************************************************************************
318 * Organization parts - this is a sub-resource of OrgAuthority
319 *************************************************************************/
321 @Path("{csid}/items")
322 public Response createOrganization(@PathParam("csid") String parentcsid, MultipartInput input) {
324 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getItemServiceName());
325 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
326 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
327 UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
328 path.path(parentcsid + "/items/" + itemcsid);
329 Response response = Response.created(path.build()).build();
331 } catch (UnauthorizedException ue) {
332 Response response = Response.status(
333 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
334 throw new WebApplicationException(response);
335 } catch (Exception e) {
336 if (logger.isDebugEnabled()) {
337 logger.debug("Caught exception in createOrganization", e);
339 Response response = Response.status(
340 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
341 throw new WebApplicationException(response);
346 @Path("{csid}/items/{itemcsid}")
347 public MultipartOutput getOrganization(
348 @PathParam("csid") String parentcsid,
349 @PathParam("itemcsid") String itemcsid) {
350 if (logger.isDebugEnabled()) {
351 logger.debug("getOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
353 if (parentcsid == null || "".equals(parentcsid)) {
354 logger.error("getOrganization: missing csid!");
355 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
356 "get failed on Organization csid=" + parentcsid).type(
357 "text/plain").build();
358 throw new WebApplicationException(response);
360 if (itemcsid == null || "".equals(itemcsid)) {
361 logger.error("getOrganization: missing itemcsid!");
362 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
363 "get failed on Organization itemcsid=" + itemcsid).type(
364 "text/plain").build();
365 throw new WebApplicationException(response);
367 MultipartOutput result = null;
369 // Note that we have to create the service context for the Items, not the main service
370 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
371 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
372 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
373 // TODO should we assert that the item is in the passed orgAuthority?
374 result = (MultipartOutput) ctx.getOutput();
375 } catch (UnauthorizedException ue) {
376 Response response = Response.status(
377 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
378 throw new WebApplicationException(response);
379 } catch (DocumentNotFoundException dnfe) {
380 if (logger.isDebugEnabled()) {
381 logger.debug("getOrganization", dnfe);
383 Response response = Response.status(Response.Status.NOT_FOUND).entity(
384 "Get failed on Organization csid=" + itemcsid).type(
385 "text/plain").build();
386 throw new WebApplicationException(response);
387 } catch (Exception e) {
388 if (logger.isDebugEnabled()) {
389 logger.debug("getOrganization", e);
391 Response response = Response.status(
392 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
393 throw new WebApplicationException(response);
395 if (result == null) {
396 Response response = Response.status(Response.Status.NOT_FOUND).entity(
397 "Get failed, the requested Organization CSID:" + itemcsid + ": was not found.").type(
398 "text/plain").build();
399 throw new WebApplicationException(response);
405 @Path("{csid}/items")
406 @Produces("application/xml")
407 public OrganizationsCommonList getOrganizationList(
408 @PathParam("csid") String parentcsid,
409 @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
410 @Context UriInfo ui) {
411 OrganizationsCommonList organizationObjectList = new OrganizationsCommonList();
413 // Note that docType defaults to the ServiceName, so we're fine with that.
414 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
415 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
416 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
417 DocumentFilter myFilter = new DocumentFilter();
418 myFilter.setPagination(queryParams);
419 myFilter.setWhereClause(OrganizationJAXBSchema.ORGANIZATIONS_COMMON +
420 ":" + OrganizationJAXBSchema.IN_AUTHORITY + "=" +
421 "'" + parentcsid + "'");
423 // AND organizations_common:displayName LIKE '%partialTerm%'
424 if (partialTerm != null && !partialTerm.isEmpty()) {
425 String ptClause = "AND " + OrganizationJAXBSchema.ORGANIZATIONS_COMMON +
426 ":" + OrganizationJAXBSchema.DISPLAY_NAME +
427 " LIKE " + "'%" + partialTerm + "%'";
428 myFilter.appendWhereClause(ptClause);
430 handler.setDocumentFilter(myFilter);
431 getRepositoryClient(ctx).getFiltered(ctx, handler);
432 organizationObjectList = (OrganizationsCommonList) handler.getCommonPartList();
433 } catch (UnauthorizedException ue) {
434 Response response = Response.status(
435 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
436 throw new WebApplicationException(response);
437 } catch (Exception e) {
438 if (logger.isDebugEnabled()) {
439 logger.debug("Caught exception in getOrganizationList", e);
441 Response response = Response.status(
442 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
443 throw new WebApplicationException(response);
445 return organizationObjectList;
449 @Path("{csid}/items/{itemcsid}")
450 public MultipartOutput updateOrganization(
451 @PathParam("csid") String parentcsid,
452 @PathParam("itemcsid") String itemcsid,
453 MultipartInput theUpdate) {
454 if (logger.isDebugEnabled()) {
455 logger.debug("updateOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
457 if (parentcsid == null || "".equals(parentcsid)) {
458 logger.error("updateOrganization: missing csid!");
459 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
460 "update failed on Organization parentcsid=" + parentcsid).type(
461 "text/plain").build();
462 throw new WebApplicationException(response);
464 if (itemcsid == null || "".equals(itemcsid)) {
465 logger.error("updateOrganization: missing itemcsid!");
466 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
467 "update failed on Organization=" + itemcsid).type(
468 "text/plain").build();
469 throw new WebApplicationException(response);
471 MultipartOutput result = null;
473 // Note that we have to create the service context for the Items, not the main service
474 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getItemServiceName());
475 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
476 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
477 result = (MultipartOutput) ctx.getOutput();
478 } catch (UnauthorizedException ue) {
479 Response response = Response.status(
480 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
481 throw new WebApplicationException(response);
482 } catch (DocumentNotFoundException dnfe) {
483 if (logger.isDebugEnabled()) {
484 logger.debug("caugth exception in updateOrganization", dnfe);
486 Response response = Response.status(Response.Status.NOT_FOUND).entity(
487 "Update failed on Organization csid=" + itemcsid).type(
488 "text/plain").build();
489 throw new WebApplicationException(response);
490 } catch (Exception e) {
491 Response response = Response.status(
492 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
493 throw new WebApplicationException(response);
499 @Path("{csid}/items/{itemcsid}")
500 public Response deleteOrganization(
501 @PathParam("csid") String parentcsid,
502 @PathParam("itemcsid") String itemcsid) {
503 if (logger.isDebugEnabled()) {
504 logger.debug("deleteOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
506 if (parentcsid == null || "".equals(parentcsid)) {
507 logger.error("deleteOrganization: missing csid!");
508 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
509 "delete failed on Organization parentcsid=" + parentcsid).type(
510 "text/plain").build();
511 throw new WebApplicationException(response);
513 if (itemcsid == null || "".equals(itemcsid)) {
514 logger.error("deleteOrganization: missing itemcsid!");
515 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
516 "delete failed on Organization=" + itemcsid).type(
517 "text/plain").build();
518 throw new WebApplicationException(response);
521 // Note that we have to create the service context for the Items, not the main service
522 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
523 getRepositoryClient(ctx).delete(ctx, itemcsid);
524 return Response.status(HttpResponseCodes.SC_OK).build();
525 } catch (UnauthorizedException ue) {
526 Response response = Response.status(
527 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
528 throw new WebApplicationException(response);
529 } catch (DocumentNotFoundException dnfe) {
530 if (logger.isDebugEnabled()) {
531 logger.debug("caught exception in deleteOrganization", dnfe);
533 Response response = Response.status(Response.Status.NOT_FOUND).entity(
534 "Delete failed on Organization itemcsid=" + itemcsid).type(
535 "text/plain").build();
536 throw new WebApplicationException(response);
537 } catch (Exception e) {
538 Response response = Response.status(
539 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
540 throw new WebApplicationException(response);