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 ModeShape
31 * property types}.
32 */
33 @Immutable
34 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 the
40 * PropertyType on the first value, since that should be compatible with the PropertyType that was used when the values were
41 * 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 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.PATH:
77 return org.modeshape.graph.property.PropertyType.PATH;
78 case PropertyType.NAME:
79 return org.modeshape.graph.property.PropertyType.NAME;
80 case PropertyType.REFERENCE:
81 return org.modeshape.graph.property.PropertyType.REFERENCE;
82 }
83 assert false;
84 return org.modeshape.graph.property.PropertyType.STRING;
85 }
86
87 /**
88 * Compute the ModeShape {@link org.modeshape.graph.property.PropertyType} for the given JCR {@link PropertyType} value.
89 *
90 * @param dnaPropertyType the ModeShape property type; never null
91 * @return the JCR property type; always a valid value and never {@link PropertyType#UNDEFINED}.
92 */
93 static final int jcrPropertyTypeFor( org.modeshape.graph.property.PropertyType dnaPropertyType ) {
94 // Make sure the value is the correct type ...
95 switch (dnaPropertyType) {
96 case STRING:
97 return PropertyType.STRING;
98 case NAME:
99 return PropertyType.NAME;
100 case LONG:
101 return PropertyType.LONG;
102 case UUID:
103 return PropertyType.STRING; // JCR treats UUID properties as strings
104 case URI:
105 return PropertyType.STRING;
106 case PATH:
107 return PropertyType.PATH;
108 case BOOLEAN:
109 return PropertyType.BOOLEAN;
110 case DATE:
111 return PropertyType.DATE;
112 case DECIMAL:
113 return PropertyType.STRING; // better than losing information
114 case DOUBLE:
115 return PropertyType.DOUBLE;
116 case BINARY:
117 return PropertyType.BINARY;
118 case OBJECT:
119 return PropertyType.UNDEFINED;
120 case REFERENCE:
121 return PropertyType.REFERENCE;
122 }
123 assert false;
124 return PropertyType.UNDEFINED;
125 }
126
127 }