KickJava   Java API By Example, From Geeks To Geeks.

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


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 modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes 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,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE 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.DjContextManager;
39 import com.genimen.djeneric.repository.DjPersistenceManager;
40 import com.genimen.djeneric.repository.DjUser;
41 import com.genimen.djeneric.repository.DjUserContextAssociation;
42 import com.genimen.djeneric.repository.exceptions.DjenericException;
43 import com.genimen.djeneric.repository.exceptions.LogonException;
44
45 public class RdbmsContextManager extends DjContextManager
46 {
47   public RdbmsContextManager(DjPersistenceManager mgr)
48   {
49     super(mgr);
50   }
51
52   public DjContext[] getContexts() throws DjenericException
53   {
54     try
55     {
56       ArrayList JavaDoc result = new ArrayList JavaDoc();
57       RdbmsSession session = (RdbmsSession) getPersistenceManager().createSession();
58       try
59       {
60         SqlStatement stmt = session.getInternalSqlStatement("select id, code, name from "
61                                                             + RdbmsPersistenceManager.CONTEXT_TABLE + " order by name");
62         ResultSet JavaDoc rs = stmt.executeQuery();
63         while (rs.next())
64         {
65           RdbmsContext ctxt = new RdbmsContext(getPersistenceManager(), rs.getLong("id"), rs.getString("code"), rs
66               .getString("name"));
67           result.add(ctxt);
68         }
69         rs.close();
70         stmt.close();
71       }
72       finally
73       {
74         session.close();
75       }
76       return (DjContext[]) result.toArray(new DjContext[0]);
77     }
78     catch (SQLException JavaDoc x)
79     {
80       throw new DjenericException(x);
81     }
82   }
83
84   public DjUser getUser(String JavaDoc userCode) throws DjenericException
85   {
86     if(userCode == null) userCode = "null";
87     
88     DjUser result = null;
89     try
90     {
91       RdbmsSession session = (RdbmsSession) getPersistenceManager().createSession();
92       try
93       {
94         SqlStatement stmt = session
95             .getInternalSqlStatement("select id, code, name, password, administrator_yn, modeler_yn " + "from "
96                                      + RdbmsPersistenceManager.USER_TABLE + " dev " + "where code = :devCode ");
97         stmt.setString("devCode", userCode.toLowerCase());
98         ResultSet JavaDoc rs = stmt.executeQuery();
99         if (rs.next())
100         {
101           result = new RdbmsUser(getPersistenceManager(), rs.getLong("id"), rs.getString("code"), rs.getString("name"),
102               rs.getString("password"), "Y".equalsIgnoreCase(rs.getString("administrator_yn")), "Y".equalsIgnoreCase(rs
103                   .getString("modeler_yn")));
104         }
105         rs.close();
106         stmt.close();
107       }
108       finally
109       {
110         session.close();
111       }
112
113       if (result == null) throw new LogonException(Messages.getString("RdbmsContextManager.noAccess", userCode
114           .toLowerCase(), getPersistenceManager().getCurrentRepository()));
115
116       return result;
117     }
118     catch (SQLException JavaDoc x)
119     {
120       throw new DjenericException(x);
121     }
122   }
123
124   public DjUser[] getUsers() throws DjenericException
125   {
126     try
127     {
128       ArrayList JavaDoc result = new ArrayList JavaDoc();
129       RdbmsSession session = (RdbmsSession) getPersistenceManager().createSession();
130       try
131       {
132         SqlStatement stmt = session
133             .getInternalSqlStatement("select id, code, name, password, administrator_yn, modeler_yn from "
134                                      + RdbmsPersistenceManager.USER_TABLE + " order by code");
135         ResultSet JavaDoc rs = stmt.executeQuery();
136         while (rs.next())
137         {
138           DjUser dev = new RdbmsUser(getPersistenceManager(), rs.getLong("id"), rs.getString("code"), rs
139               .getString("name"), rs.getString("password"), "Y".equalsIgnoreCase(rs.getString("administrator_yn")), "Y"
140               .equalsIgnoreCase(rs.getString("modeler_yn")));
141           result.add(dev);
142         }
143         rs.close();
144         stmt.close();
145       }
146       finally
147       {
148         session.close();
149       }
150       return (DjUser[]) result.toArray(new DjUser[0]);
151     }
152     catch (SQLException JavaDoc x)
153     {
154       throw new DjenericException(x);
155     }
156   }
157
158   public DjUserContextAssociation createNewUserContextAssociation()
159   {
160     RdbmsUserContextAssociation c = new RdbmsUserContextAssociation();
161     return c;
162   }
163
164   public DjContext createNewContext()
165   {
166     return new RdbmsContext(getPersistenceManager());
167   }
168
169   public DjUser createNewUser()
170   {
171     return new RdbmsUser(getPersistenceManager());
172   }
173
174 }
Popular Tags