1 25 26 29 package net.killingar.forum.internal.managers; 30 31 import net.killingar.forum.internal.*; 32 33 import java.sql.*; 34 import java.util.ArrayList ; 35 36 public final class PlanningManager extends AbstractManager implements java.io.Serializable 37 { 38 42 public void addEvent(Event event) throws SQLException, AccessDeniedException 43 { 44 manager.checkMyAccess(AccessLevel.addEvent); 45 boolean denied = true; 46 if (event.groupID != -1) 48 { 49 Group groups[] = manager.getGroupsOfUser(manager.getUserID()); 50 for (int i = 0; i < groups.length; i++) 51 { 52 if (groups[i].ID == event.groupID) 53 { 54 denied = false; 55 break; 56 } 57 } 58 } 59 60 if (denied) 62 manager.checkMyAccess(AccessLevel.addGlobalEvent); 63 64 Connection c = null; 65 PreparedStatement statement = null; 66 ResultSet result = null; 67 68 try 69 { 70 c = getNewConnection(); 71 statement = c.prepareStatement("insert into Events (User, UserGroup, Name, Description, Time, Created, LastChanged, LastChangedUser) values(?, ?, ?, ?, ?, NOW(), NOW(), ?)"); 72 statement.setLong(1, manager.getUserID()); 73 statement.setLong(2, event.groupID); 74 statement.setString(3, event.name); 75 statement.setString(4, event.description); 76 statement.setTimestamp(5, event.time); 77 statement.setLong(6, manager.getUserID()); 78 79 statement.executeUpdate(); 80 } 81 finally { closeAll(c, statement, result); } 82 } 83 84 87 public void changeEvent(Event event, boolean clear) throws SQLException, AccessDeniedException 88 { 89 if (getEvent(event.ID).ownerID != manager.getUserID()) 90 manager.checkMyAccess(AccessLevel.changeEvent); 91 Connection c = null; 92 PreparedStatement statement = null; 93 ResultSet result = null; 94 95 if ("".equals(event.description)) 96 event.description = null; 97 98 try 99 { 100 c = getNewConnection(); 101 statement = c.prepareStatement("update Events set Name = ?, Description = ?, UserGroup = ?, Frozen = ?, Time = ?, LastChangedUser = ?, LastChanged = NOW() where ID = ?"); 102 statement.setString(1, event.name); 103 statement.setString(2, event.description); 104 statement.setLong(3, event.groupID); 105 statement.setBoolean(4, event.frozen); 106 statement.setTimestamp(5, event.time); 107 statement.setLong(6, manager.getUserID()); 108 statement.setLong(7, event.ID); 109 110 statement.executeUpdate(); 111 if (clear) 112 statement.executeUpdate("delete from EventData where event = "+event.ID); 113 } 114 finally { closeAll(c, statement, result); } 115 } 116 117 120 public void removeEvent(long eventID) throws SQLException, AccessDeniedException 121 { 122 if (getEvent(eventID).ownerID != manager.getUserID()) 123 manager.checkMyAccess(AccessLevel.removeEvent); 124 125 Connection c = null; 126 Statement statement = null; 127 ResultSet result = null; 128 129 try 130 { 131 c = getNewConnection(); 132 statement = c.createStatement(); 133 134 137 statement.executeUpdate("update Events set Visible = 0, Frozen = 1 where ID = " + eventID); 138 } 139 finally { closeAll(c, statement, result); } 140 } 141 142 145 public boolean isUnreadPlanning() throws ClassNotFoundException , InstantiationException , IllegalAccessException , SQLException 146 { 147 Connection c = null; 148 Statement statement = null; 149 ResultSet result = null; 150 151 try 152 { 153 c = getNewConnection(); 154 statement = c.createStatement(); 155 156 result = statement.executeQuery( 158 "select "+ 159 "MAX(EventData.UserTime) "+ 160 "from "+ 161 "EventData, Events "+ 162 "where "+ 163 "Events.Visible = 1 AND "+ 164 "Events.ID = EventData.Event AND "+ 165 "(Events.Usergroup is null"+manager.getGroupsString("Events.UserGroup", " OR ")+") "); 166 167 boolean r = false; 169 if (result.next() && result.getTimestamp(1).after(getTimeOnPlanning())) 170 r = true; 171 return r; 172 } 173 finally { closeAll(c, statement, result); } 174 } 175 176 179 public Timestamp getTimeOnPlanning() throws ClassNotFoundException , InstantiationException , IllegalAccessException , SQLException 180 { 181 return ((TimeManager)manager.getManager(TimeManager.class.getName())).getUserTime(TimeManager.systemPlanning); 182 } 183 184 187 public Event[] getEvents() throws SQLException 188 { 189 String groupsString; 190 { 191 Group groups[] = manager.getGroupsOfUser(manager.getUserID()); 192 if (groups.length > 0) 193 { 194 StringBuffer s = new StringBuffer ("where Visible = 1 AND ("); 195 for (int i = 0; i < groups.length; i++) 196 { 197 if (i != 0) 198 s.append(" OR "); 199 s.append(" UserGroup = "); 200 s.append(groups[i].ID); 201 } 202 203 s.append(" OR UserGroup = -1 OR UserGroup is null)"); 204 groupsString = s.toString(); 205 } 206 else 207 groupsString = "where Visible = 1"; 208 } 209 210 Connection c = null; 211 Statement statement = null; 212 ResultSet result = null; 213 214 try 215 { 216 c = getNewConnection(); 217 statement = c.createStatement(); 218 219 result = statement.executeQuery("select * from Events "+groupsString+" order by Time asc"); 220 ArrayList v = new ArrayList (); 221 while (result.next()) 222 { 223 v.add(new Event( 224 result.getLong("ID"), 225 result.getLong("User"), 226 result.getLong("UserGroup"), 227 result.getString("Name"), 228 result.getString("Description"), 229 result.getTimestamp("Time"), 230 result.getTimestamp("Created"), 231 result.getInt("Frozen") == 1)); 232 } 233 234 Event r[] = new Event[v.size()]; 235 v.toArray(r); 236 return r; 237 } 238 finally { closeAll(c, statement, result); } 239 } 240 241 244 public Event getEvent(long eventID) throws SQLException 245 { 246 Connection c = null; 247 Statement statement = null; 248 ResultSet result = null; 249 250 try 251 { 252 c = getNewConnection(); 253 statement = c.createStatement(); 254 255 result = statement.executeQuery("select * from Events where ID = "+eventID); 256 Event r = null; 257 if (result.next()) 258 { 259 r = new Event( 260 eventID, 261 result.getLong("User"), 262 result.getLong("UserGroup"), 263 result.getString("Name"), 264 result.getString("Description"), 265 result.getTimestamp("Time"), 266 result.getTimestamp("Created"), 267 result.getInt("Frozen") == 1); 268 } 269 270 return r; 271 } 272 finally { closeAll(c, statement, result); } 273 } 274 275 278 public EventData getEventData(long eventID, long userID) throws SQLException 279 { 280 Connection c = null; 281 Statement statement = null; 282 ResultSet result = null; 283 284 try 285 { 286 c = getNewConnection(); 287 statement = c.createStatement(); 288 289 result = statement.executeQuery("select * from EventData where Event = "+eventID+" AND User = "+userID); 290 EventData r = null; 291 if (result.next() && result.getString(1) != null) 292 r = new EventData(eventID, userID, result.getTimestamp("UserTime"), result.getInt("Data")); 293 return r; 294 } 295 finally { closeAll(c, statement, result); } 296 } 297 298 301 public void setEventData(long userID, long eventID, int data) throws SQLException, AccessDeniedException 302 { 303 if (userID != manager.getUserID()) 304 manager.checkMyAccess(AccessLevel.setEventData); 305 if (getEvent(eventID).frozen) 306 throw new AccessDeniedException("attempt to change data in a frozen event"); 307 308 Connection c = null; 309 PreparedStatement statement = null; 310 ResultSet result = null; 311 312 try 313 { 314 c = getNewConnection(); 315 316 statement = c.prepareStatement("delete from EventData where Event = ? and User = ?"); 317 statement.setLong(1, eventID); 318 statement.setLong(2, userID); 319 statement.executeUpdate(); 320 statement.close(); 321 322 statement = c.prepareStatement("insert into EventData (Event, User, Data, UserTime) values(?, ?, ?, NOW())"); 323 statement.setLong(1, eventID); 324 statement.setLong(2, userID); 325 statement.setInt(3, data); 326 statement.executeUpdate(); 327 } 328 finally { closeAll(c, statement, result); } 329 } 330 331 334 public void setReadPlanning() throws Exception 335 { 336 ((TimeManager)manager.getManager(TimeManager.class.getName())).setUserTime(TimeManager.systemPlanning); 337 } 338 } | Popular Tags |