]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9235008b3be17fffca8dbecc8b2474b979b960df
[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.common.api.RefName;
29 import org.collectionspace.services.common.api.Tools;
30 import org.collectionspace.services.common.context.MultipartServiceContext;
31 import org.collectionspace.services.common.document.DocumentWrapper;
32 import org.collectionspace.services.common.service.ObjectPartType;
33 import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema;
34
35 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
36 import org.nuxeo.ecm.core.api.ClientException;
37 import org.nuxeo.ecm.core.api.DocumentModel;
38
39 /**
40  * AuthorityDocumentModelHandler
41  *
42  * $LastChangedRevision: $
43  * $LastChangedDate: $
44  */
45 public abstract class AuthorityDocumentModelHandler<AuthCommon>
46         extends DocHandlerBase<AuthCommon> {
47
48     private String authorityCommonSchemaName;
49
50     public AuthorityDocumentModelHandler(String authorityCommonSchemaName) {
51         this.authorityCommonSchemaName = authorityCommonSchemaName;
52     }
53
54     /*
55      * Non standard injection of CSID into common part, since caller may access through
56      * shortId, and not know the CSID.
57      * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
58      */
59     @Override
60     protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
61             throws Exception {
62         Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
63
64         // Add the CSID to the common part
65         if (partMeta.getLabel().equalsIgnoreCase(authorityCommonSchemaName)) {
66             String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
67             unQObjectProperties.put("csid", csid);
68         }
69
70         return unQObjectProperties;
71     }
72
73     @Override
74     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
75         super.handleCreate(wrapDoc);
76         // CSPACE-3178:
77         // Uncomment once debugged and App layer is read to integrate
78         // Experimenting with this uncommented now ...
79         handleDisplayNameAsShortIdentifier(wrapDoc.getWrappedObject(), authorityCommonSchemaName);
80         updateRefnameForAuthority(wrapDoc, authorityCommonSchemaName);//CSPACE-3178
81     }
82
83     private void handleDisplayNameAsShortIdentifier(DocumentModel docModel, String schemaName) throws Exception {
84         String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
85         String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME);
86         String shortDisplayName = "";
87         if (Tools.isEmpty(shortIdentifier)) {
88             String generatedShortIdentifier = AuthorityIdentifierUtils.generateShortIdentifierFromDisplayName(displayName, shortDisplayName);
89             docModel.setProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER, generatedShortIdentifier);
90         }
91     }
92     
93     protected void updateRefnameForAuthority(DocumentWrapper<DocumentModel> wrapDoc, String schemaName) throws Exception {
94         DocumentModel docModel = wrapDoc.getWrappedObject();
95         String suppliedRefName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.REF_NAME);
96         // CSPACE-3178:
97         // Temporarily accept client-supplied refName values, rather than always generating such values,
98         // Remove the surrounding 'if' statement when clients should no longer supply refName values.
99         if (suppliedRefName == null || suppliedRefName.isEmpty()) {
100             String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
101             String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME);
102             MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
103             RefName.Authority authority = RefName.buildAuthority(ctx.getTenantName(),
104                     ctx.getServiceName(),
105                     shortIdentifier,
106                     displayName);
107             String refName = authority.toString();
108             docModel.setProperty(schemaName, AuthorityJAXBSchema.REF_NAME, refName);
109         }
110     }
111
112     public String getShortIdentifier(DocumentWrapper<DocumentModel> wrapDoc, String schemaName) {
113         DocumentModel docModel = wrapDoc.getWrappedObject();
114         String shortIdentifier = null;
115         try {
116             shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
117         } catch (ClientException ce) {
118             throw new RuntimeException("AuthorityDocHandler Internal Error: cannot get shortId!", ce);
119         }
120         return shortIdentifier;
121     }
122
123     /**
124      * Filters out selected values supplied in an update request.
125      * 
126      * @param objectProps the properties filtered out from the update payload
127      * @param partMeta metadata for the object to fill
128      */
129     @Override
130     public void filterReadOnlyPropertiesForPart(
131             Map<String, Object> objectProps, ObjectPartType partMeta) {
132         super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
133         String commonPartLabel = getServiceContext().getCommonPartLabel();
134         if (partMeta.getLabel().equalsIgnoreCase(commonPartLabel)) {
135             objectProps.remove(AuthorityJAXBSchema.CSID);
136             objectProps.remove(AuthorityJAXBSchema.SHORT_IDENTIFIER);
137             // Enable when clients should no longer supply refName values
138             // objectProps.remove(AuthorityItemJAXBSchema.REF_NAME); // CSPACE-3178
139
140         }
141     }
142 }