View Javadoc
1   /*
2    * Copyright 2008 Tobias Hochwallner
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  package org.exolab.castor.builder.info.nature;
18  
19  import java.util.Iterator;
20  import java.util.LinkedList;
21  import java.util.List;
22  
23  import org.castor.core.nature.BaseNature;
24  import org.exolab.castor.builder.info.ClassInfo;
25  import org.exolab.castor.builder.info.FieldInfo;
26  import org.exolab.castor.mapping.AccessMode;
27  
28  /**
29   * A JDO specific view of a {@link ClassInfo}. Implementation on property based
30   * {@link ClassInfo} access.
31   * 
32   * TODO Add key generator support.
33   * 
34   * @author Tobias Hochwallner
35   * @since 1.2.1
36   */
37  public final class JDOClassInfoNature extends BaseNature {
38  
39      /**
40       * Property key of primary key.
41       */
42      private static final String PRIMARY_KEY = "primarykey";
43  
44      /**
45       * Property key of table name.
46       */
47      private static final String TABLE_NAME = "tablename";
48  
49      /**
50       * The {@link AccessMode} of the jdo entity.
51       */
52      private static final String ACCESS_MODE = "accessmode";
53  
54      /**
55       * Property key of 'detachable' flag.
56       */
57      private static final String DETACHABLE = "detachable";
58  
59      /**
60       * @param classInfo
61       *            the classinfo in focus.
62       */
63      public JDOClassInfoNature(final ClassInfo classInfo) {
64          super(classInfo);
65      }
66  
67      /**
68       * Returns the id of the Nature. Implementation returns the fully qualified
69       * class name.
70       * 
71       * @return the id.
72       * @see org.exolab.castor.builder.info.nature.Nature#getId()
73       */
74      public String getId() {
75          return getClass().getName();
76      }
77  
78      /**
79       * Adds a column to the primary key. The order of the key columns is not
80       * guaranteed.
81       * 
82       * @param column
83       *            column name
84       */
85      public void addPrimaryKey(final String column) {
86          List<String> primaryKey = getPropertyAsList(PRIMARY_KEY);
87          primaryKey.add(column);
88      }
89      
90      /**
91       * Returns a List of {@String}s holding the columns of the primary key.
92       * Keep in mind that by contract of
93       * <code>addPrimaryKey(String primaryKey)</code> the order is not
94       * guaranteed.
95       * 
96       * @return the names of the primary key's columns or null if no key added
97       *         before.
98       */
99      public List<String> getPrimaryKeys() {
100         return (List<String>) this.getProperty(PRIMARY_KEY);
101     }
102 
103     /**
104      * Returns the table name.
105      * 
106      * @return the SQL table Name
107      */
108     public String getTableName() {
109         return (String) this.getProperty(TABLE_NAME);
110     }
111 
112     /**
113      * Sets the table name to the given String.
114      * 
115      * @param tableName
116      *            of the SQL table.
117      */
118     public void setTableName(final String tableName) {
119         this.setProperty(TABLE_NAME, tableName);
120     }
121 
122     /**
123      * Sets the {@link org.exolab.castor.mapping.AccessMode} to the given
124      * AccessMode.
125      * 
126      * @param accessMode
127      *            access mode
128      */
129     public void setAccessMode(final AccessMode accessMode) {
130         this.setProperty(ACCESS_MODE, accessMode);
131     }
132 
133     /**
134      * Returns the {@link org.exolab.castor.mapping.AccessMode}.
135      * 
136      * @return access mode of the jdo entity.
137      */
138     public AccessMode getAccessMode() {
139         return (AccessMode) this.getProperty(ACCESS_MODE);
140     }
141     
142     /**
143      * Sets whether the entity is 'detachable'.
144      * 
145      * @param detachable True if entity should be detachable
146      */
147     public void setDetachable(final boolean detachable) {
148         this.setProperty(DETACHABLE, new Boolean(detachable));
149     }
150     
151     /**
152      * Indicates whether the entity in question is 'detachable'.
153      * 
154      * @return True if the entity is 'detachable'.
155      */
156     public boolean getDetachable() {
157         return this.getBooleanPropertyDefaultFalse(DETACHABLE);
158     }
159     
160     /**
161      * Returns a List of {@link JDOFieldInfoNature}s of all {@link FieldInfo}s
162      * if the field has a a {@link JDOFieldInfoNature} or an empty List if no
163      * field has the Nature. Included are attribute, text and element fields.
164      * 
165      * @return List of {@link JDOFieldInfoNature}s.
166      */
167     public List<JDOFieldInfoNature> getFields() {       
168         ClassInfo holder = (ClassInfo) getHolder();
169         // Now merge all fields.
170         List<FieldInfo> mergedFields = new LinkedList<FieldInfo>();
171         mergedFields.addAll(holder.getAttributeFieldsAsCollection());
172         mergedFields.addAll(holder.getElementFieldsAsCollection());
173         FieldInfo textField = holder.getTextField();
174         if (textField != null) {
175             mergedFields.add(textField); 
176         }
177         // Walk through all fields and check for Nature.
178         Iterator<FieldInfo> fieldIterator = mergedFields.iterator();
179         List<JDOFieldInfoNature> naturedFields = new LinkedList<JDOFieldInfoNature>();
180         while (fieldIterator.hasNext()) {
181             FieldInfo field = fieldIterator.next();
182             if (field.hasNature(JDOFieldInfoNature.class.getName())) {
183                 JDOFieldInfoNature nature = new JDOFieldInfoNature(field);
184                 naturedFields.add(nature);
185             }
186         }
187         return naturedFields;
188     }
189 
190 
191 }