]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
dbbe70f757444835644ffae02d0955d32d360cb6
[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.DocumentNotFoundException;
36 import org.collectionspace.services.common.document.DocumentWrapper;
37 import org.collectionspace.services.common.document.DocumentHandler.Action;
38 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
39 import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema;
40 import org.collectionspace.services.config.service.ObjectPartType;
41 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentModelHandler;
42 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
43 import org.collectionspace.services.nuxeo.client.java.RepositoryClientImpl;
44 import org.nuxeo.ecm.core.api.ClientException;
45 import org.nuxeo.ecm.core.api.DocumentModel;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * AuthorityDocumentModelHandler
51  *
52  * $LastChangedRevision: $
53  * $LastChangedDate: $
54  */
55 public abstract class AuthorityDocumentModelHandler<AuthCommon>
56         extends NuxeoDocumentModelHandler<AuthCommon> {
57
58     private final Logger logger = LoggerFactory.getLogger(AuthorityDocumentModelHandler.class); 
59     private String authorityCommonSchemaName;
60
61     public AuthorityDocumentModelHandler(String authorityCommonSchemaName) {
62         this.authorityCommonSchemaName = authorityCommonSchemaName;
63     }
64
65     /*
66      * Non standard injection of CSID into common part, since caller may access through
67      * shortId, and not know the CSID.
68      * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
69      */
70     @Override
71     protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
72             throws Exception {
73         Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
74
75         // Add the CSID to the common part
76         if (partMeta.getLabel().equalsIgnoreCase(authorityCommonSchemaName)) {
77             String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
78             unQObjectProperties.put("csid", csid);
79         }
80
81         return unQObjectProperties;
82     }
83     
84     public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
85         super.fillAllParts(wrapDoc, action);
86         //
87         // Update the record's revision number on both CREATE and UPDATE actions
88         //
89         updateRevNumbers(wrapDoc);
90     }
91     
92     protected void updateRevNumbers(DocumentWrapper<DocumentModel> wrapDoc) {
93         DocumentModel documentModel = wrapDoc.getWrappedObject();
94         Long rev = (Long)documentModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.REV);
95         if (rev == null) {
96                 rev = (long)0;
97         } else {
98                 rev++;
99         }
100         documentModel.setProperty(authorityCommonSchemaName, AuthorityJAXBSchema.REV, rev);
101     }
102     
103     @Override
104     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
105         super.handleCreate(wrapDoc);
106         // CSPACE-3178:
107         // Uncomment once debugged and App layer is read to integrate
108         // Experimenting with this uncommented now ...
109         handleDisplayNameAsShortIdentifier(wrapDoc.getWrappedObject(), authorityCommonSchemaName);
110         updateRefnameForAuthority(wrapDoc, authorityCommonSchemaName);//CSPACE-3178
111     }
112     
113     protected String buildWhereForShortId(String name) {
114         return authorityCommonSchemaName
115                 + ":" + AuthorityJAXBSchema.SHORT_IDENTIFIER
116                 + "='" + name + "'";
117     }
118     
119     private boolean isUnique(DocumentModel docModel, String schemaName) throws DocumentException {
120         return true;
121     }
122     
123     private boolean temp_isUnique(DocumentModel docModel, String schemaName) throws DocumentException {
124         boolean result = true;
125         
126         ServiceContext ctx = this.getServiceContext();
127         String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
128         String nxqlWhereClause = buildWhereForShortId(shortIdentifier);
129         try {
130                         DocumentWrapper<DocumentModel> searchResultWrapper = getRepositoryClient(ctx).findDoc(ctx, nxqlWhereClause);
131                         if (searchResultWrapper != null) {
132                                 result = false;
133                                 if (logger.isInfoEnabled() == true) {
134                                         DocumentModel searchResult = searchResultWrapper.getWrappedObject();
135                                         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'",
136                                                         shortIdentifier, searchResult.getName());
137                                         logger.trace(debugMsg);
138                                 }
139                         }
140                 } catch (DocumentNotFoundException e) {
141                         // Not a problem, just means we couldn't find another authority with that short ID
142                 }
143         
144         return result;
145     }
146
147     /**
148      * If no short identifier was provided in the input payload,
149      * generate a short identifier from the display name. Either way though,
150      * the short identifier needs to be unique.
151      */
152     private void handleDisplayNameAsShortIdentifier(DocumentModel docModel, String schemaName) throws Exception {
153         String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
154         String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME);
155         String shortDisplayName = "";
156         String generateShortIdentifier = null;
157         if (Tools.isEmpty(shortIdentifier)) {
158                 generateShortIdentifier = AuthorityIdentifierUtils.generateShortIdentifierFromDisplayName(displayName, shortDisplayName);
159             docModel.setProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER, shortIdentifier);
160         }
161         
162         if (isUnique(docModel, schemaName) == false) {
163                 String shortId = generateShortIdentifier == null ? shortIdentifier : generateShortIdentifier;
164                 String errMsgVerb = generateShortIdentifier == null ? "supplied" : "generated";
165                 String errMsg = String.format("The %s short identifier '%s' was not unique, so the new authority could not be created.",
166                                 errMsgVerb, shortId);
167                 throw new DocumentException(errMsg);
168         }
169     }
170  
171     /**
172      * Generate a refName for the authority from the short identifier
173      * and display name.
174      * 
175      * All refNames for authorities are generated.  If a client supplies
176      * a refName, it will be overwritten during create (per this method) 
177      * or discarded during update (per filterReadOnlyPropertiesForPart).
178      * 
179      * @see #filterReadOnlyPropertiesForPart(Map<String, Object>, org.collectionspace.services.common.service.ObjectPartType)
180      * 
181      */
182     protected void updateRefnameForAuthority(DocumentWrapper<DocumentModel> wrapDoc, String schemaName) throws Exception {
183         DocumentModel docModel = wrapDoc.getWrappedObject();
184         RefName.Authority authority = (Authority) getRefName(getServiceContext(), docModel);
185         String refName = authority.toString();
186         docModel.setProperty(schemaName, AuthorityJAXBSchema.REF_NAME, refName);
187     }
188     
189     @Override
190     public RefName.RefNameInterface getRefName(ServiceContext ctx,
191                 DocumentModel docModel) {
192         RefName.RefNameInterface refname = null;
193
194         try {
195                 String shortIdentifier = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
196                 String displayName = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.DISPLAY_NAME);
197                 RefName.Authority authority = RefName.Authority.buildAuthority(ctx.getTenantName(),
198                         ctx.getServiceName(),
199                         null,   // Only use shortId form!!!
200                         shortIdentifier,
201                         displayName);
202                 refname = authority;
203         } catch (Exception e) {
204                 logger.error(e.getMessage(), e);
205         }
206         
207         return refname;
208     }
209     
210     @Override
211     protected String getRefnameDisplayName(DocumentWrapper<DocumentModel> docWrapper) {
212         String result = null;
213         
214         DocumentModel docModel = docWrapper.getWrappedObject();
215         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
216         RefName.Authority refname = (RefName.Authority)getRefName(ctx, docModel);
217         result = refname.getDisplayName();
218         
219         return result;
220     }    
221     
222     public String getShortIdentifier(String authCSID, String schemaName) throws Exception {
223         String shortIdentifier = null;
224         CoreSessionInterface repoSession = null;
225
226         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
227         RepositoryClientImpl nuxeoRepoClient = (RepositoryClientImpl)this.getRepositoryClient(ctx);
228         try {
229                 repoSession = nuxeoRepoClient.getRepositorySession(ctx);
230             DocumentWrapper<DocumentModel> wrapDoc = nuxeoRepoClient.getDocFromCsid(ctx, repoSession, authCSID);
231             DocumentModel docModel = wrapDoc.getWrappedObject();
232             shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
233         } catch (ClientException ce) {
234             throw new RuntimeException("AuthorityDocHandler Internal Error: cannot get shortId!", ce);
235         } finally {
236                 if (repoSession != null) {
237                         nuxeoRepoClient.releaseRepositorySession(ctx, repoSession);
238                 }
239         }
240         
241         return shortIdentifier;
242     }
243
244     /**
245      * Filters out selected values supplied in an update request.
246      * 
247      * @param objectProps the properties filtered out from the update payload
248      * @param partMeta metadata for the object to fill
249      */
250     @Override
251     public void filterReadOnlyPropertiesForPart(
252             Map<String, Object> objectProps, ObjectPartType partMeta) {
253         super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
254         String commonPartLabel = getServiceContext().getCommonPartLabel();
255         if (partMeta.getLabel().equalsIgnoreCase(commonPartLabel)) {
256             objectProps.remove(AuthorityJAXBSchema.CSID);
257             objectProps.remove(AuthorityJAXBSchema.SHORT_IDENTIFIER);
258             objectProps.remove(AuthorityJAXBSchema.REF_NAME);
259         }
260     }    
261 }