KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.GregorianCalendar JavaDoc;
15 import java.sql.Date JavaDoc;
16 import java.sql.Time JavaDoc;
17 import java.lang.*;
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 AdministeringSearch extends BasePO {
26
27    private static String JavaDoc EMPLOYEE = "employee";
28    private static String JavaDoc PROJECT = "project";
29    private static String JavaDoc FROMYEAR = "fromYear";
30    private static String JavaDoc FROMMONTH = "fromMonth";
31    private static String JavaDoc FROMDAY = "fromDay";
32    private static String JavaDoc TOYEAR = "toYear";
33    private static String JavaDoc TOMONTH = "toMonth";
34    private static String JavaDoc TODAY = "toDay";
35
36    private static String JavaDoc IS_PERSONAL = "isPersonal";
37
38    /**
39     * Superclass method override. Returns 1 or 2, depending on action.
40     */

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

57    public XMLObject handleDefault()
58          throws HttpPresentationException,ClientPageRedirectException {
59
60      AdministeringHTML page = new AdministeringHTML();
61       String JavaDoc employee=null;
62       String JavaDoc project=null;
63       boolean multiple = false;
64         //for advanced search
65
java.util.ArrayList JavaDoc employeeIDs=new java.util.ArrayList JavaDoc();;
66       int index=0;
67       String JavaDoc employeeID=EMPLOYEE+String.valueOf("0");
68
69       while ( this.getComms().request.getParameter(employeeID) != null ) {
70         employeeIDs.add( this.getComms().request.getParameter(employeeID) );
71         index++;
72         employeeID = EMPLOYEE+String.valueOf(index);
73       }
74
75         //checking if there are multiple selection or is ALL selected
76
try{
77       if( employeeIDs.size() == 1 || employeeIDs.get(0).toString().equals("") ) {
78         employee = employeeIDs.get(0).toString();
79         if (employee!=null && employee.length()==0) {
80              employee=null;
81         }
82       } else {
83         multiple = true;
84       }
85      }catch(IndexOutOfBoundsException JavaDoc e){}
86       java.util.ArrayList JavaDoc projectIDs=new java.util.ArrayList JavaDoc();
87       index=0;
88       String JavaDoc projectID=PROJECT+String.valueOf("0");
89       while ( this.getComms().request.getParameter(projectID) != null ) {
90        
91         projectIDs.add( this.getComms().request.getParameter(projectID) );
92         index++;
93         projectID = PROJECT+String.valueOf(index);
94        
95       }
96         //checking if there are multiple selection or is ALL selected
97
if( projectIDs.size() == 1 || projectIDs.get(0).toString().equals("")) {
98         project = projectIDs.get(0).toString();
99         if (project!=null && project.length()==0) {
100              project=null;
101         }
102       } else {
103         multiple = true;
104       }
105
106
107       String JavaDoc fromYear = this.getComms().request.getParameter(FROMYEAR);
108       if (fromYear!=null && fromYear.length()==0) {
109          fromYear=null;
110       }
111       String JavaDoc fromMonth = this.getComms().request.getParameter(FROMMONTH);
112       if (fromMonth!=null && fromMonth.length()==0) {
113          fromMonth=null;
114       }
115       String JavaDoc fromDay = this.getComms().request.getParameter(FROMDAY);
116       if (fromDay!=null && fromDay.length()==0) {
117          fromDay=null;
118       }
119
120       String JavaDoc toYear = this.getComms().request.getParameter(TOYEAR);
121       if ( toYear != null && toYear.length()==0) {
122          toYear=null;
123       }
124       String JavaDoc toMonth = this.getComms().request.getParameter(TOMONTH);
125       if (toMonth!=null && toMonth.length()==0) {
126          toMonth=null;
127       }
128       String JavaDoc toDay = this.getComms().request.getParameter(TODAY);
129       if (toDay!=null && toDay.length()==0) {
130          toDay=null;
131       }
132       boolean checkDates = true;
133       String JavaDoc params = "";
134       if ( !checkDate( fromYear , fromMonth , fromDay ) ) {
135          params += "?checkFromDate=false";
136          checkDates = false;
137       }
138       if ( !checkDate( toYear , toMonth , toDay ) ) {
139          if ( !checkDates ) {
140           params += "&checkToDate=false";
141          } else {
142           params += "?checkToDate=false";
143         }
144          checkDates = false;
145       }
146       if ( !checkDates ) {
147         throw new ClientPageRedirectException("Search.po"+params);
148       }
149
150       HTMLFormElement form = page.getElementForm();
151       form.setAttribute("target","_self");
152
153
154       // fetches the table element from HTML document
155
HTMLTableElement table = page.getElementTblWorkSheets();
156       try {
157          WorkSheet[] workSheets=null;
158          WorkSheetManager workSheetManager = WorkSheetManagerFactory.
159                getWorkSheetManager("projectmanagement.business.timewage.WorkSheetManagerImpl");
160       
161     
162          Calendar JavaDoc c1=Calendar.getInstance();
163          Calendar JavaDoc c2=Calendar.getInstance();
164
165          if( fromYear!=null && fromMonth!=null && fromDay!=null && toYear!=null && toMonth!=null && toDay!=null ) {
166
167              c1.set( Integer.parseInt(fromYear),Integer.parseInt(fromMonth)-1,Integer.parseInt(fromDay) );
168              c2.set( Integer.parseInt(toYear),Integer.parseInt(toMonth)-1,Integer.parseInt(toDay) );
169
170              if ( !multiple ) {
171                 workSheets=workSheetManager.
172                    getAllWorkSheetsForEmployeeProjectPairFromDate1ToDate2(
173                     employee,project,new Date JavaDoc(c1.getTimeInMillis()),new Date JavaDoc(c2.getTimeInMillis()));
174              } else {
175                 workSheets=workSheetManager.
176                    getAllWorksheetsForEmployeeArrayAndProjectArrayBetweenDates(
177                     employeeIDs,projectIDs,new Date JavaDoc(c1.getTimeInMillis()),new Date JavaDoc(c2.getTimeInMillis()));
178              }
179          }
180          double sumOfHours=0;
181          double sumOfMoney=0;
182          if (workSheets!=null) {
183             HoursAndMoney ham=new HoursAndMoney();
184             for (int i=0; i<workSheets.length; i++) {
185                // creating row in HTML table which describes table properties
186
HTMLTableRowElement htmlRE=createNewRow(page,workSheets[i],ham);
187                // appending row at the end of table
188
table.appendChild(htmlRE);
189             }
190             sumOfHours=ham.getHours();
191             sumOfMoney=ham.getMoney();
192             sumOfMoney=(double)(Math.round(sumOfMoney*100))/100;
193             sumOfHours=(double)(Math.round(sumOfHours*100))/100;
194          }
195          page.setTextTxtSumOfHours(String.valueOf(sumOfHours));
196          page.setTextTxtSumOfMoney(String.valueOf(sumOfMoney));
197  
198  /* Catch Null pointer exception ( we canot make a instances of classes from business layer when we run ProjectManagement_pres )
199  * We need to allow ProjectManagement_pres to be functional , response
200  * will be default HTML page
201  */

202  
203      } catch(NullPointerException JavaDoc ex) {
204          return page;
205      } catch(Exception JavaDoc ex) {
206          this.writeDebugMsg("Error populating list of work sheets: " + ex);
207          throw new ProjectManagementPresentationException("Error getting list of work sheets: ", ex);
208       }
209
210       //Remove template selection row from table
211
table.removeChild(page.getElementTemplateRow());
212
213       String JavaDoc errorMsg = this.getSessionData().getAndClearUserMessage();
214       /*if(null != errorMsg) {
215          page.setTextLblErrorText(errorMsg);
216       } else {
217          page.getElementLblErrorText().getParentNode().removeChild(page.getElementLblErrorText());
218       }*/

219
220       String JavaDoc isPersonal=this.getComms().request.getParameter(IS_PERSONAL);
221       if (isPersonal!=null && isPersonal.equalsIgnoreCase("true")) {
222          HTMLInputElement ip=page.getElementIsPersonal();
223          ip.setValue("true");
224       }
225       // write out HTML
226
return page;
227    }
228    /**
229    * Check if given date is OK
230    */

231     private boolean checkDate(String JavaDoc year, String JavaDoc month, String JavaDoc day) {
232         GregorianCalendar JavaDoc cal=new GregorianCalendar JavaDoc();
233         boolean retVal=true;
234         cal.setLenient(false);
235         cal.set( Integer.parseInt(year),Integer.parseInt(month)-1,1 );
236         if ( cal.getActualMaximum(Calendar.DAY_OF_MONTH) < Integer.parseInt(day) ) {
237             retVal = false;
238         } else if( cal.isLeapYear( Integer.parseInt(year) ) && Integer.parseInt(month) == 1 && Integer.parseInt(day) == 29 ) {
239             retVal = true;
240         } else {
241             retVal = true;
242         }
243         return retVal;
244     }
245
246    /**
247    * Creates the new row of HTML table element, based on given parameters.
248    */

249    private HTMLTableRowElement createNewRow(AdministeringHTML administeringHTML,
250          WorkSheet workSheet,HoursAndMoney ham) {
251
252       String JavaDoc workSheetID="";
253
254       try {
255          workSheetID=workSheet.getHandle();
256          PayRate pr=workSheet.getPayRate();
257          Employee emp=pr.getEmployee();
258          Project prj=pr.getProject();
259          Time JavaDoc timeStarted=workSheet.getTimeStarted();
260          Time JavaDoc timeFinished=workSheet.getTimeFinished();
261          administeringHTML.setTextTxtEmployee(emp.getFirstName()+" "+
262             emp.getLastName());
263          administeringHTML.setTextTxtProject(prj.getName());
264          Calendar JavaDoc cal=new GregorianCalendar JavaDoc();
265          cal.setTime(workSheet.getWorkingDate());
266          String JavaDoc wd=String.valueOf(cal.get(Calendar.YEAR))+"-"+
267                String.valueOf(cal.get(Calendar.MONTH)+1)+"-"+
268                String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
269          administeringHTML.setTextTxtWorkingDate(wd);
270          cal.setTime(timeStarted);
271          String JavaDoc tst=String.valueOf(cal.get(Calendar.HOUR))+":"+
272                String.valueOf(cal.get(Calendar.MINUTE));
273          administeringHTML.setTextTxtTimeStarted(tst);
274          cal.setTime(timeFinished);
275          String JavaDoc tfn=String.valueOf(cal.get(Calendar.HOUR))+":"+
276                String.valueOf(cal.get(Calendar.MINUTE));
277          administeringHTML.setTextTxtTimeFinished(tfn);
278          double mny=(double)(Math.round(
279                ham.calculate(calculateTimeDifferenceInHours(timeStarted,timeFinished),
280             prj.getMoneyPerHour(),pr.getRate())*100))/100;
281          administeringHTML.setTextTxtMoney(String.valueOf(mny));
282       } catch (Exception JavaDoc ex) {}
283
284       // image to get details on work sheet
285
HTMLImageElement detailsImg=administeringHTML.getElementImgDetails();
286       detailsImg.setName(workSheetID);
287
288       // image to modify work sheet information
289
HTMLImageElement modifyImg=administeringHTML.getElementImgModify();
290       modifyImg.setName(workSheetID);
291
292       // image to delete work sheet
293
HTMLImageElement deleteImg=administeringHTML.getElementImgDelete();
294       deleteImg.setName(workSheetID);
295
296       HTMLTableRowElement row=(HTMLTableRowElement)administeringHTML.
297          getElementTemplateRow().cloneNode(true);
298
299       return row;
300    }
301
302    private double calculateTimeDifferenceInHours (Time JavaDoc timeStarted,
303          Time JavaDoc timeFinished) {
304       long ts=timeStarted.getTime();
305       long tf=timeFinished.getTime();
306       long diffInMilliseconds=tf-ts;
307       double diffInHours=diffInMilliseconds/3600000.0;
308       return diffInHours;
309    }
310
311    class HoursAndMoney {
312       private double hours=0;
313       private double money=0;
314
315       public double calculate (double hrs,double moneyPerHour,double payRate) {
316          hours+=hrs;
317          double dm=hrs*moneyPerHour*payRate;
318          money+=dm;
319          return dm;
320       }
321
322       public double getHours () {
323          return hours;
324       }
325
326       public double getMoney () {
327          return money;
328       }
329    }
330
331 }
332
333
Popular Tags