001: package projectmanagement.presentation.worksheets;
002:
003: import projectmanagement.presentation.*;
004: import projectmanagement.spec.timewage.*;
005: import projectmanagement.spec.employee.*;
006: import projectmanagement.spec.project.*;
007: import com.lutris.appserver.server.httpPresentation.*;
008: import org.enhydra.xml.xmlc.XMLObject;
009:
010: import org.w3c.dom.*;
011: import org.w3c.dom.html.*;
012:
013: import java.util.Calendar;
014: import java.sql.Date;
015: import java.sql.Time;
016: import java.lang.*;
017: import java.util.*;
018:
019: /**
020: * Shows a list of work sheets to administer.
021: *
022: * @author Sasa Bojanic, Nenad Stefanovic
023: * @version 1.0
024: */
025: public class Administering extends BasePO {
026:
027: private static String EMPLOYEE = "employee";
028: private static String PROJECT = "project";
029: private static String YEAR = "year";
030: private static String MONTH = "month";
031:
032: private static String IS_PERSONAL = "isPersonal";
033:
034: /**
035: * Superclass method override. Returns 1 or 2, depending on action.
036: */
037: protected int getRequiredAuthLevel() {
038: String isPersonal = "";
039: try {
040: isPersonal = this .getComms().request
041: .getParameter(IS_PERSONAL);
042: } catch (Exception ex) {
043: }
044:
045: if (isPersonal != null && isPersonal.equalsIgnoreCase("true")) {
046: return 1;
047: } else {
048: return 2;
049: }
050: }
051:
052: /**
053: * Default event. Just show the page.
054: */
055: public XMLObject handleDefault() throws HttpPresentationException {
056:
057: AdministeringHTML page = new AdministeringHTML();
058:
059: String employee = this .getComms().request
060: .getParameter(EMPLOYEE);
061: if (employee != null && employee.length() == 0) {
062: employee = null;
063: }
064: String project = this .getComms().request.getParameter(PROJECT);
065: if (project != null && project.length() == 0) {
066: project = null;
067: }
068: String year = this .getComms().request.getParameter(YEAR);
069: if (year != null && year.length() == 0) {
070: year = null;
071: }
072: String month = this .getComms().request.getParameter(MONTH);
073: if (month != null && month.length() == 0) {
074: month = null;
075: }
076:
077: // fetches the table element from HTML document
078: HTMLTableElement table = page.getElementTblWorkSheets();
079:
080: try {
081: WorkSheet[] workSheets = null;
082:
083: WorkSheetManager workSheetManager = WorkSheetManagerFactory
084: .getWorkSheetManager("projectmanagement.business.timewage.WorkSheetManagerImpl");
085:
086: if (year == null) {
087:
088: workSheets = workSheetManager
089: .getAllWorkSheetsForEmployeeProjectPair(
090: employee, project);
091: } else {
092: Calendar c1 = Calendar.getInstance();
093: Calendar c2 = Calendar.getInstance();
094: if (month == null) {
095: c1.set(Integer.parseInt(year), 1, 1);
096: c2.set(Integer.parseInt(year), 12, 31);
097: } else {
098: c1.set(Integer.parseInt(year), Integer
099: .parseInt(month) - 1, 1);
100: c2.set(Integer.parseInt(year), Integer
101: .parseInt(month) - 1, c1
102: .getActualMaximum(Calendar.DAY_OF_MONTH));
103: }
104: workSheets = workSheetManager
105: .getAllWorkSheetsForEmployeeProjectPairFromDate1ToDate2(
106: employee, project, new Date(c1
107: .getTimeInMillis()), new Date(
108: c2.getTimeInMillis()));
109: }
110:
111: double sumOfHours = 0;
112: double sumOfMoney = 0;
113: if (workSheets != null) {
114: HoursAndMoney ham = new HoursAndMoney();
115: for (int i = 0; i < workSheets.length; i++) {
116: // creating row in HTML table which describes table properties
117: HTMLTableRowElement htmlRE = createNewRow(page,
118: workSheets[i], ham);
119: // appending row at the end of table
120: table.appendChild(htmlRE);
121: }
122: sumOfHours = ham.getHours();
123: sumOfMoney = ham.getMoney();
124: sumOfMoney = (double) (Math.round(sumOfMoney * 100)) / 100;
125: sumOfHours = (double) (Math.round(sumOfHours * 100)) / 100;
126: }
127: page.setTextTxtSumOfHours(String.valueOf(sumOfHours));
128: page.setTextTxtSumOfMoney(String.valueOf(sumOfMoney));
129: //to remove back button
130: page.getElementBackElement().getParentNode().removeChild(
131: page.getElementBackElement());
132:
133: //Remove template selection row from table
134: table.removeChild(page.getElementTemplateRow());
135:
136: String errorMsg = this .getSessionData()
137: .getAndClearUserMessage();
138: /*if(null != errorMsg) {
139: page.setTextLblErrorText(errorMsg);
140: } else {
141: page.getElementLblErrorText().getParentNode().removeChild(page.getElementLblErrorText());
142: }*/
143:
144: String isPersonal = this .getComms().request
145: .getParameter(IS_PERSONAL);
146: if (isPersonal != null
147: && isPersonal.equalsIgnoreCase("true")) {
148: HTMLInputElement ip = page.getElementIsPersonal();
149: ip.setValue("true");
150: }
151: } catch (NullPointerException ex) {
152: } catch (Exception ex) {
153: this .writeDebugMsg("Error populating list of work sheets: "
154: + ex);
155: throw new ProjectManagementPresentationException(
156: "Error getting list of work sheets: ", ex);
157: }
158:
159: // write out HTML
160: return page;
161: }
162:
163: /**
164: * Creates the new row of HTML table element, based on given parameters.
165: */
166: private HTMLTableRowElement createNewRow(
167: AdministeringHTML administeringHTML, WorkSheet workSheet,
168: HoursAndMoney ham) {
169:
170: String workSheetID = "";
171:
172: try {
173: workSheetID = workSheet.getHandle();
174: PayRate pr = workSheet.getPayRate();
175: Employee emp = pr.getEmployee();
176: Project prj = pr.getProject();
177: Time timeStarted = workSheet.getTimeStarted();
178: Time timeFinished = workSheet.getTimeFinished();
179: administeringHTML.setTextTxtEmployee(emp.getFirstName()
180: + " " + emp.getLastName());
181: administeringHTML.setTextTxtProject(prj.getName());
182: Calendar cal = new GregorianCalendar();
183: cal.setTime(workSheet.getWorkingDate());
184: String wd = String.valueOf(cal.get(Calendar.YEAR)) + "-"
185: + String.valueOf(cal.get(Calendar.MONTH) + 1) + "-"
186: + String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
187: administeringHTML.setTextTxtWorkingDate(wd);
188: cal.setTime(timeStarted);
189: String tst = String.valueOf(cal.get(Calendar.HOUR_OF_DAY))
190: + ":" + String.valueOf(cal.get(Calendar.MINUTE));
191: administeringHTML.setTextTxtTimeStarted(tst);
192: cal.setTime(timeFinished);
193: String tfn = String.valueOf(cal.get(Calendar.HOUR_OF_DAY))
194: + ":" + String.valueOf(cal.get(Calendar.MINUTE));
195: administeringHTML.setTextTxtTimeFinished(tfn);
196: double mny = (double) (Math.round(ham.calculate(
197: calculateTimeDifferenceInHours(timeStarted,
198: timeFinished), prj.getMoneyPerHour(), pr
199: .getRate()) * 100)) / 100;
200: administeringHTML.setTextTxtMoney(String.valueOf(mny));
201: } catch (Exception ex) {
202: }
203:
204: // image to get details on work sheet
205: HTMLImageElement detailsImg = administeringHTML
206: .getElementImgDetails();
207: detailsImg.setName(workSheetID);
208:
209: // image to modify work sheet information
210: HTMLImageElement modifyImg = administeringHTML
211: .getElementImgModify();
212: modifyImg.setName(workSheetID);
213:
214: // image to delete work sheet
215: HTMLImageElement deleteImg = administeringHTML
216: .getElementImgDelete();
217: deleteImg.setName(workSheetID);
218:
219: HTMLTableRowElement row = (HTMLTableRowElement) administeringHTML
220: .getElementTemplateRow().cloneNode(true);
221:
222: return row;
223: }
224:
225: private double calculateTimeDifferenceInHours(Time timeStarted,
226: Time timeFinished) {
227: long ts = timeStarted.getTime();
228: long tf = timeFinished.getTime();
229: long diffInMilliseconds = tf - ts;
230: double diffInHours = diffInMilliseconds / 3600000.0;
231: return diffInHours;
232: }
233:
234: class HoursAndMoney {
235: private double hours = 0;
236: private double money = 0;
237:
238: public double calculate(double hrs, double moneyPerHour,
239: double payRate) {
240: hours += hrs;
241: double dm = hrs * moneyPerHour * payRate;
242: money += dm;
243: return dm;
244: }
245:
246: public double getHours() {
247: return hours;
248: }
249:
250: public double getMoney() {
251: return money;
252: }
253: }
254:
255: }
|