1 2 package com.espada.bugtracker.app; 3 4 import java.io.*; 6 import java.sql.*; 7 import java.util.*; 8 9 import com.espada.*; 11 12 import com.espada.bugtracker.persistence.*; 14 15 27 28 public class Project 29 { 30 31 private int pid; 32 33 private String pname; 34 35 36 public Project(int prid) 37 { 38 try 39 { 40 41 Connection d = DatabaseConnectionPool.getConnection(); 42 Statement st = d.createStatement(); 43 ResultSet rs = st.executeQuery("select * from projects where pid=" + prid); 44 45 while (rs.next()) 46 { 47 48 pid=rs.getInt(1); 49 pname=rs.getString(2); 50 51 } 52 53 st.close(); 54 DatabaseConnectionPool.freeConnection(d); 55 } 56 57 catch (Exception E) 58 { 59 60 62 } 63 64 } 66 public Vector getProjectManagers() 67 { 68 69 Vector v = new Vector(); 70 try { 71 Connection d = DatabaseConnectionPool.getConnection(); 72 Statement st = d.createStatement(); 73 ResultSet rs = st.executeQuery("select uid from projectuser where pid=" + pid + " and rid=2"); 74 while (rs.next()) 75 { 76 77 v.add( new User(rs.getInt(1)) ); 78 } 79 80 st.close(); 81 DatabaseConnectionPool.freeConnection(d); 82 } 83 84 catch (Exception E) 85 { 86 87 89 } 90 91 return v; 92 93 } 94 95 public String getRoleID(int uid) 96 { 97 98 return getRole("select rid from projectuser where uid = " + uid + " and pid=" + pid); 99 100 } 102 public String getRoleName(String uid) 103 { 104 105 String s=null; 106 try 107 { 108 s = getRole("select roleDesc from roles, projectuser where projectuser.rid = roles.rid and projectuser.uid = " + uid + " and pid=" + pid); 109 } catch (NullPointerException npex) 110 { 111 112 } 113 finally 114 { 115 if (s==null) 116 s="-- Not Assigned --"; 117 } 118 return s; 119 120 121 } 123 private String getRole(String SQL) 124 { 125 126 String v = null; 127 128 try 129 { 130 131 Connection d = DatabaseConnectionPool.getConnection(); 132 Statement st = d.createStatement(); 133 ResultSet rs = st.executeQuery(SQL); 134 135 while (rs.next()) 136 { 137 138 v = rs.getString( 1 ); 139 140 } 141 142 st.close(); 143 DatabaseConnectionPool.freeConnection(d); 144 145 } 146 147 catch (Exception E) 148 { 149 150 151 } 152 return v; 153 154 } 156 157 public boolean delete() 158 { 159 160 try 161 { 162 163 Connection d = DatabaseConnectionPool.getConnection(); 164 Statement st = d.createStatement(); 165 int rs = st.executeUpdate("delete from projects where pid=" + pid); 166 st.close(); 167 DatabaseConnectionPool.freeConnection(d); 168 return (rs != 0); 169 170 } 171 172 catch (Exception E) 173 { 174 175 return false; 176 177 } 178 179 } 181 182 public static Vector getProjects() 183 { 184 185 return getProjectsDef("select * from projects order by pid"); 186 187 } 189 190 public static Vector getProjectsByUser(int uid) 191 { 192 193 return getProjectsDef("select projects.* from projects, projectuser where projectuser.uid=" + uid + " and projectuser.pid=projects.pid order by projects.pid"); 194 195 } 197 198 public static Vector getProjectsByUserRole(int uid,int rid) 199 { 200 201 return getProjectsDef("select projects.pid from projects,projectuser where projectuser.uid=" + uid + " and projectuser.rid=" + rid + " and projectuser.pid=projects.pid"); 202 203 } 205 206 private static Vector getProjectsDef(String SQL) 207 { 208 209 Vector v = new Vector(); 210 211 try 212 { 213 214 Connection d = DatabaseConnectionPool.getConnection(); 215 Statement st = d.createStatement(); 216 ResultSet rs = st.executeQuery(SQL); 217 218 while (rs.next()) 219 { 220 v.add( new Project( rs.getInt( 1 ) ) ); 221 } 222 223 st.close(); 224 DatabaseConnectionPool.freeConnection(d); 225 226 return v; 227 } 228 229 catch (Exception E) 230 { 231 232 E.printStackTrace(); 233 return v; 234 235 } 236 237 } 239 240 public int getPID() 241 { 242 243 return pid; 244 245 } 247 248 public String getName() 249 { 250 251 return pname; 252 253 } 255 256 public static Project createProject(String pn,int uid) 257 throws CannotCreateProjectException 258 { 259 260 Project p = null; 261 262 try 263 { 264 265 Connection d = DatabaseConnectionPool.getConnection(); 266 267 try 268 { 269 270 Statement st = d.createStatement(); 271 st.executeQuery("insert into projects values(0, '" + pn + "')"); 272 ResultSet rs = st.executeQuery("select pid from projects where pname='" + pn + "'"); 273 274 int pd = 0; 275 276 while(rs.next()) 277 { 278 279 pd = rs.getInt(1); 280 p = new Project(pd); 281 282 } 283 284 286 287 288 289 String SQL = "insert into projectuser select " + pd + " , " + uid + " , 1 from user"; 290 291 st.executeQuery(SQL); 292 st.close(); 293 294 } 295 296 finally 297 { 298 299 if(d!=null) 300 { 301 302 DatabaseConnectionPool.freeConnection(d); 303 } 304 305 } 306 } 307 308 catch(Exception e) 309 { 310 311 throw new CannotCreateProjectException(pn,e); 312 313 } 314 315 return p; 316 317 } 319 320 } | Popular Tags |