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    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed 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 javax.jcr.Value;
28  import javax.jcr.nodetype.ConstraintViolationException;
29  import org.modeshape.graph.ExecutionContext;
30  import org.modeshape.graph.property.Name;
31  import org.modeshape.graph.property.Path;
32  import org.modeshape.graph.property.ValueFactories;
33  import org.modeshape.jcr.nodetype.PropertyDefinitionTemplate;
34  
35  /**
36   * ModeShape implementation of the JCR 2 PropertyDefinitionTemplate interface.
37   */
38  class JcrPropertyDefinitionTemplate extends JcrItemDefinitionTemplate implements PropertyDefinitionTemplate {
39  
40      private boolean multiple = false;
41      private Value[] defaultValues = null;
42      private int requiredType = PropertyType.STRING;
43      private String[] valueConstraints = null;
44      private boolean fullTextSearchable = true;
45      private boolean queryOrderable = true;
46      private String[] availableQueryOperators;
47  
48      JcrPropertyDefinitionTemplate( ExecutionContext context ) {
49          super(context);
50      }
51  
52      JcrPropertyDefinitionTemplate( JcrPropertyDefinitionTemplate original,
53                                     ExecutionContext context ) {
54          super(original, context);
55          this.multiple = original.multiple;
56          this.requiredType = original.requiredType;
57          this.valueConstraints = original.valueConstraints;
58          this.fullTextSearchable = original.fullTextSearchable;
59          this.queryOrderable = original.queryOrderable;
60          this.availableQueryOperators = original.availableQueryOperators;
61          this.defaultValues = original.defaultValues != null ? new Value[original.defaultValues.length] : null;
62          if (original.defaultValues != null) {
63              for (int i = 0; i != original.defaultValues.length; ++i) {
64                  Value originalValue = original.defaultValues[i];
65                  assert originalValue instanceof JcrValue;
66                  JcrValue jcrValue = ((JcrValue)originalValue);
67                  SessionCache cache = jcrValue.sessionCache();
68                  this.defaultValues[i] = new JcrValue(context.getValueFactories(), cache, jcrValue.getType(), jcrValue.value());
69                  switch (jcrValue.getType()) {
70                      case PropertyType.NAME:
71                          Name nameValue = original.getContext().getValueFactories().getNameFactory().create(jcrValue.value());
72                          JcrItemDefinitionTemplate.registerMissingNamespaces(original.getContext(), context, nameValue);
73                          break;
74                      case PropertyType.PATH:
75                          Path pathValue = original.getContext().getValueFactories().getPathFactory().create(jcrValue.value());
76                          JcrItemDefinitionTemplate.registerMissingNamespaces(original.getContext(), context, pathValue);
77                          break;
78                  }
79              }
80          }
81      }
82  
83      JcrPropertyDefinitionTemplate with( ExecutionContext context ) {
84          return context == super.getContext() ? this : new JcrPropertyDefinitionTemplate(this, context);
85      }
86  
87      /**
88       * {@inheritDoc}
89       * 
90       * @see org.modeshape.jcr.nodetype.PropertyDefinitionTemplate#setDefaultValues(java.lang.String[])
91       */
92      public void setDefaultValues( String[] defaultValues ) {
93          if (defaultValues != null) {
94              this.defaultValues = new Value[defaultValues.length];
95              ValueFactories factories = getExecutionContext().getValueFactories();
96              for (int i = 0; i < defaultValues.length; i++) {
97                  this.defaultValues[i] = new JcrValue(factories, null, PropertyType.STRING, defaultValues[i]);
98              }
99          } else {
100             this.defaultValues = null;
101         }
102     }
103 
104     /**
105      * {@inheritDoc}
106      * 
107      * @see org.modeshape.jcr.nodetype.PropertyDefinitionTemplate#setDefaultValues(Value[])
108      */
109     public void setDefaultValues( Value[] defaultValues ) {
110         this.defaultValues = defaultValues;
111     }
112 
113     /**
114      * {@inheritDoc}
115      * 
116      * @see org.modeshape.jcr.nodetype.PropertyDefinitionTemplate#setMultiple(boolean)
117      */
118     public void setMultiple( boolean multiple ) {
119         this.multiple = multiple;
120     }
121 
122     /**
123      * {@inheritDoc}
124      * 
125      * @see org.modeshape.jcr.nodetype.PropertyDefinitionTemplate#setRequiredType(int)
126      */
127     public void setRequiredType( int requiredType ) {
128         assert requiredType == PropertyType.BINARY || requiredType == PropertyType.BOOLEAN || requiredType == PropertyType.DATE
129                || requiredType == PropertyType.DOUBLE || requiredType == PropertyType.DECIMAL
130                || requiredType == PropertyType.LONG || requiredType == PropertyType.NAME || requiredType == PropertyType.PATH
131                || requiredType == PropertyType.REFERENCE || requiredType == PropertyType.WEAKREFERENCE
132                || requiredType == PropertyType.URI || requiredType == PropertyType.STRING
133                || requiredType == PropertyType.UNDEFINED;
134         this.requiredType = requiredType;
135     }
136 
137     /**
138      * {@inheritDoc}
139      * 
140      * @see org.modeshape.jcr.nodetype.PropertyDefinitionTemplate#setValueConstraints(java.lang.String[])
141      */
142     public void setValueConstraints( String[] constraints ) {
143         this.valueConstraints = constraints;
144     }
145 
146     /**
147      * {@inheritDoc}
148      * 
149      * @see javax.jcr.nodetype.PropertyDefinition#getDefaultValues()
150      */
151     public Value[] getDefaultValues() {
152         return this.defaultValues;
153     }
154 
155     /**
156      * {@inheritDoc}
157      * 
158      * @see javax.jcr.nodetype.PropertyDefinition#getRequiredType()
159      */
160     public int getRequiredType() {
161         return requiredType;
162     }
163 
164     /**
165      * {@inheritDoc}
166      * 
167      * @see javax.jcr.nodetype.PropertyDefinition#getValueConstraints()
168      */
169     public String[] getValueConstraints() {
170         return valueConstraints;
171     }
172 
173     /**
174      * {@inheritDoc}
175      * 
176      * @see javax.jcr.nodetype.PropertyDefinition#isMultiple()
177      */
178     public boolean isMultiple() {
179         return multiple;
180     }
181 
182     public boolean isFullTextSearchable() {
183         return this.fullTextSearchable;
184     }
185 
186     /**
187      * {@inheritDoc}
188      * 
189      * @see PropertyDefinitionTemplate#setFullTextSearchable(boolean)
190      */
191     public void setFullTextSearchable( boolean fullTextSearchable ) {
192         this.fullTextSearchable = fullTextSearchable;
193     }
194 
195     public String[] getAvailableQueryOperators() {
196         return this.availableQueryOperators;
197     }
198 
199     /**
200      * {@inheritDoc}
201      * 
202      * @see PropertyDefinitionTemplate#setAvailableQueryOperators(String[])
203      */
204     public void setAvailableQueryOperators( String[] queryOperators ) {
205         this.availableQueryOperators = queryOperators;
206     }
207 
208     public boolean isQueryOrderable() {
209         return this.queryOrderable;
210     }
211 
212     /**
213      * {@inheritDoc}
214      * 
215      * @see PropertyDefinitionTemplate#setQueryOrderable(boolean)
216      */
217     public void setQueryOrderable( boolean queryOrderable ) {
218         this.queryOrderable = queryOrderable;
219     }
220 
221     /**
222      * {@inheritDoc}
223      * 
224      * @see javax.jcr.nodetype.PropertyDefinitionTemplate#setName(java.lang.String)
225      */
226     @Override
227     public void setName( String name ) throws ConstraintViolationException {
228         super.setName(name);
229     }
230 
231     /**
232      * {@inheritDoc}
233      * 
234      * @see javax.jcr.nodetype.PropertyDefinitionTemplate#setAutoCreated(boolean)
235      */
236     @Override
237     public void setAutoCreated( boolean autoCreated ) {
238         super.setAutoCreated(autoCreated);
239     }
240 
241     /**
242      * {@inheritDoc}
243      * 
244      * @see javax.jcr.nodetype.PropertyDefinitionTemplate#setMandatory(boolean)
245      */
246     @Override
247     public void setMandatory( boolean mandatory ) {
248         super.setMandatory(mandatory);
249     }
250 
251     /**
252      * {@inheritDoc}
253      * 
254      * @see javax.jcr.nodetype.PropertyDefinitionTemplate#setOnParentVersion(int)
255      */
256     @Override
257     public void setOnParentVersion( int onParentVersion ) {
258         super.setOnParentVersion(onParentVersion);
259     }
260 
261     /**
262      * {@inheritDoc}
263      * 
264      * @see javax.jcr.nodetype.PropertyDefinitionTemplate#setProtected(boolean)
265      */
266     @Override
267     public void setProtected( boolean isProtected ) {
268         super.setProtected(isProtected);
269     }
270 }