View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * Unless otherwise indicated, all code in ModeShape is licensed
10   * to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.jcr;
25  
26  import javax.jcr.PropertyType;
27  import net.jcip.annotations.Immutable;
28  
29  /**
30   * A utility for working with {@link PropertyType JCR property types} and {@link org.modeshape.graph.property.PropertyType
31   * ModeShape property types}.
32   */
33  @Immutable
34  public class PropertyTypeUtil {
35  
36      /**
37       * Compute the JCR {@link PropertyType} for the given ModeShape {@link org.modeshape.graph.property.PropertyType}.
38       * <p>
39       * See ModeShape-293 for complete discussion on why this method works the way it does. The best option appears to be basing
40       * the PropertyType on the first value, since that should be compatible with the PropertyType that was used when the values
41       * were set on the property in the first place.
42       * </p>
43       * 
44       * @param property the ModeShape property for which the {@link PropertyType} is to be determined; never null
45       * @return the JCR property type; always a valid value and never {@link PropertyType#UNDEFINED}.
46       */
47      static final int jcrPropertyTypeFor( org.modeshape.graph.property.Property property ) {
48          Object value = property.getFirstValue();
49          if (value == null) return PropertyType.UNDEFINED;
50  
51          // Get the ModeShape property type for this ...
52          return jcrPropertyTypeFor(org.modeshape.graph.property.PropertyType.discoverType(value));
53      }
54  
55      /**
56       * Compute the ModeShape {@link org.modeshape.graph.property.PropertyType} for the given JCR {@link PropertyType} value.
57       * 
58       * @param jcrPropertyType the ModeShape property for which the {@link PropertyType} is to be determined; never null
59       * @return the JCR property type; always a valid value and never {@link PropertyType#UNDEFINED}.
60       */
61      public static final org.modeshape.graph.property.PropertyType dnaPropertyTypeFor( int jcrPropertyType ) {
62          // Make sure the value is the correct type ...
63          switch (jcrPropertyType) {
64              case PropertyType.STRING:
65                  return org.modeshape.graph.property.PropertyType.STRING;
66              case PropertyType.BINARY:
67                  return org.modeshape.graph.property.PropertyType.BINARY;
68              case PropertyType.BOOLEAN:
69                  return org.modeshape.graph.property.PropertyType.BOOLEAN;
70              case PropertyType.DOUBLE:
71                  return org.modeshape.graph.property.PropertyType.DOUBLE;
72              case PropertyType.LONG:
73                  return org.modeshape.graph.property.PropertyType.LONG;
74              case PropertyType.DATE:
75                  return org.modeshape.graph.property.PropertyType.DATE;
76              case PropertyType.DECIMAL:
77                  return org.modeshape.graph.property.PropertyType.DECIMAL;
78              case PropertyType.PATH:
79                  return org.modeshape.graph.property.PropertyType.PATH;
80              case PropertyType.NAME:
81                  return org.modeshape.graph.property.PropertyType.NAME;
82              case PropertyType.REFERENCE:
83                  return org.modeshape.graph.property.PropertyType.REFERENCE;
84              case PropertyType.WEAKREFERENCE:
85                  return org.modeshape.graph.property.PropertyType.WEAKREFERENCE;
86          }
87          assert false;
88          return org.modeshape.graph.property.PropertyType.STRING;
89      }
90  
91      /**
92       * Compute the ModeShape {@link org.modeshape.graph.property.PropertyType} for the given JCR {@link PropertyType} value.
93       * 
94       * @param dnaPropertyType the ModeShape property type; never null
95       * @return the JCR property type; always a valid value and never {@link PropertyType#UNDEFINED}.
96       */
97      public static final int jcrPropertyTypeFor( org.modeshape.graph.property.PropertyType dnaPropertyType ) {
98          // Make sure the value is the correct type ...
99          switch (dnaPropertyType) {
100             case STRING:
101                 return PropertyType.STRING;
102             case NAME:
103                 return PropertyType.NAME;
104             case LONG:
105                 return PropertyType.LONG;
106             case UUID:
107                 return PropertyType.STRING; // JCR treats UUID properties as strings
108             case URI:
109                 return PropertyType.STRING;
110             case PATH:
111                 return PropertyType.PATH;
112             case BOOLEAN:
113                 return PropertyType.BOOLEAN;
114             case DATE:
115                 return PropertyType.DATE;
116             case DECIMAL:
117                 return PropertyType.DECIMAL;
118             case DOUBLE:
119                 return PropertyType.DOUBLE;
120             case BINARY:
121                 return PropertyType.BINARY;
122             case OBJECT:
123                 return PropertyType.UNDEFINED;
124             case REFERENCE:
125                 return PropertyType.REFERENCE;
126             case WEAKREFERENCE:
127                 return PropertyType.WEAKREFERENCE;
128         }
129         assert false;
130         return PropertyType.UNDEFINED;
131     }
132 
133 }