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 + ".* "
100 /*+ strOtherTableName + ".*, "
101 + STR_COMM_TABLE_NAME + ".ACCESS "*/
103 + strTableName + ", "
104 + strOtherTableName + ", "
105 + STR_COMM_TABLE_NAME
107 + strTableName + ".ID="
108 + STR_COMM_TABLE_NAME + ".ID_SOURCE"
110 + strOtherTableName + ".ID="
111 + STR_COMM_TABLE_NAME + ".ID_DESTINATION";
112 // Check for strWhere to construct a more complex
113 if (strWhere != null) {
114 strCommand = strCommand + " AND " + strWhere;
116 strCommand = strCommand + ";";
117 RuntimeOutput.print(strCommand, bVerbose);
118 rs = sqlInterface.sqlCommandQuery(strCommand);
119 rsmd = rs.getMetaData();
120 } catch(SQLException ex) {
121 System.out.println("Table: Exception: ");
122 ex.printStackTrace();
126 RuntimeOutput.print("Table: The other table name is not set! Illegal use of this method!", bVerbose);
131 * A method to create a table for IoTRelation and display just the second table
133 * We always base our search on the communication (IoTComm) table
134 * This function is capable of constructing a more complex SQL query
135 * Note: We check here that strOtherTableName is not NULL; this represents
136 * that this use of Table object is for IoTRelation
140 public void selectRelationOnOtherTable() {
142 if (strOtherTableName != null) {
144 String strCommand = "SELECT "/*+ strTableName + ".*, "*/
145 + strOtherTableName + ".* "
146 /*+ STR_COMM_TABLE_NAME + ".ACCESS "*/
148 + strTableName + ", "
149 + strOtherTableName + ", "
150 + STR_COMM_TABLE_NAME
152 + strTableName + ".ID="
153 + STR_COMM_TABLE_NAME + ".ID_SOURCE"
155 + strOtherTableName + ".ID="
156 + STR_COMM_TABLE_NAME + ".ID_DESTINATION";
157 // Check for strWhere to construct a more complex
158 if (strWhere != null) {
159 strCommand = strCommand + " AND " + strWhere;
161 strCommand = strCommand + ";";
162 RuntimeOutput.print(strCommand, bVerbose);
163 rs = sqlInterface.sqlCommandQuery(strCommand);
164 rsmd = rs.getMetaData();
165 } catch(SQLException ex) {
166 System.out.println("Table: Exception: ");
167 ex.printStackTrace();
170 RuntimeOutput.print("Table: The other table name is not set! Illegal use of this method!", bVerbose);
175 * A method to set table name and select entry from a SQL query config file
177 * @param strCfgFileName String config file name for device/entity
179 public void setTableRelationFromQueryFile(String strQueryFileName) {
182 // Parse configuration file
183 // Assumption here is that .config file is written with the correct syntax (need typechecking)
184 File file = new File(strQueryFileName);
185 Scanner scanFile = new Scanner(new FileReader(file));
186 // String for scanning the file
188 while (scanFile.hasNext()) {
189 strScan = scanFile.next();
190 // if this is for IoTSet table
191 if (strScan.equals("SELECT FROM")) {
192 // The next token is definitely the table name
193 strScan = scanFile.next();
194 this.setTableName(strScan);
196 // it means that this is for IoTRelation table
197 if (strScan.equals("FIRST")) {
198 // The next token is definitely the first table name
199 strScan = scanFile.next();
200 this.setTableName(strScan);
202 if (strScan.equals("OTHER")) {
203 // The next token is definitely the other table name
204 strScan = scanFile.next();
205 this.setOtherTableName(strScan);
207 // Scan WHERE for either IoTSet or IoTRelation
208 if (strScan.equals("WHERE")) {
209 // The next token is definitely the WHERE statement
211 String strWhere = scanFile.next();
212 while (!strWhere.equals(";")) {
213 strScan = strScan + " " + strWhere;
214 strWhere = scanFile.next();
216 this.setWhereCondition(strScan);
219 } catch (FileNotFoundException ex) {
220 System.out.println("Table: Exception: ");
221 ex.printStackTrace();
226 * A method to set the other table name
228 * @param strOthTblName String table name that this Table object operates on
231 public void setOtherTableName(String strOthTblName) {
233 strOtherTableName = strOthTblName;
238 * A method to get the other table name
242 public String getOtherTableName() {
244 return strOtherTableName;