1 /*
2 * Copyright 2008 Lukas Lang
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.relation;
18
19 import java.util.List;
20
21 import org.castor.core.nature.BaseNature;
22 import org.exolab.castor.builder.info.FieldInfo;
23
24 /**
25 * A {@link JDOOneToOneNature} defines a one-to-one relation between two
26 * {@link FieldInfo}s.
27 *
28 * @author Lukas Lang, Filip Hianik
29 */
30 public final class JDOOneToOneNature extends BaseNature {
31
32 /**
33 * Property key for foreign key.
34 */
35 private static final String FOREIGN_KEY = "foreignkey";
36 /**
37 * Property key for the property read only.
38 */
39 private static final String READONLY = "readonly";
40 /**
41 * Property key for the property dirty.
42 */
43 private static final String DIRTY = "dirty";
44
45 /**
46 * Constructor taking a {@link FieldInfo}.
47 *
48 * @param field
49 * The field.
50 */
51 public JDOOneToOneNature(final FieldInfo field) {
52 super(field);
53 }
54
55 /**
56 * Returns the Nature Id.
57 *
58 * @see org.castor.core.nature.Nature#getId()
59 * @return The fully qualified {@link Class} name.
60 */
61 public String getId() {
62 return getClass().getName();
63 }
64
65 /**
66 * Returns true if no update on the column can be performed, false
67 * otherwise. Default value is false.
68 *
69 * @return true if readonly, false if not or not set.
70 */
71 public boolean isReadOnly() {
72 return getBooleanPropertyDefaultFalse(READONLY);
73 }
74
75 /**
76 * Sets the column read only.
77 *
78 * @param readOnly
79 * true if read only.
80 */
81 public void setReadOnly(final boolean readOnly) {
82 setProperty(READONLY, new Boolean(readOnly));
83 }
84
85 /**
86 * Returns true if field will NOT be checked against the database for
87 * modification, otherwise false. Default value is false.
88 *
89 * @return true if field is not updated, false if not or not set.
90 */
91 public boolean isDirty() {
92 return getBooleanPropertyDefaultFalse(DIRTY);
93 }
94
95 /**
96 * If set true, field will NOT be checked against the database for
97 * modification, otherwise set false.
98 *
99 * @param dirty
100 * true if field should not be updated.
101 */
102 public void setDirty(final boolean dirty) {
103 setProperty(DIRTY, new Boolean(dirty));
104 }
105
106 /**
107 * Returns a List of {@String}s holding the columns of the foreign key.
108 * Keep in mind that by contract of
109 * <code>addPrimaryKey(String foreignKey)</code> the order is not
110 * guaranteed.
111 *
112 * @return the names of the foreign key's columns or null if no keys added
113 * before.
114 */
115 @SuppressWarnings("unchecked")
116 public List<String> getForeignKeys() {
117 return (List<String>) this.getProperty(FOREIGN_KEY);
118 }
119
120 /**
121 * Adds a column to the foreign key. By contract, the order of the key
122 * columns is not guaranteed and depends on the returned List implementation
123 * the {@link BaseNature} is using.
124 *
125 * @param column
126 * The column name.
127 */
128 @SuppressWarnings("unchecked")
129 public void addForeignKey(final String column) {
130 List<String> foreignKey = getPropertyAsList(FOREIGN_KEY);
131 foreignKey.add(column);
132 }
133
134 }