More features for Android app access to device installation, etc.
[iot2.git] / iotjava / iotinstaller / IoTInstaller.java
1 package iotinstaller;
2
3 import iotinstaller.MySQLInterface;
4 import iotinstaller.TableProperty;
5 import iotinstaller.Table;
6
7 import java.io.*;
8 import java.sql.*;
9 import java.util.Scanner;
10 import java.util.Properties;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Set;
15
16 /** A class that creates an object for IoT device/entity installation into database
17  *
18  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
19  * @version     1.0
20  * @since       2015-12-01
21  */
22 public final class IoTInstaller {
23
24         /**
25          * IoTInstaller class properties
26          */
27         private Table tbl;
28
29         /**
30          * IoTInstaller class constants
31          */
32         private static final String STR_MAIN_TABLE_NAME = "IoTMain";
33         private static final String STR_COMM_TABLE_NAME = "IoTComm";
34         private static final String STR_HOST_TABLE_NAME = "IoTComputeNode";
35         private static final String STR_ADDRESS_TABLE_NAME = "IoTAddress";
36         private static final String STR_DEV_ADD_TABLE_NAME = "IoTDeviceAddress";
37         private static final String STR_ZB_ADD_TABLE_NAME = "IoTZigbeeAddress";
38         private static final int INT_NUM_COMM_FIELDS = 5;
39         private static final int INT_NUM_HOST_FIELDS = 3;
40
41         private static final String STR_INSTALL_ENTITY_CMD = "-install_ent";
42         private static final String STR_INSTALL_COMMUNICATION_CMD = "-install_comm";
43         private static final String STR_INSTALL_COMPLETE_CMD = "-install_comp";
44         private static final String STR_INSTALL_ADDRESS_CMD = "-install_add";
45         private static final String STR_INSTALL_DEV_ADDRESS_CMD = "-install_dev_add";
46         private static final String STR_INSTALL_ZB_ADDRESS_CMD = "-install_zb_add";
47         private static final String STR_INSTALL_HOST_CMD = "-install_host";
48         private static final String STR_DELETE_COMMUNICATION_CMD = "-delete_comm";      
49         private static final String STR_DELETE_ENTITY_CMD = "-delete_ent";
50         private static final String STR_DELETE_ADDRESS_CMD = "-delete_add";
51         private static final String STR_DELETE_DEV_ADD_CMD = "-delete_dev_add";
52         private static final String STR_DELETE_ZB_ADD_CMD = "-delete_zb_add";
53         private static final String STR_DELETE_HOST_CMD = "-delete_host";
54         private static final String STR_HELP_CMD = "-help";
55         private static final String DATABASE_GET = "-get";
56         private static final String APPEND_TYPE = "-appendType";
57         private static final String UPDATE_SETLIST = "-updateSetList";
58         private static final String UPDATE_IOTDEVICEADDRESS = "-updateIoTDeviceAddress";
59         private static final String UPDATE_IDSUBTYPE = "-updateIDSubtype";
60
61         /**
62          * Class constructor
63          */
64         public IoTInstaller() {
65
66                 // Make this not verbose by default
67                 tbl = new Table(false);
68                 System.out.println("IoTInstaller: Initializing installation..");
69         }
70
71         /**
72          * A method to insert a new entry to the main table (IoTMain)
73          * <p>
74          * This entry can be a new device or a new entity that we should keep track about
75          * A new entry will need a unique ID and a type name from the driver
76          *
77          * @param  strID    string ID to insert device into the main table
78          * @param  strType  string type to insert device into the main table
79          * @return          void
80          */
81         private void insertMainDBEntry(String strID, String strType) {
82
83                 // Creating String array
84                 String[] strFlds = new String[2];
85                 for(int i=0; i<2; i++) {
86                         strFlds[i] = new String();
87                 }
88                 strFlds[0] = strID;
89                 strFlds[1] = strType;
90
91                 // Insert entry through Table object
92                 tbl.setTableName(STR_MAIN_TABLE_NAME);
93                 tbl.insertEntry(strFlds);
94                 System.out.println("IoTInstaller: Inserting a new entry into main table");
95         }
96         
97         /**
98          * A method to extract device/entity information from the user
99          * <p>
100          * Users are supposed to supply the information needed for installation
101          * This is the core of the operation after getting the Scanner object.
102          *
103          * @param  scanFile     Scanner object of source
104          * @return              void
105          */
106         public void extractTableAndInstallCore(Scanner scanFile) {
107
108                 // Parse configuration file
109                 // Assumption here is that .config file is written with the correct syntax (need typechecking)
110                 // Initialize String for ID and TYPE
111                 String strID = "";
112                 String strType = "";
113                 String strTypeSpecific = "";
114
115                 // Initialize TableProperty for devices and specific devices
116                 // We have 2 tables,
117                 // e.g. ProximitySensor - table of many ProximitySensor devices
118                 //      ProximitySensorBrandA - table that contains the constructor
119                 //                              information for a specific device
120                 TableProperty[] tpDevice = new TableProperty[1];
121                 TableProperty[] tpDeviceSpecific = new TableProperty[1];
122
123                 // Initialize array of string
124                 String[] strFields = new String[1];
125                 String[] strFieldsSpecific = new String[1];
126
127                 // String for scanning the file
128                 String strScan = "";
129
130                 // Store number of fields here
131                 int iFields = 0;
132                 while (scanFile.hasNext()) {
133
134                         strScan = scanFile.next();
135                         if (strScan.equals("IoTMain")) {
136
137                                 while (scanFile.hasNext()) {
138                                         strScan = scanFile.next();
139
140                                         // Get ID
141                                         if (strScan.equals("ID")) {
142                                                 strID = scanFile.next();
143                                         }
144                                         // Get TYPE
145                                         else if (strScan.equals("TYPE")) {
146                                                 strType = scanFile.next();
147                                         }
148                                         // Get TYPE
149                                         else if (strScan.equals("TYPESPECIFIC")) {
150                                                 strTypeSpecific = scanFile.next();
151                                         } else if (strScan.equals("END")) {
152                                                 // Break out of loop
153                                                 break;
154                                         }
155                                 }
156                         } else if (strScan.equals("Table")) {
157
158                                 // Get number of fields, e.g. Table 3
159                                 iFields = scanFile.nextInt();
160
161                                 // We have device ID and device specific names
162                                 // e.g. ID = PS1; TYPE
163                                 tpDevice = new TableProperty[2];
164                                 tpDevice[0] = new TableProperty();
165                                 tpDevice[0].setField("ID");
166                                 tpDevice[0].setType("VARCHAR");
167                                 tpDevice[0].setLength("5");
168                                 tpDevice[1] = new TableProperty();
169                                 tpDevice[1].setField("TYPE");
170                                 tpDevice[1].setType("VARCHAR");
171                                 tpDevice[1].setLength("30");
172
173                                 // Prepare properties for a specific device
174                                 tpDeviceSpecific = new TableProperty[iFields];
175                                 for (int i=0; i<iFields; i++) {
176                                         tpDeviceSpecific[i] = new TableProperty();
177
178                                         // Looping over the fields
179                                         strScan = scanFile.next();
180                                         tpDeviceSpecific[i].setField(strScan);
181                                         strScan = scanFile.next();
182                                         tpDeviceSpecific[i].setType(strScan);
183                                         strScan = scanFile.next();
184                                         tpDeviceSpecific[i].setLength(strScan);
185                                 }
186                         } else if (strScan.equals("Data")) {
187
188                                 // Get the device information
189                                 strFields = new String[2];
190                                 strFields[0] = strID;
191                                 strFields[1] = strTypeSpecific;
192
193                                 if ((tpDeviceSpecific.length == 1) &&
194                                                 (tpDeviceSpecific[0].getField().equals("EMPTY"))) {
195
196                                         // Get the fields for specific device
197                                         strFieldsSpecific = null;
198                                         System.out.println("IoTInstaller: Empty constructor for: " + strTypeSpecific);
199
200                                 } else {
201
202                                         // Get the fields for specific device
203                                         strFieldsSpecific = new String[iFields];
204                                         for (int i=0; i<iFields; i++) {
205                                                 strScan = scanFile.next();
206                                                 strFieldsSpecific[i] = strScan;
207                                         }
208                                 }
209                         }
210                 }
211
212                 installNewEntity(strType, strTypeSpecific, strID, tpDevice,
213                                                                                  tpDeviceSpecific, strFields, strFieldsSpecific);
214                 System.out.println("IoTInstaller: Installing a new entity/device into the system");
215         }
216
217         /**
218          * A method to extract device/entity information from the user
219          * <p>
220          * Users are supposed to supply the information needed for installation
221          *
222          * @param  strCfgFileName String config file name for device/entity
223          * @return                void
224          */
225         public void extractTableAndInstall(String strCfgFileName) {
226                 // TO DO: WE PROBABLY NEED TO IMPROVE THE FILE PARSING BUT FOR NOW KEEP IT MINIMUM
227                 try {
228
229                         // Parse configuration file
230                         // Assumption here is that .config file is written with the correct syntax (need typechecking)
231                         File file = new File(strCfgFileName);
232                         Scanner scanFile = new Scanner(new FileReader(file));
233                         System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
234
235             extractTableAndInstallCore(scanFile);
236
237                 } catch (FileNotFoundException ex) {
238
239                         System.out.println("IoTInstaller: Exception: ");
240                         ex.printStackTrace();
241
242                 }
243         }
244
245         /**
246          * A method to install a new entity/device into the database
247          * <p>
248          * 1) Insert this device/entity into the main table IoTMain
249          * 2) Create a new device/entity table if it doesn't exist yet
250          * 3) Insert this entry into the specific device/entity table
251          *
252          * @param  strType           String device type
253          * @param  strTypeSpecific   String device specific type
254          * @param  strID             String unique device/entity ID
255          * @param  tpDevice          array of TableProperty to construct the new table
256          * @param  tpDeviceSpecific  array of TableProperty to construct the new table
257          * @param  strFields         field values of device table
258          * @param  strFieldsSpecific field values of device specific table
259          */
260         private void installNewEntity(String strType, String strTypeSpecific, String strID,
261                 TableProperty[] tpDevice, TableProperty[] tpDeviceSpecific, String[] strFields, String[] strFieldsSpecific) {
262
263                 // Create a new IoTInstaller object
264                 System.out.println("IoTInstaller: Installing device " + strType + " with specific type " + strTypeSpecific);
265                 tbl.setTableName(strType);
266
267                 // 1) Insert this device/entity into the main table IoTMain
268                 insertMainDBEntry(strID, strType);
269
270                 // Device table
271                 // 2) Create a new device/entity table if it doesn't exist yet
272                 tbl.setTableName(strType);
273                 if (tbl.isTableExisting()) {
274                         // table does exist
275                         System.out.println("IoTInstaller: Table " + strType + " exists.. just insert new entry!");
276                 } else {
277                         // table does not exist yet
278                         tbl.createTable(tpDevice, "ID");
279                 }
280
281                 // 3) Insert this entry into the device/entity table
282                 tbl.insertEntry(strFields);
283
284                 // Device specific table
285                 // 2) Create a new device/entity table if it doesn't exist yet
286                 // P.S. We should assume that table doesn't exist yet, and we throw error otherwise!
287                 tbl.setTableName(strTypeSpecific + strID);
288                 tbl.createTable(tpDeviceSpecific, null);
289
290                 // 3) Insert this entry into the device/entity table
291                 if (strFieldsSpecific != null) {
292                         tbl.insertEntry(strFieldsSpecific);
293                 }
294         }
295
296         /**
297          * A method to extract device/entity communication configuration from the user
298          * <p>
299          * Users are supposed to supply the information needed for installation
300          *
301          * @param  strCfgFileName String config file name for device/entity
302          * @return                void
303          */
304         public void extractCommAndInstall(String strCfgFileName) {
305                 // TODO: WE PROBABLY NEED TO IMPROVE THE FILE PARSING BUT FOR NOW KEEP IT MINIMUM
306
307                 try {
308
309                         // Parse configuration file
310                         // Assumption here is that .config file is written with the correct syntax (need typechecking)
311                         File file = new File(strCfgFileName);
312                         Scanner scanFile = new Scanner(new FileReader(file));
313
314                         System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
315
316                         // Field counter
317                         int iFieldCnt = 0;
318
319                         // Initialize array of string
320                         String[] strFields = new String[INT_NUM_COMM_FIELDS];
321                         for(int i=0; i<INT_NUM_COMM_FIELDS; i++) {
322                                 strFields[i] = new String();
323                         }
324                         while (scanFile.hasNext() && (iFieldCnt < INT_NUM_COMM_FIELDS)) {
325
326                                 strFields[iFieldCnt++] = scanFile.next();
327                         }
328                         // Create a new installer object
329                         tbl.setTableName(STR_COMM_TABLE_NAME);
330                         tbl.insertEntry(strFields);
331
332                         System.out.println("IoTInstaller: Installing a new communication pattern into the system");
333
334                 } catch (FileNotFoundException ex) {
335
336                         System.out.println("IoTInstaller: Exception: ");
337                         ex.printStackTrace();
338
339                 }
340         }
341
342         /**
343          * A method to delete relation/communication information from database
344          *
345          * @param  strEntIDSource       String for entity ID source
346          * @param  strEntIDDest         String for entity ID destination
347          * @return                      void
348          */
349         public void deleteComm(String strEntIDSource, String strEntIDDest) {
350
351                 // Delete from table IoTMain
352                 tbl.setTableName(STR_COMM_TABLE_NAME);
353                 String strWhere = "ID_SOURCE='" + strEntIDSource + "' AND ID_DESTINATION='" + strEntIDDest + "';";
354                 tbl.deleteEntry(strWhere);
355                 System.out.println("IoTInstaller: Removing relation/communication from table " + STR_COMM_TABLE_NAME);
356         }
357         
358         /**
359          * A method to extract device/entity addresses information
360          * <p>
361          * Users are supposed to supply the information needed for installation
362          *
363          * @param  is   InputStream of the input source
364          * @return      void
365          */
366         public void installDeviceAddressCore(InputStream is) {
367
368                 Properties prop = new Properties();
369                 try {
370                         prop.load(is);
371                 } catch (IOException ex) {
372                         ex.printStackTrace();
373                 }
374                 // Initialize string
375                 // We can only install one device address per one time with the following sequence
376                 String[] strFields = new String[2];
377                 String[] strFieldsAddress = null;
378                 // Check for wildcard feature
379                 if ((prop.getProperty("SOURCEWILDCARD", null) != null) &&
380                         (prop.getProperty("DESTWILDCARD", null) != null)) {
381                         strFieldsAddress = new String[5];
382                         strFieldsAddress[3] = prop.getProperty("SOURCEWILDCARD");
383                         strFieldsAddress[4] = prop.getProperty("DESTWILDCARD");
384                 } else {
385                         strFieldsAddress = new String[3];
386                 }
387                 strFields[0] = prop.getProperty("ID");
388                 strFields[1] = prop.getProperty("ADDRESSFOR");
389                 strFieldsAddress[0] = prop.getProperty("DEVICEADDRESS");
390                 strFieldsAddress[1] = prop.getProperty("PORTNUMBER");
391                 strFieldsAddress[2] = prop.getProperty("PROTOCOL");
392
393                 // Insert this entry into the main device address table
394                 tbl.setTableName(STR_DEV_ADD_TABLE_NAME);
395                 tbl.insertEntry(strFields);
396
397                 // Create a new table for a specific device address
398                 // e.g. AmcrestCameraAdd + CM1 = AmcrestCameraAddCM1
399                 tbl.setTableName(strFields[1] + strFields[0]);
400
401                 // Table does not exist yet
402                 // Set TableProperty for device address (MAC address)
403                 TableProperty[] tp = null;
404                 // Check for wildcard feature
405                 if (strFieldsAddress.length == 5) {
406                         tp = new TableProperty[5];
407                         tp[3] = new TableProperty();
408                         tp[3].setField("SOURCEWILDCARD");
409                         tp[3].setType("VARCHAR");
410                         tp[3].setLength("5");
411                         tp[4] = new TableProperty();
412                         tp[4].setField("DESTWILDCARD");
413                         tp[4].setType("VARCHAR");
414                         tp[4].setLength("5");
415                 } else {
416                         tp = new TableProperty[3];
417                 }
418                 tp[0] = new TableProperty();
419                 tp[0].setField("DEVICEADDRESS");
420                 tp[0].setType("VARCHAR");
421                 tp[0].setLength("20");
422                 tp[1] = new TableProperty();
423                 tp[1].setField("PORTNUMBER");
424                 tp[1].setType("INT");
425                 tp[1].setLength("11");
426                 tp[2] = new TableProperty();
427                 tp[2].setField("PROTOCOL");
428                 tp[2].setType("VARCHAR");
429                 tp[2].setLength("5");
430                 tbl.createTable(tp, "DEVICEADDRESS");
431
432                 // Insert new address entry
433                 tbl.insertEntry(strFieldsAddress);
434                 System.out.println("IoTInstaller: Installing a new device/entity address into the system");
435         }
436
437         /**
438          * A method to extract device/entity addresses information
439          * <p>
440          * Users are supposed to supply the information needed for installation
441          *
442          * @param  strCfgFileName String config file name for device/entity
443          * @return                void
444          */
445         public void installDeviceAddress(String strCfgFileName) {
446
447                 try {
448
449                         // Parse configuration file
450                         File file = new File(strCfgFileName);
451                         FileInputStream fis = new FileInputStream(file);
452                         System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
453                         installDeviceAddressCore(fis);
454
455                 } catch (FileNotFoundException ex) {
456
457                         System.out.println("IoTInstaller: Exception: ");
458                         ex.printStackTrace();
459
460                 }
461         }
462
463         /**
464          * A method to extract Zigbee device addresses information
465          * <p>
466          * Users are supposed to supply the information needed for installation
467          *
468          * @param  strCfgFileName String config file name for device/entity
469          * @return                void
470          */
471         public void installZigbeeAddress(String strCfgFileName) {
472
473                 try {
474
475                         // Parse configuration file
476                         Properties prop = new Properties();
477                         File file = new File(strCfgFileName);
478                         FileInputStream fis = new FileInputStream(file);
479                         try {
480                                 prop.load(fis);
481                         } catch (IOException ex) {
482                                 ex.printStackTrace();
483                         }
484                         System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
485                         // Initialize string
486                         // We can only install one device address per one time with the following sequence
487                         String[] strFields = new String[2];
488                         String[] strFieldsAddress = new String[1];
489                         strFields[0] = prop.getProperty("ID");
490                         strFields[1] = prop.getProperty("ADDRESSFOR");
491                         strFieldsAddress[0] = prop.getProperty("DEVICEADDRESS");
492
493                         // Insert this entry into the main device address table
494                         tbl.setTableName(STR_ZB_ADD_TABLE_NAME);
495                         tbl.insertEntry(strFields);
496
497                         // Create a new table for a specific device address
498                         // e.g. AmcrestCameraZBAdd + CM1 = AmcrestCameraZBAddCM1
499                         tbl.setTableName(strFields[1] + strFields[0]);
500
501                         // Table does not exist yet
502                         // Set TableProperty for device address (MAC address)
503                         TableProperty[] tp = new TableProperty[1];
504                         tp[0] = new TableProperty();
505                         tp[0].setField("DEVICEADDRESS");
506                         tp[0].setType("VARCHAR");
507                         tp[0].setLength("25");
508                         tbl.createTable(tp, "DEVICEADDRESS");
509
510                         // Insert new address entry
511                         tbl.insertEntry(strFieldsAddress);
512                         System.out.println("IoTInstaller: Installing a new device/entity address into the system");
513
514                 } catch (FileNotFoundException ex) {
515
516                         System.out.println("IoTInstaller: Exception: ");
517                         ex.printStackTrace();
518
519                 }
520         }
521
522         /**
523          * A method to extract simple addresses information, e.g. www.google.com
524          * <p>
525          * Users are supposed to supply the information needed for installation
526          *
527          * @param  strCfgFileName String config file name for device/entity
528          * @return                void
529          */
530         public void installAddress(String strCfgFileName) {
531
532                 try {
533
534                         // Parse configuration file
535                         Properties prop = new Properties();
536                         File file = new File(strCfgFileName);
537                         FileInputStream fis = new FileInputStream(file);
538                         try {
539                                 prop.load(fis);
540                         } catch (IOException ex) {
541                                 ex.printStackTrace();
542                         }
543                         System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
544                         // Initialize string
545                         // We can only install one device address per one time with the following sequence
546                         String[] strFields = new String[2];
547                         String[] strFieldsAddress = new String[1];
548                         strFields[0] = prop.getProperty("ID");
549                         strFields[1] = prop.getProperty("ADDRESSFOR");
550                         strFieldsAddress[0] = prop.getProperty("ADDRESS");
551
552                         // Insert this entry into the main device address table
553                         tbl.setTableName(STR_ADDRESS_TABLE_NAME);
554                         tbl.insertEntry(strFields);
555
556                         // Create a new table for a specific device address
557                         // e.g. WeatherForecastAdd + WF1 = WeatherForecastAddCM1
558                         tbl.setTableName(strFields[1] + strFields[0]);
559
560                         // Table does not exist yet
561                         // Set TableProperty for device address (MAC address)
562                         TableProperty[] tp = new TableProperty[1];
563                         tp[0] = new TableProperty();
564                         tp[0].setField("ADDRESS");
565                         tp[0].setType("VARCHAR");
566                         tp[0].setLength("50");
567                         tbl.createTable(tp, "ADDRESS");
568
569                         // Insert new address entry
570                         tbl.insertEntry(strFieldsAddress);
571                         System.out.println("IoTInstaller: Installing a new device/entity address into the system");
572
573                 } catch (FileNotFoundException ex) {
574
575                         System.out.println("IoTInstaller: Exception: ");
576                         ex.printStackTrace();
577
578                 }
579         }
580
581         /**
582          * A method to extract host information for host installation
583          * <p>
584          * Users are supposed to supply the information needed for installation
585          *
586          * @param  strCfgFileName String config file name for device/entity
587          * @return                void
588          */
589         public void installHost(String strCfgFileName) {
590                 try {
591                         // Parse configuration file
592                         Properties prop = new Properties();
593                         File file = new File(strCfgFileName);
594                         FileInputStream fis = new FileInputStream(file);
595                         try {
596                                 prop.load(fis);
597                         } catch (IOException ex) {
598                                 ex.printStackTrace();
599                         }
600                         System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
601                         // Initialize array of string
602                         String[] strFields = new String[3];
603                         strFields[0] = prop.getProperty("HOSTADDRESS");
604                         strFields[1] = prop.getProperty("PROCESSOR");
605                         strFields[2] = prop.getProperty("MEMORY");
606                         // Create a new installer object
607                         tbl.setTableName(STR_HOST_TABLE_NAME);
608                         tbl.insertEntry(strFields);
609                         System.out.println("IoTInstaller: Installing a new host into the system");
610
611                 } catch (FileNotFoundException ex) {
612
613                         System.out.println("IoTInstaller: Exception: ");
614                         ex.printStackTrace();
615
616                 }
617         }
618
619         /**
620          * A method to delete host information from database by putting in host address
621          *
622          * @param  strHostAddress String for host address
623          * @return                void
624          */
625         public void deleteHost(String strHostAddress) {
626
627                 tbl.setTableName(STR_HOST_TABLE_NAME);
628                 String strWhere = "HOSTADDRESS='" + strHostAddress + "';";
629                 tbl.deleteEntry(strWhere);
630                 System.out.println("IoTInstaller: Deleting a host from the system");
631         }
632
633         /**
634          * A method to delete entity information from database
635          *
636          * @param  strEntID             String for entity ID
637          * @param  strEntType   String for entity type
638          * @param  strEntName   String for entity name
639          * @return              void
640          */
641         public void deleteEntity(String strEntID, String strEntType, String strEntName) {
642
643                 // Delete from table IoTMain
644                 tbl.setTableName(STR_MAIN_TABLE_NAME);
645                 String strWhere = "ID='" + strEntID + "';";
646                 tbl.deleteEntry(strWhere);
647                 System.out.println("IoTInstaller: Removing entity from table " + STR_MAIN_TABLE_NAME);
648                 // Delete from table with type name, e.g. Camera
649                 tbl.setTableName(strEntType);
650                 strWhere = "ID='" + strEntID + "';";
651                 tbl.deleteEntry(strWhere);
652                 System.out.println("IoTInstaller: Removing entity from table type: " + strEntType);
653                 // Drop table if this was the last entry
654                 if (tbl.isTableEmpty()) {
655                         tbl.dropTable();
656                         System.out.println("IoTInstaller: Dropping the table.. It was the last entry!");
657                 }
658
659                 // Drop the table that contains constructor information
660                 tbl.setTableName(strEntName + strEntID);
661                 tbl.dropTable();
662                 System.out.println("IoTInstaller: Dropping class constructor table...");
663
664                 System.out.println("IoTInstaller: Deleting an entry from the system...");
665         }
666         
667         /**
668          * A method to delete address information from database
669          *
670          * @param  strTableName         String for main table, i.e. IoTAddress, IoTDeviceAddress, or IoTZigbeeAddress
671          * @param  strEntID                     String for entity ID, e.g. CM1
672          * @param  strEntAddType        String for entity address type, e.g. AmcrestCameraAdd
673          * @return                                      void
674          */
675         public void deleteAddress(String strTableName, String strEntID, String strEntAddType) {
676
677                 // Delete from main table, e.g. IoTAddress, IoTDeviceAddress, or IoTZigbeeAddress
678                 tbl.setTableName(strTableName);
679                 String strWhere = "ID='" + strEntID + "';";
680                 tbl.deleteEntry(strWhere);
681                 System.out.println("IoTInstaller: Removing entity from table " + strTableName);
682
683                 // Drop the table that contains constructor information
684                 tbl.setTableName(strEntAddType + strEntID);
685                 tbl.dropTable();
686                 System.out.println("IoTInstaller: Dropping class constructor table...");
687                 System.out.println("IoTInstaller: Deleting an entry from the system...");
688         }
689
690
691         /**
692          * A method to install a pair of new devices with their communication pattern
693          *
694          * @param  strFirstDeviceFile  String that contains the file name of the fist device
695          * @param  strSecondDeviceFile String that contains the file name of the second device
696          * @param  strCommFile         String that contains the file name of the communication file
697          * @return                     void
698          */
699         public void installPairOfEntities(String strFirstEntityFile,
700                 String strSecondEntityFile, String strCommFile) {
701                 // TODO: NEED TO DO THE INPUT FAILURE CHECKING HERE
702                 // NOW JUST ASSUME THAT THE INPUT FILES ARE GOOD
703
704                 extractTableAndInstall(strFirstEntityFile);
705                 extractTableAndInstall(strSecondEntityFile);
706                 extractCommAndInstall(strCommFile);
707         }
708
709         /**
710          * A method to output help messages
711          *
712          * @return void
713          */
714         public void helpMessages() {
715                 System.out.println();
716                 System.out.println("IoTInstaller: Command line options:");
717                 System.out.println("IoTInstaller: 1) Install one device, e.g. java iotinstaller.IoTInstaller -install_ent <filename>");
718                 System.out.println("IoTInstaller: 2) Install comm pattern, e.g. java iotinstaller.IoTInstaller -install_comm <filename>");
719                 System.out.print("IoTInstaller: 3) Install two devices and comm pattern, e.g. java iotinstaller.IoTInstaller ");
720                 System.out.println("-install_comp <first_entity_filename> <second_entity_filename> <communication_filename>");
721                 System.out.println("IoTInstaller: 4) Install address, e.g. java iotinstaller.IoTInstaller -install_add <filename>");
722                 System.out.println("IoTInstaller: 5) Install device address, e.g. java iotinstaller.IoTInstaller -install_dev_add <filename>");
723                 System.out.println("IoTInstaller: 6) Install zigbee device address, e.g. java iotinstaller.IoTInstaller -install_zb_add <filename>");
724                 System.out.println("IoTInstaller: 7) Install host, e.g. java iotinstaller.IoTInstaller -install_host <filename>");
725                 System.out.println("IoTInstaller: 8) Delete entity, e.g. java iotinstaller.IoTInstaller -delete_ent <ent_id> <ent_type> <ent_name>");
726                 System.out.println("IoTInstaller: 9) Delete comm pattern, e.g. java iotinstaller.IoTInstaller -delete_comm <ent_id_source> <ent_id_dest>");
727                 System.out.println("IoTInstaller: 10) Delete address, e.g. java iotinstaller.IoTInstaller -delete_add <ent_id> <ent_type>");
728                 System.out.println("IoTInstaller: 11) Delete device address, e.g. java iotinstaller.IoTInstaller -delete_dev_add <ent_id> <ent_type>");
729                 System.out.println("IoTInstaller: 12) Delete zigbee device address, e.g. java iotinstaller.IoTInstaller -delete_zb_add <ent_id> <ent_type>");
730                 System.out.println("IoTInstaller: 13) Delete host, e.g. java iotinstaller.IoTInstaller -delete_host <host_address>");
731                 System.out.println("IoTInstaller: Type 'java iotinstaller.IoTInstaller -help' to display this help.");
732                 System.out.println();
733         }
734
735         /**
736          * A method to output help messages
737          *
738          * @return void
739          */
740         public String helpMessagesString() {
741                 String helpTxt = "\n";
742                 helpTxt = helpTxt + "IoTInstaller: Command line options:\n";
743                 helpTxt = helpTxt + "IoTInstaller: 1) Install one device, e.g. java iotinstaller.IoTInstaller -install_ent <filename>\n";
744                 helpTxt = helpTxt + "IoTInstaller: 2) Install comm pattern, e.g. java iotinstaller.IoTInstaller -install_comm <filename>\n";
745                 helpTxt = helpTxt + "IoTInstaller: 3) Install two devices and comm pattern, e.g. java iotinstaller.IoTInstaller ";
746                 helpTxt = helpTxt + "-install_comp <first_entity_filename> <second_entity_filename> <communication_filename>\n";
747                 helpTxt = helpTxt + "IoTInstaller: 4) Install address, e.g. java iotinstaller.IoTInstaller -install_add <filename>\n";
748                 helpTxt = helpTxt + "IoTInstaller: 5) Install device address, e.g. java iotinstaller.IoTInstaller -install_dev_add <filename>\n";
749                 helpTxt = helpTxt + "IoTInstaller: 6) Install zigbee device address, e.g. java iotinstaller.IoTInstaller -install_zb_add <filename>\n";
750                 helpTxt = helpTxt + "IoTInstaller: 7) Install host, e.g. java iotinstaller.IoTInstaller -install_host <filename>\n";
751                 helpTxt = helpTxt + "IoTInstaller: 8) Delete entity, e.g. java iotinstaller.IoTInstaller -delete_ent <ent_id> <ent_type> <ent_name>\n";
752                 helpTxt = helpTxt + "IoTInstaller: 9) Delete comm pattern, e.g. java iotinstaller.IoTInstaller -delete_comm <ent_id_source> <ent_id_dest>\n";
753                 helpTxt = helpTxt + "IoTInstaller: 10) Delete address, e.g. java iotinstaller.IoTInstaller -delete_add <ent_id>\n";
754                 helpTxt = helpTxt + "IoTInstaller: 11) Delete device address, e.g. java iotinstaller.IoTInstaller -delete_dev_add <ent_id>\n";
755                 helpTxt = helpTxt + "IoTInstaller: 12) Delete zigbee device address, e.g. java iotinstaller.IoTInstaller -delete_zb_add <ent_id>\n";
756                 helpTxt = helpTxt + "IoTInstaller: 13) Delete host, e.g. java iotinstaller.IoTInstaller -delete_host <host_address>\n";
757                 helpTxt = helpTxt + "IoTInstaller: Type 'java iotinstaller.IoTInstaller -help' to display this help.\n";
758                 helpTxt = helpTxt + "\n";
759
760                 return helpTxt;
761         }
762     
763      /**
764      *  Kevin's Code
765      *  @author Kevin Truong<kctruon1@uci.edu>  
766      * */
767
768     public static void formatWrite(String string, HashMap<String, ArrayList<HashSet<String>>> hashMap, HashMap<String, HashSet<String>> hashMap2, HashMap<String, String> hashMap3) throws IOException {
769         BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(string));
770         
771         for (String string2 : hashMap2.keySet()) {
772             bufferedWriter.write("Type: \n");
773             bufferedWriter.write(string2 + '\n');
774             bufferedWriter.write("TAG: \n");
775             int n = 0;
776             for (String string3 : hashMap2.get(string2)) {
777                 String string4 = hashMap3.get(string3);
778                 if (string4 != null && n == 0) {
779                     string4 = string4.substring(0, 2);
780                     bufferedWriter.write(string4 + '\n');
781                 } else if (string4 == null && n == 0) {
782                     bufferedWriter.write(string4 + '\n');
783                 }
784                 ++n;
785                 bufferedWriter.write("Subtype: \n");
786                 bufferedWriter.write(string3 + '\n');
787                 for (int i = 0; i < hashMap.get(string3).size(); ++i) {
788                     if (i == 0) {
789                         bufferedWriter.write("AddressInfo:\n");
790                         for (String string5 : hashMap.get(string3).get(0)) {
791                             bufferedWriter.write(string5 + '\n');
792                         }
793                         continue;
794                     }
795                     bufferedWriter.write("DeviceInfo:\n");
796                     for (String string5 : hashMap.get(string3).get(1)) {
797                         bufferedWriter.write(string5 + '\n');
798                     }
799                 }
800             }
801             bufferedWriter.write("endType \n\n");
802         }
803         bufferedWriter.close();
804     }
805
806     public static void actualDeviceInfoWrite(ArrayList<String[][]> arrayList, String string) throws IOException {
807         BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(string));
808         for (String[][] arrstring : arrayList) {
809             for (int i = 0; i < arrstring.length; ++i) {
810                 bufferedWriter.write(arrstring[i][1] + " " + arrstring[i][0] + '\n');
811             }
812             bufferedWriter.newLine();
813         }
814         bufferedWriter.close();
815     }
816
817     public static void configSetWrite(String string, ArrayList<String[][]> arrayList, HashMap<String, HashSet<String>> hashMap) throws IOException {
818         BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(string));
819         for (String string2 : hashMap.keySet()) {
820             boolean bl = false;
821             bufferedWriter.write("START\n" + string2 + "\n");
822             block1 : for (String string3 : hashMap.get(string2)) {
823                 for (String[][] arrstring : arrayList) {
824                     if (!string3.equals(arrstring[0][1])) continue;
825                     for (int i = 0; i < arrstring.length; ++i) {
826                         bufferedWriter.write(arrstring[i][1] + " " + arrstring[i][0] + '\n');
827                     }
828                     continue block1;
829                 }
830             }
831             bufferedWriter.write("END\n\n");
832         }
833         bufferedWriter.close();
834     }
835
836     public static HashMap<String, Integer> getNumAddresses(HashSet<String> hashSet) {
837         HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
838         Properties properties = new Properties();
839         for (String string : hashSet) {
840             String string2 = "../SupportedDevices/AddressInformation/" + string + ".config";
841             try {
842                 properties.load(new FileInputStream(string2));
843             }
844             catch (IOException iOException) {
845                 iOException.printStackTrace();
846             }
847             int n = Integer.parseInt(properties.getProperty("ADDRESSES"));
848             hashMap.put(string, n);
849         }
850         return hashMap;
851     }
852
853     public static HashMap<String, Integer> getZBAddresses(HashSet<String> hashSet) {
854         HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
855         Properties properties = new Properties();
856         for (String string : hashSet) {
857             String string2 = "../SupportedDevices/AddressInformation/" + string + ".config";
858             try {
859                 properties.load(new FileInputStream(string2));
860             }
861             catch (IOException iOException) {
862                 iOException.printStackTrace();
863             }
864             int n = Integer.parseInt(properties.getProperty("ZBADDRESSES"));
865             hashMap.put(string, n);
866         }
867         return hashMap;
868     }
869
870     public static void appendToFile(String string, String string2) {
871         try {
872             FileWriter fileWriter = new FileWriter(string2, true);
873             BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
874             PrintWriter printWriter = new PrintWriter(bufferedWriter);
875             printWriter.print(string);
876             printWriter.close();
877             bufferedWriter.close();
878             fileWriter.close();
879         }
880         catch (IOException iOException) {
881             iOException.printStackTrace();
882         }
883     }
884
885     public static void overrideFileWrite(String string, String string2) {
886         try {
887             BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(string2));
888             bufferedWriter.write(string);
889             bufferedWriter.close();
890         }
891         catch (IOException iOException) {
892             iOException.printStackTrace();
893         }
894     }
895
896     public static void formatForMultipleAddresses(String string, int n, int n2, int n3, HashMap<String, ArrayList<HashSet<String>>> hashMap, Properties properties, int n4, String string2) {
897         string = string + "TYPE_" + n + "_" + n2 + "_ADDRESS_FIELDS=";
898         n4 = 0;
899         for (int i = 0; i < n3; ++i) {
900             String string3 = new String();
901             for (String string4 : hashMap.get(string2).get(0)) {
902                 if (n4 == 0) {
903                     string = string + string4 + " ";
904                 }
905                 string3 = string3 + string4 + "_" + n + "_" + n2 + "_ADD_" + i + "=" + properties.getProperty(new StringBuilder().append(string4).append("_").append(i + 1).toString()) + "\n";
906             }
907             if (n4 == 0) {
908                 string = string + "\n";
909             }
910             string = string + string3;
911             ++n4;
912         }
913     }
914
915     public static void getAddressInfo(String string, HashMap<String, ArrayList<HashSet<String>>> hashMap, HashMap<String, String> hashMap2, HashMap<String, HashSet<String>> hashMap3) {
916         Properties properties = new Properties();
917         int n = 0;
918         Set set = new HashSet();
919         set = hashMap3.keySet();
920         IoTInstaller.overrideFileWrite((String)("NUM_OF_TYPES=" + set.size() + "\n\n"), (String)string);
921         for (String string2 : hashMap3.keySet()) {
922             int n2 = 0;
923             int n3 = 0;
924             String string3 = new String();
925             string3 = string3 + "TYPE_" + n + "=" + string2 + "\n";
926             for (String string4 : hashMap3.get(string2)) {
927                 int n4;
928                 String string5;
929                 String string6 = "../SupportedDevices/AddressInformation/" + string4 + ".config";
930                 try {
931                     properties.load(new FileInputStream(string6));
932                 }
933                 catch (IOException iOException) {
934                     iOException.printStackTrace();
935                 }
936                 if (n3 == 0) {
937                     string3 = string3 + "TAG_" + n + "=" + hashMap2.get(string4).substring(0, 2) + "\n";
938                     string3 = string3 + "SUBTYPE_" + n + "=" + hashMap3.get(string2).size() + "\n";
939                     ++n3;
940                 }
941                 string3 = string3 + "TYPE_" + n + "_" + n2 + "=" + string4 + "\n";
942                 int n5 = Integer.parseInt(properties.getProperty("ADDRESSES"));
943                 int n6 = Integer.parseInt(properties.getProperty("ZBADDRESSES"));
944                 string3 = string3 + "TYPE_" + n + "_" + n2 + "_NUM_OF_ZBADDRESSES=" + n6 + "\n";
945                 if (n6 != 0) {
946                     string3 = string3 + "TYPE_" + n + "_" + n2 + "_ZBADDRESS_FIELDS=";
947                     n3 = 0;
948                     for (n4 = 0; n4 < n6; ++n4) {
949                         string5 = new String();
950                         for (String string7 : hashMap.get(string4).get(2)) {
951                             if (n3 == 0) {
952                                 string3 = string3 + string7 + " ";
953                             }
954                             string5 = string5 + "ZB" + string7 + "_" + n + "_" + n2 + "_ZBADD_" + n4 + "=" + properties.getProperty(new StringBuilder().append(string7).append("_").append(n4 + 1).toString()) + "\n";
955                         }
956                         if (n3 == 0) {
957                             string3 = string3 + "\n";
958                         }
959                         string3 = string3 + string5;
960                         ++n3;
961                     }
962                 }
963                 string3 = string3 + "TYPE_" + n + "_" + n2 + "_NUM_OF_ADDRESSES=" + n5 + "\n";
964                 if (n5 != 0) {
965                     string3 = string3 + "TYPE_" + n + "_" + n2 + "_ADDRESS_FIELDS=";
966                     n3 = 0;
967                     for (n4 = 0; n4 < n5; ++n4) {
968                         string5 = new String();
969                         for (String string7 : hashMap.get(string4).get(0)) {
970                             if (n3 == 0) {
971                                 string3 = string3 + string7 + " ";
972                             }
973                             string5 = string5 + string7 + "_" + n + "_" + n2 + "_ADD_" + n4 + "=" + properties.getProperty(new StringBuilder().append(string7).append("_").append(n4 + 1).toString()) + "\n";
974                         }
975                         if (n3 == 0) {
976                             string3 = string3 + "\n";
977                         }
978                         string3 = string3 + string5;
979                         ++n3;
980                     }
981                 }
982                 n4 = Integer.parseInt(properties.getProperty("DEVICEINFO"));
983                 string3 = string3 + "TYPE_" + n + "_" + n2 + "_NUM_OF_DEVICE_INFO=" + n4 + "\n";
984                 if (n4 > 0) {
985                     string5 = new String();
986                     n3 = 0;
987                     for (String string7 : hashMap.get(string4).get(1)) {
988                         if (string7.length() > 0) {
989                             if (n3 == 0) {
990                                 string3 = string3 + "TYPE_" + n + "_" + n2 + "_DEVICE_FIELDS=";
991                             }
992                             string3 = string3 + string7 + " ";
993                             string5 = string5 + string7 + "_" + n + "_" + n2 + "=USER\n";
994                         }
995                         ++n3;
996                     }
997                     string3 = string3 + "\n";
998                     ++n3;
999                     string3 = string3 + string5;
1000                 }
1001                 ++n2;
1002             }
1003             string3 = string3 + "\n";
1004             ++n;
1005             IoTInstaller.appendToFile((String)string3, (String)string);
1006         }
1007     }
1008
1009     void getIoTDeviceAddressInfoAndOutput(HashMap<String, HashSet<String>> hashMap) {
1010         int n;
1011         Table table = new Table("IoTDeviceAddress", false);
1012         String[][] arrstring = table.getGeneralDBTable();
1013         String string = new String();
1014         string = string + "START\n";
1015         string = string + "IoTDeviceAddress\n";
1016         for (n = 0; n < arrstring.length; ++n) {
1017             string = string + arrstring[n][1] + " " + arrstring[n][0] + "\n";
1018         }
1019         string = string + "END\n\n";
1020         IoTInstaller.overrideFileWrite((String)string, (String)"IoTDeviceAddress.config");
1021         table = new Table("IoTZigbeeAddress", false);
1022         arrstring = table.getGeneralDBTable();
1023         string = new String();
1024         string = string + "START\n";
1025         string = string + "IoTZigbeeAddress\n";
1026         for (n = 0; n < arrstring.length; ++n) {
1027             string = string + arrstring[n][1] + " " + arrstring[n][0] + "\n";
1028         }
1029         string = string + "END\n\n";
1030         IoTInstaller.appendToFile((String)string, (String)"IoTDeviceAddress.config");
1031     }
1032
1033     public void getInformationFromDatabase(String string) {
1034         Scanner scanner;
1035         System.out.println("Initializing -get...");
1036         ArrayList<String> arrayList = new ArrayList<String>();
1037         ArrayList<String[][]> arrayList2 = new ArrayList<String[][]>();
1038         String string2 = new String("AddressInformation.config");
1039         String string3 = new String("SetList.config");
1040         String string4 = new String("DatabaseInformation.config");
1041         String string5 = new String("IDSubtypeInformation.config");
1042         HashMap<String, HashSet<String>> hashMap = new HashMap<String, HashSet<String>>();
1043         HashMap<String, ArrayList<HashSet<String>>> hashMap2 = new HashMap<String, ArrayList<HashSet<String>>>();
1044         HashMap<String, String> hashMap3 = new HashMap<String, String>();
1045         try {
1046             File file = new File(string);
1047             InputStream IS = new FileInputStream(file);
1048             scanner = new Scanner(IS);
1049             while (scanner.hasNext()) {
1050                 arrayList.add(scanner.next());
1051             }
1052         }
1053         catch (FileNotFoundException fileNotFoundException) {
1054             System.out.print("File Error: ");
1055             fileNotFoundException.printStackTrace();
1056         }
1057         
1058         for (int i = 0; i < arrayList.size(); ++i) {
1059             HashSet<String> object = new HashSet<String>();
1060             Table tbl = new Table(arrayList.get(i), false);
1061             String[][] arrstring = tbl.getGeneralDBTable();
1062             arrayList2.add(arrstring);
1063             object.clear();
1064             for (int j = 0; j < arrstring.length; ++j) {
1065                 object.add(arrstring[j][1]);
1066             }
1067             hashMap.putIfAbsent(arrayList.get(i), object);
1068             HashMap<String, Integer> hashMap4 = IoTInstaller.getNumAddresses(object);
1069             HashMap<String, Integer> hashMap5 = IoTInstaller.getZBAddresses(object);
1070             for(String x : object){
1071                 ArrayList<HashSet<String>> arrayList3 = new ArrayList<HashSet<String>>();
1072                 for (int j = 0; j < arrstring.length; ++j) {
1073                     if (!x.equals(arrstring[j][1])) continue;
1074                     hashMap3.putIfAbsent(x, arrstring[j][0]);
1075                 }
1076                 arrayList3.clear();
1077                 if (hashMap4.get(x) > 1) {
1078                     tbl.setTableName(x + "Add1" + hashMap3.get(x));
1079                     if (tbl.isTableExisting()) {
1080                         tbl = new Table(x + "Add1" + hashMap3.get(x), false);
1081                         arrayList3.add(tbl.getColumnNames());
1082                     } else {
1083                         arrayList3.add(new HashSet<String>());
1084                     }
1085                     hashMap2.putIfAbsent(x, arrayList3);
1086                 } else {
1087                     tbl.setTableName(x + "Add" + hashMap3.get(x));
1088                     if (tbl.isTableExisting()) {
1089                         tbl = new Table(x + "Add" + hashMap3.get(x), false);
1090                         arrayList3.add(tbl.getColumnNames());
1091                     } else {
1092                         arrayList3.add(new HashSet<String>());
1093                     }
1094                 }
1095                 tbl.setTableName(x + hashMap3.get(x));
1096                 
1097                 if (tbl.isTableExisting()) {
1098                     tbl = new Table(x + hashMap3.get(x), false);
1099                     arrayList3.add(tbl.getColumnNames());
1100                 } else {
1101                     arrayList3.add(new HashSet<String>());
1102                 }
1103                 if (hashMap5.get(x) != 0) {
1104                     tbl.setTableName(x + "ZBAdd" + hashMap3.get(x));
1105                     if (tbl.isTableExisting()) {
1106                         tbl = new Table(x + "ZBAdd" + hashMap3.get(x), false);
1107                         arrayList3.add(tbl.getColumnNames());
1108                     } else {
1109                         arrayList3.add(new HashSet<String>());
1110                     }
1111                 } else {
1112                     arrayList3.add(new HashSet<String>());
1113                 }
1114                 hashMap2.putIfAbsent(x, arrayList3);
1115             }
1116         }
1117         IoTInstaller.getAddressInfo((String)string2, hashMap2, hashMap3, hashMap);
1118         this.getIoTDeviceAddressInfoAndOutput(hashMap);
1119         try {
1120             IoTInstaller.actualDeviceInfoWrite(arrayList2, (String)string5);
1121             IoTInstaller.configSetWrite((String)string3, arrayList2, hashMap);
1122         }
1123         catch (IOException iOException) {
1124             iOException.printStackTrace();
1125         }
1126         System.out.println("Done Executing -get");
1127     }
1128
1129     public void addNewType(String string) {
1130         System.out.println("Initializing -appendType...");
1131         IoTInstaller.appendToFile((String)string, (String)"AllDevices.config");
1132         System.out.println("Done Executing -appendType");
1133     }
1134
1135     public void updateSetList(String string) {
1136         Scanner scanner;
1137         Table tbl;
1138         System.out.println("Updating SetList...");
1139         String string2 = new String("SetList.config");
1140         ArrayList<String> arrayList = new ArrayList<String>();
1141         ArrayList<String[][]> arrayList2 = new ArrayList<String[][]>();
1142         HashMap<String, HashSet<String>> hashMap = new HashMap<String, HashSet<String>>();
1143         try {
1144             File file = new File(string);
1145             InputStream IS = new FileInputStream(file);
1146             scanner = new Scanner(IS);
1147             while (scanner.hasNext()) {
1148                 arrayList.add(scanner.next());
1149             }
1150         }
1151         catch (FileNotFoundException fileNotFoundException) {
1152             System.out.print("File Error: ");
1153             fileNotFoundException.printStackTrace();
1154         }
1155         for (int i = 0; i < arrayList.size(); ++i) {
1156             HashSet<String> object = new HashSet<String>();
1157             tbl = new Table(arrayList.get(i), false);
1158             String[][] arrstring = tbl.getGeneralDBTable();
1159             arrayList2.add(arrstring);
1160             object.clear();
1161             for (int j = 0; j < arrstring.length; ++j) {
1162                 object.add(arrstring[j][1]);
1163             }
1164             hashMap.putIfAbsent(arrayList.get(i), object);
1165         }
1166         
1167         try {
1168             IoTInstaller.configSetWrite((String)string2, arrayList2, hashMap);
1169         }
1170         catch (IOException iOException) {
1171             iOException.printStackTrace();
1172         }
1173         System.out.println("Done Updating Setlist!");
1174     }
1175
1176     public void updateIoTDeviceAddress(String string) {
1177         Scanner scanner;
1178         Table tbl;
1179         System.out.println("Updating IoTDeviceAddress...");
1180         String string2 = new String("IoTDeviceAddress.config");
1181         ArrayList<String> arrayList = new ArrayList<String>(); // Holds types
1182         ArrayList<String[][]> arrayList2 = new ArrayList<String[][]>();
1183         HashMap<String, HashSet<String>> hashMap = new HashMap<String, HashSet<String>>();
1184         try {
1185             File file = new File(string);
1186             InputStream IS = new FileInputStream(file);
1187             scanner = new Scanner(IS);
1188             while (scanner.hasNext()) {
1189                 arrayList.add(scanner.next());
1190             }
1191         }
1192         catch (FileNotFoundException fileNotFoundException) {
1193             System.out.print("File Error: ");
1194             fileNotFoundException.printStackTrace();
1195         }
1196         for (int i = 0; i < arrayList.size(); ++i) {
1197             HashSet<String> object = new HashSet<String>();
1198             tbl = new Table(arrayList.get(i), false);
1199             String[][] arrstring = tbl.getGeneralDBTable();
1200             arrayList2.add(arrstring);
1201             object.clear();
1202             for (int j = 0; j < arrstring.length; ++j) {
1203                 object.add(arrstring[j][1]);
1204             }
1205             hashMap.putIfAbsent(arrayList.get(i), object);
1206         }
1207
1208         this.getIoTDeviceAddressInfoAndOutput(hashMap);
1209         
1210         System.out.println("Done Updating IoTDeviceAddress!");
1211     }
1212
1213     public void updateIDSub(String string) {
1214         FileInputStream fileInputStream;
1215         String[][] arrstring;
1216         Scanner scanner;
1217         Table tbl;
1218         System.out.println("Updating IDSubtype.config...");
1219         ArrayList<String> arrayList = new ArrayList<String>();
1220         ArrayList<String[][]> arrayList2 = new ArrayList<String[][]>();
1221         try {
1222             File file = new File(string);
1223             InputStream IS = new FileInputStream(file);
1224             scanner = new Scanner(IS);
1225             while (scanner.hasNext()) {
1226                 arrayList.add(scanner.next());
1227             }
1228         }
1229         catch (FileNotFoundException fileNotFoundException) {
1230             System.out.print("File Error: ");
1231             fileNotFoundException.printStackTrace();
1232         }
1233         for (int i = 0; i < arrayList.size(); ++i) {
1234             tbl = new Table((String)arrayList.get(i), false);
1235             arrstring = tbl.getGeneralDBTable();
1236             arrayList2.add(arrstring);
1237         }
1238         
1239         try {
1240             IoTInstaller.actualDeviceInfoWrite(arrayList2, (String)"IDSubtypeInformation.config");
1241         }
1242         catch (IOException iOException) {
1243             iOException.printStackTrace();
1244         }
1245         System.out.println("Done Updating!");
1246     }
1247
1248     // End of Kevin's code
1249
1250         /**
1251          * Main method that accepts inputs for installation
1252          *
1253          * @param  args[0]  String that contains the command line parameter
1254          * @param  args[1]  String that contains the first file name / entity ID / host address
1255          * @param  args[2]  String that contains the second file name
1256          * @param  args[3]  String that contains the third file name
1257          * @see    helpMessages
1258          */
1259         public static void main(String[] args) {
1260
1261                 // Testing IoTInstaller object
1262                 IoTInstaller iotinst = new IoTInstaller();
1263
1264                 // TODO: PROBABLY NEED A BETTER ERROR HANDLING FOR INPUTS HERE
1265                 // NOW ASSUME MINIMAL ERROR FOR INPUTS
1266                 if (args.length > 0) {
1267                         // Check for input parameters
1268                         if (args[0].equals(STR_INSTALL_ENTITY_CMD)) {
1269                                 iotinst.extractTableAndInstall(args[1]);
1270
1271                         } else if (args[0].equals(STR_INSTALL_COMMUNICATION_CMD)) {
1272                                 iotinst.extractCommAndInstall(args[1]);
1273
1274                         } else if (args[0].equals(STR_INSTALL_COMPLETE_CMD)) {
1275                                 iotinst.installPairOfEntities(args[1], args[2], args[3]);
1276
1277                         } else if (args[0].equals(STR_INSTALL_ADDRESS_CMD)) {
1278                                 iotinst.installAddress(args[1]);
1279
1280                         } else if (args[0].equals(STR_INSTALL_DEV_ADDRESS_CMD)) {
1281                                 iotinst.installDeviceAddress(args[1]);
1282
1283                         } else if (args[0].equals(STR_INSTALL_ZB_ADDRESS_CMD)) {
1284                                 iotinst.installZigbeeAddress(args[1]);
1285
1286                         } else if (args[0].equals(STR_INSTALL_HOST_CMD)) {
1287                                 iotinst.installHost(args[1]);
1288
1289                         } else if (args[0].equals(STR_DELETE_ENTITY_CMD)) {
1290                                 iotinst.deleteEntity(args[1], args[2], args[3]);
1291
1292                         } else if (args[0].equals(STR_DELETE_COMMUNICATION_CMD)) {
1293                                 iotinst.deleteComm(args[1], args[2]);
1294
1295                         } else if (args[0].equals(STR_DELETE_ADDRESS_CMD)) {
1296                                 iotinst.deleteAddress(STR_ADDRESS_TABLE_NAME, args[1], args[2]);
1297
1298                         } else if (args[0].equals(STR_DELETE_DEV_ADD_CMD)) {
1299                                 iotinst.deleteAddress(STR_DEV_ADD_TABLE_NAME, args[1], args[2]);
1300
1301                         } else if (args[0].equals(STR_DELETE_ZB_ADD_CMD)) {
1302                                 iotinst.deleteAddress(STR_ZB_ADD_TABLE_NAME, args[1], args[2]);
1303
1304                         } else if (args[0].equals(STR_DELETE_HOST_CMD)) {
1305                                 iotinst.deleteHost(args[1]);
1306
1307                         } else if (args[0].equals(STR_HELP_CMD)) {
1308                                 iotinst.helpMessages();
1309
1310                         } else if (args[0].equals("-get")) {
1311                                 iotinst.getInformationFromDatabase("../SupportedDevices/"+args[1]);
1312
1313                         } else if (args[0].equals("-appendType")) {
1314                                 iotinst.addNewType("../SupportedDevices/"+args[1]);
1315
1316                         } else if (args[0].equals("-updateIoTDeviceAddress")) {
1317                                 iotinst.updateIoTDeviceAddress("../SupportedDevices/"+args[1]);
1318
1319                         } else if (args[0].equals("-updateSetList")) {
1320                                 iotinst.updateSetList("../SupportedDevices/"+args[1]);
1321
1322                         } else if (args[0].equals("-updateIDSubtype")) {
1323                                 iotinst.updateIDSub("../SupportedDevices/"+args[1]);
1324
1325                         } else {
1326                                 System.out.println("IoTInstaller: ERROR: Wrong input parameters!");
1327                                 iotinst.helpMessages();
1328
1329                         }
1330                 } else {
1331                         System.out.println("IoTInstaller: ERROR: No input parameters detected!");
1332                         iotinst.helpMessages();
1333                 }
1334         }
1335 }