KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > dialog > db > TableSelector


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.modeler.dialog.db;
57
58 import java.awt.Component JavaDoc;
59 import java.util.ArrayList JavaDoc;
60 import java.util.Collection JavaDoc;
61 import java.util.HashMap JavaDoc;
62 import java.util.Iterator JavaDoc;
63 import java.util.List JavaDoc;
64 import java.util.Map JavaDoc;
65
66 import org.objectstyle.cayenne.map.DataMap;
67 import org.objectstyle.cayenne.map.DbAttribute;
68 import org.objectstyle.cayenne.map.DbEntity;
69 import org.objectstyle.cayenne.map.DbRelationship;
70 import org.objectstyle.cayenne.map.DerivedDbEntity;
71 import org.objectstyle.cayenne.map.Entity;
72 import org.objectstyle.cayenne.modeler.ProjectController;
73 import org.objectstyle.cayenne.modeler.dialog.validator.ValidationDisplayHandler;
74 import org.objectstyle.cayenne.modeler.util.CayenneController;
75 import org.objectstyle.cayenne.project.validator.ValidationInfo;
76 import org.objectstyle.cayenne.project.validator.Validator;
77 import org.objectstyle.cayenne.swing.ObjectBinding;
78 import org.objectstyle.cayenne.swing.TableBindingBuilder;
79
80 /**
81  * @author Andrei Adamchik
82  */

83 public class TableSelector extends CayenneController {
84
85     protected TableSelectorView view;
86     protected ObjectBinding tableBinding;
87
88     protected DbEntity table;
89     protected List JavaDoc tables;
90     protected Map JavaDoc excludedTables;
91     protected Map JavaDoc validationMessages;
92
93     public TableSelector(ProjectController parent) {
94         super(parent);
95         this.view = new TableSelectorView();
96         this.excludedTables = new HashMap JavaDoc();
97         this.validationMessages = new HashMap JavaDoc();
98         initController();
99     }
100
101     // ----- properties -----
102

103     public Component JavaDoc getView() {
104         return view;
105     }
106
107     /**
108      * Called by table binding script to set current table.
109      */

110     public void setTable(DbEntity table) {
111         this.table = table;
112     }
113
114     /**
115      * Returns DbEntities that are excluded from DB generation.
116      */

117     public Collection JavaDoc getExcludedTables() {
118         return excludedTables.values();
119     }
120
121     public List JavaDoc getTables() {
122         return tables;
123     }
124
125     public boolean isIncluded() {
126         if (table == null) {
127             return false;
128         }
129
130         return !excludedTables.containsKey(table.getName());
131     }
132
133     public void setIncluded(boolean b) {
134         if (table == null) {
135             return;
136         }
137
138         if (b) {
139             excludedTables.remove(table.getName());
140         }
141         else {
142             excludedTables.put(table.getName(), table);
143         }
144     }
145
146     public Object JavaDoc getProblem() {
147         return (table != null) ? validationMessages.get(table.getName()) : null;
148     }
149
150     // ------ other stuff ------
151

152     protected void initController() {
153         TableBindingBuilder tableBuilder = new TableBindingBuilder(getApplication()
154                 .getBindingFactory(), this);
155
156         tableBuilder.addColumn("Table", "#item.name", String JavaDoc.class, false);
157         tableBuilder.addColumn(
158                 "Generate",
159                 "setTable(#item), included",
160                 Boolean JavaDoc.class,
161                 true);
162         tableBuilder.addColumn(
163                 "Problems",
164                 "setTable(#item), problem",
165                 String JavaDoc.class,
166                 false);
167
168         this.tableBinding = tableBuilder.bindToTable(view.getTables(), "tables");
169     }
170
171     /**
172      * Performs validation of DbEntities in the current DataMap. Returns a collection of
173      * ValidationInfo objects describing the problems.
174      */

175     public void updateTables(DataMap dataMap) {
176         this.tables = new ArrayList JavaDoc(dataMap.getDbEntities());
177
178         excludedTables.clear();
179         validationMessages.clear();
180
181         // if there were errors, filter out those related to
182
// non-derived DbEntities...
183

184         // TODO: this is inefficient.. we need targeted validation
185
// instead of doing it on the whole project
186

187         Validator validator = ((ProjectController) getParent())
188                 .getProject()
189                 .getValidator();
190         int validationCode = validator.validate();
191         if (validationCode >= ValidationDisplayHandler.WARNING) {
192
193             Iterator JavaDoc it = validator.validationResults().iterator();
194             while (it.hasNext()) {
195                 ValidationInfo nextProblem = (ValidationInfo) it.next();
196                 Entity failedEntity = null;
197
198                 if (nextProblem.getValidatedObject() instanceof DbAttribute) {
199                     DbAttribute failedAttribute = (DbAttribute) nextProblem
200                             .getValidatedObject();
201                     failedEntity = failedAttribute.getEntity();
202                 }
203                 else if (nextProblem.getValidatedObject() instanceof DbRelationship) {
204                     DbRelationship failedRelationship = (DbRelationship) nextProblem
205                             .getValidatedObject();
206                     failedEntity = failedRelationship.getSourceEntity();
207                 }
208                 else if (nextProblem.getValidatedObject() instanceof DbEntity) {
209                     failedEntity = (Entity) nextProblem.getValidatedObject();
210                 }
211
212                 if (failedEntity == null) {
213                     continue;
214                 }
215
216                 excludedTables.put(failedEntity.getName(), failedEntity);
217                 validationMessages.put(failedEntity.getName(), nextProblem.getMessage());
218             }
219         }
220
221         // now do a pass through the tables and exclude derived
222
Iterator JavaDoc tablesIt = tables.iterator();
223         while (tablesIt.hasNext()) {
224             DbEntity table = (DbEntity) tablesIt.next();
225             if (table instanceof DerivedDbEntity) {
226                 excludedTables.put(table.getName(), table);
227                 validationMessages.put(table.getName(), "derived entity");
228             }
229         }
230
231         tableBinding.updateView();
232     }
233 }
Popular Tags