Cleanup exception handling
[remote/remote-ws.git] / src / moteaccess / AbstractMoteAccess.java
blob2b84905de231e950a88bc392c50b8c6024f3ec24
1 package moteaccess;
3 import java.sql.ResultSet;
4 import util.SQLHelper;
6 /**
7 * @author zept
8 * This is an abstract class with the generic code to assign mote usage privileges.
9 * To integrate any custom reservation system, this class must be inherited and the
10 * abstract methods implemented.
12 public abstract class AbstractMoteAccess {
14 public boolean[] getMoteControlPrivileges(long[] mote_ids, String session_id) throws Exception
16 boolean[] results = new boolean[mote_ids.length];
17 for (int i=0; i<mote_ids.length;i++)
19 results[i] = getMoteControlPrivilege(mote_ids[i],session_id);
21 return results;
24 protected boolean getMoteControlPrivilege(long mote_id, String session_id) throws Exception
26 boolean access = false;
27 SQLHelper sql = null;
29 try {
30 // Check for current privileged session
31 String query = "select priv_session_id from mote " +
32 "where id="+mote_id;
33 sql = new SQLHelper();
34 sql.openDB();
35 ResultSet sqlres = sql.retrieve(query);
36 if (sqlres.next()){
37 String privsess = sqlres.getString("priv_session_id");
38 if (privsess == null || privsess.compareTo(session_id) == 0) {
39 access = true;
40 } else {
41 // check privileges with reservation system
42 if (hasReservation(mote_id,session_id))
44 access = true;
48 if (access) {
49 query = "update mote set priv_session_id="+session_id+
50 " where id="+mote_id;
51 sql.execute(query);
53 return access;
55 } finally {
56 sql.closeDB();
60 /**
61 * This method must be implemented to integrate a custom reservation system.
62 * It is also a point for integration between user account system and reservation system.
63 * @param mote_id Database key for the mote to check
64 * @param session_id Database key for the session requesting access. Note that
65 * the user model should specify the relevant reservation key upon session authentication.
66 * The details of how to do this are application specific.
67 * @return true if there is a current reservation, false otherwise
69 abstract protected boolean hasReservation(long mote_id, String session_id) throws Exception;