View Javadoc

1   package org.devaki.nextobjects.workspace.models;
2   /*
3   
4   nextobjects Copyright (C) 2001-2005 Emmanuel Florent
5   
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by the
8   Free Software Foundation; either version 2 of the License, or (at your
9   option) any later version.
10  
11  This program is distributed in the hope that it will
12  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the GNU General Public License for more details.
15  
16  You should have received a copy of the GNU General Public License along
17  with this program; if not, write to the Free Software Foundation, Inc., 59
18  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  
20  */
21  import java.io.Serializable;
22  import java.util.Vector;
23  import org.devaki.nextobjects.workspace.models.graphics.PhysicalView;
24  import org.devaki.nextobjects.workspace.models.graphics.BaseModelView;
25  import org.devaki.nextobjects.workspace.models.objects.BaseObject;
26  import org.devaki.nextobjects.workspace.models.objects.Constraint;
27  import org.devaki.nextobjects.workspace.models.objects.Table;
28  /***
29   * Physical model represent the database as it is
30   *
31   *@see  http://www.devaki.org/pdm.html
32   * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
33   */
34  public class PhysicalModel extends BaseModel implements Serializable
35  {
36      /***
37       * tables
38       */
39      private Vector tables = new Vector();
40      /***
41       * constraints
42       */
43      private Vector constraints = new Vector();
44      /***
45       *  the model view
46       */
47      private PhysicalView myPhysicalView;
48      /***
49       * If a SQL file has been generated for this model, this variable indicates
50      * where to find it
51      */
52      private String sqlFile;
53      /***
54       * Construct a new 'PhysicalModel' object
55       * @param pName Nae of the model
56       */
57      public PhysicalModel(final String pName)
58      {
59          super(pName);
60          myPhysicalView = new PhysicalView(this);
61      }
62      /***
63       * Return the best possible filename for save
64       * @return filename
65       */
66      public final String getBestFilename()
67      {
68          if (this.getFileForSave() == null)
69          {
70              return (new StringBuffer().append(this.getId()).append(".pdm"))
71                  .toString();
72          }
73          else
74          {
75              return this.getFileForSave();
76          }
77      }
78      /***
79       * Get the model view
80       * @return model view
81       */
82      public final BaseModelView getModelView()
83      {
84          return this.myPhysicalView;
85      }
86      /***
87       * Get the list of tables in the model
88       * @return tables
89       */
90      public final Vector getTables()
91      {
92          return this.tables;
93      }
94      /***
95       * Get the list of constraints in the model
96       * @return constraints
97       */
98      public final Vector getConstraints()
99      {
100         return this.constraints;
101     }
102     /***
103      * Get constraint(s) for the given table
104      * @param t the context table
105      * @return vctConstraints
106      */
107     public final Vector getConstraints(final Table t)
108     {
109         Vector cst = new Vector();
110         for (int i = 0; i < this.countConstraint(); i++)
111         {
112             if (this.getConstraintAt(i).getParentClass().equals(t))
113             {
114                 cst.addElement(this.getConstraintAt(i));
115             }
116         }
117         return cst;
118     }
119     /***
120      * Get table for the given tableCode
121      * @param pString the table code
122      * @return table
123      */
124     public final Table getTableForId(final String pString)
125     {
126         Table tmp = null;
127         for (int i = 0; i < getTables().size(); i++)
128         {
129             if (this.getTableAt(i).getCode().equals(pString))
130             {
131                 tmp = (Table) this.tables.elementAt(i);
132             }
133         }
134         return tmp;
135     }
136     /***
137      * Return the table identified by the parameter in the list of tables.
138      * @param at index
139      * @return table
140      */
141     public final Table getTableAt(final int at)
142     {
143         return (Table) this.tables.elementAt(at);
144     }
145     /***
146      * Return the constraint identified by the parameter in the list
147      * of constraints.
148      * @param at index
149      * @return constraint
150      */
151     public final Constraint getConstraintAt(final int at)
152     {
153         return (Constraint) constraints.elementAt(at);
154     }
155     /***
156      * Return the number of tables in the model
157      * @return count
158      */
159     public final int countTable()
160     {
161         return this.tables.size();
162     }
163     /***
164      * Return the number of constraint in the model
165      * @return count
166      */
167     public final int countConstraint()
168     {
169         return this.constraints.size();
170     }
171     /***
172      * Get the path for the SQL file generated from this model
173      * @return filename
174      */
175     public final String getSqlFile()
176     {
177         return this.sqlFile;
178     }
179     /***
180      * Set the path for the SQL file generated from this model
181      * @param pSqlFile filename
182      */
183     public final void setSqlFile(final String pSqlFile)
184     {
185         this.sqlFile = pSqlFile;
186     }
187     /***
188      * Set the model view
189      * @param pView the model view
190      */
191     public final void setPhysicalView(final PhysicalView pView)
192     {
193         this.myPhysicalView = pView;
194     }
195     /***
196     * Conveniency method wich return all the objects in the models
197     * @return array of base objects
198     */
199     public final BaseObject[] getModelObjects()
200     {
201         BaseObject[] baseObjects =
202             new BaseObject[tables.size()
203                 + getInheritanceLinks().size()
204                 + constraints.size()];
205         int offset = 0;
206         for (int i = 0; i < tables.size(); i++)
207         {
208             baseObjects[offset] = getTableAt(i);
209             offset++;
210         }
211         for (int i = 0; i < getInheritanceLinks().size(); i++)
212         {
213             baseObjects[offset] = getInheritanceLinkAt(i);
214             offset++;
215         }
216         for (int i = 0; i < constraints.size(); i++)
217         {
218             baseObjects[offset] = getConstraintAt(i);
219             offset++;
220         }
221         return baseObjects;
222     }
223 }