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