This project has retired. For details please refer to its
Attic page.
TypeManagerImpl xref
1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing,13 * software distributed under the License is distributed on an14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15 * KIND, either express or implied. See the License for the16 * specific language governing permissions and limitations17 * under the License.18 */19package org.apache.chemistry.opencmis.inmemory;
2021import java.util.ArrayList;
22import java.util.Collection;
23import java.util.HashMap;
24import java.util.List;
25import java.util.Map;
26import java.util.Map.Entry;
2728import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
29import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
30import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionContainer;
31import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
32import org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException;
33import org.apache.chemistry.opencmis.commons.impl.Converter;
34import org.apache.chemistry.opencmis.commons.impl.dataobjects.AbstractPropertyDefinition;
35import org.apache.chemistry.opencmis.commons.impl.dataobjects.TypeDefinitionContainerImpl;
36import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisPropertyDefinitionType;
37import org.apache.chemistry.opencmis.inmemory.storedobj.api.TypeManagerCreatable;
38import org.apache.chemistry.opencmis.inmemory.types.DocumentTypeCreationHelper;
39import org.apache.chemistry.opencmis.inmemory.types.InMemoryDocumentTypeDefinition;
40import org.apache.chemistry.opencmis.inmemory.types.InMemoryFolderTypeDefinition;
41import org.apache.chemistry.opencmis.inmemory.types.InMemoryPolicyTypeDefinition;
42import org.apache.chemistry.opencmis.inmemory.types.InMemoryRelationshipTypeDefinition;
43import org.apache.commons.logging.Log;
44import org.apache.commons.logging.LogFactory;
4546/**47 * Class that manages a type system for a repository types can be added, the48 * inheritance can be managed and type can be retrieved for a given type id.49 *50 * @author Jens51 *52 */53publicclassTypeManagerImpl implements TypeManagerCreatable {
5455privatestaticfinal Log LOG = LogFactory.getLog(TypeManagerImpl.class.getName());
56/**57 * map from repository id to a types map58 */59privatefinal Map<String, TypeDefinitionContainer> fTypesMap = new HashMap<String, TypeDefinitionContainer>();
6061/* (non-Javadoc)62 * @see org.apache.chemistry.opencmis.inmemory.TypeManager#getTypeById(java.lang.String)63 */64public TypeDefinitionContainer getTypeById(String typeId) {
65return fTypesMap.get(typeId);
66 }
6768/* (non-Javadoc)69 * @see org.apache.chemistry.opencmis.inmemory.TypeManager#getTypeByQueryName(java.lang.String)70 */71public TypeDefinition getTypeByQueryName(String typeQueryName) {
72for (Entry<String, TypeDefinitionContainer> entry : fTypesMap.entrySet()) {
73if (entry.getValue().getTypeDefinition().getQueryName().equals(typeQueryName)) {
74return entry.getValue().getTypeDefinition();
75 }
76 }
77returnnull;
78 }
7980/* (non-Javadoc)81 * @see org.apache.chemistry.opencmis.inmemory.TypeManager#getTypeDefinitionList()82 */83publicsynchronized Collection<TypeDefinitionContainer> getTypeDefinitionList() {
8485 List<TypeDefinitionContainer> typeRoots = new ArrayList<TypeDefinitionContainer>();
86// iterate types map and return a list collecting the root types:87for (TypeDefinitionContainer typeDef : fTypesMap.values()) {
88if (typeDef.getTypeDefinition().getParentTypeId() == null) {
89 typeRoots.add(typeDef);
90 }
91 }
9293return typeRoots;
94 }
9596/* (non-Javadoc)97 * @see org.apache.chemistry.opencmis.inmemory.TypeManager#getRootTypes()98 */99public List<TypeDefinitionContainer> getRootTypes() {
100// just take first repository101 List<TypeDefinitionContainer> rootTypes = new ArrayList<TypeDefinitionContainer>();
102103for (TypeDefinitionContainer type : fTypesMap.values()) {
104if (isRootType(type)) {
105 rootTypes.add(type);
106 }
107 }
108109return rootTypes;
110 }
111112/**113 * Initialize the type system with the given types. This list must not114 * contain the CMIS default types. The default type are always contained by115 * default.116 *117 * @param typesList118 * list of types to add to the repository119 *120 */121publicvoid initTypeSystem(List<TypeDefinition> typesList) {
122123 createCmisDefaultTypes();
124125// merge all types from the list and build the correct hierachy with126// children127// and property lists128if (null != typesList) {
129for (TypeDefinition typeDef : typesList) {
130 addTypeDefinition(typeDef);
131 }
132 }
133134 }
135136/**137 * Add a type to the type system. Add all properties from inherited types,138 * add type to children of parent types.139 *140 * @param repositoryId141 * repository to which the type is added142 * @param cmisType143 * new type to add144 */145publicvoid addTypeDefinition(TypeDefinition cmisType) {
146147 TypeDefinitionContainerImpl typeContainer = new TypeDefinitionContainerImpl(cmisType);
148149// add new type to children of parent types150 TypeDefinitionContainer parentTypeContainer = fTypesMap.get(cmisType.getParentTypeId());
151 parentTypeContainer.getChildren().add(typeContainer);
152153// recursively add inherited properties154 Map<String, PropertyDefinition<?>> propDefs = typeContainer.getTypeDefinition().getPropertyDefinitions();
155 addInheritedProperties(propDefs, parentTypeContainer.getTypeDefinition());
156157 LOG.info("Adding type definition with name " + cmisType.getLocalName() + " and id "158 + cmisType.getId() + " to repository.");
159// add type to type map160 fTypesMap.put(cmisType.getId(), typeContainer);
161 }
162163publicvoid updateTypeDefinition(TypeDefinition typeDefinition) {
164thrownew CmisNotSupportedException("updating a type definition is not supported.");
165 }
166167/**168 * Remove a type from a type system169 * @param typeId170 */171publicvoid deleteTypeDefinition(String typeId) {
172 fTypesMap.remove(typeId);
173 }
174175176/**177 * Remove all types from the type system. After this call only the default178 * CMIS types are present in the type system. Use this method with care, its179 * mainly intended for unit tests180 *181 * @param repositoryId182 */183publicvoid clearTypeSystem() {
184 fTypesMap.clear();
185 createCmisDefaultTypes();
186 }
187188/* (non-Javadoc)189 * @see org.apache.chemistry.opencmis.inmemory.TypeManager#getPropertyIdForQueryName(org.apache.chemistry.opencmis.commons.definitions.TypeDefinition, java.lang.String)190 */191public String getPropertyIdForQueryName(TypeDefinition typeDefinition, String propQueryName) {
192for (PropertyDefinition<?> pd : typeDefinition.getPropertyDefinitions().values()) {
193if (pd.getQueryName().equals(propQueryName)) {
194return pd.getId();
195 }
196 }
197returnnull;
198 }
199200privatevoid addInheritedProperties(Map<String, PropertyDefinition<?>> propDefs, TypeDefinition typeDefinition) {
201202if (null == typeDefinition) {
203return;
204 }
205206if (null != typeDefinition.getPropertyDefinitions())
207 {
208 addInheritedPropertyDefinitions(propDefs, typeDefinition.getPropertyDefinitions());
209// propDefs.putAll(typeDefinition.getPropertyDefinitions());210 }
211212 TypeDefinitionContainer parentTypeContainer = fTypesMap.get(typeDefinition.getParentTypeId());
213 TypeDefinition parentType = (null == parentTypeContainer ? null : parentTypeContainer.getTypeDefinition());
214 addInheritedProperties(propDefs, parentType);
215 }
216217privatestaticvoid addInheritedPropertyDefinitions(Map<String, PropertyDefinition<?>> propDefs,
218 Map<String, PropertyDefinition<?>> superPropDefs) {
219220for (Entry<String, PropertyDefinition<?>> superProp : superPropDefs.entrySet()) {
221 PropertyDefinition<?> superPropDef = superProp.getValue();
222 PropertyDefinition<?> clone = clonePropertyDefinition(superPropDef);
223 ((AbstractPropertyDefinition<?>) clone).setIsInherited(true);
224 propDefs.put(superProp.getKey(), clone);
225 }
226 }
227228privatevoid createCmisDefaultTypes() {
229 List<TypeDefinition> typesList = DocumentTypeCreationHelper.createDefaultTypes();
230for (TypeDefinition typeDef : typesList) {
231 TypeDefinitionContainerImpl typeContainer = new TypeDefinitionContainerImpl(typeDef);
232 fTypesMap.put(typeDef.getId(), typeContainer);
233 }
234 }
235236privatestaticboolean isRootType(TypeDefinitionContainer c) {
237if (c.getTypeDefinition().equals(InMemoryFolderTypeDefinition.getRootFolderType())
238 || c.getTypeDefinition().equals(InMemoryDocumentTypeDefinition.getRootDocumentType())
239 || c.getTypeDefinition().equals(InMemoryRelationshipTypeDefinition.getRootRelationshipType())
240 || c.getTypeDefinition().equals(InMemoryPolicyTypeDefinition.getRootPolicyType())) {
241returntrue;
242 } else {
243return false;
244 }
245 }
246247privatestatic PropertyDefinition<?> clonePropertyDefinition(PropertyDefinition<?> src) {
248// use JAXB converter to easily clone a property definition249 CmisPropertyDefinitionType tmp = Converter.convert(src);
250 PropertyDefinition<?> clone = Converter.convert(tmp);
251return clone;
252 }
253254// private static PropertyDefinition<?>255// clonePropertyDefinition2(PropertyDefinition<?> src)256// throws IOException, ClassNotFoundException {257// ByteArrayOutputStream bout = new ByteArrayOutputStream();258// ObjectOutputStream oout = new ObjectOutputStream(bout);259// oout.writeObject(src);260// byte[] bytes = bout.toByteArray();261//262// ByteArrayInputStream bin = new ByteArrayInputStream(bytes);263// ObjectInputStream oin = new ObjectInputStream(bin);264// PropertyDefinition<?> clone = (PropertyDefinition<?>) oin.readObject();265// return clone;266// }267268 }