]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e195c4801112009c9286b6a38984178c46f87b16
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.common.vocabulary;
2
3 import javax.ws.rs.core.Response;
4
5 import org.collectionspace.services.client.AuthorityClient;
6 import org.collectionspace.services.client.PoxPayloadIn;
7 import org.collectionspace.services.common.api.RefNameUtils.AuthorityTermInfo;
8 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
9 import org.collectionspace.services.common.context.ServiceContext;
10 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.AuthorityItemSpecifier;
11 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.Specifier;
12 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityIdentifierUtils;
13 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
14 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
15 import org.nuxeo.ecm.core.api.DocumentModel;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public class AuthorityServiceUtils {
20     private static final Logger logger = LoggerFactory.getLogger(AuthorityIdentifierUtils.class);
21     //
22     // Used to keep track if an authority item's is deprecated
23     public static final String IS_DEPRECATED_PROPERTY = "IS_DEPRECATED_PROPERTY";
24     public static final Boolean DEPRECATED = true;
25     public static final Boolean NOT_DEPRECATED = !DEPRECATED;
26     
27     // Used to keep track if an authority item's rev number should be updated
28     public static final String SHOULD_UPDATE_REV_PROPERTY = "SHOULD_UPDATE_REV_PROPERTY";
29     public static final boolean UPDATE_REV = true;
30     public static final boolean DONT_UPDATE_REV = !UPDATE_REV;
31
32     // Used to keep track if an authority item is a locally proposed member of a SAS authority
33     public static final String IS_PROPOSED_PROPERTY = "IS_PROPOSED";
34     public static final Boolean PROPOSED = true;
35     public static final Boolean NOT_PROPOSED = !PROPOSED;
36     public static final Boolean NO_CHANGE = null;
37
38     static public PoxPayloadIn requestPayloadIn(ServiceContext ctx, Specifier specifier, Class responseType) throws Exception {
39         PoxPayloadIn result = null;
40         
41         AuthorityClient client = (AuthorityClient) ctx.getClient();
42         Response res = client.read(specifier.getURNValue());
43         try {
44                 int statusCode = res.getStatus();
45         
46                 // Check the status code of the response: does it match
47                 // the expected response(s)?
48                 if (logger.isDebugEnabled()) {
49                     logger.debug(client.getClass().getCanonicalName() + ": status = " + statusCode);
50                 }
51                 
52             result = new PoxPayloadIn((String)res.readEntity(responseType)); // Get the entire response!                
53         } finally {
54                 res.close();
55         }
56         
57         return result;
58     }
59     
60     //
61     // Makes a call to the SAS server for a authority item payload
62     //    
63     static public PoxPayloadIn requestPayloadIn(AuthorityItemSpecifier specifier, String serviceName, Class responseType) throws Exception {
64         PoxPayloadIn result = null;
65         
66         ServiceContext parentCtx = new MultipartServiceContextImpl(serviceName);
67         AuthorityClient client = (AuthorityClient) parentCtx.getClient();
68         Response res = client.readItem(specifier.getParentSpecifier().getURNValue(), specifier.getItemSpecifier().getURNValue());
69         try {
70                 int statusCode = res.getStatus();
71         
72                 // Check the status code of the response: does it match
73                 // the expected response(s)?
74                 if (logger.isDebugEnabled()) {
75                     logger.debug(client.getClass().getCanonicalName() + ": status = " + statusCode);
76                 }
77                 
78             result = new PoxPayloadIn((String)res.readEntity(responseType)); // Get the entire response!                
79         } finally {
80                 res.close();
81         }
82         
83         return result;
84     }
85     
86     static public boolean setAuthorityItemDeprecated(ServiceContext ctx,
87                 DocumentModel docModel, String authorityItemCommonSchemaName, Boolean flag) throws Exception {
88         boolean result = false;
89         
90         docModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.DEPRECATED,
91                         new Boolean(flag));
92         CoreSessionInterface repoSession = (CoreSessionInterface) ctx.getCurrentRepositorySession();
93         repoSession.saveDocument(docModel);
94         result = true;
95         
96         return result;
97     }
98     
99     /**
100      * Mark the authority item as deprecated.
101      * 
102      * @param ctx
103      * @param itemInfo
104      * @throws Exception
105      */
106     static public boolean markAuthorityItemAsDeprecated(ServiceContext ctx, String authorityItemCommonSchemaName, String itemCsid) throws Exception {
107         boolean result = false;
108         
109         try {
110                 DocumentModel docModel = NuxeoUtils.getDocFromCsid(ctx, (CoreSessionInterface)ctx.getCurrentRepositorySession(), itemCsid);
111                 result = setAuthorityItemDeprecated(ctx, docModel, authorityItemCommonSchemaName, AuthorityServiceUtils.DEPRECATED);
112         } catch (Exception e) {
113                 logger.warn(String.format("Could not mark item '%s' as deprecated.", itemCsid), e);
114                 throw e;
115         }
116         
117         return result;
118     }
119 }