KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > rdbms > RdbmsUser


1 /*
2  Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  Redistribution and use in source and binary forms, with or without modification, is permitted
4  provided that the following conditions are met:
5  - Redistributions of source code must retain the above copyright notice, this list of conditions
6  and the following disclaimer.
7  - Redistributions in binary form must reproduce the above copyright notice, this list of
8  conditions and the following disclaimer in the documentation and/or other materials
9  provided with the distribution.
10  - All advertising materials mentioning features or use of this software must display the
11  following acknowledgment: "This product includes Djeneric."
12  - Products derived from this software may not be called "Djeneric" nor may
13  "Djeneric" appear in their names without prior written permission of Genimen BV.
14  - Redistributions of any form whatsoever must retain the following acknowledgment: "This
15  product includes Djeneric."
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
17  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
20  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */

28 package com.genimen.djeneric.repository.rdbms;
29
30 import java.io.IOException JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.WeakHashMap JavaDoc;
35
36 import com.genimen.djeneric.repository.DjContext;
37 import com.genimen.djeneric.repository.DjIdProvider;
38 import com.genimen.djeneric.repository.DjModelView;
39 import com.genimen.djeneric.repository.DjPersistenceManager;
40 import com.genimen.djeneric.repository.DjSession;
41 import com.genimen.djeneric.repository.DjUser;
42 import com.genimen.djeneric.repository.DjUserContextAssociation;
43 import com.genimen.djeneric.repository.exceptions.DjenericException;
44
45 public class RdbmsUser extends DjUser
46 {
47   protected RdbmsUser(DjPersistenceManager mgr)
48   {
49     super(mgr);
50   }
51
52   protected RdbmsUser(DjPersistenceManager mgr, long id, String JavaDoc code, String JavaDoc name, String JavaDoc password,
53                       boolean administrator, boolean modeler)
54   {
55     super(mgr, id, code, name, password, administrator, modeler);
56   }
57
58   public void delete(DjSession session) throws DjenericException
59   {
60     try
61     {
62       SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from "
63                                                                            + RdbmsPersistenceManager.USER_CTX_TABLE
64                                                                            + " where user_id = :id");
65       stmt.setLong("id", getId());
66       stmt.executeUpdate();
67
68       stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from " + RdbmsPersistenceManager.USER_VWS_TABLE
69                                                               + " where user_id = :id");
70       stmt.setLong("id", getId());
71       stmt.executeUpdate();
72
73       stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from " + RdbmsPersistenceManager.USER_TABLE
74                                                               + " where id = :id");
75       stmt.setLong("id", getId());
76       stmt.executeUpdate();
77     }
78     catch (SQLException JavaDoc x)
79     {
80       throw new DjenericException(x);
81     }
82   }
83
84   public void reload(DjSession session) throws DjenericException
85   {
86     try
87     {
88       SqlStatement stmt = ((RdbmsSession) session)
89           .getInternalSqlStatement("select code, name, password, administrator_yn, modeler_yn from "
90                                    + RdbmsPersistenceManager.USER_TABLE + " where id = :id");
91       stmt.setLong("id", getId());
92       ResultSet JavaDoc rs = stmt.executeQuery();
93       if (rs.next())
94       {
95         setCode(rs.getString("code"));
96         setName(rs.getString("name"));
97         setPassword(rs.getString("password"));
98         setAdministrator("Y".equalsIgnoreCase(rs.getString("administrator_yn")));
99         setModeler("Y".equalsIgnoreCase(rs.getString("modeler_yn")));
100       }
101       rs.close();
102     }
103     catch (SQLException JavaDoc x)
104     {
105       throw new DjenericException(x);
106     }
107   }
108
109   public void persist(DjSession session) throws DjenericException
110   {
111     try
112     {
113       if (getId() == -1)
114       {
115         DjIdProvider ip = new RdbmsIdProvider(1);
116         // do not cache id's
117
ip.setSequenceName(RdbmsPersistenceManager.USER_TABLE);
118
119         setId(ip.getNextId(session));
120       }
121       SqlStatement stmt = ((RdbmsSession) session)
122           .getInternalSqlStatement("update "
123                                    + RdbmsPersistenceManager.USER_TABLE
124                                    + " set code = :code, name = :name, password = :password, administrator_yn = :administrator_yn, modeler_yn = :modeler_yn where id = :id");
125       stmt.setString("code", getCode());
126       stmt.setString("name", getName());
127       stmt.setString("password", getPassword());
128       stmt.setString("administrator_yn", isAdministrator() ? "Y" : "N");
129       stmt.setString("modeler_yn", isModeler() ? "Y" : "N");
130       stmt.setLong("id", getId());
131
132       if (stmt.executeUpdate() == 0)
133       {
134         // Deleted or noy yet inserted?
135
insert(session);
136         // then insert it again!
137
}
138     }
139     catch (SQLException JavaDoc x)
140     {
141       throw new DjenericException(x);
142     }
143   }
144
145   public void insert(DjSession session) throws DjenericException
146   {
147     try
148     {
149       SqlStatement stmt = ((RdbmsSession) session)
150           .getInternalSqlStatement("insert into "
151                                    + RdbmsPersistenceManager.USER_TABLE
152                                    + "(id, code, name, password, administrator_yn, modeler_yn) values(:id, :code, :name, :password, :administrator_yn, :modeler_yn)");
153       stmt.setLong("id", getId());
154       stmt.setString("code", getCode());
155       stmt.setString("name", getName());
156       stmt.setString("password", getPassword());
157       stmt.setString("administrator_yn", isAdministrator() ? "Y" : "N");
158       stmt.setString("modeler_yn", isModeler() ? "Y" : "N");
159       stmt.executeUpdate();
160     }
161     catch (SQLException JavaDoc x)
162     {
163       throw new DjenericException(x);
164     }
165   }
166
167   public DjUserContextAssociation[] getContextAssociations() throws DjenericException
168   {
169     DjSession session = null;
170
171     try
172     {
173       session = getPersistenceManager().createSession();
174
175       ArrayList JavaDoc result = new ArrayList JavaDoc();
176       SqlStatement stmt = ((RdbmsSession) session)
177           .getInternalSqlStatement("select ctxt.id, ctxt.code, ctxt.name, "
178                                    + " dc.cre_yn, dc.mod_yn, dc.del_yn, dc.qry_yn " + "from "
179                                    + RdbmsPersistenceManager.CONTEXT_TABLE + " ctxt, " + " "
180                                    + RdbmsPersistenceManager.USER_CTX_TABLE + " dc "
181                                    + "where ctxt.id = dc.context_id " + "and dc.user_id = :id "
182                                    + "order by ctxt.name ");
183       stmt.setLong("id", getId());
184       ResultSet JavaDoc rs = stmt.executeQuery();
185       while (rs.next())
186       {
187         DjContext ctxt = new RdbmsContext(getPersistenceManager(), rs.getLong("id"), rs.getString("code"), rs
188             .getString("name"));
189         DjUserContextAssociation dca = new RdbmsUserContextAssociation(this, ctxt, rs.getString("cre_yn")
190             .equalsIgnoreCase("Y"), rs.getString("mod_yn").equalsIgnoreCase("Y"), rs.getString("del_yn")
191             .equalsIgnoreCase("Y"), rs.getString("qry_yn").equalsIgnoreCase("Y"));
192         result.add(dca);
193       }
194       rs.close();
195       stmt.close();
196
197       return (DjUserContextAssociation[]) result.toArray(new DjUserContextAssociation[0]);
198     }
199     catch (SQLException JavaDoc x)
200     {
201       throw new DjenericException(x);
202     }
203     finally
204     {
205       if (session != null) session.close();
206     }
207   }
208
209   public DjUserContextAssociation getContextAssociation(DjContext ctxt) throws DjenericException
210   {
211     DjSession session = null;
212
213     try
214     {
215       session = getPersistenceManager().createSession();
216
217       DjUserContextAssociation result = null;
218       SqlStatement stmt = ((RdbmsSession) session)
219           .getInternalSqlStatement("select dc.cre_yn, dc.mod_yn, dc.del_yn, dc.qry_yn " + "from "
220                                    + RdbmsPersistenceManager.USER_CTX_TABLE + " dc " + "where dc.context_id = :cid "
221                                    + "and dc.user_id = :uid ");
222       stmt.setLong("uid", getId());
223       stmt.setLong("cid", ctxt.getId());
224       ResultSet JavaDoc rs = stmt.executeQuery();
225       if (rs.next())
226       {
227         result = new RdbmsUserContextAssociation(this, ctxt, rs.getString("cre_yn").equalsIgnoreCase("Y"), rs
228             .getString("mod_yn").equalsIgnoreCase("Y"), rs.getString("del_yn").equalsIgnoreCase("Y"), rs
229             .getString("qry_yn").equalsIgnoreCase("Y"));
230       }
231       rs.close();
232       stmt.close();
233
234       return result;
235     }
236     catch (SQLException JavaDoc x)
237     {
238       throw new DjenericException(x);
239     }
240     finally
241     {
242       if (session != null) session.close();
243     }
244   }
245
246   public void removeAllContextAssociations(DjSession session) throws DjenericException
247   {
248     try
249     {
250       SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from "
251                                                                            + RdbmsPersistenceManager.USER_CTX_TABLE
252                                                                            + " where user_id = :devId");
253       stmt.setLong("devId", getId());
254       stmt.executeUpdate();
255     }
256     catch (SQLException JavaDoc x)
257     {
258       throw new DjenericException(x);
259     }
260   }
261
262   // We use a static cache to avoid loading duplicates of views into memory since this
263
// can cause a waste of memory (every user might have multiple views, if there
264
// are 30 dj_users with each 3 views we will avoid 90 views being instantiated
265
// if there are only 3 views defined)
266
private static WeakHashMap JavaDoc _cachedViews = new WeakHashMap JavaDoc();
267
268   public DjModelView[] getViews() throws DjenericException
269   {
270     DjSession session = null;
271     try
272     {
273       session = getPersistenceManager().createSession();
274       ArrayList JavaDoc result = new ArrayList JavaDoc();
275
276       String JavaDoc sql;
277
278       if (!isAdministrator())
279       {
280         sql = "select mvw.id, mvw.code, mvw.metaview " + "from " + RdbmsPersistenceManager.VIEW_TABLE + " mvw, "
281               + " " + RdbmsPersistenceManager.USER_VWS_TABLE + " dv " + "where mvw.id = dv.metaview_id "
282               + "and dv.user_id = :id " + "order by mvw.code ";
283       }
284       else
285       {
286         sql = "select mvw.id, mvw.code, mvw.metaview " + "from " + RdbmsPersistenceManager.VIEW_TABLE + " mvw "
287               + "order by mvw.code ";
288       }
289
290       SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement(sql);
291
292       if (!isAdministrator()) stmt.setLong("id", getId());
293
294       ResultSet JavaDoc rs = stmt.executeQuery();
295       while (rs.next())
296       {
297         Long JavaDoc key = new Long JavaDoc(rs.getLong("id"));
298
299         if (_cachedViews.containsKey(key))
300         {
301           result.add(_cachedViews.get(key));
302         }
303         else
304         {
305           DjModelView vw = new RdbmsModelView(getPersistenceManager(), rs.getLong("id"), rs.getString("code"),
306               new String JavaDoc(SqlStatement.getBlob(rs, "metaview")));
307           _cachedViews.put(key, vw);
308           result.add(vw);
309         }
310       }
311       rs.close();
312       stmt.close();
313
314       return (DjModelView[]) result.toArray(new DjModelView[0]);
315     }
316     catch (SQLException JavaDoc x)
317     {
318       throw new DjenericException(x);
319     }
320     catch (IOException JavaDoc x)
321     {
322       throw new DjenericException(x);
323     }
324     finally
325     {
326       if (session != null) session.close();
327     }
328   }
329
330   public void addView(DjSession session, DjModelView view) throws DjenericException
331   {
332     try
333     {
334       SqlStatement stmt = ((RdbmsSession) session)
335           .getInternalSqlStatement("insert into " + RdbmsPersistenceManager.USER_VWS_TABLE
336                                    + "(user_id, metaview_id) values(:devId, :mvwId)");
337       stmt.setLong("devId", getId());
338       stmt.setLong("mvwId", view.getId());
339       stmt.executeUpdate();
340     }
341     catch (SQLException JavaDoc x)
342     {
343       throw new DjenericException(x);
344     }
345   }
346
347   public void removeView(DjSession session, DjModelView view) throws DjenericException
348   {
349     try
350     {
351       SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from "
352                                                                            + RdbmsPersistenceManager.USER_VWS_TABLE
353                                                                            + " " + "where user_id = :devId "
354                                                                            + "and metaview_id = :mvwId");
355       stmt.setLong("devId", getId());
356       stmt.setLong("mvwId", view.getId());
357       stmt.executeUpdate();
358     }
359     catch (SQLException JavaDoc x)
360     {
361       throw new DjenericException(x);
362     }
363   }
364
365   public void removeAllViews(DjSession session) throws DjenericException
366   {
367     try
368     {
369       SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from "
370                                                                            + RdbmsPersistenceManager.USER_VWS_TABLE
371                                                                            + " " + "where user_id = :devId");
372       stmt.setLong("devId", getId());
373       stmt.executeUpdate();
374     }
375     catch (SQLException JavaDoc x)
376     {
377       throw new DjenericException(x);
378     }
379   }
380 }
Popular Tags