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.MultipartServiceContext;
34 import org.collectionspace.services.common.context.ServiceContext;
35 import org.collectionspace.services.common.document.DocumentWrapper;
36 import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema;
37 import org.collectionspace.services.config.service.ObjectPartType;
39 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
40 import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl;
41 import org.nuxeo.ecm.core.api.ClientException;
42 import org.nuxeo.ecm.core.api.DocumentModel;
43 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * AuthorityDocumentModelHandler
50 * $LastChangedRevision: $
53 public abstract class AuthorityDocumentModelHandler<AuthCommon>
54 extends DocHandlerBase<AuthCommon> {
56 private final Logger logger = LoggerFactory.getLogger(AuthorityDocumentModelHandler.class);
57 private String authorityCommonSchemaName;
59 public AuthorityDocumentModelHandler(String authorityCommonSchemaName) {
60 this.authorityCommonSchemaName = authorityCommonSchemaName;
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)
69 protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
71 Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
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);
79 return unQObjectProperties;
83 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
84 super.handleCreate(wrapDoc);
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
93 * If no short identifier was provided in the input payload,
94 * generate a short identifier from the display name.
96 private void handleDisplayNameAsShortIdentifier(DocumentModel docModel, String schemaName) throws Exception {
97 String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
98 String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME);
99 String shortDisplayName = "";
100 if (Tools.isEmpty(shortIdentifier)) {
101 String generatedShortIdentifier = AuthorityIdentifierUtils.generateShortIdentifierFromDisplayName(displayName, shortDisplayName);
102 docModel.setProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER, generatedShortIdentifier);
107 * Generate a refName for the authority from the short identifier
110 * All refNames for authorities are generated. If a client supplies
111 * a refName, it will be overwritten during create (per this method)
112 * or discarded during update (per filterReadOnlyPropertiesForPart).
114 * @see #filterReadOnlyPropertiesForPart(Map<String, Object>, org.collectionspace.services.common.service.ObjectPartType)
117 protected void updateRefnameForAuthority(DocumentWrapper<DocumentModel> wrapDoc, String schemaName) throws Exception {
118 DocumentModel docModel = wrapDoc.getWrappedObject();
119 RefName.Authority authority = (Authority) getRefName(getServiceContext(), docModel);
120 String refName = authority.toString();
121 docModel.setProperty(schemaName, AuthorityJAXBSchema.REF_NAME, refName);
125 public RefName.RefNameInterface getRefName(ServiceContext ctx,
126 DocumentModel docModel) {
127 RefName.RefNameInterface refname = null;
130 String shortIdentifier = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
131 String displayName = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.DISPLAY_NAME);
132 RefName.Authority authority = RefName.Authority.buildAuthority(ctx.getTenantName(),
133 ctx.getServiceName(),
134 null, // Only use shortId form!!!
138 } catch (Exception e) {
139 logger.error(e.getMessage(), e);
146 protected String getRefnameDisplayName(DocumentWrapper<DocumentModel> docWrapper) {
147 String result = null;
149 DocumentModel docModel = docWrapper.getWrappedObject();
150 ServiceContext ctx = this.getServiceContext();
151 RefName.Authority refname = (RefName.Authority)getRefName(ctx, docModel);
152 result = refname.getDisplayName();
157 public String getShortIdentifier(String authCSID, String schemaName) throws Exception {
158 String shortIdentifier = null;
159 RepositoryInstance repoSession = null;
161 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
162 RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx);
164 repoSession = nuxeoRepoClient.getRepositorySession(ctx);
165 DocumentWrapper<DocumentModel> wrapDoc = nuxeoRepoClient.getDocFromCsid(ctx, repoSession, authCSID);
166 DocumentModel docModel = wrapDoc.getWrappedObject();
167 shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
168 } catch (ClientException ce) {
169 throw new RuntimeException("AuthorityDocHandler Internal Error: cannot get shortId!", ce);
171 if (repoSession != null) {
172 nuxeoRepoClient.releaseRepositorySession(ctx, repoSession);
176 return shortIdentifier;
180 * Filters out selected values supplied in an update request.
182 * @param objectProps the properties filtered out from the update payload
183 * @param partMeta metadata for the object to fill
186 public void filterReadOnlyPropertiesForPart(
187 Map<String, Object> objectProps, ObjectPartType partMeta) {
188 super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
189 String commonPartLabel = getServiceContext().getCommonPartLabel();
190 if (partMeta.getLabel().equalsIgnoreCase(commonPartLabel)) {
191 objectProps.remove(AuthorityJAXBSchema.CSID);
192 objectProps.remove(AuthorityJAXBSchema.SHORT_IDENTIFIER);
193 objectProps.remove(AuthorityJAXBSchema.REF_NAME);