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.DocumentWrapper;
35 import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema;
36 import org.collectionspace.services.config.service.ObjectPartType;
37 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentModelHandler;
38 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
39 import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl;
40 import org.nuxeo.ecm.core.api.ClientException;
41 import org.nuxeo.ecm.core.api.DocumentModel;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * AuthorityDocumentModelHandler
48 * $LastChangedRevision: $
51 public abstract class AuthorityDocumentModelHandler<AuthCommon>
52 extends NuxeoDocumentModelHandler<AuthCommon> {
54 private final Logger logger = LoggerFactory.getLogger(AuthorityDocumentModelHandler.class);
55 private String authorityCommonSchemaName;
57 public AuthorityDocumentModelHandler(String authorityCommonSchemaName) {
58 this.authorityCommonSchemaName = authorityCommonSchemaName;
62 * Non standard injection of CSID into common part, since caller may access through
63 * shortId, and not know the CSID.
64 * @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 protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
69 Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
71 // Add the CSID to the common part
72 if (partMeta.getLabel().equalsIgnoreCase(authorityCommonSchemaName)) {
73 String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
74 unQObjectProperties.put("csid", csid);
77 return unQObjectProperties;
81 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
82 super.handleCreate(wrapDoc);
84 // Uncomment once debugged and App layer is read to integrate
85 // Experimenting with this uncommented now ...
86 handleDisplayNameAsShortIdentifier(wrapDoc.getWrappedObject(), authorityCommonSchemaName);
87 updateRefnameForAuthority(wrapDoc, authorityCommonSchemaName);//CSPACE-3178
91 * If no short identifier was provided in the input payload,
92 * generate a short identifier from the display name.
94 private void handleDisplayNameAsShortIdentifier(DocumentModel docModel, String schemaName) throws Exception {
95 String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
96 String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME);
97 String shortDisplayName = "";
98 if (Tools.isEmpty(shortIdentifier)) {
99 String generatedShortIdentifier = AuthorityIdentifierUtils.generateShortIdentifierFromDisplayName(displayName, shortDisplayName);
100 docModel.setProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER, generatedShortIdentifier);
105 * Generate a refName for the authority from the short identifier
108 * All refNames for authorities are generated. If a client supplies
109 * a refName, it will be overwritten during create (per this method)
110 * or discarded during update (per filterReadOnlyPropertiesForPart).
112 * @see #filterReadOnlyPropertiesForPart(Map<String, Object>, org.collectionspace.services.common.service.ObjectPartType)
115 protected void updateRefnameForAuthority(DocumentWrapper<DocumentModel> wrapDoc, String schemaName) throws Exception {
116 DocumentModel docModel = wrapDoc.getWrappedObject();
117 RefName.Authority authority = (Authority) getRefName(getServiceContext(), docModel);
118 String refName = authority.toString();
119 docModel.setProperty(schemaName, AuthorityJAXBSchema.REF_NAME, refName);
123 public RefName.RefNameInterface getRefName(ServiceContext ctx,
124 DocumentModel docModel) {
125 RefName.RefNameInterface refname = null;
128 String shortIdentifier = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
129 String displayName = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.DISPLAY_NAME);
130 RefName.Authority authority = RefName.Authority.buildAuthority(ctx.getTenantName(),
131 ctx.getServiceName(),
132 null, // Only use shortId form!!!
136 } catch (Exception e) {
137 logger.error(e.getMessage(), e);
144 protected String getRefnameDisplayName(DocumentWrapper<DocumentModel> docWrapper) {
145 String result = null;
147 DocumentModel docModel = docWrapper.getWrappedObject();
148 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
149 RefName.Authority refname = (RefName.Authority)getRefName(ctx, docModel);
150 result = refname.getDisplayName();
155 public String getShortIdentifier(String authCSID, String schemaName) throws Exception {
156 String shortIdentifier = null;
157 CoreSessionInterface repoSession = null;
159 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
160 RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx);
162 repoSession = nuxeoRepoClient.getRepositorySession(ctx);
163 DocumentWrapper<DocumentModel> wrapDoc = nuxeoRepoClient.getDocFromCsid(ctx, repoSession, authCSID);
164 DocumentModel docModel = wrapDoc.getWrappedObject();
165 shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
166 } catch (ClientException ce) {
167 throw new RuntimeException("AuthorityDocHandler Internal Error: cannot get shortId!", ce);
169 if (repoSession != null) {
170 nuxeoRepoClient.releaseRepositorySession(ctx, repoSession);
174 return shortIdentifier;
178 * Filters out selected values supplied in an update request.
180 * @param objectProps the properties filtered out from the update payload
181 * @param partMeta metadata for the object to fill
184 public void filterReadOnlyPropertiesForPart(
185 Map<String, Object> objectProps, ObjectPartType partMeta) {
186 super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
187 String commonPartLabel = getServiceContext().getCommonPartLabel();
188 if (partMeta.getLabel().equalsIgnoreCase(commonPartLabel)) {
189 objectProps.remove(AuthorityJAXBSchema.CSID);
190 objectProps.remove(AuthorityJAXBSchema.SHORT_IDENTIFIER);
191 objectProps.remove(AuthorityJAXBSchema.REF_NAME);