]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c3b72271f935931555e4e59b28d9e42d057fcd06
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.common.vocabulary;
2
3 import java.util.regex.Matcher;
4 import java.util.regex.Pattern;
5
6 import javax.ws.rs.core.Response;
7
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;
24
25 public class AuthorityServiceUtils {
26     private static final Logger logger = LoggerFactory.getLogger(AuthorityIdentifierUtils.class);
27     //
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;
32     
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;
37
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;
44
45     public static final Boolean NO_CHANGE = null;
46
47     /**
48      * Make a request to the SAS Server for an authority payload.
49      * 
50      * @param ctx
51      * @param specifier
52      * @param responseType
53      * @return
54      * @throws Exception
55      */
56     static public PoxPayloadIn requestPayloadIn(ServiceContext ctx, Specifier specifier, Class responseType) throws Exception {
57         PoxPayloadIn result = null;
58         
59         AuthorityClient client = (AuthorityClient) ctx.getClient(CollectionSpaceClient.SAS_CLIENT_PROPERTIES_FILENAME);
60         Response res = client.read(specifier.getURNValue());
61         try {
62                 int statusCode = res.getStatus();
63         
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);
68                 }
69                 
70             result = new PoxPayloadIn((String)res.readEntity(responseType)); // Get the entire response!                
71         } finally {
72                 res.close();
73         }
74         
75         return result;
76     }
77     
78     //
79     // Makes a call to the SAS server for a authority item payload
80     //    
81     static public PoxPayloadIn requestPayloadIn(AuthorityItemSpecifier specifier, String serviceName, Class responseType, boolean syncHierarchicalRelationships) throws Exception {
82         PoxPayloadIn result = null;
83         
84         ServiceContext parentCtx = new MultipartServiceContextImpl(serviceName);
85         AuthorityClient client = (AuthorityClient) parentCtx.getClient(CollectionSpaceClient.SAS_CLIENT_PROPERTIES_FILENAME);
86         Response res = client.readNamedItemInNamedAuthority(specifier.getParentSpecifier().getURNValue(), specifier.getItemSpecifier().getURNValue(),
87                         AuthorityClient.INCLUDE_DELETED_ITEMS, syncHierarchicalRelationships);
88         
89         try {
90                 int statusCode = res.getStatus();
91         
92                 // Check the status code of the response: does it match
93                 // the expected response(s)?
94                 if (logger.isDebugEnabled()) {
95                     logger.debug(client.getClass().getCanonicalName() + ": status = " + statusCode);
96                 }
97                 
98             result = new PoxPayloadIn((String)res.readEntity(responseType)); // Get the entire response!                
99         } finally {
100                 res.close();
101         }
102         
103         return result;
104     }
105     
106     static public boolean setAuthorityItemDeprecated(ServiceContext ctx,
107                 DocumentModel docModel, String authorityItemCommonSchemaName, Boolean flag) throws Exception {
108         boolean result = false;
109         
110         docModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.DEPRECATED,
111                         new Boolean(flag));
112         CoreSessionInterface repoSession = (CoreSessionInterface) ctx.getCurrentRepositorySession();
113         repoSession.saveDocument(docModel);
114         result = true;
115         
116         return result;
117     }
118     
119     /*
120      * The domain name part of refnames on SAS may not match that of local refnames, so we need to update all the payload's
121      * refnames with the correct domain name
122      */
123         static public PoxPayloadIn filterRefnameDomains(ServiceContext ctx,
124                         PoxPayloadIn payload) throws DocumentException {
125                 PoxPayloadIn result = null;
126
127                 
128                 String payloadStr = payload.getXmlPayload();
129                 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'
130                 Matcher m = p.matcher(payloadStr);
131
132                 StringBuffer filteredPayloadStr = new StringBuffer();
133                 while (m.find() == true) {
134                         if (logger.isDebugEnabled()) {
135                                 logger.debug("Replacing: " + m.group(2));
136                         }
137                         m.appendReplacement(filteredPayloadStr, m.group(1) + ctx.getTenantName());
138                 }
139                 m.appendTail(filteredPayloadStr);
140                 result = new PoxPayloadIn(filteredPayloadStr.toString());
141
142                 if (logger.isDebugEnabled()) {
143                         logger.debug(String.format("", filteredPayloadStr));
144                 }
145
146                 return result;
147         }
148     
149     /**
150      * Mark the authority item as deprecated.
151      * 
152      * @param ctx
153      * @param itemInfo
154      * @throws Exception
155      */
156     static public boolean markAuthorityItemAsDeprecated(ServiceContext ctx, String authorityItemCommonSchemaName, AuthorityItemSpecifier authorityItemSpecifier) throws Exception {
157         boolean result = false;
158         
159         try {
160                 DocumentModel docModel = NuxeoUtils.getDocFromSpecifier(ctx, (CoreSessionInterface)ctx.getCurrentRepositorySession(),
161                                 authorityItemCommonSchemaName, authorityItemSpecifier);
162                 result = setAuthorityItemDeprecated(ctx, docModel, authorityItemCommonSchemaName, AuthorityServiceUtils.DEPRECATED);
163         } catch (Exception e) {
164                 logger.warn(String.format("Could not mark item '%s' as deprecated.", authorityItemSpecifier.getItemSpecifier().getURNValue()), e);
165                 throw e;
166         }
167         
168         return result;
169     }
170 }