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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.common.vocabulary.nuxeo;
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;
50 * AuthorityDocumentModelHandler
52 * $LastChangedRevision: $
55 public abstract class AuthorityDocumentModelHandler<AuthCommon>
56 extends NuxeoDocumentModelHandler<AuthCommon> {
58 private final Logger logger = LoggerFactory.getLogger(AuthorityDocumentModelHandler.class);
59 private String authorityCommonSchemaName;
61 public AuthorityDocumentModelHandler(String authorityCommonSchemaName) {
62 this.authorityCommonSchemaName = authorityCommonSchemaName;
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)
71 protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
73 Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
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);
81 return unQObjectProperties;
84 public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
85 super.fillAllParts(wrapDoc, action);
87 // Update the record's revision number on both CREATE and UPDATE actions
89 DocumentModel documentModel = wrapDoc.getWrappedObject();
90 Integer rev = (Integer)documentModel.getProperty(authorityCommonSchemaName, AuthorityItemJAXBSchema.REV);
96 documentModel.setProperty(authorityCommonSchemaName, AuthorityItemJAXBSchema.REV, rev);
101 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
102 super.handleCreate(wrapDoc);
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
110 protected String buildWhereForShortId(String name) {
111 return authorityCommonSchemaName
112 + ":" + AuthorityJAXBSchema.SHORT_IDENTIFIER
116 private boolean isUnique(DocumentModel docModel, String schemaName) throws DocumentException {
120 private boolean temp_isUnique(DocumentModel docModel, String schemaName) throws DocumentException {
121 boolean result = true;
123 ServiceContext ctx = this.getServiceContext();
124 String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
125 String nxqlWhereClause = buildWhereForShortId(shortIdentifier);
127 DocumentWrapper<DocumentModel> searchResultWrapper = getRepositoryClient(ctx).findDoc(ctx, nxqlWhereClause);
128 if (searchResultWrapper != null) {
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);
137 } catch (DocumentNotFoundException e) {
138 // Not a problem, just means we couldn't find another authority with that short ID
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.
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);
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);
169 * Generate a refName for the authority from the short identifier
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).
176 * @see #filterReadOnlyPropertiesForPart(Map<String, Object>, org.collectionspace.services.common.service.ObjectPartType)
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);
187 public RefName.RefNameInterface getRefName(ServiceContext ctx,
188 DocumentModel docModel) {
189 RefName.RefNameInterface refname = null;
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!!!
200 } catch (Exception e) {
201 logger.error(e.getMessage(), e);
208 protected String getRefnameDisplayName(DocumentWrapper<DocumentModel> docWrapper) {
209 String result = null;
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();
219 public String getShortIdentifier(String authCSID, String schemaName) throws Exception {
220 String shortIdentifier = null;
221 CoreSessionInterface repoSession = null;
223 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
224 RepositoryClientImpl nuxeoRepoClient = (RepositoryClientImpl)this.getRepositoryClient(ctx);
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);
233 if (repoSession != null) {
234 nuxeoRepoClient.releaseRepositorySession(ctx, repoSession);
238 return shortIdentifier;
242 * Filters out selected values supplied in an update request.
244 * @param objectProps the properties filtered out from the update payload
245 * @param partMeta metadata for the object to fill
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);