View Javadoc

1   /*
2    * joey-gen and its relative products are published under the terms
3    * of the Apache Software License.
4    * 
5    * Created on 2004/08/10 17:15:16
6    */
7   package org.asyrinx.joey.gen.model.rdb;
8   
9   import java.util.ArrayList;
10  import java.util.Iterator;
11  import java.util.List;
12  
13  import org.apache.commons.lang.builder.EqualsBuilder;
14  import org.asyrinx.brownie.core.lang.StringUtils;
15  import org.asyrinx.joey.gen.model.ElementSet;
16  
17  /***
18   * @author akima
19   */
20  public class ForeignKey extends ElementSet {
21  
22      /***
23       *  
24       */
25      public ForeignKey() {
26          this(null, null, null);
27      }
28  
29      /***
30       * @param parent
31       * @param name
32       */
33      public ForeignKey(String foreignTable) {
34          this(null, null, foreignTable);
35      }
36  
37      /***
38       * @param parent
39       * @param name
40       */
41      public ForeignKey(String name, String foreignTable) {
42          this(null, name, foreignTable);
43      }
44  
45      /***
46       * @param parent
47       * @param name
48       */
49      public ForeignKey(Table parent, String foreignTable) {
50          this(parent, null, foreignTable);
51      }
52  
53      /***
54       * @param parent
55       * @param name
56       */
57      public ForeignKey(Table parent, String name, String foreignTable) {
58          super(parent, name);
59          this.foreign = foreignTable;
60      }
61  
62      /*
63       * (non-Javadoc)
64       * 
65       * @see org.asyrinx.joey.gen.model.ElementSet#isEntity()
66       */
67      public boolean isEntity() {
68          return true;
69      }
70  
71      /*
72       * (non-Javadoc)
73       * 
74       * @see org.asyrinx.joey.gen.model.ElementSet#add(org.asyrinx.joey.gen.model.Element)
75       */
76      public void add(ForeignKeyEntry entry) {
77          super.add(entry);
78      }
79  
80      /*
81       * (non-Javadoc)
82       * 
83       * @see org.asyrinx.joey.gen.model.ElementSet#contains(org.asyrinx.joey.gen.model.Element)
84       */
85      public boolean contains(ForeignKeyEntry entry) {
86          return super.contains(entry);
87      }
88  
89      /*
90       * (non-Javadoc)
91       * 
92       * @see org.asyrinx.joey.gen.model.ElementSet#getElement(int)
93       */
94      public ForeignKeyEntry getEntry(int idx) {
95          return (ForeignKeyEntry) super.getElement(idx);
96      }
97  
98      /*
99       * (non-Javadoc)
100      * 
101      * @see org.asyrinx.joey.gen.model.ElementSet#get(java.lang.String)
102      */
103     public ForeignKeyEntry getEntry(String name) {
104         return (ForeignKeyEntry) super.getElement(name);
105     }
106 
107     /*
108      * (non-Javadoc)
109      * 
110      * @see org.asyrinx.joey.gen.model.ElementSet#remove(java.lang.String)
111      */
112     public ForeignKeyEntry removeEntry(String name) {
113         return (ForeignKeyEntry) super.removeElement(name);
114     }
115 
116     /*
117      * (non-Javadoc)
118      * 
119      * @see org.asyrinx.joey.gen.model.Element#getParentElement()
120      */
121     public Table getParent() {
122         return (Table) super.getParentElement();
123     }
124 
125     public Table getLocal() {
126         return getParent();
127     }
128 
129     protected final Table getTable(String name) {
130         final Table parent = getParent();
131         if (parent == null)
132             return null;
133         final Database database = parent.getParent();
134         if (database == null)
135             return null;
136         return database.getTables().getTable(name);
137     }
138 
139     public Table getForeignTable() {
140         return getTable(getForeign());
141     }
142 
143     public String getLocalColumnNames() {
144         final List list = new ArrayList();
145         for (final Iterator i = this.iterator(); i.hasNext();) {
146             final ForeignKeyEntry entry = (ForeignKeyEntry) i.next();
147             list.add(entry.getLocalColumn().getName());
148         }
149         return StringUtils.join(list.iterator(), ",");
150     }
151 
152     public String getForeignColumnNames() {
153         final List list = new ArrayList();
154         for (final Iterator i = this.iterator(); i.hasNext();) {
155             final ForeignKeyEntry entry = (ForeignKeyEntry) i.next();
156             list.add(entry.getForeignColumn().getName());
157         }
158         return StringUtils.join(list.iterator(), ",");
159     }
160 
161     private String foreign = null;
162 
163     private boolean indexed = true;
164 
165     private Index index = null;
166 
167     private ForeignKeyType type = ForeignKeyType.NORMAL;
168 
169     /***
170      * @return Returns the foreign.
171      */
172     public String getForeign() {
173         return foreign;
174     }
175 
176     /***
177      * @param foreign
178      *            The foreign to set.
179      */
180     public void setForeign(String foreignTabe) {
181         this.foreign = foreignTabe;
182     }
183 
184     /***
185      * @return Returns the indexed.
186      */
187     public boolean isIndexed() {
188         return indexed;
189     }
190 
191     /***
192      * @param indexed
193      *            The indexed to set.
194      */
195     public void setIndexed(boolean indexed) {
196         this.indexed = indexed;
197     }
198 
199     /***
200      * @return Returns the index.
201      */
202     public Index getIndex() {
203         return index;
204     }
205 
206     /***
207      * @param index
208      *            The index to set.
209      */
210     public void setIndex(Index index) {
211         this.index = index;
212     }
213 
214     /***
215      * @return Returns the type.
216      */
217     public ForeignKeyType getType() {
218         return type;
219     }
220 
221     /***
222      * @param type
223      *            The type to set.
224      */
225     public void setType(ForeignKeyType type) {
226         this.type = type;
227     }
228 
229     /***
230      * @param column
231      * @return
232      */
233     public boolean containsAsLocal(Column column) {
234         for (Iterator i = this.iterator(); i.hasNext();) {
235             final ForeignKeyEntry entry = (ForeignKeyEntry) i.next();
236             if (entry.getLocalColumn() == column)
237                 return true;
238         }
239         return false;
240     }
241 
242     private String cascade = null;
243 
244     public String getCascade() {
245         return cascade;
246     }
247 
248     public void setCascade(String cascade) {
249         this.cascade = cascade;
250     }
251 
252     /*
253      * (non-Javadoc)
254      * 
255      * @see java.lang.Object#equals(java.lang.Object)
256      */
257     public boolean equals(Object obj) {
258         if (!super.equals(obj))
259             return false;
260         if (!(obj instanceof ForeignKey))
261             return false;
262         final ForeignKey other = (ForeignKey) obj;
263         return new EqualsBuilder() //
264                 .append(this.getForeign(), other.getForeign()) //
265                 .append(this.getType(), other.getType()) //
266                 .append(this.getCascade(), other.getCascade()) //
267                 .isEquals();
268     }
269 
270     /*
271      * (non-Javadoc)
272      * 
273      * @see org.asyrinx.joey.gen.model.ElementSet#careChildOrder()
274      */
275     public boolean careChildOrder() {
276         return true;
277     }
278 }