View Javadoc

1   package org.wcb.plugins.beans;
2   
3   import org.wcb.autohome.AutoHomeDaemonSession;
4   import org.wcb.autohome.exceptions.HomeException;
5   import org.wcb.autohome.interfaces.IX10Module;
6   import org.wcb.autohome.interfaces.X10DeviceConstants;
7   import org.wcb.autohome.implementations.X10Module;
8   
9   import javax.servlet.http.HttpServletRequest;
10  import java.util.Vector;
11  import java.util.Enumeration;
12  
13  /***
14   * Copyright (C) 1999  Walter Bogaardt
15   *
16   * This library is free software; you can redistribute it and/or
17   * modify it under the terms of the GNU Lesser General Public
18   * License as published by the Free Software Foundation; either
19   * version 2 of the License, or (at your option) any later version.
20   *
21   * This library is distributed in the hope that it will be useful,
22   * but WITHOUT ANY WARRANTY; without even the implied warranty of
23   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24   * Lesser General Public License for more details.
25   *
26   * You should have received a copy of the GNU Lesser General Public
27   * License along with this library; if not, write to the Free Software
28   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
29   *
30   *   Project:   Home Automation Interface
31   *  Filename:  $Id: ModuleController.java,v 1.14 2004/01/16 00:53:43 wbogaardt Exp $
32   *  Abstract:  Bean for modules page.
33   *
34   * $Log: ModuleController.java,v $
35   * Revision 1.14  2004/01/16 00:53:43  wbogaardt
36   * Fixed a very obscure bug with the Macro Panel that it didn't added new
37   * x10 devices to the drop down of available x10 device for the macro. Modified Macro triggers to change the events to integer verses strings cleaner this way.
38   *
39   * Revision 1.13  2004/01/15 21:05:20  wbogaardt
40   * major revamp of Modules and interfaces changes overall structure of how information is stored
41   *
42   * Revision 1.12  2003/12/18 00:02:52  wbogaardt
43   * more javadoc comments
44   *
45   * Revision 1.11  2003/12/17 23:34:57  wbogaardt
46   * fixed capturing of house code to pass it to upper case.
47   *
48   * Revision 1.10  2003/12/17 18:14:06  wbogaardt
49   * moved strings to static variables for both jsp and servlet
50   *
51   * Revision 1.9  2003/12/16 23:03:09  wbogaardt
52   * added update and delete function to events controller and removed dead imports in module controller
53   *
54   * Revision 1.8  2003/12/12 23:17:36  wbogaardt
55   * javadoc comments refactored methods so they are more descriptive
56   *
57   * Revision 1.7  2003/12/11 23:10:12  wbogaardt
58   * cleaned up exception handeling and logging of system.out messages
59   *
60   * Revision 1.6  2003/12/10 21:06:53  wbogaardt
61   * modules page adds but does not reload from saved state
62   *
63   * Revision 1.5  2003/12/09 23:11:42  wbogaardt
64   *
65   * C: ----------------------------------------------------------------------
66   *
67   * Revision 1.4  2003/12/09 00:37:06  wbogaardt
68   * moved methods from authomAdminSession to parent class
69   *
70   * Revision 1.3  2003/12/09 00:27:46  wbogaardt
71   * modified loading of session information
72   *
73   *
74   */
75  public class ModuleController {
76  
77      private Vector vModules;
78      private String sStatus_change;
79      public static String X10_MODULE_TYPE = "type";
80      public static String X10_MODULE_ID = "fulldevicecode";
81      public static String DESCRIPTION = "description";
82      public static String UPDATE_BUTTON = "update";
83      public static String DELETE_BUTTON = "delete";
84      public static String ADD_BUTTON = "add";
85      public static String MODULE_ROW = "row";
86  
87      public ModuleController(){
88          vModules = AutoHomeDaemonSession.getInstance().loadAllX10Devices();
89      }
90  
91      /***
92       * String that is returned for the jsp page to know the status of
93       * the user's last form submit. If the form processed successfully or not.
94       * @return Printable message
95       */
96      public String getStatus(){
97          return sStatus_change;
98      }
99  
100     /***
101      * Takes the interface of a X10 module and returns
102      * a string, which displays an HTML image
103      *
104      * @param imodule Interface to get html image name
105      * @return string of "Plug.gif" or "Bulb.gif"
106      */
107     public String getHtmlImage(IX10Module imodule)
108     {
109         if(imodule.getType() == X10DeviceConstants.APPLIANCE_MODULE_ON)
110         {
111             return "Plug.gif";
112         }
113         return "Bulb.gif";
114     }
115 
116     /***
117      * Get all the X10 modules that have been saved and
118      * return them as a vector.
119      * @return  All x10 modules
120      */
121     public Vector getAllX10Modules(){
122         Vector _returnVal = vModules;
123         if(_returnVal!=null){
124             return _returnVal;
125         }
126         _returnVal=new Vector();
127         _returnVal.addElement(new X10Module());
128         return _returnVal;
129     }
130 
131     /***
132      * Saves the form input to an x10 module object then stores this into
133      * the file system.
134      * @param sModuleID House code value of A1 - P15
135      * @param sName Name of the module
136      * @param sDesc Description of this module
137      * @param iType Value of 1 for lamp 2 for appliance
138      * @return  success on saving file
139      */
140     public boolean setSaveModule(String sModuleID,String sName, String sDesc, int iType)
141     {
142         X10Module module = new X10Module(this.getHouseCode(sModuleID),this.getDeviceCode(sModuleID), sName,sDesc, iType);
143         boolean bReturnValue = false;
144         if(vModules != null)
145         {
146             boolean matched = false;
147             Enumeration eModules = vModules.elements();
148             int iCounter = 0;
149             while(eModules.hasMoreElements())
150             {
151                 IX10Module i_module = (IX10Module)eModules.nextElement();
152                 if(i_module.getFullDeviceCode().equalsIgnoreCase(module.getFullDeviceCode()))
153                 {
154                     matched = true;
155                     vModules.setElementAt(module,iCounter);
156                     bReturnValue = this.saveAllData();
157                 }
158                 iCounter++;
159             }
160             if(!matched)
161             {
162                 vModules.add(module);
163                 bReturnValue = this.saveAllData();
164             }
165 
166         } else if (vModules == null) {
167             vModules = new Vector();
168             vModules.addElement(module);
169             bReturnValue = this.saveAllData();
170         }
171         return bReturnValue;
172     }
173 
174     /***
175      * This deletes the table row from the table model
176      * and then updates the Hash Table to set the
177      * Key value(ModuleID) to null;
178      */
179     public void setDeleteRow(int iRow) {
180         vModules.remove(iRow);
181         this.saveAllData();
182     }
183 
184     /***
185      * Used to process the modules.jsp page requests
186      *
187      * @param request process servlet request
188      */
189     public void processRequest(HttpServletRequest request){
190         sStatus_change = "";
191         if(request.getMethod().equalsIgnoreCase("post")){
192             String action = request.getParameter("action");
193             int iType = Integer.parseInt(request.getParameter(X10_MODULE_TYPE));
194             if(action!=null){
195                 if(action.equalsIgnoreCase(UPDATE_BUTTON)){
196                     if(this.setSaveModule(request.getParameter(X10_MODULE_ID),"Name",request.getParameter(DESCRIPTION),iType))
197                         sStatus_change = "Updated Module <B>"+request.getParameter(X10_MODULE_ID)+"</B> - <B>"+ request.getParameter(DESCRIPTION)+"</B>";
198                     else
199                         sStatus_change = "Failed to update Module";
200                 }
201                 if(action.equalsIgnoreCase(DELETE_BUTTON)){
202                     int iRow = Integer.parseInt(request.getParameter("row"));
203                     this.setDeleteRow(iRow);
204                     sStatus_change = "Removed Module <B>"+request.getParameter(X10_MODULE_ID)+"</B> - "+ request.getParameter(DESCRIPTION);
205                 }
206                 if(action.equalsIgnoreCase(ADD_BUTTON)){
207                     if(this.setSaveModule(request.getParameter(X10_MODULE_ID),"Name",request.getParameter(DESCRIPTION),iType))
208                         sStatus_change = "ADDED Module <B>"+request.getParameter(X10_MODULE_ID)+"</B> - "+ request.getParameter(DESCRIPTION);
209                     else
210                         sStatus_change = "Failed to add module";
211                 }
212             }
213         }
214     }
215 
216     /***
217      * Save the entire table model into a properties
218      * Hash table key for quick access and storage;
219      */
220     private boolean saveAllData(){
221         AutoHomeDaemonSession.getInstance().saveAllX10Devices(vModules);
222          try
223          {
224         AutoHomeDaemonSession.getInstance().saveFile(AutoHomeDaemonSession.getInstance().getServletPath()+System.getProperty("file.separator")+"x10default.x10");
225          }
226         catch(HomeException he)
227          {
228              System.err.println("Home exception "+he);
229              return false;
230          }
231         return true;
232     }
233 
234     /***
235      * Gets the first position in the DeviceID text field
236      * and returns a Upper Case Character value.
237      */
238     private char getHouseCode(String sDeviceID)
239     {
240         char cReturnValue='A';
241         try
242         {
243             cReturnValue = sDeviceID.toUpperCase().charAt(0);
244         }
245         catch(Exception err)
246         {
247             cReturnValue = 'A';
248         }
249         return cReturnValue;
250     }
251 
252     /***
253      * Gets the last positions in the DeviceID text field
254      * and returns an integer value.  If the entered integers
255      * are greater than 16 then 16 is returned;
256      */
257     private int getDeviceCode(String sDeviceID)
258     {
259         int returnValue = 0;
260         try
261         {
262             returnValue = Integer.parseInt(sDeviceID.substring(1));
263         }
264         catch(Exception err)
265         {
266                   returnValue = 1;
267         }
268         if (returnValue >16)
269         {
270             //invalude digit
271             returnValue = 16;
272         }
273         return returnValue;
274     }
275 }