1 package org.collectionspace.services.common.vocabulary;
3 import java.util.regex.Matcher;
4 import java.util.regex.Pattern;
6 import javax.ws.rs.core.Response;
8 import org.collectionspace.services.client.AuthorityClient;
9 import org.collectionspace.services.client.CollectionSpaceClient;
10 import org.collectionspace.services.client.PoxPayloadIn;
11 import org.collectionspace.services.common.api.RefNameUtils.AuthorityTermInfo;
12 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
13 import org.collectionspace.services.common.context.ServiceContext;
14 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.AuthorityItemSpecifier;
15 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.Specifier;
16 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.SpecifierForm;
17 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityIdentifierUtils;
18 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
19 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
20 import org.dom4j.DocumentException;
21 import org.nuxeo.ecm.core.api.DocumentModel;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class AuthorityServiceUtils {
26 private static final Logger logger = LoggerFactory.getLogger(AuthorityIdentifierUtils.class);
28 // Used to keep track if an authority item's is deprecated
29 public static final String IS_DEPRECATED_PROPERTY = "IS_DEPRECATED_PROPERTY";
30 public static final Boolean DEPRECATED = true;
31 public static final Boolean NOT_DEPRECATED = !DEPRECATED;
33 // Used to keep track if an authority item's rev number should be updated
34 public static final String SHOULD_UPDATE_REV_PROPERTY = "SHOULD_UPDATE_REV_PROPERTY";
35 public static final boolean UPDATE_REV = true;
36 public static final boolean DONT_UPDATE_REV = !UPDATE_REV;
38 // Used to keep track if an authority item is a locally proposed member of a SAS authority
39 public static final String IS_PROPOSED_PROPERTY = "IS_PROPOSED";
40 public static final Boolean PROPOSED = true;
41 public static final Boolean NOT_PROPOSED = !PROPOSED;
42 public static final Boolean SAS_ITEM = true;
43 public static final Boolean NOT_SAS_ITEM = !SAS_ITEM;
45 public static final Boolean NO_CHANGE = null;
48 * Make a request to the SAS Server for an authority payload.
56 static public PoxPayloadIn requestPayloadIn(ServiceContext ctx, Specifier specifier, Class responseType) throws Exception {
57 PoxPayloadIn result = null;
59 AuthorityClient client = (AuthorityClient) ctx.getClient(CollectionSpaceClient.SAS_CLIENT_PROPERTIES_FILENAME);
60 Response res = client.read(specifier.getURNValue());
62 int statusCode = res.getStatus();
64 // Check the status code of the response: does it match
65 // the expected response(s)?
66 if (logger.isDebugEnabled()) {
67 logger.debug(client.getClass().getCanonicalName() + ": status = " + statusCode);
70 result = new PoxPayloadIn((String)res.readEntity(responseType)); // Get the entire response!
79 // Makes a call to the SAS server for a authority item payload
81 static public PoxPayloadIn requestPayloadIn(AuthorityItemSpecifier specifier, String serviceName, Class responseType) throws Exception {
82 PoxPayloadIn result = null;
84 ServiceContext parentCtx = new MultipartServiceContextImpl(serviceName);
85 AuthorityClient client = (AuthorityClient) parentCtx.getClient(CollectionSpaceClient.SAS_CLIENT_PROPERTIES_FILENAME);
86 Response res = client.readItem(specifier.getParentSpecifier().getURNValue(), specifier.getItemSpecifier().getURNValue());
88 int statusCode = res.getStatus();
90 // Check the status code of the response: does it match
91 // the expected response(s)?
92 if (logger.isDebugEnabled()) {
93 logger.debug(client.getClass().getCanonicalName() + ": status = " + statusCode);
96 result = new PoxPayloadIn((String)res.readEntity(responseType)); // Get the entire response!
104 static public boolean setAuthorityItemDeprecated(ServiceContext ctx,
105 DocumentModel docModel, String authorityItemCommonSchemaName, Boolean flag) throws Exception {
106 boolean result = false;
108 docModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.DEPRECATED,
110 CoreSessionInterface repoSession = (CoreSessionInterface) ctx.getCurrentRepositorySession();
111 repoSession.saveDocument(docModel);
118 * The domain name part of refnames on SAS may not match that of local refnames, so we need to update all the payload's
119 * refnames with the correct domain name
121 static public PoxPayloadIn filterRefnameDomains(ServiceContext ctx,
122 PoxPayloadIn payload) throws DocumentException {
123 PoxPayloadIn result = null;
126 String payloadStr = payload.getXmlPayload();
127 Pattern p = Pattern.compile("(urn:cspace:)(([a-z]{1,}\\.?)*)"); // matches the domain name part of a RefName. For example, matches "core.collectionspace.org" of RefName urn:cspace:core.collectionspace.org:personauthorities:name(person):item:name(BigBird1461101206103)'Big Bird'
128 Matcher m = p.matcher(payloadStr);
130 StringBuffer filteredPayloadStr = new StringBuffer();
131 while (m.find() == true) {
132 if (logger.isDebugEnabled()) {
133 logger.debug("Replacing: " + m.group(2));
135 m.appendReplacement(filteredPayloadStr, m.group(1) + ctx.getTenantName());
137 m.appendTail(filteredPayloadStr);
138 result = new PoxPayloadIn(filteredPayloadStr.toString());
140 if (logger.isDebugEnabled()) {
141 logger.debug(String.format("", filteredPayloadStr));
148 * Mark the authority item as deprecated.
154 static public boolean markAuthorityItemAsDeprecated(ServiceContext ctx, String authorityItemCommonSchemaName, AuthorityItemSpecifier authorityItemSpecifier) throws Exception {
155 boolean result = false;
158 DocumentModel docModel = NuxeoUtils.getDocFromSpecifier(ctx, (CoreSessionInterface)ctx.getCurrentRepositorySession(),
159 authorityItemCommonSchemaName, authorityItemSpecifier);
160 result = setAuthorityItemDeprecated(ctx, docModel, authorityItemCommonSchemaName, AuthorityServiceUtils.DEPRECATED);
161 } catch (Exception e) {
162 logger.warn(String.format("Could not mark item '%s' as deprecated.", authorityItemSpecifier.getItemSpecifier().getURNValue()), e);