Tidyup and remove whitespace at end of line
[remote/remote-ws.git] / src / moteaccess / AbstractMoteAccess.java
blobb041fa737725333565121fd1ddaec4f4835a7509
1 package moteaccess;
3 import java.sql.ResultSet;
4 import database.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;
28 Exception ex = 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 } catch (Exception e)
55 ex = e;
56 } finally {
57 sql.closeDB();
58 if (ex != null) throw ex;
60 return access;
63 /**
64 * This method must be implemented to integrate a custom reservation system.
65 * It is also a point for integration between user account system and reservation system.
66 * @param mote_id Database key for the mote to check
67 * @param session_id Database key for the session requesting access. Note that
68 * the user model should specify the relevant reservation key upon session authentication.
69 * The details of how to do this are application specific.
70 * @return true if there is a current reservation, false otherwise
72 abstract protected boolean hasReservation(long mote_id, String session_id) throws Exception;