KickJava   Java API By Example, From Geeks To Geeks.

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


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
35 import com.genimen.djeneric.repository.DjIdProvider;
36 import com.genimen.djeneric.repository.DjModelView;
37 import com.genimen.djeneric.repository.DjPersistenceManager;
38 import com.genimen.djeneric.repository.DjSession;
39 import com.genimen.djeneric.repository.DjUser;
40 import com.genimen.djeneric.repository.exceptions.DjenericException;
41
42 public class RdbmsModelView extends DjModelView
43 {
44   String JavaDoc _originalDefinition = null;
45   String JavaDoc _originalCode = null;
46
47   protected RdbmsModelView(DjPersistenceManager mgr)
48   {
49     super(mgr);
50   }
51
52   protected RdbmsModelView(DjPersistenceManager mgr, long id, String JavaDoc code, String JavaDoc definition)
53   {
54     super(mgr, id, code, definition);
55     _originalDefinition = definition;
56     _originalCode = code;
57   }
58
59   public void delete(DjSession session) throws DjenericException
60   {
61     try
62     {
63       SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from "
64                                                                            + RdbmsPersistenceManager.USER_VWS_TABLE
65                                                                            + " where metaview_id = :id");
66       stmt.setLong("id", getId());
67       stmt.executeUpdate();
68
69       stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from " + RdbmsPersistenceManager.VIEW_TABLE
70                                                               + " where id = :id");
71       stmt.setLong("id", getId());
72       stmt.executeUpdate();
73     }
74     catch (SQLException JavaDoc x)
75     {
76       throw new DjenericException(x);
77     }
78   }
79
80   public void reload(DjSession session) throws DjenericException
81   {
82     try
83     {
84       SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("select code, metaview from "
85                                                                            + RdbmsPersistenceManager.VIEW_TABLE
86                                                                            + " where id = :id");
87       stmt.setLong("id", getId());
88       ResultSet JavaDoc rs = stmt.executeQuery();
89       if (rs.next())
90       {
91         _originalCode = rs.getString("code");
92         setCode(_originalCode);
93         _originalDefinition = new String JavaDoc(SqlStatement.getBlob(rs, "metaview"));
94         setDefinition(_originalDefinition);
95       }
96       rs.close();
97     }
98     catch (SQLException JavaDoc x)
99     {
100       throw new DjenericException(x);
101     }
102     catch (IOException JavaDoc x)
103     {
104       throw new DjenericException(x);
105     }
106   }
107
108   // Checks to see if this view, with this name has been updated by another session
109
// If this returns false it is save to persist this view (without overwriting somebody else's changes)
110
public boolean isModifiedExternally(DjSession session) throws DjenericException
111   {
112     if (_originalCode == null || _originalDefinition == null) return false;
113     try
114     {
115       if (!_originalCode.equals(getCode())) return false;
116
117       boolean modified = true;
118       SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("select code, metaview from "
119                                                                            + RdbmsPersistenceManager.VIEW_TABLE
120                                                                            + " where id = :id");
121       stmt.setLong("id", getId());
122       ResultSet JavaDoc rs = stmt.executeQuery();
123       if (rs.next())
124       {
125         String JavaDoc inDb = new String JavaDoc(SqlStatement.getBlob(rs, "metaview"));
126         modified = !inDb.equals(_originalDefinition);
127       }
128       rs.close();
129       return modified;
130     }
131     catch (SQLException JavaDoc x)
132     {
133       throw new DjenericException(x);
134     }
135     catch (IOException JavaDoc x)
136     {
137       throw new DjenericException(x);
138     }
139   }
140
141   public void persist(DjSession session) throws DjenericException
142   {
143     try
144     {
145       if (getId() == -1)
146       {
147         DjIdProvider ip = new RdbmsIdProvider(1);
148         // do not cache id's
149
ip.setSequenceName(RdbmsPersistenceManager.VIEW_TABLE);
150
151         setId(ip.getNextId(session));
152       }
153       SqlStatement stmt = ((RdbmsSession) session)
154           .getInternalSqlStatement("update " + RdbmsPersistenceManager.VIEW_TABLE
155                                    + " set code = :code, metaview = :metaview where id = :id");
156       stmt.setString("code", getCode());
157       stmt.setBlob("metaview", getDefinition(), DjPersistenceManager.ENCODING_METHOD);
158       stmt.setLong("id", getId());
159
160       if (stmt.executeUpdate() == 0)
161       {
162         // Deleted or noy yet inserted?
163
insert(session);
164         // then insert it again!
165
}
166       _originalDefinition = getDefinition();
167       _originalCode = getCode();
168     }
169     catch (SQLException JavaDoc x)
170     {
171       throw new DjenericException(x);
172     }
173
174   }
175
176   public void insert(DjSession session) throws DjenericException
177   {
178     try
179     {
180       SqlStatement stmt = ((RdbmsSession) session)
181           .getInternalSqlStatement("insert into " + RdbmsPersistenceManager.VIEW_TABLE
182                                    + "(id, code, metaview) values(:id, :code, :metaview)");
183       stmt.setLong("id", getId());
184       stmt.setString("code", getCode());
185       stmt.setBlob("metaview", getDefinition(), DjPersistenceManager.ENCODING_METHOD);
186       stmt.executeUpdate();
187     }
188     catch (SQLException JavaDoc x)
189     {
190       throw new DjenericException(x);
191     }
192   }
193
194   public DjUser[] getUsers(DjSession session) throws DjenericException
195   {
196     try
197     {
198       ArrayList JavaDoc result = new ArrayList JavaDoc();
199       SqlStatement stmt = ((RdbmsSession) session)
200           .getInternalSqlStatement("select dev.id, dev.code, dev.name, dev.password, dev.administrator_yn, dev.modeler_yn "
201                                    + "from "
202                                    + RdbmsPersistenceManager.USER_TABLE
203                                    + " dev, "
204                                    + " "
205                                    + RdbmsPersistenceManager.USER_VWS_TABLE
206                                    + " dv "
207                                    + "where dev.id = dv.user_id "
208                                    + "and dv.metaview_id = :id " + "order by dev.code ");
209       stmt.setLong("id", getId());
210       ResultSet JavaDoc rs = stmt.executeQuery();
211       while (rs.next())
212       {
213         DjUser dev = new RdbmsUser(getPersistenceManager(), rs.getLong("id"), rs.getString("code"), rs
214             .getString("name"), rs.getString("password"), "Y".equalsIgnoreCase(rs.getString("administrator_yn")), "Y"
215             .equalsIgnoreCase(rs.getString("modeler_yn")));
216         result.add(dev);
217       }
218       rs.close();
219       stmt.close();
220
221       return (DjUser[]) result.toArray(new DjUser[0]);
222     }
223     catch (SQLException JavaDoc x)
224     {
225       throw new DjenericException(x);
226     }
227   }
228
229   public void removeUser(DjSession session, DjUser user) throws DjenericException
230   {
231     try
232     {
233       SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from "
234                                                                            + RdbmsPersistenceManager.USER_VWS_TABLE
235                                                                            + " " + "where user_id = :devId "
236                                                                            + "and metaview_id = :mvwId");
237       stmt.setLong("devId", user.getId());
238       stmt.setLong("mvwId", getId());
239       stmt.executeUpdate();
240     }
241     catch (SQLException JavaDoc x)
242     {
243       throw new DjenericException(x);
244     }
245   }
246
247 }
Popular Tags