This project has retired. For details please refer to its Attic page.
TypeValidator 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;
20  
21  import java.util.Collection;
22  import java.util.Iterator;
23  
24  import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
25  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
26  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionContainer;
27  import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
28  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
29  import org.apache.chemistry.opencmis.server.support.TypeManager;
30  
31  /**
32   * A helper class doing some consistency checks when new type definitions are added to the system
33   * 
34   * @author Jens
35   *
36   */
37  public class TypeValidator {
38      
39      public static void checkType(TypeManager tm, TypeDefinition td) {
40  
41          if (null == tm.getTypeById(td.getParentTypeId())) {
42              throw new CmisInvalidArgumentException("Cannot add type, because parent with id " + td.getParentTypeId()
43                      + " does not exist.");
44          }
45  
46          checkTypeId(tm, td.getId());
47          checkTypeQueryName(tm, td.getQueryName());
48          checkTypeLocalName(tm, td.getLocalName());
49      }
50      
51      public static void checkProperties(TypeManager tm, Collection<PropertyDefinition<?>> pds) {
52  
53          Collection<TypeDefinitionContainer> tdl = tm.getTypeDefinitionList();
54          for (PropertyDefinition<?> pd2 : pds) {
55              // check id syntax
56              if (null == pd2.getId())
57                  throw new CmisInvalidArgumentException("property id cannot be null.");
58              if (!NameValidator.isValidId(pd2.getId()))
59                  throw new CmisInvalidArgumentException(NameValidator.ERROR_ILLEGAL_NAME);
60      
61              // check query name syntax
62              if (null == pd2.getQueryName())
63                  throw new CmisInvalidArgumentException("property query name cannot be null.");
64              if (!NameValidator.isValidQueryName(pd2.getQueryName()))
65                  throw new CmisInvalidArgumentException(NameValidator.ERROR_ILLEGAL_NAME);
66      
67              // check local name syntax
68              if (null == pd2.getLocalName())
69                  throw new CmisInvalidArgumentException("property local name cannot be null.");
70              if (!NameValidator.isValidLocalName(pd2.getLocalName()))
71                  throw new CmisInvalidArgumentException(NameValidator.ERROR_ILLEGAL_NAME);
72              
73              for (TypeDefinitionContainer tdc : tdl) {
74                  TypeDefinition td = tdc.getTypeDefinition();
75                  for (PropertyDefinition<?> pd1 : td.getPropertyDefinitions().values()) {
76                      // check if id is used
77                      if (pd1.getId().equals(pd2.getId()))
78                          throw new CmisConstraintException("Property id " + pd2.getId() + " already in use in type "
79                                  + td.getId());
80                      // check if query name is used
81                      if (pd1.getQueryName().equals(pd2.getQueryName()))
82                          throw new CmisConstraintException("Property query name " + pd2.getQueryName() + " already in use in type "
83                                  + td.getQueryName());
84                      // check if local name is used
85                      if (pd1.getLocalName().equals(pd2.getLocalName()))
86                          throw new CmisConstraintException("Property local name " + pd2.getLocalName() + " already in use in type "
87                                  + td.getId());
88                  }
89              }
90          }        
91      }
92      
93      private static void checkTypeId(TypeManager tm, String typeId) {
94  
95          if (null == typeId) {
96              throw new CmisInvalidArgumentException("Type id cannot be null.");
97          }
98  
99          // check name syntax
100         if (!NameValidator.isValidId(typeId)) {
101             throw new CmisInvalidArgumentException(NameValidator.ERROR_ILLEGAL_ID);
102         }
103 
104         if (null != tm.getTypeById(typeId)) {
105             throw new CmisInvalidArgumentException("You cannot add type with id " + typeId
106                     + " because it already exists.");           
107         }
108     }
109     
110     private static void checkTypeQueryName(TypeManager tm, String queryName) {
111 
112         if (null == queryName) {
113             throw new CmisInvalidArgumentException("Query name cannot be null.");
114         }
115 
116         if (!NameValidator.isValidQueryName(queryName)) {
117             throw new CmisInvalidArgumentException(NameValidator.ERROR_ILLEGAL_NAME);
118         }
119         
120         // check set query name is unique in the type system
121         if (null != tm.getTypeByQueryName(queryName)) {
122             throw new CmisInvalidArgumentException("You cannot add type with query name " + queryName
123                     + " because it already exists.");           
124         }
125     }
126     
127     private static void checkTypeLocalName(TypeManager tm, String localName) {
128         
129         if (null == localName) {
130             throw new CmisInvalidArgumentException("Local name cannot be null.");
131         }
132 
133         if (!NameValidator.isValidLocalName(localName)) {
134             throw new CmisInvalidArgumentException(NameValidator.ERROR_ILLEGAL_NAME);
135         }
136         
137         for (TypeDefinitionContainer tdc : tm.getTypeDefinitionList()) {
138             if (tdc.getTypeDefinition().getLocalName().equals(localName))
139                 throw new CmisConstraintException("You cannot add type with local name " + localName
140                         + " because it already exists.");                       
141         }
142     }
143 
144 }