]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
498e979080865da7c898250df99a3c550d3092ab
[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.PoxPayloadIn;
10 import org.collectionspace.services.common.api.RefNameUtils.AuthorityTermInfo;
11 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
12 import org.collectionspace.services.common.context.ServiceContext;
13 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.AuthorityItemSpecifier;
14 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.Specifier;
15 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.SpecifierForm;
16 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityIdentifierUtils;
17 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
18 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
19 import org.dom4j.DocumentException;
20 import org.nuxeo.ecm.core.api.DocumentModel;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class AuthorityServiceUtils {
25     private static final Logger logger = LoggerFactory.getLogger(AuthorityIdentifierUtils.class);
26     //
27     // Used to keep track if an authority item's is deprecated
28     public static final String IS_DEPRECATED_PROPERTY = "IS_DEPRECATED_PROPERTY";
29     public static final Boolean DEPRECATED = true;
30     public static final Boolean NOT_DEPRECATED = !DEPRECATED;
31     
32     // Used to keep track if an authority item's rev number should be updated
33     public static final String SHOULD_UPDATE_REV_PROPERTY = "SHOULD_UPDATE_REV_PROPERTY";
34     public static final boolean UPDATE_REV = true;
35     public static final boolean DONT_UPDATE_REV = !UPDATE_REV;
36
37     // Used to keep track if an authority item is a locally proposed member of a SAS authority
38     public static final String IS_PROPOSED_PROPERTY = "IS_PROPOSED";
39     public static final Boolean PROPOSED = true;
40     public static final Boolean NOT_PROPOSED = !PROPOSED;
41     public static final Boolean SAS_ITEM = true;
42     public static final Boolean NOT_SAS_ITEM = !SAS_ITEM;
43
44     public static final Boolean NO_CHANGE = null;
45
46     static public PoxPayloadIn requestPayloadIn(ServiceContext ctx, Specifier specifier, Class responseType) throws Exception {
47         PoxPayloadIn result = null;
48         
49         AuthorityClient client = (AuthorityClient) ctx.getClient();
50         Response res = client.read(specifier.getURNValue());
51         try {
52                 int statusCode = res.getStatus();
53         
54                 // Check the status code of the response: does it match
55                 // the expected response(s)?
56                 if (logger.isDebugEnabled()) {
57                     logger.debug(client.getClass().getCanonicalName() + ": status = " + statusCode);
58                 }
59                 
60             result = new PoxPayloadIn((String)res.readEntity(responseType)); // Get the entire response!                
61         } finally {
62                 res.close();
63         }
64         
65         return result;
66     }
67     
68     //
69     // Makes a call to the SAS server for a authority item payload
70     //    
71     static public PoxPayloadIn requestPayloadIn(AuthorityItemSpecifier specifier, String serviceName, Class responseType) throws Exception {
72         PoxPayloadIn result = null;
73         
74         ServiceContext parentCtx = new MultipartServiceContextImpl(serviceName);
75         AuthorityClient client = (AuthorityClient) parentCtx.getClient();
76         Response res = client.readItem(specifier.getParentSpecifier().getURNValue(), specifier.getItemSpecifier().getURNValue());
77         try {
78                 int statusCode = res.getStatus();
79         
80                 // Check the status code of the response: does it match
81                 // the expected response(s)?
82                 if (logger.isDebugEnabled()) {
83                     logger.debug(client.getClass().getCanonicalName() + ": status = " + statusCode);
84                 }
85                 
86             result = new PoxPayloadIn((String)res.readEntity(responseType)); // Get the entire response!                
87         } finally {
88                 res.close();
89         }
90         
91         return result;
92     }
93     
94     static public boolean setAuthorityItemDeprecated(ServiceContext ctx,
95                 DocumentModel docModel, String authorityItemCommonSchemaName, Boolean flag) throws Exception {
96         boolean result = false;
97         
98         docModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.DEPRECATED,
99                         new Boolean(flag));
100         CoreSessionInterface repoSession = (CoreSessionInterface) ctx.getCurrentRepositorySession();
101         repoSession.saveDocument(docModel);
102         result = true;
103         
104         return result;
105     }
106     
107     /*
108      * The domain name part of refnames on SAS may not match that of local refnames, so we need to update all the payload's
109      * refnames with the correct domain name
110      */
111         static public PoxPayloadIn filterRefnameDomains(ServiceContext ctx,
112                         PoxPayloadIn payload) throws DocumentException {
113                 PoxPayloadIn result = null;
114
115                 
116                 String payloadStr = payload.getXmlPayload();
117                 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'
118                 Matcher m = p.matcher(payloadStr);
119
120                 StringBuffer filteredPayloadStr = new StringBuffer();
121                 while (m.find() == true) {
122                         if (logger.isDebugEnabled()) {
123                                 logger.debug("Replacing: " + m.group(2));
124                         }
125                         m.appendReplacement(filteredPayloadStr, m.group(1) + ctx.getTenantName());
126                 }
127                 m.appendTail(filteredPayloadStr);
128                 result = new PoxPayloadIn(filteredPayloadStr.toString());
129
130                 if (logger.isDebugEnabled()) {
131                         logger.debug(String.format("", filteredPayloadStr));
132                 }
133
134                 return result;
135         }
136     
137     /**
138      * Mark the authority item as deprecated.
139      * 
140      * @param ctx
141      * @param itemInfo
142      * @throws Exception
143      */
144     static public boolean markAuthorityItemAsDeprecated(ServiceContext ctx, String authorityItemCommonSchemaName, AuthorityItemSpecifier authorityItemSpecifier) throws Exception {
145         boolean result = false;
146         
147         try {
148                 DocumentModel docModel = NuxeoUtils.getDocFromSpecifier(ctx, (CoreSessionInterface)ctx.getCurrentRepositorySession(),
149                                 authorityItemCommonSchemaName, authorityItemSpecifier);
150                 result = setAuthorityItemDeprecated(ctx, docModel, authorityItemCommonSchemaName, AuthorityServiceUtils.DEPRECATED);
151         } catch (Exception e) {
152                 logger.warn(String.format("Could not mark item '%s' as deprecated.", authorityItemSpecifier.getItemSpecifier().getURNValue()), e);
153                 throw e;
154         }
155         
156         return result;
157     }
158 }