]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
78b1b106a0aefa68e2e3dff819a3e1e4fc140b1b
[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.context;
25
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import org.collectionspace.services.common.ClientType;
30 import org.collectionspace.services.common.ServiceMain;
31 import org.collectionspace.services.common.config.TenantBindingConfigReader;
32 import org.collectionspace.services.common.service.ObjectPartType;
33 import org.collectionspace.services.common.service.ServiceBindingType;
34 import org.collectionspace.services.common.service.ServiceObjectType;
35 import org.collectionspace.services.common.tenant.TenantBindingType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * AbstractServiceContext
41  *
42  * $LastChangedRevision: $
43  * $LastChangedDate: $
44  */
45 public abstract class AbstractServiceContext<IT, OT>
46         implements ServiceContext<IT, OT> {
47
48     final Logger logger = LoggerFactory.getLogger(AbstractServiceContext.class);
49     Map<String, ObjectPartType> objectPartMap = new HashMap<String, ObjectPartType>();
50     private ServiceBindingType serviceBinding;
51     private TenantBindingType tenantBinding;
52     
53     private String overrideDocumentType = null; 
54     
55     public AbstractServiceContext(String serviceName) {
56         TenantBindingConfigReader tReader =
57                 ServiceMain.getInstance().getTenantBindingConfigReader();
58         //TODO: get tenant binding from security context (Subject.g
59         String tenantId = "1"; //hardcoded for movingimages.us
60         tenantBinding = tReader.getTenantBinding(tenantId);
61         if(tenantBinding == null){
62             String msg = "No tenant binding found while processing request for " +
63                     serviceName;
64             logger.error(msg);
65             throw new IllegalStateException(msg);
66         }
67         serviceBinding = tReader.getServiceBinding(tenantId, serviceName);
68         if(serviceBinding == null){
69             String msg = "No service binding found while processing request for " +
70                     serviceName + " for tenant id=" + getTenantId() +
71                     " name=" + getTenantName();
72             logger.error(msg);
73             throw new IllegalStateException(msg);
74         }
75         if(logger.isDebugEnabled()){
76             logger.debug("tenantId=" + tenantId +
77                     " service binding=" + serviceBinding.getName());
78         }
79     }
80
81     /**
82      * getCommonPartLabel get common part label
83      * @return
84      */
85     @Override
86     public String getCommonPartLabel() {
87         return getCommonPartLabel(getServiceName());
88     }
89
90     /**
91      * getCommonPartLabel get common part label
92      * @return
93      */
94     public String getCommonPartLabel(String schemaName) {
95         return schemaName.toLowerCase() + PART_LABEL_SEPERATOR + PART_COMMON_LABEL;
96     }
97
98     @Override
99     public Map<String, ObjectPartType> getPartsMetadata() {
100         if(objectPartMap.size() != 0){
101             return objectPartMap;
102         }
103         ServiceBindingType serviceBinding = getServiceBinding();
104         List<ServiceObjectType> objectTypes = serviceBinding.getObject();
105         for(ServiceObjectType objectType : objectTypes){
106             List<ObjectPartType> objectPartTypes = objectType.getPart();
107             for(ObjectPartType objectPartType : objectPartTypes){
108                 objectPartMap.put(objectPartType.getLabel(), objectPartType);
109             }
110         }
111         return objectPartMap;
112     }
113
114     @Override
115     public String getQualifiedServiceName() {
116         return TenantBindingConfigReader.getTenantQualifiedServiceName(getTenantId(), getServiceName());
117     }
118
119     @Override
120     public String getRepositoryClientName() {
121         return serviceBinding.getRepositoryClient();
122     }
123
124     @Override
125     public ClientType getRepositoryClientType() {
126         //assumption: there is only one repository client configured
127         return ServiceMain.getInstance().getClientType();
128     }
129
130     @Override
131     public String getRepositoryDomainName() {
132         return tenantBinding.getRepositoryDomain();
133     }
134
135     @Override
136     public String getRepositoryWorkspaceId() {
137         TenantBindingConfigReader tbConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();
138         return tbConfigReader.getWorkspaceId(getTenantId(), getServiceName());
139     }
140
141     @Override
142     public String getRepositoryWorkspaceName() {
143         //service name is workspace name by convention
144         return serviceBinding.getName();
145     }
146
147     @Override
148     public ServiceBindingType getServiceBinding() {
149         return serviceBinding;
150     }
151
152     @Override
153     public String getServiceName() {
154         return serviceBinding.getName();
155     } 
156     
157     @Override
158     public String getDocumentType() {
159         // If they have not overridden the setting, use the type of the service
160         // object.
161         return(overrideDocumentType!=null)?overrideDocumentType:
162                 serviceBinding.getObject().get(0).getName();
163     }
164
165     @Override
166     public void setDocumentType(String docType) {
167         overrideDocumentType = docType;
168     }
169
170     @Override
171     public String getTenantId() {
172         return tenantBinding.getId();
173     }
174
175     @Override
176     public String getTenantName() {
177         return tenantBinding.getName();
178     }
179
180     @Override
181     public abstract IT getInput();
182
183     @Override
184     public abstract void setInput(IT input) throws Exception;
185
186     @Override
187     public abstract OT getOutput();
188
189     @Override
190     public abstract void setOutput(OT output) throws Exception;
191
192     @Override
193     public String toString() {
194         return "AbstractServiceContext [" +
195                 "service name=" + serviceBinding.getName() + " " +
196                 "service version=" + serviceBinding.getVersion() + " " +
197                 "tenant id=" + tenantBinding.getId() + " " +
198                 "tenant name=" + tenantBinding.getName() + " " +
199                 tenantBinding.getDisplayName() + " " +
200                 "tenant repository domain=" + tenantBinding.getRepositoryDomain() + " " +
201                 "]";
202     }
203 }