KickJava   Java API By Example, From Geeks To Geeks.

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


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

30 package com.genimen.djeneric.repository.rdbms;
31
32 import java.sql.ResultSet JavaDoc;
33 import java.sql.SQLException JavaDoc;
34 import java.util.ArrayList JavaDoc;
35
36 import com.genimen.djeneric.language.Messages;
37 import com.genimen.djeneric.repository.DjContext;
38 import com.genimen.djeneric.repository.DjIdProvider;
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 import com.genimen.djeneric.util.DjLogger;
45
46 public class RdbmsContext extends DjContext
47 {
48   protected RdbmsContext(DjPersistenceManager mgr)
49   {
50     super(mgr);
51   }
52
53   protected RdbmsContext(DjPersistenceManager mgr, long id, String JavaDoc code, String JavaDoc name)
54   {
55     super(mgr, id, code, name);
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 context_id = :id");
65       stmt.setLong("id", getId());
66       stmt.executeUpdate();
67
68       stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from " + RdbmsPersistenceManager.CONTEXT_TABLE
69                                                               + " where id = :id");
70       stmt.setLong("id", getId());
71       stmt.executeUpdate();
72     }
73     catch (SQLException JavaDoc x)
74     {
75       DjLogger.log(x);
76       if (x.getSQLState().equals("01000")) throw new DjenericException(Messages
77           .getString("RdbmsContext.CouldNotDeleteContext"));
78       throw new DjenericException(x);
79     }
80   }
81
82   public void deleteContents(DjSession session) throws DjenericException
83   {
84     try
85     {
86
87       // Workaround for hypersonic v1.7.1 bug: cannot delete when referencing
88
// 'self' record
89
String JavaDoc connName = session.getPersistenceManager().getCurrentRepository().getClass().getName().toLowerCase();
90       if (connName.indexOf("hsqldb") != -1) for (int i = 1; i <= DjPersistenceManager.MAPPING_COLUMN_COUNT_REL; i++)
91       {
92         String JavaDoc idxStr = String.valueOf(i);
93         if (idxStr.length() == 1) idxStr = "0" + idxStr;
94         String JavaDoc colName = DjPersistenceManager.MAPPING_REL + idxStr;
95
96         SqlStatement stmt = ((RdbmsSession) session)
97             .getInternalSqlStatement("update " + RdbmsPersistenceManager.POLYMORPH_TABLE + " set " + colName
98                                      + " = null " + " where " + RdbmsPersistenceManager.INTERNAL_CONTEXT_COLUMN
99                                      + " = :id");
100         stmt.setLong("id", getId());
101         stmt.executeUpdate();
102       }
103
104       SqlStatement stmt = ((RdbmsSession) session)
105           .getInternalSqlStatement("delete from " + RdbmsPersistenceManager.POLYMORPH_TABLE + " where "
106                                    + RdbmsPersistenceManager.INTERNAL_CONTEXT_COLUMN + " = :id");
107       stmt.setLong("id", getId());
108       stmt.executeUpdate();
109     }
110     catch (SQLException JavaDoc x)
111     {
112       DjLogger.log(x);
113       if (x.getSQLState().equals("01000")) throw new DjenericException(Messages
114           .getString("RdbmsContext.CouldNotDeleteContext"));
115       throw new DjenericException(x);
116     }
117
118   }
119
120   public void reload(DjSession session) throws DjenericException
121   {
122     try
123     {
124       SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("select code, name from "
125                                                                            + RdbmsPersistenceManager.CONTEXT_TABLE
126                                                                            + " where id = :id");
127       stmt.setLong("id", getId());
128       ResultSet JavaDoc rs = stmt.executeQuery();
129       if (rs.next())
130       {
131         setCode(rs.getString("code"));
132         setName(rs.getString("name"));
133       }
134       rs.close();
135       stmt.close();
136     }
137     catch (SQLException JavaDoc x)
138     {
139       throw new DjenericException(x);
140     }
141   }
142
143   public void persist(DjSession session) throws DjenericException
144   {
145     if (getId() == -1)
146     {
147       DjIdProvider ip = new RdbmsIdProvider(1);
148       // do not cache id's
149
ip.setSequenceName(RdbmsPersistenceManager.CONTEXT_TABLE);
150       setId(ip.getNextId(session));
151     }
152
153     SqlStatement stmt = ((RdbmsSession) session)
154         .getInternalSqlStatement("update " + RdbmsPersistenceManager.CONTEXT_TABLE
155                                  + " set code = :code, name = :name where id = :id");
156     try
157     {
158       stmt.setString("code", getCode());
159       stmt.setString("name", getName());
160       stmt.setLong("id", getId());
161       if (stmt.executeUpdate() == 0)
162       {
163         // Deleted or noy yet inserted?
164
insert(session);
165         // then insert it (again)!
166
}
167     }
168     catch (SQLException JavaDoc x)
169     {
170       throw new DjenericException(x);
171     }
172   }
173
174   public void insert(DjSession session) throws DjenericException
175   {
176     SqlStatement stmt = ((RdbmsSession) session)
177         .getInternalSqlStatement("insert into " + RdbmsPersistenceManager.CONTEXT_TABLE
178                                  + "(id, code, name) values(:id, :code, :name)");
179     try
180     {
181       stmt.setLong("id", getId());
182       stmt.setString("code", getCode());
183       stmt.setString("name", getName());
184       stmt.executeUpdate();
185     }
186     catch (SQLException JavaDoc x)
187     {
188       throw new DjenericException(x);
189     }
190   }
191
192   public DjUserContextAssociation[] getUsers(DjSession session) throws DjenericException
193   {
194     ArrayList JavaDoc result = new ArrayList JavaDoc();
195     SqlStatement stmt = ((RdbmsSession) session)
196         .getInternalSqlStatement("select dev.id, dev.code, dev.name, dev.administrator_yn, dev.modeler_yn, dev.password, "
197                                  + " dc.cre_yn, dc.mod_yn, dc.del_yn, dc.qry_yn "
198                                  + "from "
199                                  + RdbmsPersistenceManager.USER_TABLE
200                                  + " dev, "
201                                  + RdbmsPersistenceManager.USER_CTX_TABLE
202                                  + " dc "
203                                  + "where dc.context_id = :id "
204                                  + "and dc.user_id = dev.id " + "order by dev.code");
205     try
206     {
207       stmt.setLong("id", getId());
208       ResultSet JavaDoc rs = stmt.executeQuery();
209       while (rs.next())
210       {
211         DjUser dev = new RdbmsUser(getPersistenceManager(), rs.getLong("id"), rs.getString("code"), rs
212             .getString("name"), rs.getString("password"), "Y".equalsIgnoreCase(rs.getString("administrator_yn")), "Y"
213             .equalsIgnoreCase(rs.getString("modeler_yn")));
214         DjUserContextAssociation dca = new RdbmsUserContextAssociation(dev, this, rs.getString("cre_yn")
215             .equalsIgnoreCase("Y"), rs.getString("mod_yn").equalsIgnoreCase("Y"), rs.getString("del_yn")
216             .equalsIgnoreCase("Y"), rs.getString("qry_yn").equalsIgnoreCase("Y"));
217         result.add(dca);
218       }
219       rs.close();
220       stmt.close();
221
222       return (DjUserContextAssociation[]) result.toArray(new DjUserContextAssociation[0]);
223     }
224     catch (SQLException JavaDoc x)
225     {
226       throw new DjenericException(x);
227     }
228   }
229
230   public void removeAllUserAssociations(DjSession session) throws DjenericException
231   {
232     try
233     {
234       SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from "
235                                                                            + RdbmsPersistenceManager.USER_CTX_TABLE
236                                                                            + " where context_id = :ctxtId");
237       stmt.setLong("ctxtId", getId());
238       stmt.executeUpdate();
239     }
240     catch (SQLException JavaDoc x)
241     {
242       throw new DjenericException(x);
243     }
244   }
245
246 }
Popular Tags