1 package org.devaki.nextobjects.workspace.models.objects;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import java.io.Serializable;
17 import org.devaki.nextobjects.workspace.models.PhysicalModel;
18 import org.devaki.nextobjects.workspace.models.columns.Column;
19 import org.devaki.nextobjects.workspace.models.graphics.ConstraintView;
20 import org.devaki.nextobjects.workspace.models.graphics.ObjectView;
21 import java.util.ArrayList;
22 import java.util.List;
23 /***
24 * A constraint
25 *
26 * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
27 * @TODO check that primary keys are of size=1
28 */
29 public class Constraint extends BaseLine implements Serializable
30 {
31 /*** The foreign columns fields */
32 private List references = new ArrayList(3);
33 /*** The local columns */
34 private List localColumns = new ArrayList(3);
35 /*** The constraintView */
36 private ConstraintView cstView;
37 /*** Self referenced */
38 private boolean selfRef = false;
39 /*** on delete */
40 private String onDelete = "none";
41 /*** on update */
42 private String onUpdate = "none";
43
44
45 /***
46 * Construct a new 'Constraint' object
47 *
48 * @param pModel the context model
49 * @param pPrtClass The parent class, the one who owns the constraint (*)
50 * @param pLocalKeys the primary keys
51 * @param pChdClass The child class, the one who is master (1)
52 * @param pDistantKeys the foreign keys
53 */
54 public Constraint(
55 final PhysicalModel pModel,
56 final Table pPrtClass,
57 final List pLocalKeys,
58 final Table pChdClass,
59 final List pDistantKeys)
60 {
61 super(pModel);
62 if (pPrtClass.getCode().equals(pChdClass.getCode()))
63 {
64 selfRef = true;
65 }
66 this.setParentClass(pPrtClass);
67 ((Table) this.getParentClass()).getConstraints().addElement(this);
68 this.localColumns = pLocalKeys;
69 this.setChildClass(pChdClass);
70 this.references = pDistantKeys;
71 for (int i=0;i<pLocalKeys.size();i++) {
72 ((Column)pLocalKeys.get(i)).setForeignKey(true);
73 }
74 this.setName(
75 new StringBuffer()
76 .append(this.getParentClass().getCode())
77 .append(" _FK_")
78 .append(pModel.countConstraint())
79 .toString());
80 this.cstView = new ConstraintView(this);
81 }
82
83
84 /***
85 * Clone a 'Constraint' object
86 *
87 * @param pCst the constraint to clone
88 */
89 public Constraint(final Constraint pCst)
90 {
91 super(pCst.getMyModel());
92 if (pCst.getParentClass()
93 .getCode()
94 .equals(pCst.getChildClass().getCode()))
95 {
96 selfRef = true;
97 }
98 this.setParentClass(pCst.getParentClass());
99 this.setChildClass(pCst.getChildClass());
100 setName(pCst.getName());
101 this.setCode(pCst.getCode());
102 this.localColumns = pCst.getLocalColumns();
103 for (int i=0;i<localColumns.size();i++) {
104 ((Column)localColumns.get(i)).setForeignKey(true);
105 }
106 this.references = pCst.getReferences();
107 this.cstView = new ConstraintView(this);
108 }
109
110
111 /***
112 * get the ConstraintView
113 *
114 * @return constraintView
115 */
116 public final ObjectView getObjectView()
117 {
118 return cstView;
119 }
120
121
122 /***
123 * Get all the foreigns key for this constraint
124 *
125 * @return the foreign keys
126 */
127 public final List getReferences()
128 {
129 return references;
130 }
131
132
133 /***
134 * Get the local columns
135 *
136 * @return the local column
137 */
138 public final List getLocalColumns()
139 {
140 return localColumns;
141 }
142
143
144 /***
145 * Add a reference ti the constraint
146 *
147 * @param localColumn local column
148 * @param foreignColumn fioreign column
149 */
150 public final void addReference(
151 final Column localColumn,
152 final Column foreignColumn)
153 {
154 localColumns.add(localColumn);
155 references.add(foreignColumn);
156 localColumn.setForeignKey(true);
157 }
158
159
160 /***
161 * Say if the constraint is self reference a table
162 *
163 * @return is self referenced
164 */
165 public final boolean isSelfRef()
166 {
167 return selfRef;
168 }
169
170
171 /***
172 * Get the onUpdate
173 *
174 * @return onUpdate
175 */
176 public final String getOnUpdate()
177 {
178 return onUpdate;
179 }
180
181
182 /***
183 * Get the onDelete
184 *
185 * @return onDelete
186 */
187 public final String getOnDelete()
188 {
189 return onDelete;
190 }
191
192
193 /***
194 * Set the onUpdate
195 *
196 * @param pUpdate onUpdate
197 */
198 public final void setOnUpdate(String pUpdate)
199 {
200 onUpdate = pUpdate;
201 }
202
203
204 /***
205 * Set the onDelete
206 *
207 * @param pDelete The new onDelete value
208 */
209 public final void setOnDelete(String pDelete)
210 {
211 onDelete = pDelete;
212 }
213 }
214