]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e2b22a664784fa8e2a373da7a59a6fe4becaaa71
[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.Tools;
32 import org.collectionspace.services.common.context.MultipartServiceContext;
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
38 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
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.nuxeo.ecm.core.api.repository.RepositoryInstance;
43
44 /**
45  * AuthorityDocumentModelHandler
46  *
47  * $LastChangedRevision: $
48  * $LastChangedDate: $
49  */
50 public abstract class AuthorityDocumentModelHandler<AuthCommon>
51         extends DocHandlerBase<AuthCommon> {
52
53     private String authorityCommonSchemaName;
54
55     public AuthorityDocumentModelHandler(String authorityCommonSchemaName) {
56         this.authorityCommonSchemaName = authorityCommonSchemaName;
57     }
58
59     /*
60      * Non standard injection of CSID into common part, since caller may access through
61      * shortId, and not know the CSID.
62      * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
63      */
64     @Override
65     protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
66             throws Exception {
67         Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
68
69         // Add the CSID to the common part
70         if (partMeta.getLabel().equalsIgnoreCase(authorityCommonSchemaName)) {
71             String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
72             unQObjectProperties.put("csid", csid);
73         }
74
75         return unQObjectProperties;
76     }
77
78     @Override
79     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
80         super.handleCreate(wrapDoc);
81         // CSPACE-3178:
82         // Uncomment once debugged and App layer is read to integrate
83         // Experimenting with this uncommented now ...
84         handleDisplayNameAsShortIdentifier(wrapDoc.getWrappedObject(), authorityCommonSchemaName);
85         updateRefnameForAuthority(wrapDoc, authorityCommonSchemaName);//CSPACE-3178
86     }
87
88     /**
89      * If no short identifier was provided in the input payload,
90      * generate a short identifier from the display name.
91      */
92     private void handleDisplayNameAsShortIdentifier(DocumentModel docModel, String schemaName) throws Exception {
93         String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
94         String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME);
95         String shortDisplayName = "";
96         if (Tools.isEmpty(shortIdentifier)) {
97             String generatedShortIdentifier = AuthorityIdentifierUtils.generateShortIdentifierFromDisplayName(displayName, shortDisplayName);
98             docModel.setProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER, generatedShortIdentifier);
99         }
100     }
101  
102     /**
103      * Generate a refName for the authority from the short identifier
104      * and display name.
105      * 
106      * All refNames for authorities are generated.  If a client supplies
107      * a refName, it will be overwritten during create (per this method) 
108      * or discarded during update (per filterReadOnlyPropertiesForPart).
109      * 
110      * @see #filterReadOnlyPropertiesForPart(Map<String, Object>, org.collectionspace.services.common.service.ObjectPartType)
111      * 
112      */
113     protected void updateRefnameForAuthority(DocumentWrapper<DocumentModel> wrapDoc, String schemaName) throws Exception {
114         DocumentModel docModel = wrapDoc.getWrappedObject();
115         String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
116         String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME);
117         MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
118         RefName.Authority authority = RefName.buildAuthority(ctx.getTenantName(),
119                 ctx.getServiceName(),
120                 shortIdentifier,
121                 displayName);
122         String refName = authority.toString();
123         docModel.setProperty(schemaName, AuthorityJAXBSchema.REF_NAME, refName);
124     }
125
126     public String getShortIdentifier(String authCSID, String schemaName) throws Exception {
127         String shortIdentifier = null;
128         RepositoryInstance repoSession = null;
129
130         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
131         RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx);
132         try {
133                 repoSession = nuxeoRepoClient.getRepositorySession();
134             DocumentWrapper<DocumentModel> wrapDoc = nuxeoRepoClient.getDocFromCsid(ctx, repoSession, authCSID);
135             DocumentModel docModel = wrapDoc.getWrappedObject();
136             shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
137         } catch (ClientException ce) {
138             throw new RuntimeException("AuthorityDocHandler Internal Error: cannot get shortId!", ce);
139         } finally {
140                 if (repoSession != null) {
141                         nuxeoRepoClient.releaseRepositorySession(repoSession);
142                 }
143         }
144         
145         return shortIdentifier;
146     }
147
148     /**
149      * Filters out selected values supplied in an update request.
150      * 
151      * @param objectProps the properties filtered out from the update payload
152      * @param partMeta metadata for the object to fill
153      */
154     @Override
155     public void filterReadOnlyPropertiesForPart(
156             Map<String, Object> objectProps, ObjectPartType partMeta) {
157         super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
158         String commonPartLabel = getServiceContext().getCommonPartLabel();
159         if (partMeta.getLabel().equalsIgnoreCase(commonPartLabel)) {
160             objectProps.remove(AuthorityJAXBSchema.CSID);
161             objectProps.remove(AuthorityJAXBSchema.SHORT_IDENTIFIER);
162             objectProps.remove(AuthorityJAXBSchema.REF_NAME);
163         }
164     }
165 }