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.common.vocabulary;
26 import java.util.List;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.Encoded;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.PUT;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.QueryParam;
38 import javax.ws.rs.WebApplicationException;
39 import javax.ws.rs.core.Context;
40 import javax.ws.rs.core.MultivaluedMap;
41 import javax.ws.rs.core.Request;
42 import javax.ws.rs.core.Response;
43 import javax.ws.rs.core.UriBuilder;
44 import javax.ws.rs.core.UriInfo;
46 import org.collectionspace.services.client.IQueryManager;
47 import org.collectionspace.services.client.PoxPayloadIn;
48 import org.collectionspace.services.client.PoxPayloadOut;
49 import org.collectionspace.services.client.workflow.WorkflowClient;
50 import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema;
51 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
52 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityItemDocumentModelHandler;
53 import org.collectionspace.services.common.workflow.service.nuxeo.WorkflowDocumentModelHandler;
54 import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
55 import org.collectionspace.services.common.ClientType;
56 import org.collectionspace.services.common.ServiceMain;
57 import org.collectionspace.services.common.ServiceMessages;
58 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
59 import org.collectionspace.services.common.authorityref.AuthorityRefList;
60 import org.collectionspace.services.common.context.JaxRsContext;
61 import org.collectionspace.services.common.context.MultipartServiceContext;
62 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
63 import org.collectionspace.services.common.context.RemoteServiceContext;
64 import org.collectionspace.services.common.context.ServiceBindingUtils;
65 import org.collectionspace.services.common.context.ServiceContext;
66 import org.collectionspace.services.common.document.BadRequestException;
67 import org.collectionspace.services.common.document.DocumentFilter;
68 import org.collectionspace.services.common.document.DocumentHandler;
69 import org.collectionspace.services.common.document.DocumentNotFoundException;
70 import org.collectionspace.services.common.document.DocumentWrapper;
71 import org.collectionspace.services.common.repository.RepositoryClient;
72 import org.collectionspace.services.common.security.UnauthorizedException;
73 import org.collectionspace.services.common.query.QueryManager;
74 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
75 import org.jboss.resteasy.util.HttpResponseCodes;
76 import org.nuxeo.ecm.core.api.DocumentModel;
77 import org.slf4j.Logger;
78 import org.slf4j.LoggerFactory;
81 * The Class AuthorityResource.
83 @Consumes("application/xml")
84 @Produces("application/xml")
85 public abstract class AuthorityResource<AuthCommon, AuthCommonList, AuthItemCommonList, AuthItemHandler> extends
86 AbstractMultiPartCollectionSpaceResourceImpl {
88 protected Class<AuthCommon> authCommonClass;
89 protected Class<?> resourceClass;
90 protected String authorityCommonSchemaName;
91 protected String authorityItemCommonSchemaName;
93 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
95 final static String URN_PREFIX = "urn:cspace:";
96 final static int URN_PREFIX_LEN = URN_PREFIX.length();
97 final static String URN_PREFIX_NAME = "name(";
98 final static int URN_NAME_PREFIX_LEN = URN_PREFIX_LEN + URN_PREFIX_NAME.length();
99 final static String URN_PREFIX_ID = "id(";
100 final static int URN_ID_PREFIX_LEN = URN_PREFIX_LEN + URN_PREFIX_ID.length();
102 final Logger logger = LoggerFactory.getLogger(AuthorityResource.class);
104 public enum SpecifierForm { CSID, URN_NAME };
106 public class Specifier {
107 public SpecifierForm form;
109 Specifier(SpecifierForm form, String value) {
115 protected Specifier getSpecifier(String specifierIn, String method, String op) throws WebApplicationException {
116 if (logger.isDebugEnabled()) {
117 logger.debug("getSpecifier called by: "+method+" with specifier: "+specifierIn);
119 if (specifierIn != null) {
120 if(!specifierIn.startsWith(URN_PREFIX)) {
121 // We'll assume it is a CSID and complain if it does not match
122 return new Specifier(SpecifierForm.CSID, specifierIn);
124 if(specifierIn.startsWith(URN_PREFIX_NAME, URN_PREFIX_LEN)) {
125 int closeParen = specifierIn.indexOf(')', URN_NAME_PREFIX_LEN);
127 return new Specifier(SpecifierForm.URN_NAME,
128 specifierIn.substring(URN_NAME_PREFIX_LEN, closeParen));
130 } else if(specifierIn.startsWith(URN_PREFIX_ID, URN_PREFIX_LEN)) {
131 int closeParen = specifierIn.indexOf(')', URN_ID_PREFIX_LEN);
133 return new Specifier(SpecifierForm.CSID,
134 specifierIn.substring(URN_ID_PREFIX_LEN, closeParen));
139 logger.error(method+": bad or missing specifier!");
140 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
141 op+" failed on bad or missing Authority specifier").type(
142 "text/plain").build();
143 throw new WebApplicationException(response);
147 * Instantiates a new Authority resource.
149 public AuthorityResource(Class<AuthCommon> authCommonClass, Class<?> resourceClass,
150 String authorityCommonSchemaName, String authorityItemCommonSchemaName) {
151 this.authCommonClass = authCommonClass;
152 this.resourceClass = resourceClass;
153 this.authorityCommonSchemaName = authorityCommonSchemaName;
154 this.authorityItemCommonSchemaName = authorityItemCommonSchemaName;
157 public abstract String getItemServiceName();
160 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
163 protected String getVersionString() {
164 /** The last change revision. */
165 final String lastChangeRevision = "$LastChangedRevision: 2617 $";
166 return lastChangeRevision;
170 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
173 public Class<AuthCommon> getCommonPartClass() {
174 return authCommonClass;
178 * Creates the item document handler.
181 * @param inAuthority the in vocabulary
183 * @return the document handler
185 * @throws Exception the exception
187 public DocumentHandler createItemDocumentHandler(
188 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
191 AuthItemHandler docHandler;
193 docHandler = (AuthItemHandler)createDocumentHandler(ctx,
194 ctx.getCommonPartLabel(getItemServiceName()),
196 ((AuthorityItemDocumentModelHandler<?,?>)docHandler).setInAuthority(inAuthority);
198 return (DocumentHandler)docHandler;
202 * Creates the authority.
204 * @param input the input
206 * @return the response
209 public Response createAuthority(String xmlPayload) {
211 PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
212 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(input);
213 DocumentHandler handler = createDocumentHandler(ctx);
214 String csid = getRepositoryClient(ctx).create(ctx, handler);
215 UriBuilder path = UriBuilder.fromResource(resourceClass);
216 path.path("" + csid);
217 Response response = Response.created(path.build()).build();
219 } catch (BadRequestException bre) {
220 Response response = Response.status(
221 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
222 throw new WebApplicationException(response);
223 } catch (UnauthorizedException ue) {
224 Response response = Response.status(
225 Response.Status.UNAUTHORIZED).entity("Create 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 createVocabulary", e);
231 Response response = Response.status(
232 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
233 throw new WebApplicationException(response);
237 protected String buildWhereForAuthByName(String name) {
238 return authorityCommonSchemaName+
239 ":"+AuthorityJAXBSchema.SHORT_IDENTIFIER+
243 protected String buildWhereForAuthItemByName(String name, String parentcsid) {
245 authorityItemCommonSchemaName+
246 ":"+AuthorityItemJAXBSchema.SHORT_IDENTIFIER+
248 + authorityItemCommonSchemaName + ":"
249 + AuthorityItemJAXBSchema.IN_AUTHORITY + "="
250 + "'" + parentcsid + "'";
254 * Gets the authority.
256 * @param specifier either a CSID or one of the urn forms
258 * @return the authority
262 public byte[] getAuthority(
264 @PathParam("csid") String specifier) {
265 PoxPayloadOut result = null;
267 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
268 Specifier spec = getSpecifier(specifier, "getAuthority", "GET");
269 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(queryParams);
270 DocumentHandler handler = createDocumentHandler(ctx);
271 if(spec.form == SpecifierForm.CSID) {
272 if (logger.isDebugEnabled()) {
273 logger.debug("getAuthority with csid=" + spec.value);
275 getRepositoryClient(ctx).get(ctx, spec.value, handler);
277 String whereClause = buildWhereForAuthByName(spec.value);
278 DocumentFilter myFilter = new DocumentFilter(whereClause, 0, 1);
279 handler.setDocumentFilter(myFilter);
280 getRepositoryClient(ctx).get(ctx, handler);
282 result = ctx.getOutput();
283 } catch (UnauthorizedException ue) {
284 Response response = Response.status(
285 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
286 throw new WebApplicationException(response);
287 } catch (DocumentNotFoundException dnfe) {
288 if (logger.isDebugEnabled()) {
289 logger.debug("getAuthority", dnfe);
291 Response response = Response.status(Response.Status.NOT_FOUND).entity(
292 "Get failed on Authority specifier=" + specifier).type(
293 "text/plain").build();
294 throw new WebApplicationException(response);
295 } catch (Exception e) {
296 if (logger.isDebugEnabled()) {
297 logger.debug("getAuthority", e);
299 Response response = Response.status(
300 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
301 throw new WebApplicationException(response);
304 if (result == null) {
305 Response response = Response.status(Response.Status.NOT_FOUND).entity(
306 "Get failed, the requested Authority specifier:" + specifier + ": was not found.").type(
307 "text/plain").build();
308 throw new WebApplicationException(response);
311 return result.getBytes();
315 * Finds and populates the authority list.
319 * @return the authority list
322 @Produces("application/xml")
323 public AuthCommonList getAuthorityList(@Context UriInfo ui) {
325 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
326 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(queryParams);
327 DocumentHandler handler = createDocumentHandler(ctx);
328 DocumentFilter myFilter = handler.getDocumentFilter();
329 String nameQ = queryParams.getFirst("refName");
331 myFilter.setWhereClause(authorityCommonSchemaName+":refName='" + nameQ + "'");
333 getRepositoryClient(ctx).getFiltered(ctx, handler);
334 return (AuthCommonList) handler.getCommonPartList();
335 } catch (UnauthorizedException ue) {
336 Response response = Response.status(
337 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
338 throw new WebApplicationException(response);
339 } catch (Exception e) {
340 if (logger.isDebugEnabled()) {
341 logger.debug("Caught exception in getAuthorityList", e);
343 Response response = Response.status(
344 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
345 throw new WebApplicationException(response);
352 * @param specifier the csid or id
353 * @param theUpdate the the update
355 * @return the multipart output
359 public byte[] updateAuthority(
360 @PathParam("csid") String specifier,
362 PoxPayloadOut result = null;
364 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
365 Specifier spec = getSpecifier(specifier, "updateAuthority", "UPDATE");
366 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(theUpdate);
367 DocumentHandler handler = createDocumentHandler(ctx);
369 if(spec.form==SpecifierForm.CSID) {
372 String whereClause = buildWhereForAuthByName(spec.value);
373 csid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
375 getRepositoryClient(ctx).update(ctx, csid, handler);
376 result = ctx.getOutput();
377 } catch (UnauthorizedException ue) {
378 Response response = Response.status(
379 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
380 throw new WebApplicationException(response);
381 } catch (DocumentNotFoundException dnfe) {
382 if (logger.isDebugEnabled()) {
383 logger.debug("caught exception in updateAuthority", dnfe);
385 Response response = Response.status(Response.Status.NOT_FOUND).entity(
386 "Update failed on Authority specifier=" + specifier).type(
387 "text/plain").build();
388 throw new WebApplicationException(response);
389 } catch (Exception e) {
390 Response response = Response.status(
391 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
392 throw new WebApplicationException(response);
394 return result.getBytes();
400 * @param csid the csid
402 * @return the response
406 public Response deleteAuthority(@PathParam("csid") String csid) {
408 if (logger.isDebugEnabled()) {
409 logger.debug("deleteAuthority with csid=" + csid);
411 if (csid == null || "".equals(csid)) {
412 logger.error("deleteAuthority: missing csid!");
413 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
414 "delete failed on Authority csid=" + csid).type(
415 "text/plain").build();
416 throw new WebApplicationException(response);
419 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
420 getRepositoryClient(ctx).delete(ctx, csid);
421 return Response.status(HttpResponseCodes.SC_OK).build();
422 } catch (UnauthorizedException ue) {
423 Response response = Response.status(
424 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
425 throw new WebApplicationException(response);
426 } catch (DocumentNotFoundException dnfe) {
427 if (logger.isDebugEnabled()) {
428 logger.debug("caught exception in deleteAuthority", dnfe);
430 Response response = Response.status(Response.Status.NOT_FOUND).entity(
431 "Delete failed on Authority csid=" + csid).type(
432 "text/plain").build();
433 throw new WebApplicationException(response);
434 } catch (Exception e) {
435 Response response = Response.status(
436 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
437 throw new WebApplicationException(response);
442 /*************************************************************************
443 * Create an AuthorityItem - this is a sub-resource of Authority
444 * @param specifier either a CSID or one of the urn forms
445 * @param input the payload
446 * @return Authority item response
447 *************************************************************************/
449 @Path("{csid}/items")
450 public Response createAuthorityItem(@PathParam("csid") String specifier, String xmlPayload) {
452 PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
453 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
454 Specifier spec = getSpecifier(specifier, "createAuthorityItem", "CREATE_ITEM");
456 if(spec.form==SpecifierForm.CSID) {
457 parentcsid = spec.value;
459 String whereClause = buildWhereForAuthByName(spec.value);
460 ctx = createServiceContext(getServiceName());
461 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
463 ctx = createServiceContext(getItemServiceName(), input);
464 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
465 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
466 UriBuilder path = UriBuilder.fromResource(resourceClass);
467 path.path(parentcsid + "/items/" + itemcsid);
468 Response response = Response.created(path.build()).build();
470 } catch (BadRequestException bre) {
471 Response response = Response.status(
472 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
473 throw new WebApplicationException(response);
474 } catch (UnauthorizedException ue) {
475 Response response = Response.status(
476 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
477 throw new WebApplicationException(response);
478 } catch (Exception e) {
479 if (logger.isDebugEnabled()) {
480 logger.debug("Caught exception in createAuthorityItem", e);
482 Response response = Response.status(
483 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
484 throw new WebApplicationException(response);
489 @Path("{csid}/items/{itemcsid}" + WorkflowClient.SERVICE_PATH)
490 public byte[] getItemWorkflow(
491 @PathParam("csid") String csid,
492 @PathParam("itemcsid") String itemcsid) {
493 PoxPayloadOut result = null;
496 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext(getItemServiceName());
497 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
499 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME);
500 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
501 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
502 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
503 result = ctx.getOutput();
504 } catch (Exception e) {
505 throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
508 return result.getBytes();
512 @Path("{csid}/items/{itemcsid}" + WorkflowClient.SERVICE_PATH)
513 public byte[] updateWorkflow(
514 @PathParam("csid") String csid,
515 @PathParam("itemcsid") String itemcsid,
517 PoxPayloadOut result = null;
519 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext(getItemServiceName());
520 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
522 PoxPayloadIn workflowUpdate = new PoxPayloadIn(xmlPayload);
523 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME, workflowUpdate);
524 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
525 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
526 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
527 result = ctx.getOutput();
528 } catch (Exception e) {
529 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
531 return result.getBytes();
536 * Gets the authority item.
538 * @param parentspecifier either a CSID or one of the urn forms
539 * @param itemspecifier either a CSID or one of the urn forms
541 * @return the authority item
544 @Path("{csid}/items/{itemcsid}")
545 public byte[] getAuthorityItem(
546 @Context Request request,
548 @PathParam("csid") String parentspecifier,
549 @PathParam("itemcsid") String itemspecifier) {
550 PoxPayloadOut result = null;
552 JaxRsContext jaxRsContext = new JaxRsContext(request, ui);
554 Specifier parentSpec = getSpecifier(parentspecifier, "getAuthorityItem(parent)", "GET_ITEM");
555 Specifier itemSpec = getSpecifier(itemspecifier, "getAuthorityItem(item)", "GET_ITEM");
556 // Note that we have to create the service context for the Items, not the main service
557 RemoteServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
559 if(parentSpec.form==SpecifierForm.CSID) {
560 parentcsid = parentSpec.value;
562 String whereClause = buildWhereForAuthByName(parentSpec.value);
563 ctx = (RemoteServiceContext)createServiceContext(getServiceName());
564 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
566 ctx = (RemoteServiceContext)createServiceContext(getItemServiceName());
567 ctx.setJaxRsContext(jaxRsContext);
568 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
569 if(itemSpec.form==SpecifierForm.CSID) {
570 getRepositoryClient(ctx).get(ctx, itemSpec.value, handler);
572 String itemWhereClause =
573 buildWhereForAuthItemByName(itemSpec.value, parentcsid);
574 DocumentFilter myFilter = new DocumentFilter(itemWhereClause, 0, 1);
575 handler.setDocumentFilter(myFilter);
576 getRepositoryClient(ctx).get(ctx, handler);
578 // TODO should we assert that the item is in the passed vocab?
579 result = ctx.getOutput();
580 } catch (UnauthorizedException ue) {
581 Response response = Response.status(
582 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
583 throw new WebApplicationException(response);
584 } catch (DocumentNotFoundException dnfe) {
585 if (logger.isDebugEnabled()) {
586 logger.debug("getAuthorityItem", dnfe);
588 Response response = Response.status(Response.Status.NOT_FOUND).entity(
589 "Get failed on AuthorityItem specifier=" + itemspecifier).type(
590 "text/plain").build();
591 throw new WebApplicationException(response);
592 } catch (Exception e) {
593 if (logger.isDebugEnabled()) {
594 logger.debug("getAuthorityItem", e);
596 Response response = Response.status(
597 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
598 throw new WebApplicationException(response);
600 if (result == null) {
601 Response response = Response.status(Response.Status.NOT_FOUND).entity(
602 "Get failed, the requested AuthorityItem specifier:" + itemspecifier + ": was not found.").type(
603 "text/plain").build();
604 throw new WebApplicationException(response);
606 return result.getBytes();
611 * Gets the authorityItem list for the specified authority
612 * If partialPerm is specified, keywords will be ignored.
614 * @param specifier either a CSID or one of the urn forms
615 * @param partialTerm if non-null, matches partial terms
616 * @param keywords if non-null, matches terms in the keyword index for items
617 * @param ui passed to include additional parameters, like pagination controls
619 * @return the authorityItem list
622 @Path("{csid}/items")
623 @Produces("application/xml")
624 public AuthItemCommonList getAuthorityItemList(
625 @PathParam("csid") String specifier,
626 @QueryParam(IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
627 @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS_KW) String keywords,
628 @Context UriInfo ui) {
630 Specifier spec = getSpecifier(specifier, "getAuthorityItemList", "LIST");
631 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
632 // Note that docType defaults to the ServiceName, so we're fine with that.
633 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
635 if(spec.form==SpecifierForm.CSID) {
636 parentcsid = spec.value;
638 String whereClause = buildWhereForAuthByName(spec.value);
639 ctx = createServiceContext(getServiceName());
640 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
642 ctx = createServiceContext(getItemServiceName(), queryParams);
643 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
644 DocumentFilter myFilter = handler.getDocumentFilter();
645 myFilter.setWhereClause(
646 authorityItemCommonSchemaName + ":"
647 + AuthorityItemJAXBSchema.IN_AUTHORITY + "="
648 + "'" + parentcsid + "'");
650 // AND vocabularyitems_common:displayName LIKE '%partialTerm%'
651 if (partialTerm != null && !partialTerm.isEmpty()) {
652 String ptClause = QueryManager.createWhereClauseForPartialMatch(
653 authorityItemCommonSchemaName + ":"
654 + AuthorityItemJAXBSchema.DISPLAY_NAME, partialTerm );
655 myFilter.appendWhereClause(ptClause, IQueryManager.SEARCH_QUALIFIER_AND);
656 } else if (keywords != null) {
657 String kwdClause = QueryManager.createWhereClauseFromKeywords(keywords);
658 myFilter.appendWhereClause(kwdClause, IQueryManager.SEARCH_QUALIFIER_AND);
660 if (logger.isDebugEnabled()) {
661 logger.debug("getAuthorityItemList filtered WHERE clause: "
662 + myFilter.getWhereClause());
664 getRepositoryClient(ctx).getFiltered(ctx, handler);
665 return (AuthItemCommonList) handler.getCommonPartList();
666 } catch (UnauthorizedException ue) {
667 Response response = Response.status(
668 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
669 throw new WebApplicationException(response);
670 } catch (Exception e) {
671 if (logger.isDebugEnabled()) {
672 logger.debug("Caught exception in getAuthorityItemList", e);
674 Response response = Response.status(
675 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
676 throw new WebApplicationException(response);
681 * Gets the entities referencing this Authority item instance. The service type
682 * can be passed as a query param "type", and must match a configured type
683 * for the service bindings. If not set, the type defaults to
684 * ServiceBindingUtils.SERVICE_TYPE_PROCEDURE.
686 * @param parentspecifier either a CSID or one of the urn forms
687 * @param itemspecifier either a CSID or one of the urn forms
690 * @return the info for the referencing objects
693 @Path("{csid}/items/{itemcsid}/refObjs")
694 @Produces("application/xml")
695 public AuthorityRefDocList getReferencingObjects(
696 @PathParam("csid") String parentspecifier,
697 @PathParam("itemcsid") String itemspecifier,
698 @Context UriInfo ui) {
699 AuthorityRefDocList authRefDocList = null;
701 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
702 Specifier parentSpec = getSpecifier(parentspecifier,
703 "getReferencingObjects(parent)", "GET_ITEM_REF_OBJS");
704 Specifier itemSpec = getSpecifier(itemspecifier,
705 "getReferencingObjects(item)", "GET_ITEM_REF_OBJS");
706 // Note that we have to create the service context for the Items, not the main service
707 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
709 if(parentSpec.form==SpecifierForm.CSID) {
710 parentcsid = parentSpec.value;
712 String whereClause = buildWhereForAuthByName(parentSpec.value);
713 ctx = createServiceContext(getServiceName());
714 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
716 ctx = createServiceContext(getItemServiceName(), queryParams);
718 if(itemSpec.form==SpecifierForm.CSID) {
719 itemcsid = itemSpec.value;
721 String itemWhereClause =
722 buildWhereForAuthItemByName(itemSpec.value, parentcsid);
723 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause);
725 // Note that we have to create the service context for the Items, not the main service
726 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
727 RepositoryClient repoClient = getRepositoryClient(ctx);
728 DocumentFilter myFilter = handler.getDocumentFilter();
729 String serviceType = ServiceBindingUtils.SERVICE_TYPE_PROCEDURE;
730 List<String> list = queryParams.remove(ServiceBindingUtils.SERVICE_TYPE_PROP);
732 serviceType = list.get(0);
734 DocumentWrapper<DocumentModel> docWrapper = repoClient.getDoc(ctx, itemcsid);
735 DocumentModel docModel = docWrapper.getWrappedObject();
736 String refName = (String)docModel.getPropertyValue(AuthorityItemJAXBSchema.REF_NAME);
738 authRefDocList = RefNameServiceUtils.getAuthorityRefDocs(ctx,
742 myFilter.getPageSize(), myFilter.getStartPage(), true /*computeTotal*/ );
743 } catch (UnauthorizedException ue) {
744 Response response = Response.status(
745 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
746 throw new WebApplicationException(response);
747 } catch (DocumentNotFoundException dnfe) {
748 if (logger.isDebugEnabled()) {
749 logger.debug("getReferencingObjects", dnfe);
751 Response response = Response.status(Response.Status.NOT_FOUND).entity(
752 "GetReferencingObjects failed with parentspecifier="
753 + parentspecifier + " and itemspecifier=" + itemspecifier).type(
754 "text/plain").build();
755 throw new WebApplicationException(response);
756 } catch (Exception e) { // Includes DocumentException
757 if (logger.isDebugEnabled()) {
758 logger.debug("GetReferencingObjects", e);
760 Response response = Response.status(
761 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
762 throw new WebApplicationException(response);
764 if (authRefDocList == null) {
765 Response response = Response.status(Response.Status.NOT_FOUND).entity(
766 "Get failed, the requested Item CSID:" + itemspecifier + ": was not found.").type(
767 "text/plain").build();
768 throw new WebApplicationException(response);
770 return authRefDocList;
774 * Gets the authority terms used in the indicated Authority item.
776 * @param parentspecifier either a CSID or one of the urn forms
777 * @param itemspecifier either a CSID or one of the urn forms
778 * @param ui passed to include additional parameters, like pagination controls
780 * @return the authority refs for the Authority item.
783 @Path("{csid}/items/{itemcsid}/authorityrefs")
784 @Produces("application/xml")
785 public AuthorityRefList getAuthorityItemAuthorityRefs(
786 @PathParam("csid") String parentspecifier,
787 @PathParam("itemcsid") String itemspecifier,
788 @Context UriInfo ui) {
789 AuthorityRefList authRefList = null;
791 Specifier parentSpec = getSpecifier(parentspecifier, "getAuthorityItemAuthRefs(parent)", "GET_ITEM_AUTH_REFS");
792 Specifier itemSpec = getSpecifier(itemspecifier, "getAuthorityItemAuthRefs(item)", "GET_ITEM_AUTH_REFS");
793 // Note that we have to create the service context for the Items, not the main service
794 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
795 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
797 if(parentSpec.form==SpecifierForm.CSID) {
798 parentcsid = parentSpec.value;
800 String whereClause = buildWhereForAuthByName(parentSpec.value);
801 ctx = createServiceContext(getServiceName());
802 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
804 ctx = createServiceContext(getItemServiceName(), queryParams);
805 RemoteDocumentModelHandlerImpl handler =
806 (RemoteDocumentModelHandlerImpl) createItemDocumentHandler(ctx, parentcsid);
808 if(itemSpec.form==SpecifierForm.CSID) {
809 itemcsid = itemSpec.value;
811 String itemWhereClause =
812 buildWhereForAuthItemByName(itemSpec.value, parentcsid);
813 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause);
815 DocumentWrapper<DocumentModel> docWrapper =
816 getRepositoryClient(ctx).getDoc(ctx, itemcsid);
817 List<String> authRefFields =
818 ((MultipartServiceContextImpl)ctx).getCommonPartPropertyValues(
819 ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES);
820 authRefList = handler.getAuthorityRefs(docWrapper, authRefFields);
821 } catch (UnauthorizedException ue) {
822 Response response = Response.status(
823 Response.Status.UNAUTHORIZED).entity("Failed to retrieve authority references: reason " + ue.getErrorReason()).type("text/plain").build();
824 throw new WebApplicationException(response);
825 } catch (Exception e) {
826 if (logger.isDebugEnabled()) {
827 logger.debug("Caught exception in getAuthorityRefs", e);
829 Response response = Response.status(
830 Response.Status.INTERNAL_SERVER_ERROR).entity("Failed to retrieve authority references").type("text/plain").build();
831 throw new WebApplicationException(response);
837 * Update authorityItem.
839 * @param parentspecifier either a CSID or one of the urn forms
840 * @param itemspecifier either a CSID or one of the urn forms
841 * @param theUpdate the the update
843 * @return the multipart output
846 @Path("{csid}/items/{itemcsid}")
847 public byte[] updateAuthorityItem(
848 @PathParam("csid") String parentspecifier,
849 @PathParam("itemcsid") String itemspecifier,
851 PoxPayloadOut result = null;
853 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
854 Specifier parentSpec = getSpecifier(parentspecifier,
855 "updateAuthorityItem(parent)", "UPDATE_ITEM");
856 Specifier itemSpec = getSpecifier(itemspecifier,
857 "updateAuthorityItem(item)", "UPDATE_ITEM");
858 // Note that we have to create the service context for the Items, not the main service
859 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
861 if(parentSpec.form==SpecifierForm.CSID) {
862 parentcsid = parentSpec.value;
864 String whereClause = buildWhereForAuthByName(parentSpec.value);
865 ctx = createServiceContext(getServiceName());
866 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
868 ctx = createServiceContext(getItemServiceName(), theUpdate);
870 if(itemSpec.form==SpecifierForm.CSID) {
871 itemcsid = itemSpec.value;
873 String itemWhereClause =
874 buildWhereForAuthItemByName(itemSpec.value, parentcsid);
875 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause);
877 // Note that we have to create the service context for the Items, not the main service
878 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
879 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
880 result = ctx.getOutput();
881 } catch (BadRequestException bre) {
882 Response response = Response.status(
883 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
884 throw new WebApplicationException(response);
885 } catch (UnauthorizedException ue) {
886 Response response = Response.status(
887 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
888 throw new WebApplicationException(response);
889 } catch (DocumentNotFoundException dnfe) {
890 if (logger.isDebugEnabled()) {
891 logger.debug("caught DNF exception in updateAuthorityItem", dnfe);
893 Response response = Response.status(Response.Status.NOT_FOUND).entity(
894 "Update failed on AuthorityItem csid=" + itemspecifier).type(
895 "text/plain").build();
896 throw new WebApplicationException(response);
897 } catch (Exception e) {
898 Response response = Response.status(
899 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
900 throw new WebApplicationException(response);
902 return result.getBytes();
906 * Delete authorityItem.
908 * @param parentcsid the parentcsid
909 * @param itemcsid the itemcsid
911 * @return the response
914 @Path("{csid}/items/{itemcsid}")
915 public Response deleteAuthorityItem(
916 @PathParam("csid") String parentcsid,
917 @PathParam("itemcsid") String itemcsid) {
918 if (logger.isDebugEnabled()) {
919 logger.debug("deleteAuthorityItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
921 if (parentcsid == null || "".equals(parentcsid)) {
922 logger.error("deleteVocabularyItem: missing csid!");
923 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
924 "delete failed on AuthorityItem parentcsid=" + parentcsid).type(
925 "text/plain").build();
926 throw new WebApplicationException(response);
928 if (itemcsid == null || "".equals(itemcsid)) {
929 logger.error("deleteVocabularyItem: missing itemcsid!");
930 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
931 "delete failed on AuthorityItem=" + itemcsid).type(
932 "text/plain").build();
933 throw new WebApplicationException(response);
936 // Note that we have to create the service context for the Items, not the main service
937 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName());
938 getRepositoryClient(ctx).delete(ctx, itemcsid);
939 return Response.status(HttpResponseCodes.SC_OK).build();
940 } catch (UnauthorizedException ue) {
941 Response response = Response.status(
942 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
943 throw new WebApplicationException(response);
944 } catch (DocumentNotFoundException dnfe) {
945 if (logger.isDebugEnabled()) {
946 logger.debug("caught exception in deleteAuthorityItem", dnfe);
948 Response response = Response.status(Response.Status.NOT_FOUND).entity(
949 "Delete failed on AuthorityItem itemcsid=" + itemcsid).type(
950 "text/plain").build();
951 throw new WebApplicationException(response);
952 } catch (Exception e) {
953 Response response = Response.status(
954 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
955 throw new WebApplicationException(response);