]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
81260435ad866dfc6d6089e49c7e832ded3e33d4
[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.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;
38
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;
46
47 /**
48  * AuthorityDocumentModelHandler
49  *
50  * $LastChangedRevision: $
51  * $LastChangedDate: $
52  */
53 public abstract class AuthorityDocumentModelHandler<AuthCommon>
54         extends DocHandlerBase<AuthCommon> {
55
56     private final Logger logger = LoggerFactory.getLogger(AuthorityDocumentModelHandler.class); 
57     private String authorityCommonSchemaName;
58
59     public AuthorityDocumentModelHandler(String authorityCommonSchemaName) {
60         this.authorityCommonSchemaName = authorityCommonSchemaName;
61     }
62
63     /*
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)
67      */
68     @Override
69     protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
70             throws Exception {
71         Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
72
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);
77         }
78
79         return unQObjectProperties;
80     }
81
82     @Override
83     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
84         super.handleCreate(wrapDoc);
85         // CSPACE-3178:
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
90     }
91
92     /**
93      * If no short identifier was provided in the input payload,
94      * generate a short identifier from the display name.
95      */
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);
103         }
104     }
105  
106     /**
107      * Generate a refName for the authority from the short identifier
108      * and display name.
109      * 
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).
113      * 
114      * @see #filterReadOnlyPropertiesForPart(Map<String, Object>, org.collectionspace.services.common.service.ObjectPartType)
115      * 
116      */
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);
122     }
123     
124     @Override
125     public RefName.RefNameInterface getRefName(ServiceContext ctx,
126                 DocumentModel docModel) {
127         RefName.RefNameInterface refname = null;
128
129         try {
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!!!
135                         shortIdentifier,
136                         displayName);
137                 refname = authority;
138         } catch (Exception e) {
139                 logger.error(e.getMessage(), e);
140         }
141         
142         return refname;
143     }
144     
145     @Override
146     protected String getRefnameDisplayName(DocumentWrapper<DocumentModel> docWrapper) {
147         String result = null;
148         
149         DocumentModel docModel = docWrapper.getWrappedObject();
150         ServiceContext ctx = this.getServiceContext();
151         RefName.Authority refname = (RefName.Authority)getRefName(ctx, docModel);
152         result = refname.getDisplayName();
153         
154         return result;
155     }    
156     
157     public String getShortIdentifier(String authCSID, String schemaName) throws Exception {
158         String shortIdentifier = null;
159         RepositoryInstance repoSession = null;
160
161         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
162         RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx);
163         try {
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);
170         } finally {
171                 if (repoSession != null) {
172                         nuxeoRepoClient.releaseRepositorySession(ctx, repoSession);
173                 }
174         }
175         
176         return shortIdentifier;
177     }
178
179     /**
180      * Filters out selected values supplied in an update request.
181      * 
182      * @param objectProps the properties filtered out from the update payload
183      * @param partMeta metadata for the object to fill
184      */
185     @Override
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);
194         }
195     }
196 }