This project has retired. For details please refer to its Attic page.
PropertyHelper 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  
20  package org.apache.chemistry.opencmis.jcr;
21  
22  import org.apache.chemistry.opencmis.commons.PropertyIds;
23  import org.apache.chemistry.opencmis.commons.data.Properties;
24  import org.apache.chemistry.opencmis.commons.data.PropertyData;
25  import org.apache.chemistry.opencmis.commons.data.PropertyId;
26  import org.apache.chemistry.opencmis.commons.data.PropertyString;
27  import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
28  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
29  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
30  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyBooleanImpl;
31  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyDateTimeImpl;
32  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyDecimalImpl;
33  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyHtmlImpl;
34  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyIdImpl;
35  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyIntegerImpl;
36  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyStringImpl;
37  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyUriImpl;
38  
39  import java.math.BigDecimal;
40  import java.math.BigInteger;
41  import java.util.GregorianCalendar;
42  import java.util.List;
43  
44  /**
45   * Helper class with utility functions for handling {@link Properties}.
46   */
47  public final class PropertyHelper {
48      private PropertyHelper() {
49      }
50  
51      /**
52       * Retrieve a string value.
53       *
54       * @param properties
55       * @param name  the name of the value to retrieve
56       * @return  the first value of the given <code>name</code> or <code>null</code> if either
57       *      these are no string properties or no property of <code>name</code> exists. 
58       */
59      public static String getStringProperty(Properties properties, String name) {
60          PropertyData<?> property = properties.getProperties().get(name);
61          if (!(property instanceof PropertyString)) {
62              return null;
63          }
64  
65          return ((PropertyString) property).getFirstValue();
66      }
67  
68      /**
69       * Gets the type id from a set of properties.
70       */
71      public static String getTypeId(Properties properties) {
72          PropertyData<?> typeProperty = properties.getProperties().get(PropertyIds.OBJECT_TYPE_ID);
73          if (!(typeProperty instanceof PropertyId)) {
74              throw new CmisInvalidArgumentException("Type id must be set!");
75          }
76  
77          String typeId = ((PropertyId) typeProperty).getFirstValue();
78          if (typeId == null) {
79              throw new CmisInvalidArgumentException("Type id must be set!");
80          }
81  
82          return typeId;
83      }
84  
85      /**
86       * @param prop
87       * @return  <code>true</code> iff <code>prop</code> denotes an empty property data value
88       */
89      public static boolean isPropertyEmpty(PropertyData<?> prop) {
90          return prop == null || prop.getValues() == null || prop.getValues().isEmpty();
91      }
92  
93      /**
94       * Determine the default property data value for a given property definition.
95       * @param propDef
96       * @return
97       * @throws CmisRuntimeException  if <code>propDef</code> is invalid or unknown.
98       */
99      @SuppressWarnings("unchecked")
100     public static PropertyData<?> getDefaultValue(PropertyDefinition<?> propDef) {
101         if (propDef == null) {
102             return null;
103         }
104 
105         List<?> defaultValue = propDef.getDefaultValue();
106         if (defaultValue != null && !defaultValue.isEmpty()) {
107             switch (propDef.getPropertyType()) {
108                 case BOOLEAN:
109                     return new PropertyBooleanImpl(propDef.getId(), (List<Boolean>) defaultValue);
110                 case DATETIME:
111                     return new PropertyDateTimeImpl(propDef.getId(), (List<GregorianCalendar>) defaultValue);
112                 case DECIMAL:
113                     return new PropertyDecimalImpl(propDef.getId(), (List<BigDecimal>) defaultValue);
114                 case HTML:
115                     return new PropertyHtmlImpl(propDef.getId(), (List<String>) defaultValue);
116                 case ID:
117                     return new PropertyIdImpl(propDef.getId(), (List<String>) defaultValue);
118                 case INTEGER:
119                     return new PropertyIntegerImpl(propDef.getId(), (List<BigInteger>) defaultValue);
120                 case STRING:
121                     return new PropertyStringImpl(propDef.getId(), (List<String>) defaultValue);
122                 case URI:
123                     return new PropertyUriImpl(propDef.getId(), (List<String>) defaultValue);
124                 default:
125                     throw new CmisRuntimeException("Unknown datatype: " + propDef.getPropertyType());
126             }
127         }
128         return null;
129     }
130 
131 }