]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
be07bd4c608dd166293751efc5726b43774070d0
[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         DocumentModel documentModel = wrapDoc.getWrappedObject();
90         Integer rev = (Integer)documentModel.getProperty(authorityCommonSchemaName, AuthorityItemJAXBSchema.REV);
91         if (rev == null) {
92                 rev = 0;
93         } else {
94                 rev++;
95         }
96         documentModel.setProperty(authorityCommonSchemaName, AuthorityItemJAXBSchema.REV, rev);
97     }
98     
99
100     @Override
101     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
102         super.handleCreate(wrapDoc);
103         // CSPACE-3178:
104         // Uncomment once debugged and App layer is read to integrate
105         // Experimenting with this uncommented now ...
106         handleDisplayNameAsShortIdentifier(wrapDoc.getWrappedObject(), authorityCommonSchemaName);
107         updateRefnameForAuthority(wrapDoc, authorityCommonSchemaName);//CSPACE-3178
108     }
109     
110     protected String buildWhereForShortId(String name) {
111         return authorityCommonSchemaName
112                 + ":" + AuthorityJAXBSchema.SHORT_IDENTIFIER
113                 + "='" + name + "'";
114     }
115     
116     private boolean isUnique(DocumentModel docModel, String schemaName) throws DocumentException {
117         return true;
118     }
119     
120     private boolean temp_isUnique(DocumentModel docModel, String schemaName) throws DocumentException {
121         boolean result = true;
122         
123         ServiceContext ctx = this.getServiceContext();
124         String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
125         String nxqlWhereClause = buildWhereForShortId(shortIdentifier);
126         try {
127                         DocumentWrapper<DocumentModel> searchResultWrapper = getRepositoryClient(ctx).findDoc(ctx, nxqlWhereClause);
128                         if (searchResultWrapper != null) {
129                                 result = false;
130                                 if (logger.isInfoEnabled() == true) {
131                                         DocumentModel searchResult = searchResultWrapper.getWrappedObject();
132                                         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'",
133                                                         shortIdentifier, searchResult.getName());
134                                         logger.trace(debugMsg);
135                                 }
136                         }
137                 } catch (DocumentNotFoundException e) {
138                         // Not a problem, just means we couldn't find another authority with that short ID
139                 }
140         
141         return result;
142     }
143
144     /**
145      * If no short identifier was provided in the input payload,
146      * generate a short identifier from the display name. Either way though,
147      * the short identifier needs to be unique.
148      */
149     private void handleDisplayNameAsShortIdentifier(DocumentModel docModel, String schemaName) throws Exception {
150         String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
151         String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME);
152         String shortDisplayName = "";
153         String generateShortIdentifier = null;
154         if (Tools.isEmpty(shortIdentifier)) {
155                 generateShortIdentifier = AuthorityIdentifierUtils.generateShortIdentifierFromDisplayName(displayName, shortDisplayName);
156             docModel.setProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER, shortIdentifier);
157         }
158         
159         if (isUnique(docModel, schemaName) == false) {
160                 String shortId = generateShortIdentifier == null ? shortIdentifier : generateShortIdentifier;
161                 String errMsgVerb = generateShortIdentifier == null ? "supplied" : "generated";
162                 String errMsg = String.format("The %s short identifier '%s' was not unique, so the new authority could not be created.",
163                                 errMsgVerb, shortId);
164                 throw new DocumentException(errMsg);
165         }
166     }
167  
168     /**
169      * Generate a refName for the authority from the short identifier
170      * and display name.
171      * 
172      * All refNames for authorities are generated.  If a client supplies
173      * a refName, it will be overwritten during create (per this method) 
174      * or discarded during update (per filterReadOnlyPropertiesForPart).
175      * 
176      * @see #filterReadOnlyPropertiesForPart(Map<String, Object>, org.collectionspace.services.common.service.ObjectPartType)
177      * 
178      */
179     protected void updateRefnameForAuthority(DocumentWrapper<DocumentModel> wrapDoc, String schemaName) throws Exception {
180         DocumentModel docModel = wrapDoc.getWrappedObject();
181         RefName.Authority authority = (Authority) getRefName(getServiceContext(), docModel);
182         String refName = authority.toString();
183         docModel.setProperty(schemaName, AuthorityJAXBSchema.REF_NAME, refName);
184     }
185     
186     @Override
187     public RefName.RefNameInterface getRefName(ServiceContext ctx,
188                 DocumentModel docModel) {
189         RefName.RefNameInterface refname = null;
190
191         try {
192                 String shortIdentifier = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
193                 String displayName = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.DISPLAY_NAME);
194                 RefName.Authority authority = RefName.Authority.buildAuthority(ctx.getTenantName(),
195                         ctx.getServiceName(),
196                         null,   // Only use shortId form!!!
197                         shortIdentifier,
198                         displayName);
199                 refname = authority;
200         } catch (Exception e) {
201                 logger.error(e.getMessage(), e);
202         }
203         
204         return refname;
205     }
206     
207     @Override
208     protected String getRefnameDisplayName(DocumentWrapper<DocumentModel> docWrapper) {
209         String result = null;
210         
211         DocumentModel docModel = docWrapper.getWrappedObject();
212         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
213         RefName.Authority refname = (RefName.Authority)getRefName(ctx, docModel);
214         result = refname.getDisplayName();
215         
216         return result;
217     }    
218     
219     public String getShortIdentifier(String authCSID, String schemaName) throws Exception {
220         String shortIdentifier = null;
221         CoreSessionInterface repoSession = null;
222
223         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
224         RepositoryClientImpl nuxeoRepoClient = (RepositoryClientImpl)this.getRepositoryClient(ctx);
225         try {
226                 repoSession = nuxeoRepoClient.getRepositorySession(ctx);
227             DocumentWrapper<DocumentModel> wrapDoc = nuxeoRepoClient.getDocFromCsid(ctx, repoSession, authCSID);
228             DocumentModel docModel = wrapDoc.getWrappedObject();
229             shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
230         } catch (ClientException ce) {
231             throw new RuntimeException("AuthorityDocHandler Internal Error: cannot get shortId!", ce);
232         } finally {
233                 if (repoSession != null) {
234                         nuxeoRepoClient.releaseRepositorySession(ctx, repoSession);
235                 }
236         }
237         
238         return shortIdentifier;
239     }
240
241     /**
242      * Filters out selected values supplied in an update request.
243      * 
244      * @param objectProps the properties filtered out from the update payload
245      * @param partMeta metadata for the object to fill
246      */
247     @Override
248     public void filterReadOnlyPropertiesForPart(
249             Map<String, Object> objectProps, ObjectPartType partMeta) {
250         super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
251         String commonPartLabel = getServiceContext().getCommonPartLabel();
252         if (partMeta.getLabel().equalsIgnoreCase(commonPartLabel)) {
253             objectProps.remove(AuthorityJAXBSchema.CSID);
254             objectProps.remove(AuthorityJAXBSchema.SHORT_IDENTIFIER);
255             objectProps.remove(AuthorityJAXBSchema.REF_NAME);
256         }
257     }    
258 }