KickJava   Java API By Example, From Geeks To Geeks.

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


1 package projectmanagement.presentation.worksheets;
2
3 import projectmanagement.presentation.*;
4 import projectmanagement.spec.timewage.*;
5 import projectmanagement.spec.employee.*;
6 import projectmanagement.spec.project.*;
7 import com.lutris.appserver.server.httpPresentation.*;
8 import org.enhydra.xml.xmlc.XMLObject;
9
10 import org.w3c.dom.*;
11 import org.w3c.dom.html.*;
12
13 import java.util.Calendar JavaDoc;
14 import java.sql.Date JavaDoc;
15 import java.sql.Time JavaDoc;
16 import java.lang.*;
17 import java.util.*;
18
19 /**
20  * Shows a list of work sheets to administer.
21  *
22  * @author Sasa Bojanic, Nenad Stefanovic
23  * @version 1.0
24  */

25 public class Administering extends BasePO {
26
27    private static String JavaDoc EMPLOYEE = "employee";
28    private static String JavaDoc PROJECT = "project";
29    private static String JavaDoc YEAR = "year";
30    private static String JavaDoc MONTH = "month";
31
32    private static String JavaDoc IS_PERSONAL = "isPersonal";
33
34    /**
35     * Superclass method override. Returns 1 or 2, depending on action.
36     */

37    protected int getRequiredAuthLevel() {
38       String JavaDoc isPersonal="";
39       try {
40          isPersonal=this.getComms().request.getParameter(IS_PERSONAL);
41       } catch (Exception JavaDoc ex) {}
42
43       if (isPersonal!=null && isPersonal.equalsIgnoreCase("true")) {
44          return 1;
45       } else {
46          return 2;
47       }
48    }
49
50    /**
51     * Default event. Just show the page.
52     */

53    public XMLObject handleDefault()
54          throws HttpPresentationException {
55
56       AdministeringHTML page = new AdministeringHTML();
57
58       String JavaDoc employee = this.getComms().request.getParameter(EMPLOYEE);
59       if (employee!=null && employee.length()==0) {
60          employee=null;
61       }
62       String JavaDoc project = this.getComms().request.getParameter(PROJECT);
63       if (project!=null && project.length()==0) {
64          project=null;
65       }
66       String JavaDoc year = this.getComms().request.getParameter(YEAR);
67       if (year!=null && year.length()==0) {
68          year=null;
69       }
70       String JavaDoc month = this.getComms().request.getParameter(MONTH);
71       if (month!=null && month.length()==0) {
72          month=null;
73       }
74
75       // fetches the table element from HTML document
76
HTMLTableElement table = page.getElementTblWorkSheets();
77
78       try {
79          WorkSheet[] workSheets=null;
80         
81          WorkSheetManager workSheetManager = WorkSheetManagerFactory.
82                getWorkSheetManager("projectmanagement.business.timewage.WorkSheetManagerImpl");
83         
84          if (year==null) {
85                
86         workSheets=workSheetManager.getAllWorkSheetsForEmployeeProjectPair(employee,project);
87          } else {
88             Calendar JavaDoc c1=Calendar.getInstance();
89             Calendar JavaDoc c2=Calendar.getInstance();
90             if (month==null) {
91                c1.set(Integer.parseInt(year),1,1);
92                c2.set(Integer.parseInt(year),12,31);
93             } else {
94                c1.set(Integer.parseInt(year),Integer.parseInt(month)-1,1);
95                c2.set( Integer.parseInt(year),Integer.parseInt(month)-1,c1.getActualMaximum(Calendar.DAY_OF_MONTH) );
96             }
97          workSheets=workSheetManager.getAllWorkSheetsForEmployeeProjectPairFromDate1ToDate2(
98                   employee,project,new Date JavaDoc(c1.getTimeInMillis()),new Date JavaDoc(c2.getTimeInMillis()));
99          }
100
101
102          double sumOfHours=0;
103          double sumOfMoney=0;
104          if (workSheets!=null) {
105             HoursAndMoney ham=new HoursAndMoney();
106             for (int i=0; i<workSheets.length; i++) {
107                // creating row in HTML table which describes table properties
108
HTMLTableRowElement htmlRE=createNewRow(page,workSheets[i],ham);
109                // appending row at the end of table
110
table.appendChild(htmlRE);
111             }
112             sumOfHours=ham.getHours();
113             sumOfMoney=ham.getMoney();
114             sumOfMoney=(double)(Math.round(sumOfMoney*100))/100;
115             sumOfHours=(double)(Math.round(sumOfHours*100))/100;
116          }
117          page.setTextTxtSumOfHours(String.valueOf(sumOfHours));
118          page.setTextTxtSumOfMoney(String.valueOf(sumOfMoney));
119          //to remove back button
120
page.getElementBackElement().getParentNode().removeChild( page.getElementBackElement() );
121      
122        //Remove template selection row from table
123
table.removeChild(page.getElementTemplateRow());
124
125       String JavaDoc errorMsg = this.getSessionData().getAndClearUserMessage();
126       /*if(null != errorMsg) {
127          page.setTextLblErrorText(errorMsg);
128       } else {
129          page.getElementLblErrorText().getParentNode().removeChild(page.getElementLblErrorText());
130       }*/

131
132       String JavaDoc isPersonal=this.getComms().request.getParameter(IS_PERSONAL);
133       if (isPersonal!=null && isPersonal.equalsIgnoreCase("true")) {
134          HTMLInputElement ip=page.getElementIsPersonal();
135          ip.setValue("true");
136         }
137       } catch(NullPointerException JavaDoc ex) {
138       } catch(Exception JavaDoc ex) {
139          this.writeDebugMsg("Error populating list of work sheets: " + ex);
140          throw new ProjectManagementPresentationException("Error getting list of work sheets: ", ex);
141       }
142
143     
144      
145       // write out HTML
146
return page;
147    }
148
149    /**
150    * Creates the new row of HTML table element, based on given parameters.
151    */

152    private HTMLTableRowElement createNewRow(AdministeringHTML administeringHTML,
153          WorkSheet workSheet,HoursAndMoney ham) {
154
155       String JavaDoc workSheetID="";
156
157       try {
158          workSheetID=workSheet.getHandle();
159          PayRate pr=workSheet.getPayRate();
160          Employee emp=pr.getEmployee();
161          Project prj=pr.getProject();
162          Time JavaDoc timeStarted=workSheet.getTimeStarted();
163          Time JavaDoc timeFinished=workSheet.getTimeFinished();
164          administeringHTML.setTextTxtEmployee(emp.getFirstName()+" "+
165             emp.getLastName());
166          administeringHTML.setTextTxtProject(prj.getName());
167          Calendar JavaDoc cal=new GregorianCalendar();
168          cal.setTime(workSheet.getWorkingDate());
169          String JavaDoc wd=String.valueOf(cal.get(Calendar.YEAR))+"-"+
170                String.valueOf(cal.get(Calendar.MONTH)+1)+"-"+
171                String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
172          administeringHTML.setTextTxtWorkingDate(wd);
173          cal.setTime(timeStarted);
174          String JavaDoc tst=String.valueOf(cal.get(Calendar.HOUR_OF_DAY))+":"+
175                String.valueOf(cal.get(Calendar.MINUTE));
176          administeringHTML.setTextTxtTimeStarted(tst);
177          cal.setTime(timeFinished);
178          String JavaDoc tfn=String.valueOf(cal.get(Calendar.HOUR_OF_DAY))+":"+
179                String.valueOf(cal.get(Calendar.MINUTE));
180          administeringHTML.setTextTxtTimeFinished(tfn);
181          double mny=(double)(Math.round(
182                ham.calculate(calculateTimeDifferenceInHours(timeStarted,timeFinished),
183                prj.getMoneyPerHour(),pr.getRate())*100))/100;
184          administeringHTML.setTextTxtMoney(String.valueOf(mny));
185       } catch (Exception JavaDoc ex) {}
186
187       // image to get details on work sheet
188
HTMLImageElement detailsImg=administeringHTML.getElementImgDetails();
189       detailsImg.setName(workSheetID);
190
191       // image to modify work sheet information
192
HTMLImageElement modifyImg=administeringHTML.getElementImgModify();
193       modifyImg.setName(workSheetID);
194
195       // image to delete work sheet
196
HTMLImageElement deleteImg=administeringHTML.getElementImgDelete();
197       deleteImg.setName(workSheetID);
198
199       HTMLTableRowElement row=(HTMLTableRowElement)administeringHTML.
200          getElementTemplateRow().cloneNode(true);
201
202       return row;
203    }
204
205    private double calculateTimeDifferenceInHours (Time JavaDoc timeStarted,
206          Time JavaDoc timeFinished) {
207       long ts=timeStarted.getTime();
208       long tf=timeFinished.getTime();
209       long diffInMilliseconds=tf-ts;
210       double diffInHours=diffInMilliseconds/3600000.0;
211       return diffInHours;
212    }
213
214    class HoursAndMoney {
215       private double hours=0;
216       private double money=0;
217
218       public double calculate (double hrs,double moneyPerHour,double payRate) {
219          hours+=hrs;
220          double dm=hrs*moneyPerHour*payRate;
221          money+=dm;
222          return dm;
223       }
224
225       public double getHours () {
226          return hours;
227       }
228
229       public double getMoney () {
230          return money;
231       }
232    }
233
234 }
235
236
Popular Tags