This project has retired. For details please refer to its Attic page.
DefaultTypeSystemCreator xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.inmemory.types;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.LinkedList;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.chemistry.opencmis.commons.definitions.Choice;
29  import org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition;
30  import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
31  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
32  import org.apache.chemistry.opencmis.commons.enums.Updatability;
33  import org.apache.chemistry.opencmis.commons.impl.dataobjects.ChoiceImpl;
34  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyDateTimeDefinitionImpl;
35  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyDecimalDefinitionImpl;
36  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyHtmlDefinitionImpl;
37  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyIdDefinitionImpl;
38  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyIntegerDefinitionImpl;
39  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyStringDefinitionImpl;
40  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyUriDefinitionImpl;
41  import org.apache.chemistry.opencmis.inmemory.TypeCreator;
42  
43  public class DefaultTypeSystemCreator implements TypeCreator {
44      public static final List<TypeDefinition> singletonTypes = buildTypesList();
45      public static final String COMPLEX_TYPE = "ComplexType";
46      public static final String TOPLEVEL_TYPE = "DocumentTopLevel";
47      public static final String VERSIONED_TYPE = "VersionableType";
48      public static final String LEVEL1_TYPE = "DocumentLevel1";
49      public static final String LEVEL2_TYPE = "DocumentLevel2";
50  
51      /**
52       * in the public interface of this class we return the singleton containing
53       * the required types for testing
54       */
55      public List<TypeDefinition> createTypesList() {
56          return singletonTypes;
57      }
58  
59      public static List<TypeDefinition> getTypesList() {
60          return singletonTypes;
61      }
62  
63      public static TypeDefinition getTypeById(String typeId) {
64          for (TypeDefinition typeDef : singletonTypes) {
65              if (typeDef.getId().equals(typeId)) {
66                  return typeDef;
67              }
68          }
69          return null;
70      }
71  
72      /**
73       * Create root types and a collection of sample types.
74       * 
75       * @return typesMap a map filled with created types
76       */
77      private static List<TypeDefinition> buildTypesList() {
78          // always add CMIS default types
79          List<TypeDefinition> typesList = new LinkedList<TypeDefinition>();
80  
81          InMemoryDocumentTypeDefinition cmisType1 = new InMemoryDocumentTypeDefinition("MyDocType1",
82                  "My Type 1 Level 1", InMemoryDocumentTypeDefinition.getRootDocumentType());
83          typesList.add(cmisType1);
84  
85          InMemoryDocumentTypeDefinition cmisType2 = new InMemoryDocumentTypeDefinition("MyDocType2",
86                  "My Type 2 Level 1", InMemoryDocumentTypeDefinition.getRootDocumentType());
87          typesList.add(cmisType2);
88  
89          InMemoryDocumentTypeDefinition cmisType11 = new InMemoryDocumentTypeDefinition("MyDocType1.1",
90                  "My Type 3 Level 2", cmisType1);
91          typesList.add(cmisType11);
92  
93          InMemoryDocumentTypeDefinition cmisType111 = new InMemoryDocumentTypeDefinition("MyDocType1.1.1",
94                  "My Type 4 Level 3", cmisType11);
95          typesList.add(cmisType111);
96  
97          InMemoryDocumentTypeDefinition cmisType112 = new InMemoryDocumentTypeDefinition("MyDocType1.1.2",
98                  "My Type 5 Level 3", cmisType11);
99          typesList.add(cmisType112);
100 
101         InMemoryDocumentTypeDefinition cmisType12 = new InMemoryDocumentTypeDefinition("MyDocType1.2",
102                 "My Type 6 Level 2", cmisType1);
103         typesList.add(cmisType12);
104 
105         InMemoryDocumentTypeDefinition cmisType21 = new InMemoryDocumentTypeDefinition("MyDocType2.1",
106                 "My Type 7 Level 2", cmisType2);
107         typesList.add(cmisType21);
108 
109         InMemoryDocumentTypeDefinition cmisType22 = new InMemoryDocumentTypeDefinition("MyDocType2.2",
110                 "My Type 8 Level 2", cmisType2);
111         typesList.add(cmisType22);
112         InMemoryDocumentTypeDefinition cmisType23 = new InMemoryDocumentTypeDefinition("MyDocType2.3",
113                 "My Type 9 Level 2", cmisType2);
114         typesList.add(cmisType23);
115         DocumentTypeDefinition cmisType24 = new InMemoryDocumentTypeDefinition("MyDocType2.4", "My Type 10 Level 2",
116                 cmisType2);
117         typesList.add(cmisType24);
118         InMemoryDocumentTypeDefinition cmisType25 = new InMemoryDocumentTypeDefinition("MyDocType2.5",
119                 "My Type 11 Level 2", cmisType2);
120         typesList.add(cmisType25);
121 
122         InMemoryDocumentTypeDefinition cmisType26 = new InMemoryDocumentTypeDefinition("MyDocType2.6",
123                 "My Type 12 Level 2", cmisType2);
124         typesList.add(cmisType26);
125         InMemoryDocumentTypeDefinition cmisType27 = new InMemoryDocumentTypeDefinition("MyDocType2.7",
126                 "My Type 13 Level 2", cmisType2);
127         typesList.add(cmisType27);
128         InMemoryDocumentTypeDefinition cmisType28 = new InMemoryDocumentTypeDefinition("MyDocType2.8",
129                 "My Type 14 Level 2", cmisType2);
130         typesList.add(cmisType28);
131         InMemoryDocumentTypeDefinition cmisType29 = new InMemoryDocumentTypeDefinition("MyDocType2.9",
132                 "My Type 15 Level 2", cmisType2);
133         typesList.add(cmisType29);
134 
135         // create a complex type with properties
136         InMemoryDocumentTypeDefinition cmisComplexType = new InMemoryDocumentTypeDefinition(COMPLEX_TYPE,
137                 "Complex type with properties, Level 1", InMemoryDocumentTypeDefinition.getRootDocumentType());
138 
139         // create a boolean property definition
140 
141         Map<String, PropertyDefinition<?>> propertyDefinitions = new HashMap<String, PropertyDefinition<?>>();
142 
143         PropertyDefinition<Boolean> prop = PropertyCreationHelper.createBooleanDefinition("BooleanProp",
144                 "Sample Boolean Property", Updatability.READWRITE);
145         propertyDefinitions.put(prop.getId(), prop);
146 
147         prop = PropertyCreationHelper.createBooleanMultiDefinition("BooleanPropMV",
148                 "Sample Boolean multi-value Property", Updatability.READWRITE);
149         propertyDefinitions.put(prop.getId(), prop);
150 
151         PropertyDateTimeDefinitionImpl prop2 = PropertyCreationHelper.createDateTimeDefinition("DateTimeProp",
152                 "Sample DateTime Property", Updatability.READWRITE);
153         propertyDefinitions.put(prop2.getId(), prop2);
154 
155         prop2 = PropertyCreationHelper.createDateTimeMultiDefinition("DateTimePropMV",
156                 "Sample DateTime multi-value Property", Updatability.READWRITE);
157         propertyDefinitions.put(prop2.getId(), prop2);
158 
159         PropertyDecimalDefinitionImpl prop3 = PropertyCreationHelper.createDecimalDefinition("DecimalProp",
160                 "Sample Decimal Property", Updatability.READWRITE);
161         propertyDefinitions.put(prop3.getId(), prop3);
162 
163         prop3 = PropertyCreationHelper.createDecimalMultiDefinition("DecimalPropMV",
164                 "Sample Decimal multi-value Property", Updatability.READWRITE);
165         propertyDefinitions.put(prop3.getId(), prop3);
166 
167         PropertyHtmlDefinitionImpl prop4 = PropertyCreationHelper.createHtmlDefinition("HtmlProp",
168                 "Sample Html Property", Updatability.READWRITE);
169         propertyDefinitions.put(prop4.getId(), prop4);
170 
171         prop4 = PropertyCreationHelper.createHtmlMultiDefinition("HtmlPropMV", "Sample Html multi-value Property",
172                 Updatability.READWRITE);
173         propertyDefinitions.put(prop4.getId(), prop4);
174 
175         PropertyIdDefinitionImpl prop5 = PropertyCreationHelper.createIdDefinition("IdProp", "Sample Id Property",
176                 Updatability.READWRITE);
177         propertyDefinitions.put(prop5.getId(), prop5);
178 
179         prop5 = PropertyCreationHelper.createIdMultiDefinition("IdPropMV", "Sample Id Html multi-value Property",
180                 Updatability.READWRITE);
181         propertyDefinitions.put(prop5.getId(), prop5);
182 
183         PropertyIntegerDefinitionImpl prop6 = PropertyCreationHelper.createIntegerDefinition("IntProp",
184                 "Sample Int Property", Updatability.READWRITE);
185         propertyDefinitions.put(prop6.getId(), prop6);
186 
187         prop6 = PropertyCreationHelper.createIntegerMultiDefinition("IntPropMV", "Sample Int multi-value Property",
188                 Updatability.READWRITE);
189         propertyDefinitions.put(prop6.getId(), prop6);
190 
191         PropertyStringDefinitionImpl prop7 = PropertyCreationHelper.createStringDefinition("StringProp",
192                 "Sample String Property", Updatability.READWRITE);
193         propertyDefinitions.put(prop7.getId(), prop7);
194 
195         PropertyUriDefinitionImpl prop8 = PropertyCreationHelper.createUriDefinition("UriProp", "Sample Uri Property",
196                 Updatability.READWRITE);
197         propertyDefinitions.put(prop8.getId(), prop8);
198 
199         prop8 = PropertyCreationHelper.createUriMultiDefinition("UriPropMV", "Sample Uri multi-value Property",
200                 Updatability.READWRITE);
201         propertyDefinitions.put(prop8.getId(), prop8);
202 
203         PropertyStringDefinitionImpl prop9 = PropertyCreationHelper.createStringDefinition("PickListProp",
204                 "Sample Pick List Property", Updatability.READWRITE);
205         List<Choice<String>> choiceList = new ArrayList<Choice<String>>();
206         ChoiceImpl<String> elem = new ChoiceImpl<String>();
207         elem.setValue(Collections.singletonList("red"));
208         choiceList.add(elem);
209         elem = new ChoiceImpl<String>();
210         elem.setValue(Collections.singletonList("green"));
211         choiceList.add(elem);
212         elem = new ChoiceImpl<String>();
213         elem.setValue(Collections.singletonList("blue"));
214         choiceList.add(elem);
215         elem = new ChoiceImpl<String>();
216         elem.setValue(Collections.singletonList("black"));
217         choiceList.add(elem);
218         prop9.setChoices(choiceList);
219         prop9.setDefaultValue(Collections.singletonList("blue"));
220         propertyDefinitions.put(prop9.getId(), prop9);
221 
222         /*
223          * try short form: / PropertyCreationHelper.addElemToPicklist(prop9,
224          * "red"); PropertyCreationHelper.addElemToPicklist(prop9, "green");
225          * PropertyCreationHelper.addElemToPicklist(prop9, "blue");
226          * PropertyCreationHelper.addElemToPicklist(prop9, "black");
227          * PropertyCreationHelper.setDefaultValue(prop9, "blue"); /
228          */
229 
230         cmisComplexType.setPropertyDefinitions(propertyDefinitions);
231 
232         // add type to types collection
233         typesList.add(cmisComplexType);
234 
235         // create a type hierarchy with inherited properties
236         InMemoryDocumentTypeDefinition cmisDocTypeTopLevel = new InMemoryDocumentTypeDefinition(TOPLEVEL_TYPE,
237                 "Document type with properties, Level 1", InMemoryDocumentTypeDefinition.getRootDocumentType());
238 
239         InMemoryDocumentTypeDefinition cmisDocTypeLevel1 = new InMemoryDocumentTypeDefinition(LEVEL1_TYPE,
240                 "Document type with inherited properties, Level 2", cmisDocTypeTopLevel);
241 
242         InMemoryDocumentTypeDefinition cmisDocTypeLevel2 = new InMemoryDocumentTypeDefinition(LEVEL2_TYPE,
243                 "Document type with inherited properties, Level 3", cmisDocTypeLevel1);
244 
245         propertyDefinitions = new HashMap<String, PropertyDefinition<?>>();
246         PropertyStringDefinitionImpl propTop = PropertyCreationHelper.createStringDefinition("StringPropTopLevel",
247                 "Sample String Property", Updatability.READWRITE);
248         propertyDefinitions.put(propTop.getId(), propTop);
249         cmisDocTypeTopLevel.setPropertyDefinitions(propertyDefinitions);
250 
251         propertyDefinitions = new HashMap<String, PropertyDefinition<?>>();
252         PropertyStringDefinitionImpl propLevel1 = PropertyCreationHelper.createStringDefinition("StringPropLevel1",
253                 "String Property Level 1", Updatability.READWRITE);
254         propertyDefinitions.put(propLevel1.getId(), propLevel1);
255         cmisDocTypeLevel1.setPropertyDefinitions(propertyDefinitions);
256 
257         propertyDefinitions = new HashMap<String, PropertyDefinition<?>>();
258         PropertyStringDefinitionImpl propLevel2 = PropertyCreationHelper.createStringDefinition("StringPropLevel2",
259                 "String Property Level 2", Updatability.READWRITE);
260         propertyDefinitions.put(propLevel2.getId(), propLevel2);
261         cmisDocTypeLevel2.setPropertyDefinitions(propertyDefinitions);
262 
263         // add type to types collection
264         typesList.add(cmisDocTypeTopLevel);
265         typesList.add(cmisDocTypeLevel1);
266         typesList.add(cmisDocTypeLevel2);
267 
268         // Create a type that is versionable
269         InMemoryDocumentTypeDefinition cmisVersionedType = new InMemoryDocumentTypeDefinition(VERSIONED_TYPE,
270                 "VersionedType", InMemoryDocumentTypeDefinition.getRootDocumentType());
271 
272         // create a single String property definition
273 
274         propertyDefinitions = new HashMap<String, PropertyDefinition<?>>();
275 
276         PropertyStringDefinitionImpl prop1 = PropertyCreationHelper.createStringDefinition("VersionedStringProp",
277                 "Sample String Property", Updatability.WHENCHECKEDOUT);
278         propertyDefinitions.put(prop1.getId(), prop1);
279 
280         cmisVersionedType.addCustomPropertyDefinitions(propertyDefinitions);
281         cmisVersionedType.setIsVersionable(true); // make it a versionable type;
282 
283         // add type to types collection
284         typesList.add(cmisVersionedType);
285 
286         return typesList;
287     }
288 
289 }