KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > projectmanagement > presentation > worksheets > Controls


1 /*
2  * projectManagement
3  *
4  * Enhydra super-servlet presentation object
5  *
6  */

7
8 package projectmanagement.presentation.worksheets;
9
10 import projectmanagement.spec.employee.*;
11 import projectmanagement.spec.project.*;
12 import projectmanagement.presentation.*;
13
14 import org.w3c.dom.*;
15 import org.w3c.dom.html.*;
16
17 // Enhydra SuperServlet imports
18
import com.lutris.appserver.server.httpPresentation.HttpPresentation;
19 import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
20 import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
21 import org.enhydra.xml.xmlc.XMLObject;
22
23 // Standard imports
24
import java.io.IOException JavaDoc;
25 import java.util.Calendar JavaDoc;
26
27 /**
28 * Generates the blank HTML page.
29 */

30 public class Controls extends BasePO {
31
32    /**
33     * Constants
34     */

35    private static String JavaDoc EMPLOYEE = "employee";
36    private static String JavaDoc PROJECT = "project";
37    private static String JavaDoc YEAR = "year";
38    private static String JavaDoc MONTH = "month";
39
40    private static String JavaDoc IS_PERSONAL = "isPersonal";
41
42    /**
43     * Superclass method override. Returns 1 or 2, depending on action.
44     */

45    protected int getRequiredAuthLevel() {
46       String JavaDoc isPersonal="";
47       try {
48          isPersonal=this.getComms().request.getParameter(IS_PERSONAL);
49       } catch (Exception JavaDoc ex) {}
50
51       if (isPersonal!=null && isPersonal.equalsIgnoreCase("true")) {
52          return 1;
53       } else {
54          return 2;
55       }
56    }
57
58    /**
59     * Default event. Just show the page.
60     */

61    public XMLObject handleDefault()
62          throws HttpPresentationException {
63
64       ControlsHTML page=new ControlsHTML();
65
66       String JavaDoc employee = this.getComms().request.getParameter(EMPLOYEE);
67       HTMLSelectElement sel=page.getElementCboEmployee();
68       fillEmployeeSelection(page,sel,employee);
69
70       String JavaDoc project = this.getComms().request.getParameter(PROJECT);
71       sel=page.getElementCboProject();
72       fillProjectSelection(page,sel,project);
73
74       String JavaDoc year = this.getComms().request.getParameter(YEAR);
75       String JavaDoc month = this.getComms().request.getParameter(MONTH);
76
77       if (year==null) {
78          year=String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
79       }
80       HTMLInputElement txtYear=page.getElementTxtYear();
81       txtYear.setValue(year);
82
83       if (month==null) {
84          month=String.valueOf(Calendar.getInstance().get(Calendar.MONTH)+1);
85       }
86       fillMonths(page,month);
87
88       String JavaDoc isPersonal=this.getComms().request.getParameter(IS_PERSONAL);
89       if (isPersonal!=null && isPersonal.equalsIgnoreCase("true")) {
90          page.getElementIsPersonal().setValue("true");
91       }
92
93       return page;
94    }
95
96    private void fillEmployeeSelection (ControlsHTML page,HTMLSelectElement cboEmployees,
97          String JavaDoc selectedEmployeeID) throws ProjectManagementPresentationException {
98       HTMLOptionElement optEmployee = page.getElementOptEmployee();
99    
100       String JavaDoc isPersonal="";
101       try {
102          isPersonal=this.getComms().request.getParameter(IS_PERSONAL);
103       } catch (Exception JavaDoc ex) {}
104       if (isPersonal!=null && isPersonal.equalsIgnoreCase("true")) {
105          try {
106             selectedEmployeeID=getUser().getHandle();
107          } catch (Exception JavaDoc ex) {}
108       }
109
110       try {
111    EmployeeManager employeeManager = EmployeeManagerFactory.getEmployeeManager("projectmanagement.business.employee.EmployeeManagerImpl");
112    Employee[] employees=employeeManager.getAllEmployees();
113         
114            // Remove the dummy storyboard text from the prototype HTML
115
optEmployee.removeChild(optEmployee.getFirstChild());
116
117      
118      
119          // set the text to select all employees - default option
120
HTMLOptionElement clonedOption=null;
121          Node optionTextNode=null;
122          if (isPersonal==null || !isPersonal.equalsIgnoreCase("true")) {
123             clonedOption = (HTMLOptionElement)optEmployee.cloneNode(true);
124             clonedOption.setValue("");
125             String JavaDoc all="All";
126             optionTextNode = clonedOption.getOwnerDocument().
127                createTextNode(all);
128             clonedOption.appendChild(optionTextNode);
129              clonedOption.setAttribute("selected","selected");
130             // Do only a shallow copy of the option as we don't want the text child
131
// of the node option
132
cboEmployees.appendChild(clonedOption);
133          }
134
135          // set all employees
136

137    
138          if (employees!=null) {
139             for (int i=0; i<employees.length; i++) {
140                Employee e=employees[i];
141                // if this is personal, fill the combo only with that employ
142
if (isPersonal!=null && isPersonal.equalsIgnoreCase("true") &&
143                      !e.getHandle().equals(selectedEmployeeID)) {
144                   continue;
145                }
146
147                // Now populate the combo with employees
148
// This algorithm is obscure because options
149
// are not normal HTML elements
150
// First populate the option value (the employee database ID).
151
// Then append a text child as the option
152
// text, which is what is displayed as the text
153
// in each row of the select box
154
clonedOption = (HTMLOptionElement)optEmployee.cloneNode(true);
155                clonedOption.setValue(e.getHandle());
156                optionTextNode = clonedOption.getOwnerDocument().
157                    createTextNode(e.getFirstName()+" "+e.getLastName());
158                clonedOption.appendChild(optionTextNode);
159                if (selectedEmployeeID!=null && e.getHandle().equals(selectedEmployeeID)) {
160                    clonedOption.setAttribute("selected","selected");
161                }
162                // Do only a shallow copy of the option as we don't want the text child
163
// of the node option
164
cboEmployees.appendChild(clonedOption);
165             }
166          }
167          cboEmployees.removeChild(optEmployee);
168       } catch(NullPointerException JavaDoc ex) {
169       } catch (Exception JavaDoc ex) {
170          this.writeDebugMsg("Error populating list of employees: " + ex);
171          throw new ProjectManagementPresentationException("Error populating employee list: ", ex);
172       }
173
174       
175    }
176
177    private void fillProjectSelection (ControlsHTML page,HTMLSelectElement cboProjects,
178          String JavaDoc selectedProjectID) throws ProjectManagementPresentationException {
179       HTMLOptionElement optProject = page.getElementOptProject();
180       // Remove the dummy storyboard text from the prototype HTML
181
optProject.removeChild(optProject.getFirstChild());
182
183       String JavaDoc isPersonal="";
184       try {
185          isPersonal=this.getComms().request.getParameter(IS_PERSONAL);
186       } catch (Exception JavaDoc ex) {}
187
188       try {
189  
190          // set the text to select all projects - default option
191
HTMLOptionElement clonedOption = (HTMLOptionElement)optProject.cloneNode(true);
192          clonedOption.setValue("");
193          String JavaDoc all="All";
194          Node optionTextNode = clonedOption.getOwnerDocument().
195             createTextNode(all);
196          clonedOption.appendChild(optionTextNode);
197           clonedOption.setAttribute("selected","selected");
198          // Do only a shallow copy of the option as we don't want the text child
199
// of the node option
200
cboProjects.appendChild(clonedOption);
201
202          // set all projects
203
Project[] projects=null;
204          ProjectManager projectManager = ProjectManagerFactory.getProjectManager("projectmanagement.business.project.ProjectManagerImpl");
205     
206          if (isPersonal!=null && isPersonal.equalsIgnoreCase("true")) {
207             // IMPLEMENT ME!!!
208
projects=projectManager.getAllProjects();
209         
210          } else {
211          projects=projectManager.getAllProjects();
212          }
213          if (projects!=null) {
214             for (int i=0; i<projects.length; i++) {
215                Project p=projects[i];
216                // Now populate the combo with projects
217
// This algorithm is obscure because options
218
// are not normal HTML elements
219
// First populate the option value (the project database ID).
220
// Then append a text child as the option
221
// text, which is what is displayed as the text
222
// in each row of the select box
223
clonedOption = (HTMLOptionElement)optProject.cloneNode(true);
224                clonedOption.setValue(p.getHandle());
225                optionTextNode = clonedOption.getOwnerDocument().
226                    createTextNode(p.getName());
227                clonedOption.appendChild(optionTextNode);
228                if (selectedProjectID!=null && p.getHandle().equals(selectedProjectID)) {
229                    clonedOption.setAttribute("selected","selected");
230                }
231                // Do only a shallow copy of the option as we don't want the text child
232
// of the node option
233
cboProjects.appendChild(clonedOption);
234             }
235          }
236       cboProjects.removeChild(optProject);
237      
238       } catch(NullPointerException JavaDoc ex) {
239       } catch (Exception JavaDoc ex) {
240              ex.printStackTrace();
241          this.writeDebugMsg("Error populating list of projects: " + ex);
242          throw new ProjectManagementPresentationException("Error populating project list: ", ex);
243       }
244
245      
246    }
247
248    private void fillMonths(ControlsHTML page,String JavaDoc selectedMonthID)
249          throws ProjectManagementPresentationException {
250       HTMLSelectElement cboMonth=page.getElementCboMonth();
251       HTMLOptionElement optMonth = page.getElementOptMonth();
252       // Remove the dummy storyboard text from the prototype HTML
253
optMonth.removeChild(optMonth.getFirstChild());
254
255       try {
256          // set all months
257
HTMLOptionElement clonedOption = (HTMLOptionElement)optMonth.cloneNode(true);
258          String JavaDoc all="All";
259          clonedOption.setValue("");
260          Node optionTextNode = clonedOption.getOwnerDocument().
261             createTextNode(all);
262          clonedOption.appendChild(optionTextNode);
263          cboMonth.appendChild(clonedOption);
264
265          for (int i=1; i<=12; i++) {
266             // Now populate the combo with months
267
// This algorithm is obscure because options
268
// are not normal HTML elements
269
// First populate the option value (the month database ID).
270
// Then append a text child as the option
271
// text, which is what is displayed as the text
272
// in each row of the select box
273
clonedOption = (HTMLOptionElement)optMonth.cloneNode(true);
274             String JavaDoc curMonth=String.valueOf(i);
275             clonedOption.setValue(curMonth);
276             optionTextNode = clonedOption.getOwnerDocument().
277                createTextNode(curMonth);
278             clonedOption.appendChild(optionTextNode);
279             if (selectedMonthID!=null && curMonth.equals(selectedMonthID)) {
280                 clonedOption.setAttribute("selected","selected");
281             }
282             // Do only a shallow copy of the option as we don't want the text child
283
// of the node option
284
cboMonth.appendChild(clonedOption);
285          }
286       cboMonth.removeChild(optMonth);
287       } catch(NullPointerException JavaDoc ex) {
288       } catch (Exception JavaDoc ex) {
289          this.writeDebugMsg("Error populating list of months: " + ex);
290          throw new ProjectManagementPresentationException("Error populating month list: ", ex);
291       }
292
293       
294    }
295 }
296
Popular Tags