7 import java.util.HashMap;
8 import java.util.Scanner;
9 import java.util.Properties;
11 import iotruntime.master.RuntimeOutput;
13 /** A class that extends Table/TableSet class to do table operations on IoTRelation
15 * @author Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
19 public final class TableRelation extends TableSet {
22 * TableRelation class properties
24 protected String strOtherTableName;
27 * Class constructor - for IoTRelation
29 * @param strTblName String of first table name that this Table object operates on
30 * @param strOthTblName String of the other table name that this Table object operates on
31 * @param _bVerbose Verboseness of runtime output
33 public TableRelation(String strTblName, String strOthTblName, boolean _bVerbose) {
35 super(strTblName, _bVerbose);
36 strOtherTableName = strOthTblName;
40 * A method to create a table for IoTRelation - equivalent of selectSetEntry()
42 * We always base our search on the communication (IoTComm) table
43 * This function is capable of constructing a more complex SQL query
44 * Note: We check here that strOtherTableName is not NULL; this represents
45 * that this use of Table object is for IoTRelation
49 public void selectRelationEntry() {
51 if (strOtherTableName != null) {
54 String strCommand = "SELECT " + strTableName + ".*, "
55 + strOtherTableName + ".*, "
56 + STR_COMM_TABLE_NAME + ".ACCESS "
59 + strOtherTableName + ", "
62 + strTableName + ".ID="
63 + STR_COMM_TABLE_NAME + ".ID_SOURCE"
65 + strOtherTableName + ".ID="
66 + STR_COMM_TABLE_NAME + ".ID_DESTINATION";
67 // Check for strWhere to construct a more complex
68 if (strWhere != null) {
69 strCommand = strCommand + " AND " + strWhere;
71 strCommand = strCommand + ";";
72 RuntimeOutput.print(strCommand, bVerbose);
73 rs = sqlInterface.sqlCommandQuery(strCommand);
74 rsmd = rs.getMetaData();
75 } catch(SQLException ex) {
76 System.out.println("Table: Exception: ");
80 RuntimeOutput.print("Table: The other table name is not set! Illegal use of this method!", bVerbose);
85 * A method to create a table for IoTRelation and display just the first table
87 * We always base our search on the communication (IoTComm) table
88 * This function is capable of constructing a more complex SQL query
89 * Note: We check here that strOtherTableName is not NULL; this represents
90 * that this use of Table object is for IoTRelation
94 public void selectRelationOnFirstTable() {
96 if (strOtherTableName != null) {
99 String strCommand = "SELECT " + strTableName + ".* "
101 + strTableName + ", "
102 + strOtherTableName + ", "
103 + STR_COMM_TABLE_NAME
105 + strTableName + ".ID="
106 + STR_COMM_TABLE_NAME + ".ID_SOURCE"
108 + strOtherTableName + ".ID="
109 + STR_COMM_TABLE_NAME + ".ID_DESTINATION";
110 // Check for strWhere to construct a more complex
111 if (strWhere != null) {
112 strCommand = strCommand + " AND " + strWhere;
114 strCommand = strCommand + ";";
115 RuntimeOutput.print(strCommand, bVerbose);
116 rs = sqlInterface.sqlCommandQuery(strCommand);
117 rsmd = rs.getMetaData();
118 } catch(SQLException ex) {
119 System.out.println("Table: Exception: ");
120 ex.printStackTrace();
124 RuntimeOutput.print("Table: The other table name is not set! Illegal use of this method!", bVerbose);
129 * A method to create a table for IoTRelation and display just the second table
131 * We always base our search on the communication (IoTComm) table
132 * This function is capable of constructing a more complex SQL query
133 * Note: We check here that strOtherTableName is not NULL; this represents
134 * that this use of Table object is for IoTRelation
138 public void selectRelationOnOtherTable() {
140 if (strOtherTableName != null) {
142 String strCommand = "SELECT "/*+ strTableName + ".*, "*/
143 + strOtherTableName + ".* "
145 + strTableName + ", "
146 + strOtherTableName + ", "
147 + STR_COMM_TABLE_NAME
149 + strTableName + ".ID="
150 + STR_COMM_TABLE_NAME + ".ID_SOURCE"
152 + strOtherTableName + ".ID="
153 + STR_COMM_TABLE_NAME + ".ID_DESTINATION";
154 // Check for strWhere to construct a more complex
155 if (strWhere != null) {
156 strCommand = strCommand + " AND " + strWhere;
158 strCommand = strCommand + ";";
159 RuntimeOutput.print(strCommand, bVerbose);
160 rs = sqlInterface.sqlCommandQuery(strCommand);
161 rsmd = rs.getMetaData();
162 } catch(SQLException ex) {
163 System.out.println("Table: Exception: ");
164 ex.printStackTrace();
167 RuntimeOutput.print("Table: The other table name is not set! Illegal use of this method!", bVerbose);
172 * A method to set table name and select entry from a SQL query config file
174 * @param strCfgFileName String config file name for device/entity
176 public void setTableRelationFromQueryFile(String strQueryFileName) {
179 // Parse configuration file
180 // Assumption here is that .config file is written with the correct syntax (need typechecking)
181 File file = new File(strQueryFileName);
182 Scanner scanFile = new Scanner(new FileReader(file));
183 // String for scanning the file
185 while (scanFile.hasNext()) {
186 strScan = scanFile.next();
187 // if this is for IoTSet table
188 if (strScan.equals("SELECT FROM")) {
189 // The next token is definitely the table name
190 strScan = scanFile.next();
191 this.setTableName(strScan);
193 // it means that this is for IoTRelation table
194 if (strScan.equals("FIRST")) {
195 // The next token is definitely the first table name
196 strScan = scanFile.next();
197 this.setTableName(strScan);
199 if (strScan.equals("OTHER")) {
200 // The next token is definitely the other table name
201 strScan = scanFile.next();
202 this.setOtherTableName(strScan);
204 // Scan WHERE for either IoTSet or IoTRelation
205 if (strScan.equals("WHERE")) {
206 // The next token is definitely the WHERE statement
208 String strWhere = scanFile.next();
209 while (!strWhere.equals(";")) {
210 strScan = strScan + " " + strWhere;
211 strWhere = scanFile.next();
213 this.setWhereCondition(strScan);
216 } catch (FileNotFoundException ex) {
217 System.out.println("Table: Exception: ");
218 ex.printStackTrace();
223 * A method to set the other table name
225 * @param strOthTblName String table name that this Table object operates on
228 public void setOtherTableName(String strOthTblName) {
230 strOtherTableName = strOthTblName;
235 * A method to get the other table name
239 public String getOtherTableName() {
241 return strOtherTableName;