1 40 package org.dspace.core; 41 42 import java.sql.Connection ; 43 import java.sql.SQLException ; 44 import java.util.ArrayList ; 45 import java.util.HashMap ; 46 import java.util.Iterator ; 47 import java.util.List ; 48 import java.util.Map ; 49 50 import org.apache.log4j.Logger; 51 import org.dspace.eperson.EPerson; 52 import org.dspace.eperson.Group; 53 import org.dspace.storage.rdbms.DatabaseManager; 54 55 73 public class Context 74 { 75 private static final Logger log = Logger.getLogger(Context.class); 76 77 78 private Connection connection; 79 80 81 private EPerson currentUser; 82 83 84 private String extraLogInfo; 85 86 87 private boolean ignoreAuth; 88 89 90 private Map objectCache; 91 92 93 private List specialGroups; 94 95 102 public Context() throws SQLException 103 { 104 connection = DatabaseManager.getConnection(); 106 connection.setAutoCommit(false); 107 108 currentUser = null; 109 extraLogInfo = ""; 110 ignoreAuth = false; 111 112 objectCache = new HashMap (); 113 specialGroups = new ArrayList (); 114 } 115 116 121 public Connection getDBConnection() 122 { 123 return connection; 124 } 125 126 134 public void setCurrentUser(EPerson user) 135 { 136 currentUser = user; 137 } 138 139 145 public EPerson getCurrentUser() 146 { 147 return currentUser; 148 } 149 150 156 public boolean ignoreAuthorization() 157 { 158 return ignoreAuth; 159 } 160 161 169 public void setIgnoreAuthorization(boolean b) 170 { 171 ignoreAuth = b; 172 } 173 174 184 public void setExtraLogInfo(String info) 185 { 186 extraLogInfo = info; 187 } 188 189 195 public String getExtraLogInfo() 196 { 197 return extraLogInfo; 198 } 199 200 209 public void complete() throws SQLException 210 { 211 try 214 { 215 connection.commit(); 217 } 218 finally 219 { 220 DatabaseManager.freeConnection(connection); 222 connection = null; 223 } 224 } 225 226 234 public void commit() throws SQLException 235 { 236 connection.commit(); 238 } 239 240 247 public void abort() 248 { 249 try 250 { 251 connection.rollback(); 252 } 253 catch (SQLException se) 254 { 255 log.error(se.getMessage()); 256 se.printStackTrace(); 257 } 258 finally 259 { 260 DatabaseManager.freeConnection(connection); 261 connection = null; 262 } 263 } 264 265 272 public boolean isValid() 273 { 274 return (connection != null); 276 } 277 278 289 public Object fromCache(Class objectClass, int id) 290 { 291 String key = objectClass.getName() + id; 292 293 return objectCache.get(key); 294 } 295 296 304 public void cache(Object o, int id) 305 { 306 String key = o.getClass().getName() + id; 307 objectCache.put(key, o); 308 } 309 310 318 public void removeCached(Object o, int id) 319 { 320 String key = o.getClass().getName() + id; 321 objectCache.remove(key); 322 } 323 324 330 public void setSpecialGroup(int groupID) 331 { 332 specialGroups.add(new Integer (groupID)); 333 334 } 336 337 344 public boolean inSpecialGroup(int groupID) 345 { 346 if (specialGroups.contains(new Integer (groupID))) 347 { 348 return true; 350 } 351 352 return false; 353 } 354 355 362 public Group[] getSpecialGroups() throws SQLException 363 { 364 List myGroups = new ArrayList (); 365 366 Iterator i = specialGroups.iterator(); 367 368 while (i.hasNext()) 369 { 370 myGroups.add(Group.find(this, ((Integer ) i.next()).intValue())); 371 } 372 373 return (Group[]) myGroups.toArray(new Group[0]); 374 } 375 376 protected void finalize() 377 { 378 382 if (connection != null) 383 { 384 abort(); 385 } 386 } 387 } 388 | Popular Tags |