]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8b694b8cde195f1db5730d9976e188b6230a171f
[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(DocumentModel docModel, String authorityItemCommonSchemaName, Boolean flag) throws Exception {
87         boolean result = false;
88         
89         docModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.DEPRECATED,
90                         new Boolean(flag));
91         CoreSessionInterface session = (CoreSessionInterface) docModel.getCoreSession();
92         session.saveDocument(docModel);
93         result = true;
94         
95         return result;
96     }
97     
98     /**
99      * Mark the authority item as deprecated.
100      * 
101      * @param ctx
102      * @param itemInfo
103      * @throws Exception
104      */
105     static public boolean markAuthorityItemAsDeprecated(ServiceContext ctx, String authorityItemCommonSchemaName, String itemCsid) throws Exception {
106         boolean result = false;
107         
108         try {
109                 DocumentModel docModel = NuxeoUtils.getDocFromCsid(ctx, (CoreSessionInterface)ctx.getCurrentRepositorySession(), itemCsid);
110                 result = setAuthorityItemDeprecated(docModel, authorityItemCommonSchemaName, AuthorityServiceUtils.DEPRECATED);
111         } catch (Exception e) {
112                 logger.warn(String.format("Could not mark item '%s' as deprecated.", itemCsid), e);
113                 throw e;
114         }
115         
116         return result;
117     }
118 }