KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bluecubs > xinco > core > server > XincoCoreUserServer


1 /**
2  *Copyright 2004 blueCubs.com
3  *
4  *Licensed under the Apache License, Version 2.0 (the "License");
5  *you may not use this file except in compliance with the License.
6  *You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *Unless required by applicable law or agreed to in writing, software
11  *distributed under the License is distributed on an "AS IS" BASIS,
12  *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *See the License for the specific language governing permissions and
14  *limitations under the License.
15  *
16  *************************************************************
17  * This project supports the blueCubs vision of giving back
18  * to the community in exchange for free software!
19  * More information on: http://www.bluecubs.org
20  *************************************************************
21  *
22  * Name: XincoCoreUserServer
23  *
24  * Description: user
25  *
26  * Original Author: Alexander Manes
27  * Date: 2004
28  *
29  * Modifications:
30  *
31  * Who? When? What?
32  * - - -
33  *
34  *************************************************************
35  */

36
37 package com.bluecubs.xinco.core.server;
38
39 import java.sql.*;
40 import java.util.Vector JavaDoc;
41
42 import com.bluecubs.xinco.core.*;
43
44 public class XincoCoreUserServer extends XincoCoreUser {
45     
46     private void fillXincoCoreGroups(XincoDBManager DBM) throws XincoException {
47
48         setXinco_core_groups(new Vector JavaDoc());
49
50         try {
51             Statement stmt = DBM.con.createStatement();
52             ResultSet rs = stmt.executeQuery("SELECT * FROM xinco_core_user_has_xinco_core_group WHERE xinco_core_user_id=" + getId());
53             while (rs.next()) {
54                 getXinco_core_groups().addElement(new XincoCoreGroupServer(rs.getInt("xinco_core_group_id"), DBM));
55             }
56             stmt.close();
57         } catch (Exception JavaDoc e) {
58             getXinco_core_groups().removeAllElements();
59             throw new XincoException();
60         }
61
62     }
63     
64     private void writeXincoCoreGroups(XincoDBManager DBM) throws XincoException {
65
66         Statement stmt;
67
68         try {
69             stmt = DBM.con.createStatement();
70             stmt.executeUpdate("DELETE FROM xinco_core_user_has_xinco_core_group WHERE xinco_core_user_id=" + getId());
71             stmt.close();
72     
73             for (int i=0; i<getXinco_core_groups().size(); i++) {
74                 stmt = DBM.con.createStatement();
75                 stmt.executeUpdate("INSERT INTO xinco_core_user_has_xinco_core_group VALUES (" + getId() + ", " + ((XincoCoreGroupServer)getXinco_core_groups().elementAt(i)).getId() + ", " + 1 + ")");
76                 stmt.close();
77             }
78         } catch (Exception JavaDoc e) {
79             throw new XincoException();
80         }
81         
82     }
83     
84     //create user object and login
85
public XincoCoreUserServer(String JavaDoc attrUN, String JavaDoc attrUPW, XincoDBManager DBM) throws XincoException {
86
87         try {
88             
89             Statement stmt = DBM.con.createStatement();
90             ResultSet rs = stmt.executeQuery("SELECT * FROM xinco_core_user WHERE username='" + attrUN + "' AND userpassword=MD5('" + attrUPW + "') AND status_number=1");
91
92             //throw exception if no result found
93
int RowCount = 0;
94             while (rs.next()) {
95                 RowCount++;
96                 setId(rs.getInt("id"));
97                 setUsername(rs.getString("username"));
98                 setUserpassword(rs.getString("userpassword"));
99                 setName(rs.getString("name"));
100                 setFirstname(rs.getString("firstname"));
101                 setEmail(rs.getString("email"));
102                 setStatus_number(rs.getInt("status_number"));
103             }
104             if (RowCount < 1) {
105                 throw new XincoException();
106             }
107
108             stmt.close();
109             
110             fillXincoCoreGroups(DBM);
111
112         } catch (Exception JavaDoc e) {
113             if (getXinco_core_groups() != null) {
114                 getXinco_core_groups().removeAllElements();
115             }
116             throw new XincoException();
117         }
118         
119     }
120     
121     //create user object for data structures
122
public XincoCoreUserServer(int attrID, XincoDBManager DBM) throws XincoException {
123
124         try {
125             
126             Statement stmt = DBM.con.createStatement();
127             ResultSet rs = stmt.executeQuery("SELECT * FROM xinco_core_user WHERE id=" + attrID);
128
129             //throw exception if no result found
130
int RowCount = 0;
131             while (rs.next()) {
132                 RowCount++;
133                 setId(rs.getInt("id"));
134                 setUsername(rs.getString("username"));
135                 setUserpassword(rs.getString("userpassword"));
136                 setName(rs.getString("name"));
137                 setFirstname(rs.getString("firstname"));
138                 setEmail(rs.getString("email"));
139                 setStatus_number(rs.getInt("status_number"));
140             }
141             if (RowCount < 1) {
142                 throw new XincoException();
143             }
144
145             stmt.close();
146             
147             fillXincoCoreGroups(DBM);
148
149         } catch (Exception JavaDoc e) {
150             getXinco_core_groups().removeAllElements();
151             throw new XincoException();
152         }
153         
154     }
155     
156     //create user object
157
public XincoCoreUserServer(int attrID, String JavaDoc attrUN, String JavaDoc attrUPW, String JavaDoc attrN, String JavaDoc attrFN, String JavaDoc attrE, int attrSN, XincoDBManager DBM) throws XincoException {
158
159         try {
160             
161             setId(attrID);
162             setUsername(attrUN);
163             setUserpassword(attrUPW);
164             setName(attrN);
165             setFirstname(attrFN);
166             setEmail(attrE);
167             setStatus_number(attrSN);
168
169             fillXincoCoreGroups(DBM);
170
171         } catch (Exception JavaDoc e) {
172             getXinco_core_groups().removeAllElements();
173             throw new XincoException();
174         }
175         
176     }
177     
178     //write to db
179
public int write2DB(XincoDBManager DBM) throws XincoException {
180
181         try {
182             
183             Statement stmt;
184             
185             if (getId() > 0) {
186                 stmt = DBM.con.createStatement();
187                 stmt.executeUpdate("UPDATE xinco_core_user SET username='" + getUsername().replaceAll("'","\\\\'") + "', userpassword=MD5('" + getUserpassword().replaceAll("'","\\\\'") + "'), name='" + getName().replaceAll("'","\\\\'") + "', firstname='" + getFirstname().replaceAll("'","\\\\'") + "', email='" + getEmail().replaceAll("'","\\\\'") + "', status_number=" + getStatus_number() + " WHERE id=" + getId());
188                 stmt.close();
189             } else {
190                 setId(DBM.getNewID("xinco_core_user"));
191
192                 stmt = DBM.con.createStatement();
193                 stmt.executeUpdate("INSERT INTO xinco_core_user VALUES (" + getId() + ", '" + getUsername().replaceAll("'","\\\\'") + "', MD5('" + getUserpassword().replaceAll("'","\\\\'") + "'), '" + getName().replaceAll("'","\\\\'") + "', '" + getFirstname().replaceAll("'","\\\\'") + "', '" + getEmail().replaceAll("'","\\\\'") + "', " + getStatus_number() + ")");
194                 stmt.close();
195             }
196             
197             writeXincoCoreGroups(DBM);
198             
199             DBM.con.commit();
200             
201         } catch (Exception JavaDoc e) {
202             try {
203                 DBM.con.rollback();
204             } catch (Exception JavaDoc erollback) {
205             }
206             throw new XincoException();
207         }
208         
209         return getId();
210         
211     }
212     
213     //create complete list of users
214
public static Vector JavaDoc getXincoCoreUsers(XincoDBManager DBM) {
215         
216         Vector JavaDoc coreUsers = new Vector JavaDoc();
217         
218         try {
219             
220             Statement stmt = DBM.con.createStatement();
221             ResultSet rs = stmt.executeQuery("SELECT * FROM xinco_core_user ORDER BY username");
222
223             while (rs.next()) {
224                 coreUsers.addElement(new XincoCoreUserServer(rs.getInt("id"), rs.getString("username"), rs.getString("userpassword"), rs.getString("name"), rs.getString("firstname"), rs.getString("email"), rs.getInt("status_number"), DBM));
225             }
226
227             stmt.close();
228             
229         } catch (Exception JavaDoc e) {
230             coreUsers.removeAllElements();
231         }
232
233         return coreUsers;
234     }
235
236 }
237
Popular Tags