]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
fa0a03638567ddd18dc4bd547d2b914b1be6fa5e
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.common.vocabulary.nuxeo;
25
26 import java.util.Map;
27
28 import org.collectionspace.services.client.PoxPayloadIn;
29 import org.collectionspace.services.client.PoxPayloadOut;
30 import org.collectionspace.services.common.api.RefName;
31 import org.collectionspace.services.common.api.RefName.Authority;
32 import org.collectionspace.services.common.api.Tools;
33 import org.collectionspace.services.common.context.ServiceContext;
34 import org.collectionspace.services.common.document.DocumentException;
35 import org.collectionspace.services.common.document.DocumentFilter;
36 import org.collectionspace.services.common.document.DocumentNotFoundException;
37 import org.collectionspace.services.common.document.DocumentWrapper;
38 import org.collectionspace.services.common.document.DocumentHandler.Action;
39 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
40 import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema;
41 import org.collectionspace.services.common.vocabulary.AuthorityResource.Specifier;
42 import org.collectionspace.services.common.vocabulary.AuthorityResource.SpecifierForm;
43 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
44 import org.collectionspace.services.config.service.ObjectPartType;
45 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentFilter;
46 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentModelHandler;
47 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
48 import org.collectionspace.services.nuxeo.client.java.RepositoryClientImpl;
49 import org.nuxeo.ecm.core.api.ClientException;
50 import org.nuxeo.ecm.core.api.DocumentModel;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 /**
55  * AuthorityDocumentModelHandler
56  *
57  * $LastChangedRevision: $
58  * $LastChangedDate: $
59  */
60 public abstract class AuthorityDocumentModelHandler<AuthCommon>
61         extends NuxeoDocumentModelHandler<AuthCommon> {
62
63     private final Logger logger = LoggerFactory.getLogger(AuthorityDocumentModelHandler.class); 
64     protected String authorityCommonSchemaName;
65     protected String authorityItemCommonSchemaName;
66
67     public AuthorityDocumentModelHandler(String authorityCommonSchemaName, String authorityItemCommonSchemaName) {
68         this.authorityCommonSchemaName = authorityCommonSchemaName;
69         this.authorityItemCommonSchemaName = authorityItemCommonSchemaName;
70     }
71     
72     
73     public boolean synchronize(Specifier specifier) throws DocumentNotFoundException, DocumentException {
74         boolean result = true;
75         
76         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
77         if (specifier.form == SpecifierForm.CSID) {
78             if (logger.isDebugEnabled()) {
79                 logger.debug("Synchronize Authority with csid=" + specifier.value);
80             }
81             getRepositoryClient(ctx).get(getServiceContext(), specifier.value, this);
82         } else {
83             String whereClause = RefNameServiceUtils.buildWhereForAuthByName(authorityCommonSchemaName, specifier.value);
84             DocumentFilter myFilter = new NuxeoDocumentFilter(whereClause, 0, 1);
85             this.setDocumentFilter(myFilter);
86             getRepositoryClient(ctx).get(ctx, this);
87         }
88         
89         PoxPayloadOut output = ctx.getOutput();
90         
91         return result;
92     }
93
94     /*
95      * Non standard injection of CSID into common part, since caller may access through
96      * shortId, and not know the CSID.
97      * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
98      */
99     @Override
100     protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
101             throws Exception {
102         Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
103
104         // Add the CSID to the common part
105         if (partMeta.getLabel().equalsIgnoreCase(authorityCommonSchemaName)) {
106             String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
107             unQObjectProperties.put("csid", csid);
108         }
109
110         return unQObjectProperties;
111     }
112     
113     public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
114         super.fillAllParts(wrapDoc, action);
115         //
116         // Update the record's revision number on both CREATE and UPDATE actions
117         //
118         updateRevNumbers(wrapDoc);
119     }
120     
121     protected void updateRevNumbers(DocumentWrapper<DocumentModel> wrapDoc) {
122         DocumentModel documentModel = wrapDoc.getWrappedObject();
123         Long rev = (Long)documentModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.REV);
124         if (rev == null) {
125                 rev = (long)0;
126         } else {
127                 rev++;
128         }
129         documentModel.setProperty(authorityCommonSchemaName, AuthorityJAXBSchema.REV, rev);
130     }
131     
132     @Override
133     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
134         super.handleCreate(wrapDoc);
135         // CSPACE-3178:
136         // Uncomment once debugged and App layer is read to integrate
137         // Experimenting with this uncommented now ...
138         handleDisplayNameAsShortIdentifier(wrapDoc.getWrappedObject(), authorityCommonSchemaName);
139         updateRefnameForAuthority(wrapDoc, authorityCommonSchemaName);//CSPACE-3178
140     }
141     
142     protected String buildWhereForShortId(String name) {
143         return authorityCommonSchemaName
144                 + ":" + AuthorityJAXBSchema.SHORT_IDENTIFIER
145                 + "='" + name + "'";
146     }
147     
148     private boolean isUnique(DocumentModel docModel, String schemaName) throws DocumentException {
149         return true;
150     }
151     
152     private boolean temp_isUnique(DocumentModel docModel, String schemaName) throws DocumentException {
153         boolean result = true;
154         
155         ServiceContext ctx = this.getServiceContext();
156         String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
157         String nxqlWhereClause = buildWhereForShortId(shortIdentifier);
158         try {
159                         DocumentWrapper<DocumentModel> searchResultWrapper = getRepositoryClient(ctx).findDoc(ctx, nxqlWhereClause);
160                         if (searchResultWrapper != null) {
161                                 result = false;
162                                 if (logger.isInfoEnabled() == true) {
163                                         DocumentModel searchResult = searchResultWrapper.getWrappedObject();
164                                         String debugMsg = String.format("Could not create a new authority with a short identifier of '%s', because one already exists with the same short identifer: CSID = '%s'",
165                                                         shortIdentifier, searchResult.getName());
166                                         logger.trace(debugMsg);
167                                 }
168                         }
169                 } catch (DocumentNotFoundException e) {
170                         // Not a problem, just means we couldn't find another authority with that short ID
171                 }
172         
173         return result;
174     }
175
176     /**
177      * If no short identifier was provided in the input payload,
178      * generate a short identifier from the display name. Either way though,
179      * the short identifier needs to be unique.
180      */
181     private void handleDisplayNameAsShortIdentifier(DocumentModel docModel, String schemaName) throws Exception {
182         String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
183         String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME);
184         String shortDisplayName = "";
185         String generateShortIdentifier = null;
186         if (Tools.isEmpty(shortIdentifier)) {
187                 generateShortIdentifier = AuthorityIdentifierUtils.generateShortIdentifierFromDisplayName(displayName, shortDisplayName);
188             docModel.setProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER, shortIdentifier);
189         }
190         
191         if (isUnique(docModel, schemaName) == false) {
192                 String shortId = generateShortIdentifier == null ? shortIdentifier : generateShortIdentifier;
193                 String errMsgVerb = generateShortIdentifier == null ? "supplied" : "generated";
194                 String errMsg = String.format("The %s short identifier '%s' was not unique, so the new authority could not be created.",
195                                 errMsgVerb, shortId);
196                 throw new DocumentException(errMsg);
197         }
198     }
199  
200     /**
201      * Generate a refName for the authority from the short identifier
202      * and display name.
203      * 
204      * All refNames for authorities are generated.  If a client supplies
205      * a refName, it will be overwritten during create (per this method) 
206      * or discarded during update (per filterReadOnlyPropertiesForPart).
207      * 
208      * @see #filterReadOnlyPropertiesForPart(Map<String, Object>, org.collectionspace.services.common.service.ObjectPartType)
209      * 
210      */
211     protected void updateRefnameForAuthority(DocumentWrapper<DocumentModel> wrapDoc, String schemaName) throws Exception {
212         DocumentModel docModel = wrapDoc.getWrappedObject();
213         RefName.Authority authority = (Authority) getRefName(getServiceContext(), docModel);
214         String refName = authority.toString();
215         docModel.setProperty(schemaName, AuthorityJAXBSchema.REF_NAME, refName);
216     }
217     
218     @Override
219     public RefName.RefNameInterface getRefName(ServiceContext ctx,
220                 DocumentModel docModel) {
221         RefName.RefNameInterface refname = null;
222
223         try {
224                 String shortIdentifier = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
225                 String displayName = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.DISPLAY_NAME);
226                 RefName.Authority authority = RefName.Authority.buildAuthority(ctx.getTenantName(),
227                         ctx.getServiceName(),
228                         null,   // Only use shortId form!!!
229                         shortIdentifier,
230                         displayName);
231                 refname = authority;
232         } catch (Exception e) {
233                 logger.error(e.getMessage(), e);
234         }
235         
236         return refname;
237     }
238     
239     @Override
240     protected String getRefnameDisplayName(DocumentWrapper<DocumentModel> docWrapper) {
241         String result = null;
242         
243         DocumentModel docModel = docWrapper.getWrappedObject();
244         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
245         RefName.Authority refname = (RefName.Authority)getRefName(ctx, docModel);
246         result = refname.getDisplayName();
247         
248         return result;
249     }    
250     
251     public String getShortIdentifier(String authCSID, String schemaName) throws Exception {
252         String shortIdentifier = null;
253         CoreSessionInterface repoSession = null;
254
255         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
256         RepositoryClientImpl nuxeoRepoClient = (RepositoryClientImpl)this.getRepositoryClient(ctx);
257         try {
258                 repoSession = nuxeoRepoClient.getRepositorySession(ctx);
259             DocumentWrapper<DocumentModel> wrapDoc = nuxeoRepoClient.getDocFromCsid(ctx, repoSession, authCSID);
260             DocumentModel docModel = wrapDoc.getWrappedObject();
261             shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
262         } catch (ClientException ce) {
263             throw new RuntimeException("AuthorityDocHandler Internal Error: cannot get shortId!", ce);
264         } finally {
265                 if (repoSession != null) {
266                         nuxeoRepoClient.releaseRepositorySession(ctx, repoSession);
267                 }
268         }
269         
270         return shortIdentifier;
271     }
272
273     /**
274      * Filters out selected values supplied in an update request.
275      * 
276      * @param objectProps the properties filtered out from the update payload
277      * @param partMeta metadata for the object to fill
278      */
279     @Override
280     public void filterReadOnlyPropertiesForPart(
281             Map<String, Object> objectProps, ObjectPartType partMeta) {
282         super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
283         String commonPartLabel = getServiceContext().getCommonPartLabel();
284         if (partMeta.getLabel().equalsIgnoreCase(commonPartLabel)) {
285             objectProps.remove(AuthorityJAXBSchema.CSID);
286             objectProps.remove(AuthorityJAXBSchema.SHORT_IDENTIFIER);
287             objectProps.remove(AuthorityJAXBSchema.REF_NAME);
288         }
289     }    
290 }