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