View Javadoc
1   package org.apache.fulcrum.intake.model;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.Serializable;
23  import java.lang.reflect.Constructor;
24  
25  import javax.xml.bind.annotation.XmlEnum;
26  import javax.xml.bind.annotation.XmlEnumValue;
27  
28  import org.apache.fulcrum.intake.IntakeException;
29  
30  /**
31   * Enum for valid field types.
32   *
33   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
34   */
35  @XmlEnum(String.class)
36  public enum FieldType implements Serializable
37  {
38      @XmlEnumValue("boolean") FIELD_BOOLEAN("boolean")
39      {
40          @Override
41          public Field<?> getInstance(XmlField f, Group g) throws IntakeException
42          {
43              return new BooleanField(f, g);
44          }
45      },
46      @XmlEnumValue("BigDecimal") FIELD_BIGDECIMAL("BigDecimal")
47      {
48          @Override
49          public Field<?> getInstance(XmlField f, Group g) throws IntakeException
50          {
51              return new BigDecimalField(f, g);
52          }
53      },
54      @XmlEnumValue("int") FIELD_INT("int")
55      {
56          @Override
57          public Field<?> getInstance(XmlField f, Group g) throws IntakeException
58          {
59              return new IntegerField(f, g);
60          }
61      },
62      @XmlEnumValue("float") FIELD_FLOAT("float")
63      {
64          @Override
65          public Field<?> getInstance(XmlField f, Group g) throws IntakeException
66          {
67              return new FloatField(f, g);
68          }
69      },
70      @Deprecated
71      @XmlEnumValue("FileItem") FIELD_FILEITEM("FileItem")
72      {
73          @Override
74          public Field<?> getInstance(XmlField f, Group g) throws IntakeException
75          {
76              return new UploadPartField(f, g);
77          }
78      },
79      @XmlEnumValue("UploadPart") FIELD_UPLOADPART("UploadPart")
80      {
81          @Override
82          public Field<?> getInstance(XmlField f, Group g) throws IntakeException
83          {
84              return new UploadPartField(f, g);
85          }
86      },
87      @XmlEnumValue("String") FIELD_STRING("String")
88      {
89          @Override
90          public Field<?> getInstance(XmlField f, Group g) throws IntakeException
91          {
92              return new StringField(f, g);
93          }
94      },
95      @XmlEnumValue("DateString") FIELD_DATESTRING("DateString")
96      {
97          @Override
98          public Field<?> getInstance(XmlField f, Group g) throws IntakeException
99          {
100             return new DateStringField(f, g);
101         }
102     },
103     @XmlEnumValue("ComboKey") FIELD_COMBOKEY("ComboKey")
104     {
105         @Override
106         public Field<?> getInstance(XmlField f, Group g) throws IntakeException
107         {
108             throw new IntakeException("An unsupported type has been specified for " +
109                     f.getName() + " in group " + g.getIntakeGroupName() + " type = " + value());
110         }
111     },
112     @XmlEnumValue("double") FIELD_DOUBLE("double")
113     {
114         @Override
115         public Field<?> getInstance(XmlField f, Group g) throws IntakeException
116         {
117             return new DoubleField(f, g);
118         }
119     },
120     @XmlEnumValue("short") FIELD_SHORT("short")
121     {
122         @Override
123         public Field<?> getInstance(XmlField f, Group g) throws IntakeException
124         {
125             return new ShortField(f, g);
126         }
127     },
128     @XmlEnumValue("long") FIELD_LONG("long")
129     {
130         @Override
131         public Field<?> getInstance(XmlField f, Group g) throws IntakeException
132         {
133             return new LongField(f, g);
134         }
135     },
136     @XmlEnumValue("custom") FIELD_CUSTOM("custom")
137     {
138         @Override
139         public Field<?> getInstance(XmlField f, Group g) throws IntakeException
140         {
141             String fieldClass = f.getFieldClass();
142 
143             if (fieldClass != null
144                     && fieldClass.indexOf('.') == -1)
145             {
146                 fieldClass = Field.defaultFieldPackage + fieldClass;
147             }
148 
149             if (fieldClass != null)
150             {
151                 Class<?> field;
152 
153                 try
154                 {
155                     field = Class.forName(fieldClass);
156                     Constructor<?> constructor = field.getConstructor(XmlField.class, Group.class);
157 
158                     return (Field<?>) constructor.newInstance(f, g);
159                 }
160                 catch (ClassNotFoundException e)
161                 {
162                     throw new IntakeException(
163                             "Could not load Field class("
164                                     + fieldClass + ")",
165                             e);
166                 }
167                 catch (Exception e)
168                 {
169                     throw new IntakeException(
170                             "Could not create new instance of Field("
171                                     + fieldClass + ")",
172                             e);
173                 }
174             }
175             else
176             {
177                 throw new IntakeException(
178                         "Custom field types must define a fieldClass");
179             }
180         }
181     };
182 
183     /** Serial version */
184     private static final long serialVersionUID = -8563326491799622016L;
185 
186     /** String value of the field type */
187     private String stringValue;
188 
189     /**
190      * Constructor
191      *
192      * @param stringValue
193      */
194     FieldType(String stringValue)
195     {
196         this.stringValue = stringValue;
197     }
198 
199     /**
200      * Return the string value
201      *
202      * @return a <code>String</code> value
203      */
204     public String value()
205     {
206         return stringValue;
207     }
208 
209     /**
210      * Create a specific field instance from its XML representation
211      *
212      * @param f the XML object
213      * @param g the group this field belongs to
214      * @return a Field&lt;?&gt; instance
215      *
216      * @throws IntakeException if the field could not be created
217      */
218     public abstract Field<?> getInstance(XmlField f, Group g) throws IntakeException;
219 }