View Javadoc

1   /*
2   
3   nextobjects Copyright (C) 2001-2005 Emmanuel Florent
4   
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by the
7   Free Software Foundation; either version 2 of the License, or (at your
8   option) any later version.
9   
10  This program is distributed in the hope that it will
11  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  PURPOSE. See the GNU General Public License for more details.
14  
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc., 59
17  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  
19  */
20  package org.devaki.nextobjects.workspace.models.columns;
21  import java.io.Serializable;
22  import java.util.Vector;
23  import java.util.Iterator;
24  import java.awt.datatransfer.Clipboard;
25  import java.awt.datatransfer.DataFlavor;
26  import java.awt.datatransfer.Transferable;
27  import java.awt.datatransfer.UnsupportedFlavorException;
28  import org.devaki.nextobjects.ui.components.CustomTreeNode;
29  import org.devaki.nextobjects.workspace.models.objects.BaseClass;
30  /***
31   *  This class is responsible for describing a Column object.
32   * @author     <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
33   */
34  public class Column implements Serializable, Transferable
35  {
36      /***
37       * The human readeable name
38       */
39      private String name = "";
40      /***
41       * The code aka id
42       */
43      private String code = "";
44      /***
45       * The description
46       */
47      private String description = "";
48      /***
49       * The column type
50       */
51      private ColumnType type;
52      /***
53       * The column type optional size
54       */
55      private String size = "";
56      /***
57       * Is a primary key
58       */
59      private boolean primaryKey = false;
60      /***
61       * Is a foreign key
62       */
63      private boolean foreignKey = false;
64      /***
65       * Is required
66       */
67      private boolean required = false;
68      /***
69       * has auto incrementation
70       */
71      private boolean autoIncrement = false;
72      /***
73       * The default value
74       */
75      private String defaultValue = "";
76      /***
77       * The input validator
78       */
79      private String inputValidator = "";
80      /***
81       * The java name
82       */
83      private String javaName = "";
84      /***
85       * The java type kind
86       */
87      private String javaType = "primitive";
88      /***
89       * inheritance
90       */
91      private String inheritance = "false";
92      /***
93       * Inheritance links
94       */
95      private Vector inheritanceLinks = new Vector(1);
96      /***
97       * The java naming method
98       */
99      private String javaNamingMethod = "underscore";
100     /*** The class who own the column. */
101     private BaseClass parent;
102     /*** Node associated */
103     private transient CustomTreeNode customTreeNode;
104     /*** Clipboard */
105     private static DataFlavor fieldFlavor =
106         new DataFlavor(Column.class, "Column");
107     /*** clipboard supported flavor */
108     private DataFlavor[] supportedFlavors = { fieldFlavor };
109     /***
110      * Construct a new column object
111      * @param pName the human readable name
112      * @param pCode the code aka id
113      * @param pType the type
114      * @param pSize the optional length
115      * @param pDefaultValue the default value
116      * @param pPrimaryKey is PK
117      * @param pRequired is Req.
118      * @param pAutoIncrement is autoincrmeent
119      * @param pUnique is visible
120      * @param pIndex is index
121      * @param pJavaName java name
122      * @param pJavaType java type
123      * @param pInheritance the inheritance
124      * @param pInputValidator the input validator
125      * @param pJavaNamingMethod java naming method
126      * @param pDescription the description
127      * @param pParent the owner class
128      */
129     public Column(
130         final String pName,
131         final String pCode,
132         final ColumnType pType,
133         final String pSize,
134         final String pDefaultValue,
135         final boolean pPrimaryKey,
136         final boolean pRequired,
137         final boolean pAutoIncrement,
138         final boolean pUnique,
139         final boolean pIndex,
140         final String pJavaName,
141         final String pJavaType,
142         final String pInheritance,
143         final String pInputValidator,
144         final String pJavaNamingMethod,
145         final String pDescription,
146         final BaseClass pParent)
147     {
148         //order does matter.
149         this.setParent(pParent);
150         this.setName(pName);
151         this.setCode(pCode);
152         this.setType(pType);
153         this.setSize(pSize);
154         this.setDefaultValue(pDefaultValue);
155         this.setPrimaryKey(pPrimaryKey);
156         this.setRequired(pRequired);
157         this.setAutoIncrement(pAutoIncrement);
158         this.setJavaName(pJavaName);
159         this.setJavaType(pJavaType);
160         this.setInheritance(pInheritance);
161         this.setInputValidator(pInputValidator);
162         this.setJavaNamingMethod(pJavaNamingMethod);
163         this.setDescription(pDescription);
164         this.setUnique(pUnique);
165         this.setIndex(pIndex);
166     }
167     /***
168      * Construct a new column object by copying another column object
169      * @param pcolumn the column
170      */
171     public Column(final Column pcolumn)
172     {
173         //order does matter.
174         this.setParent(pcolumn.getParent());
175         this.setName(pcolumn.getName());
176         this.setCode(pcolumn.getCode());
177         this.setType(pcolumn.getType());
178         this.setSize(pcolumn.getSize());
179         this.setDefaultValue(pcolumn.getDefaultValue());
180         this.setPrimaryKey(pcolumn.isPk());
181         this.setRequired(pcolumn.isRequired());
182         this.setAutoIncrement(pcolumn.isAutoIncrement());
183         this.setJavaName(pcolumn.getJavaName());
184         this.setJavaType(pcolumn.getJavaType());
185         this.setInheritance(pcolumn.getInheritance());
186         this.setInputValidator(pcolumn.getInputValidator());
187         this.setJavaNamingMethod(pcolumn.getJavaNamingMethod());
188         this.setDescription(pcolumn.getDescription());
189     }
190     
191     /***
192      * Returns a string representation of the field
193      * @return the human readable name
194      */
195     public final String toString()
196     {
197         StringBuffer buff= new StringBuffer()
198         .append(this.name)
199         .append(" : ")
200         .append(this.getType()) ;
201         if (primaryKey) {
202             buff.append("<pk>");
203         }        
204         if (foreignKey) {
205             buff.append("<fk>");
206         }        
207         return buff.toString();
208     }
209     /***
210      * Set description
211      * @param pDescription the description
212      */
213     public final void setDescription(final String pDescription)
214     {
215         this.description = pDescription;
216     }
217     /***
218      * Set java naming method
219      * @param pJavaNamingMethod the java naming method
220      */
221     public final void setJavaNamingMethod(final String pJavaNamingMethod)
222     {
223         if (pJavaNamingMethod.equals("nochange")
224             || pJavaNamingMethod.equals("underscore")
225             || pJavaNamingMethod.equals("javaname"))
226         {
227             this.javaNamingMethod = pJavaNamingMethod;
228         }
229         else
230         {
231             this.javaNamingMethod = "underscore";
232         }
233     }
234     /***
235      * Set input validator
236      * @param pInputValidator the input validator
237      */
238     public final void setInputValidator(final String pInputValidator)
239     {
240         this.inputValidator = pInputValidator;
241     }
242     /***
243      * Set inheritance
244      * @param pInheritance the inheritance
245      */
246     public final void setInheritance(final String pInheritance)
247     {
248         if (pInheritance.equals("single"))
249         {
250             this.inheritance = pInheritance;
251         }
252         else
253         {
254             this.inheritance = "false";
255         }
256     }
257     /***
258      * Set inheritance links
259      * @param pInheritanceLinks inheritance links
260      */
261     public final void setInheritanceLinks(final Vector pInheritanceLinks)
262     {
263         this.inheritanceLinks = pInheritanceLinks;
264     }
265     /***
266      * Set the preferred Java Type
267      * @param pJavaType the java type
268      */
269     public final void setJavaType(final String pJavaType)
270     {
271         if (pJavaType.equals("object"))
272         {
273             this.javaType = pJavaType;
274         }
275         else
276         {
277             this.javaType = "primitive";
278         }
279     }
280     /***
281      * Set the Java Name
282      * @param pJavaName the java name
283      */
284     public final void setJavaName(final String pJavaName)
285     {
286         this.javaName = pJavaName;
287     }
288     /***
289      * Give a name to the field
290      * @param pName the human readeable name
291      */
292     public final void setName(final String pName)
293     {
294         this.name = pName;
295     }
296     /***
297      * Give a code to the field
298      * @param pCode the code
299      */
300     public final void setCode(final String pCode)
301     {
302         this.code = pCode;
303     }
304     /***
305      * Give a type to the field
306      * @param pType the data type
307      */
308     public final void setType(final ColumnType pType)
309     {
310         this.type = pType;
311     }
312     /***
313      * Give a length to the field
314      * @param pSize the optional column length
315      */
316     public final void setSize(final String pSize)
317     {
318         this.size = pSize;
319     }
320     /***
321      * Give a default value to the field
322      * @param pDefaultValue the default value
323      */
324     public final void setDefaultValue(final String pDefaultValue)
325     {
326         this.defaultValue = pDefaultValue;
327     }
328     /***
329      * Define if the field is primary key
330      * @param pPrimaryKey is PK
331      */
332     public final void setPrimaryKey(final boolean pPrimaryKey)
333     {
334         if (pPrimaryKey)
335         {
336             this.required = true;
337         }
338         this.primaryKey = pPrimaryKey;
339     }
340     /***
341      * Define if the field is required
342      * @param pRequired is Req.
343      */
344     public final void setRequired(final boolean pRequired)
345     {
346         this.required = pRequired;
347     }
348     /***
349      * Define if the field has auto incrementation
350      * @param pAutoIncrement has auto incrementation
351      */
352     public final void setAutoIncrement(final boolean pAutoIncrement)
353     {
354         this.autoIncrement = pAutoIncrement;
355     }
356     /***
357      * Set the torque-gui related to the current field
358      * @param pParent the parent class
359      */
360     public final void setParent(final BaseClass pParent)
361     {
362         this.parent = pParent;
363     }
364     /***
365      * Defines the CustomTreeNode associated to the current object
366      * @param pNode the node
367      */
368     public final void setDynamicTreeNode(final CustomTreeNode pNode)
369     {
370         this.customTreeNode = pNode;
371     }
372     /***
373      * Get description
374      * @return the description
375      */
376     public final String getDescription()
377     {
378         return this.description;
379     }
380     /***
381      * Get the Java Naming Method
382      * @return the java naming method
383      */
384     public final String getJavaNamingMethod()
385     {
386         return this.javaNamingMethod;
387     }
388     /***
389      * Get input validator
390      * @return the input validator
391      */
392     public final String getInputValidator()
393     {
394         return this.inputValidator;
395     }
396     /***
397      * Get inheritance
398      * @return the inheritance
399      */
400     public final String getInheritance()
401     {
402         return this.inheritance;
403     }
404     /***
405      * Get inheritance
406      * @return the inheritance links
407      */
408     public final Vector getInheritanceLinks()
409     {
410         return this.inheritanceLinks;
411     }
412     /***
413      * Get the preferred Java Type
414      * @return the java type
415      */
416     public final String getJavaType()
417     {
418         return this.javaType;
419     }
420     /***
421      * Get the Java Name
422      * @return the java name
423      */
424     public final String getJavaName()
425     {
426         return this.javaName;
427     }
428     /***
429      * Return the name of the field
430      * @return the human readable name
431      */
432     public final String getName()
433     {
434         return this.name;
435     }
436     /***
437      * Return the code of the field
438      * @return the code, id
439      */
440     public final String getCode()
441     {
442         return this.code;
443     }
444     /***
445      * Return the type of the field
446      * @return the column type
447      */
448     public final ColumnType getType()
449     {
450         return this.type;
451     }
452     /***
453      * Return the length of the field
454      * @return the size for that type
455      */
456     public final String getSize()
457     {
458         return this.size;
459     }
460     /***
461      * Return the default value of the field
462      * @return the default value
463      */
464     public final String getDefaultValue()
465     {
466         return this.defaultValue;
467     }
468     /***
469      * Return if the field is primary key or not
470      * @return i sPK
471      */
472     public final boolean isPk()
473     {
474         return this.primaryKey;
475     }
476     /***
477      * Return if the field is required or not
478      * @return isReq.
479      */
480     public final boolean isRequired()
481     {
482         return this.required;
483     }
484     /***
485      * Return if the field has auto incrementation or not
486      * @return has autoincrementation
487      */
488     public final boolean isAutoIncrement()
489     {
490         return this.autoIncrement;
491     }
492     /***
493      * Return the torque-gui related to the current field
494      * @return the owner class
495      */
496     public final BaseClass getParent()
497     {
498         return (BaseClass) this.parent;
499     }
500     /***
501      * Get the CustomTreeNode object associated
502      * @return the node
503      */
504     public final CustomTreeNode getDynamicTreeNode()
505     {
506         return this.customTreeNode;
507     }
508     /***
509      * Return the flavors
510      * @return the data flavor
511      */
512     public final synchronized DataFlavor[] getTransferDataFlavors()
513     {
514         return this.supportedFlavors;
515     }
516     /***
517      * Is the data supported by this class
518      * @param parFlavor the data flavor
519      * @return accepted
520      */
521     public final boolean isDataFlavorSupported(final DataFlavor parFlavor)
522     {
523         return (parFlavor.equals(fieldFlavor));
524     }
525     /***
526      * Return the object to be used by the clipboard
527      * @param parFlavor the data flavor
528      * @return the transfer data
529      * @throws UnsupportedFlavorException clipboard exception
530      */
531     public final synchronized Object getTransferData(final DataFlavor parFlavor)
532         throws UnsupportedFlavorException
533     {
534         if (parFlavor.equals(fieldFlavor))
535         {
536             return (this);
537         }
538         else
539         {
540             throw new UnsupportedFlavorException(fieldFlavor);
541         }
542     }
543     /***
544      * Need to be defined
545      * @param parClipboard parent clipboard
546      * @param parTransferable the transferable object
547      */
548     public final void lostOwnership(
549         final Clipboard parClipboard,
550         final Transferable parTransferable)
551     {
552     }
553     /***
554      * Check if it is a FK
555      * @return is FK
556      */
557     public final boolean isForeignKey()
558     {
559         return foreignKey;
560     }
561     /***
562      * Set if is a FK
563      * @param b isFK
564      */
565     public final void setForeignKey(final boolean b)
566     {
567         foreignKey = b;
568     }
569     /***
570      * Set if there is an index to be created for this column
571      * @param b is index
572      */
573     public final void setIndex(final boolean b)
574     {
575         if (b)
576         {
577             if (!this.getParent().getIndexes().contains(this))
578             {
579                 this.getParent().getIndexes().addElement(this);
580             }
581         }
582         else
583         {
584             Iterator itIndex =
585                 new Vector(this.getParent().getIndexes()).iterator();
586             while (itIndex.hasNext())
587             {
588                 Column col = (Column) itIndex.next();
589                 if (col.getCode().equals(this.getCode()))
590                 {
591                     this.getParent().getIndexes().remove(col);
592                 }
593             }
594             itIndex = null;
595         }
596     }
597     /***
598      * Set if this column is unique in the class.
599      * If a unique constraint is added.
600      * @param b isUnique
601      */
602     public final void setUnique(final boolean b)
603     {
604         if (b)
605         {
606             if (!this.getParent().getUniques().contains(this))
607             {
608                 this.getParent().getUniques().addElement(this);
609             }
610         }
611         else
612         {
613             Iterator itUnique =
614                 new Vector(this.getParent().getUniques()).iterator();
615             while (itUnique.hasNext())
616             {
617                 Column col = (Column) itUnique.next();
618                 if (col.getCode().equals(this.getCode()))
619                 {
620                     this.getParent().getUniques().remove(col);
621                 }
622             }
623             itUnique = null;
624         }
625     }
626     /***
627      * Do the column have a unique constraint
628      * @return is Unique
629      */
630     public final boolean isUnique()
631     {
632         Iterator itUniques = this.getParent().getUniques().iterator();
633         boolean b = false;
634         while (itUniques.hasNext())
635         {
636             Column cl = ((Column) itUniques.next());
637             if (cl.getCode().equals(this.getCode()))
638             {
639                 return true;
640             }
641         }
642         return b;
643     }
644     /***
645      * Is the constraint to be indexed
646      * @return is Indexed
647      */
648     public final boolean isIndex()
649     {
650         Iterator itIndex = this.getParent().getIndexes().iterator();
651         boolean b = false;
652         while (itIndex.hasNext())
653         {
654             Column cl = ((Column) itIndex.next());
655             if (cl.getCode().equals(this.getCode()))
656             {
657                 return true;
658             }
659         }
660         return b;
661     }
662 /***
663  * Get the field flavor
664  * @return the field flavor
665  */
666     public static DataFlavor getFieldFlavor()
667     {
668         return fieldFlavor;
669     }
670 } // End of class column