1 36 37 package com.bluecubs.xinco.core.server; 38 39 import java.sql.*; 40 import java.util.Vector ; 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 ()); 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 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 e) { 79 throw new XincoException(); 80 } 81 82 } 83 84 public XincoCoreUserServer(String attrUN, String 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 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 e) { 113 if (getXinco_core_groups() != null) { 114 getXinco_core_groups().removeAllElements(); 115 } 116 throw new XincoException(); 117 } 118 119 } 120 121 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 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 e) { 150 getXinco_core_groups().removeAllElements(); 151 throw new XincoException(); 152 } 153 154 } 155 156 public XincoCoreUserServer(int attrID, String attrUN, String attrUPW, String attrN, String attrFN, String 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 e) { 172 getXinco_core_groups().removeAllElements(); 173 throw new XincoException(); 174 } 175 176 } 177 178 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 e) { 202 try { 203 DBM.con.rollback(); 204 } catch (Exception erollback) { 205 } 206 throw new XincoException(); 207 } 208 209 return getId(); 210 211 } 212 213 public static Vector getXincoCoreUsers(XincoDBManager DBM) { 215 216 Vector coreUsers = new Vector (); 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 e) { 230 coreUsers.removeAllElements(); 231 } 232 233 return coreUsers; 234 } 235 236 } 237 | Popular Tags |