KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.modfact.jmi.ps;
2
3 import javax.jmi.reflect.*;
4
5 import org.objectweb.modfact.jmi.helper.StringHelper;
6
7 import java.sql.*;
8
9 /**
10  * @author gregory
11  *
12  * This class is a wrapper for accessing the database for create and update
13  * operations
14  */

15
16 public class Writer {
17
18     Connection connection;
19     MOFPolymorphicIterator mofIt;
20     MOFPolymorphicIterator mofModelIt;
21     RefPackage mofModel;
22
23     public Writer(RefPackage p)
24         throws ClassNotFoundException JavaDoc, SQLException {
25
26         Class.forName("com.mysql.jdbc.Driver");
27         String JavaDoc url = "jdbc:mysql://localhost:3306/test";
28         connection = DriverManager.getConnection(url);
29
30         mofIt = new MOFPolymorphicIterator(p);
31
32         
33     }
34
35     /**
36      * Method create.
37      * @param o
38      * @param modelId
39      * @return String
40      */

41     public String JavaDoc create(RefObject o,String JavaDoc modelId) {
42         String JavaDoc query =
43             "INSERT INTO RefPackage (RefPackageID,ModelContainerID) VALUES ('"
44                 + StringHelper.replaceAll(o.refMofId(), ",", "*")
45                 + "','"+ modelId +"');";
46         try {
47             Statement statement = connection.createStatement();
48             statement.execute(query);
49             statement.close();
50             return StringHelper.replaceAll(o.refMofId(), ",", "*");
51
52         } catch (Exception JavaDoc e) {
53             e.printStackTrace();
54         }
55
56         return StringHelper.replaceAll(o.refMofId(), ",", "*");
57     }
58
59     /**
60      * Method rename.
61      * @param p
62      * @param elementId
63      */

64     public void rename(RefObject p, String JavaDoc elementId) {
65         String JavaDoc listOfPackages = null;
66         String JavaDoc listOfClasses = null;
67         String JavaDoc listOfAssociations = null;
68
69         //get the immediate pacakge
70
String JavaDoc immediatePackage;
71         if (p.refImmediatePackage() == null)
72             immediatePackage = "null";
73         else
74             immediatePackage =
75                 StringHelper.replaceAll(p.refImmediatePackage().refMofId(), ",", "*");
76
77         //get the container
78
String JavaDoc container;
79         RefObject refContainer = (RefObject) p.refGetValue("container");
80         //container =p.refMetaObject().refGetValue("container").toString();
81
if (refContainer == null)
82             container = "null";
83         else
84             container = StringHelper.replaceAll(refContainer.refMofId(), ",", "*");
85
86         String JavaDoc query =
87             "UPDATE RefPackage SET Name='"
88                 + (p).refGetValue("name")
89                 + "', annotation ='"
90             //+"annotation"
91
+p.refGetValue("annotation")
92         + "',qualifiedName='"
93         + StringHelper.replaceAll(p.refGetValue("qualifiedName").toString(), ",", "#")
94         + "',container='"
95         + container
96         + "',constraints='"
97         + p.refMetaObject().refGetValue("constraints");
98
99         if (p.refMetaObject().refGetValue("name").equals("Package")) {
100             query =
101                 query
102                     + "',visibility='"
103                     + p.refGetValue("visibility")
104                     + "',isLeaf='"
105                     + p.refGetValue("isLeaf")
106                     + "',isRoot='"
107                     + p.refGetValue("isRoot")
108                     + "',isAbstract='"
109                     + p.refGetValue("isAbstract")
110                     + "',supertypes='"
111                     + p.refGetValue("supertypes")
112                     + "',refMetaObject='"
113                     + StringHelper.replaceAll(p.refMofId(), ",", "*")
114                     + "',type='RefPackage";
115         } else if (p.refMetaObject().refGetValue("name").equals("Class")) {
116             query =
117                 query
118                     + "',visibility='"
119                     + p.refGetValue("visibility")
120                     + "',isLeaf='"
121                     + p.refGetValue("isLeaf")
122                     + "',isRoot='"
123                     + p.refGetValue("isRoot")
124                     + "',isAbstract='"
125                     + p.refGetValue("isAbstract")
126                     + "',supertypes='"
127                     + p.refGetValue("supertypes")
128                     + "',refMetaObject='"
129                     + StringHelper.replaceAll(p.refMofId(), ",", "*")
130                     + "',isSingleton='"
131                     + p.refGetValue("isSingleton")
132                     + "',type='RefClass";
133
134         } else if (
135             p.refMetaObject().refGetValue("name").equals("Association")) {
136             query =
137                 query
138                     + "',visibility='"
139                     + p.refGetValue("visibility")
140                     + "',isLeaf='"
141                     + p.refGetValue("isLeaf")
142                     + "',isRoot='"
143                     + p.refGetValue("isRoot")
144                     + "',isAbstract='"
145                     + p.refGetValue("isAbstract")
146                     + "',supertypes='"
147                     + p.refGetValue("supertypes")
148                     + "',refMetaObject='"
149                     + StringHelper.replaceAll(p.refMofId(), ",", "*")
150                     + "',isDerived='"
151                     + p.refGetValue("isDerived")
152                     + "',type='RefAssociation";
153
154         }
155
156         //if p is an associationEnd
157
else if (
158             p.refMetaObject().refGetValue("name").equals("AssociationEnd")) {
159
160             query =
161                 query
162                     + "',isNavigable='"
163                     + p.refGetValue("isNavigable")
164                     + "',aggregation='"
165                     + p.refGetValue("aggregation")
166                     + "',multiplicity='"
167                     + p.refGetValue("multiplicity")
168                     + "',isChangeable='"
169                     + p.refGetValue("isChangeable")
170                     + "',type='RefAssociationEnd";
171         }
172
173         //if p is an Attribute
174
else if (p.refMetaObject().refGetValue("name").equals("Attribute")) {
175
176             query =
177                 query
178                     + "',isChangeable='"
179                     + p.refGetValue("isChangeable")
180                     + "',visibility='"
181                     + p.refGetValue("visibility")
182                     + "',scope='"
183                     + p.refGetValue("scope")
184                     + "',isDerived='"
185                     + p.refGetValue("isDerived")
186                     + "',type='RefAttribute";
187         }
188
189         //if p is a Reference
190
else if (p.refMetaObject().refGetValue("name").equals("Reference")) {
191
192             query =
193                 query
194                     + "',isChangeable='"
195                     + p.refGetValue("isChangeable")
196                     + "',visibility='"
197                     + p.refGetValue("visibility")
198                     + "',scope='"
199                     + p.refGetValue("scope")
200                     + "',referencedEnd='"
201                     + StringHelper.replaceAll(((RefObject)p.refGetValue("referencedEnd")).refMofId(), ",","*")
202 // + "',exposedEnd='"
203
// + ((RefObject)p.refGetValue("exposedEnd")).refMofId().replaceAll(",","*")
204
+ "',type='RefReference";
205         }
206         // if p is a Tag
207
else if (p.refMetaObject().refGetValue("name").equals("Tag")) {
208
209             query =
210                 query
211                     + "',tagId='"
212                     + p.refGetValue("tagId")
213                     + "',tagvalues='"
214                     + p.refGetValue("values")
215                     + "',taggedElements='"
216                     + p.refGetValue("elements")
217                     + "',type='RefTag";
218         }
219         
220         //miss if p is a opération
221

222         
223         query = query + "' WHERE RefPackageID=" + elementId + ";";
224
225         //,RefPackageContainer,RefMetaObject,RefPackageList,RefClassList,RefAssociationList) VALUES ("
226

227         try {
228             Statement statement = connection.createStatement();
229
230             //System.out.println(query);
231
statement.execute(query);
232             statement.close();
233
234         } catch (Exception JavaDoc e) {
235             e.printStackTrace();
236         }
237     }
238
239 }
240
Popular Tags