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