KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > ps > Reader


1 package org.objectweb.modfact.jmi.ps;
2 import javax.jmi.reflect.*;
3 import java.util.*;
4 import java.sql.*;
5 /**
6  * @author gregory
7  * This class is a wrapper for accessing the database and read a metamodel from
8  * in order to build the metamodel in the M2 repository given as a parameter.
9  * The granulariry of reading is the metamodel
10  */

11 public class Reader {
12
13     Connection connection;
14     MOFPolymorphicIterator mofIt;
15     MOFPolymorphicIterator mofModelIt;
16     RefPackage mofModel;
17     String JavaDoc modelName;
18
19     Hashtable object2ContainerMofId;
20     Hashtable mofId2object;
21     Hashtable referenceObject2referencedEndmofId;
22
23     List allRefObjects;
24     String JavaDoc mofId;
25     RefObject containerObject;
26
27     /**
28      * Method Reader.
29      * @param modelName
30      * @param repository
31      * @throws ClassNotFoundException
32      * @throws SQLException
33      *
34      * read the model uniquely identified by its name and store it
35      * into the repository
36      */

37     public Reader(String JavaDoc modelName, RefPackage repository)
38         throws ClassNotFoundException JavaDoc, SQLException {
39
40         Class.forName("com.mysql.jdbc.Driver");
41         String JavaDoc url = "jdbc:mysql://localhost:3306/test";
42         connection = DriverManager.getConnection(url);
43         this.modelName = modelName;
44         this.mofModel = repository;
45         this.mofId2object = new Hashtable();
46         this.object2ContainerMofId = new Hashtable();
47         this.referenceObject2referencedEndmofId = new Hashtable();
48         this.allRefObjects = new Vector();
49     }
50
51     /**
52      * Method read.
53      *
54      * the building of me metamodel in the repository is done in a two pass way.
55      * because of dependencies between the repository object.
56      * The necessary information are temporary stored in hashtables.
57      */

58     public void read() {
59         RefObject oo = null;
60         RefObject o = null;
61         Vector params = new Vector();
62
63         String JavaDoc query =
64             "SELECT * FROM RefPackage WHERE ModelContainerId='"
65                 + PersistantHelper.getModelIdbyName(modelName)
66                 + "';";
67
68         try {
69             Statement statement = connection.createStatement();
70             ResultSet rs = statement.executeQuery(query);
71             while (rs.next()) {
72
73                 if (rs.getString("type").equals("RefPackage")) {
74
75                     o = mofModel.refClass("Package").refCreateInstance(null);
76                     o.refSetValue("name", rs.getString("name"));
77                     o.refSetValue("annotation", rs.getString("annotation"));
78                     o.refSetValue("isRoot", rs.getString("isRoot"));
79                     o.refSetValue("isLeaf", rs.getString("isLeaf"));
80                     o.refSetValue("isAbstract", rs.getString("isAbstract"));
81                     o.refSetValue("visibility", rs.getString("visibility"));
82
83                     object2ContainerMofId.put(o, rs.getString("container"));
84                     mofId2object.put(rs.getString("RefPackageId"), o);
85                     allRefObjects.add(o);
86
87                 }
88
89                 if (rs.getString("type").equals("RefClass")) {
90
91                     params.addElement(rs.getString("name"));
92                     params.addElement(rs.getString("annotation"));
93                     params.addElement(rs.getString("isRoot"));
94                     params.addElement(rs.getString("isLeaf"));
95                     params.addElement(rs.getString("isAbstract"));
96                     params.addElement(rs.getString("visibility"));
97                     params.addElement(rs.getString("isSingleton"));
98
99                     o = mofModel.refClass("Class").refCreateInstance(params);
100
101                     params.removeAllElements();
102                     object2ContainerMofId.put(o, rs.getString("container"));
103                     mofId2object.put(rs.getString("RefPackageId"), o);
104                     allRefObjects.add(o);
105                 }
106
107                 if (rs.getString("type").equals("RefAssociation")) {
108
109                     params.addElement(rs.getString("name"));
110                     params.addElement(rs.getString("annotation"));
111                     params.addElement(rs.getString("isRoot"));
112                     params.addElement(rs.getString("isLeaf"));
113                     params.addElement(rs.getString("isAbstract"));
114                     params.addElement(rs.getString("visibility"));
115                     params.addElement(rs.getString("isDerived"));
116                     
117                     o = mofModel.refClass("Association").refCreateInstance(
118                             params);
119                     params.removeAllElements();
120                     object2ContainerMofId.put(o, rs.getString("container"));
121                     mofId2object.put(rs.getString("RefPackageId"), o);
122                     allRefObjects.add(o);
123                 }
124                 if (rs.getString("type").equals("RefAssociationEnd")) {
125
126                     o =
127                         mofModel.refClass("AssociationEnd").refCreateInstance(
128                             null);
129                     o.refSetValue("name", rs.getString("name"));
130                     o.refSetValue("annotation", rs.getString("annotation"));
131                     o.refSetValue("isNavigable", rs.getString("isNavigable"));
132                     o.refSetValue("aggregation", rs.getString("aggregation"));
133                     o.refSetValue("multiplicity", rs.getString("multiplicity"));
134                     o.refSetValue("isChangeable", rs.getString("isChangeable"));
135
136                     object2ContainerMofId.put(o, rs.getString("container"));
137                     mofId2object.put(rs.getString("RefPackageId"), o);
138                     allRefObjects.add(o);
139                 }
140
141                 if (rs.getString("type").equals("RefAttribute")) {
142
143                     o = mofModel.refClass("Attribute").refCreateInstance(null);
144                     o.refSetValue("name", rs.getString("name"));
145                     o.refSetValue("annotation", rs.getString("annotation"));
146                     o.refSetValue("visibility", rs.getString("visibility"));
147                     o.refSetValue("scope", rs.getString("scope"));
148                     o.refSetValue("isChangeable", rs.getString("isChangeable"));
149                     o.refSetValue("multiplicity", rs.getString("multiplicity"));
150                     o.refSetValue("isDerived", rs.getString("isDerived"));
151
152                     object2ContainerMofId.put(o, rs.getString("container"));
153                     mofId2object.put(rs.getString("RefPackageId"), o);
154                     allRefObjects.add(o);
155                 }
156
157                 if (rs.getString("type").equals("RefReference")) {
158
159                     o = mofModel.refClass("Reference").refCreateInstance(null);
160                     o.refSetValue("name", rs.getString("name"));
161                     o.refSetValue("annotation", rs.getString("annotation"));
162                     o.refSetValue("visibility", rs.getString("visibility"));
163                     o.refSetValue("scope", rs.getString("scope"));
164                     o.refSetValue("isChangeable", rs.getString("isChangeable"));
165                     o.refSetValue("multiplicity", rs.getString("multiplicity"));
166
167                     object2ContainerMofId.put(o, rs.getString("container"));
168                     mofId2object.put(rs.getString("RefPackageId"), o);
169
170                     referenceObject2referencedEndmofId.put(
171                         o,
172                         rs.getString("referencedEnd"));
173
174                     allRefObjects.add(o);
175                 }
176
177                 if (rs.getString("type").equals("Tag")) {
178
179                     o = mofModel.refClass("Tag").refCreateInstance(null);
180                     o.refSetValue("name", rs.getString("name"));
181                     o.refSetValue("annotation", rs.getString("annotation"));
182                     o.refSetValue("tagId", rs.getString("tagId"));
183                     o.refSetValue("values", rs.getString("tagtagIdalues"));
184                     o.refSetValue("elements", rs.getString("taggedElements"));
185
186                     object2ContainerMofId.put(o, rs.getString("container"));
187                     mofId2object.put(rs.getString("RefPackageId"), o);
188                     allRefObjects.add(o);
189                 }
190
191             } //end while
192

193             rs.close();
194             statement.close();
195
196             //Get the reference of the container object and set this property
197
Iterator it = allRefObjects.iterator();
198             while (it.hasNext()) {
199
200                 oo = (RefObject) it.next();
201                 mofId = object2ContainerMofId.get(oo).toString();
202                 containerObject = (RefObject) mofId2object.get(mofId);
203                 oo.refSetValue("container", containerObject);
204             }
205
206             //Set the referenceEnd property for reference objects
207
it = allRefObjects.iterator();
208             while (it.hasNext()) {
209
210                 oo = (RefObject) it.next();
211
212                 if (oo
213                     .refMetaObject()
214                     .refGetValue("name")
215                     .equals("Reference")) {
216                     mofId =
217                         referenceObject2referencedEndmofId.get(oo).toString();
218                     oo.refSetValue("referencedEnd", mofId2object.get(mofId));
219                 }
220
221             }
222
223         } catch (Exception JavaDoc e) {
224             e.printStackTrace();
225         }
226
227     }
228
229 }
230
Popular Tags