1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.hr.time.util;
17  
18  import java.math.BigDecimal;
19  import java.net.URLEncoder;
20  import java.util.List;
21  
22  import org.apache.log4j.Logger;
23  import org.joda.time.DateTime;
24  import org.joda.time.Interval;
25  import org.kuali.hr.time.assignment.Assignment;
26  import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
27  import org.kuali.hr.time.detail.validation.TimeDetailValidationService;
28  import org.kuali.hr.time.detail.web.TimeDetailActionFormBase;
29  import org.kuali.hr.time.earncode.EarnCode;
30  import org.kuali.hr.time.test.HtmlUnitUtil;
31  import org.kuali.hr.time.timesheet.TimesheetDocument;
32  
33  import com.gargoylesoftware.htmlunit.html.HtmlButton;
34  import com.gargoylesoftware.htmlunit.html.HtmlForm;
35  import com.gargoylesoftware.htmlunit.html.HtmlPage;
36  import org.kuali.hr.util.filter.TestAutoLoginFilter;
37  
38  public class TimeDetailTestUtils {
39  
40      private static final Logger LOG = Logger.getLogger(TimeDetailTestUtils.class);
41  
42      
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58      public static TimeDetailActionFormBase buildDetailActionForm(TimesheetDocument timeshetDocument, Assignment assignment, EarnCode earnCode, DateTime start, DateTime end, BigDecimal amount, boolean acrossDays, String timeblockId, boolean spanningWeeks) {
59          TimeDetailActionFormBase tdaf = new TimeDetailActionFormBase();
60  
61          BigDecimal hours = null;
62          String startTimeS = null;
63          String endTimeS = null;
64          String startDateS;
65          String endDateS;
66          String selectedEarnCode;
67          String selectedAssignment;
68  
69          if (amount == null) {
70              if (start != null && end != null) {
71                  Interval se_i = new Interval(start, end);
72                  hours = TKUtils.convertMillisToHours(se_i.toDurationMillis());
73              }
74  
75              
76              startTimeS = start.toString("H:mm");
77              endTimeS = end.toString("H:mm");
78          }
79  
80          startDateS = start.toString("MM/dd/YYYY");
81          endDateS = end.toString("MM/dd/YYYY");
82  
83          AssignmentDescriptionKey adk = new AssignmentDescriptionKey(assignment);
84          selectedAssignment = adk.toAssignmentKeyString();
85  
86          selectedEarnCode = earnCode.getEarnCode();
87  
88          tdaf.setAcrossDays(acrossDays ? "y" : "n");
89          tdaf.setSpanningWeeks(spanningWeeks ? "y" : "n"); 
90          tdaf.setAmount(amount);
91          tdaf.setHours(hours);
92          tdaf.setStartTime(startTimeS);
93          tdaf.setEndTime(endTimeS);
94          tdaf.setStartDate(startDateS);
95          tdaf.setEndDate(endDateS);
96          tdaf.setTkTimeBlockId(timeblockId);
97          tdaf.setTimesheetDocument(timeshetDocument);
98          tdaf.setSelectedAssignment(selectedAssignment);
99          tdaf.setSelectedEarnCode(selectedEarnCode);
100         tdaf.setMethodToCall("addTimeBlock");
101 
102         return tdaf;
103     }
104 
105     
106 
107 
108 
109 
110 
111 
112 
113 
114     public static List<String> setTimeBlockFormDetails(HtmlForm form, TimeDetailActionFormBase tdaf) {
115         
116         List<String> errors = TimeDetailValidationService.validateTimeEntryDetails(tdaf);
117 
118         
119         if (errors.size() == 0) {
120             if (tdaf.getTkTimeBlockId() != null) {
121                 form.setAttribute("tkTimeBlockId", tdaf.getTkTimeBlockId().toString());
122             }
123             form.setAttribute("startDate", tdaf.getStartDate());
124             form.setAttribute("endDate", tdaf.getEndDate());
125 
126             if (tdaf.getAmount() != null) {
127                 form.setAttribute("amount", tdaf.getAmount().toString());
128             } else {
129                 form.setAttribute("startTime", tdaf.getStartTime());
130                 form.setAttribute("endTime", tdaf.getEndTime());
131                 form.setAttribute("hours", tdaf.getHours().toString());
132             }
133 
134             form.setAttribute("selectedEarnCode", tdaf.getSelectedEarnCode());
135             form.setAttribute("selectedAssignment", tdaf.getSelectedAssignment());
136             form.setAttribute("acrossDays", tdaf.getAcrossDays());
137             form.setAttribute("methodToCall", tdaf.getMethodToCall());
138         }
139 
140         return errors;
141     }
142 
143     
144 
145 
146 
147 
148 
149 
150 
151 
152     private static HtmlPage submitTimeDetailsDep(HtmlPage page, HtmlForm form) {
153         HtmlButton submitButton = null;
154 
155         
156 
157         if (submitButton == null) {
158             submitButton = (HtmlButton)page.createElement("button");
159             submitButton.setAttribute("type", "submit");
160             form.appendChild(submitButton);
161         }
162 
163         HtmlPage newPage = null;
164         try {
165             submitButton.click();
166         } catch (Exception e) {
167             LOG.error("While submitting time detail form", e);
168         }
169 
170         return newPage;
171     }
172 
173 
174     
175 
176 
177 
178 
179 
180     public static HtmlPage submitTimeDetails(String baseUrl, TimeDetailActionFormBase tdaf) {
181         
182         
183         
184 
185         String url = baseUrl + buildPostFromFormParams(tdaf);
186         HtmlPage page = null;
187 
188         try {
189             page = HtmlUnitUtil.gotoPageAndLogin(url);
190         } catch (Exception e) {
191             LOG.error("Error while submitting form", e);
192         }
193 
194         return page;
195     }
196 
197     
198 
199 
200 
201 
202 
203     public static HtmlPage submitTimeDetails(String principalId, String baseUrl, TimeDetailActionFormBase tdaf) {
204         
205         
206         
207 
208         String url = baseUrl + buildPostFromFormParams(tdaf);
209         HtmlPage page = null;
210 
211         try {
212             TestAutoLoginFilter.OVERRIDE_ID = principalId;
213             page = HtmlUnitUtil.gotoPageAndLogin(url);
214             TestAutoLoginFilter.OVERRIDE_ID = "";
215         } catch (Exception e) {
216             LOG.error("Error while submitting form", e);
217         }
218 
219         return page;
220     }
221 
222     private static String buildPostFromFormParams(TimeDetailActionFormBase tdaf) {
223         StringBuilder builder = new StringBuilder();
224 
225         try {
226             builder.append("&methodToCall=").append(URLEncoder.encode(tdaf.getMethodToCall(), "UTF-8"));
227             builder.append("&acrossDays=").append(URLEncoder.encode(tdaf.getAcrossDays(), "UTF-8"));
228             if (tdaf.getAmount() != null) {
229                 builder.append("&amount=").append(URLEncoder.encode(tdaf.getAmount().toString(), "UTF-8"));
230             } else {
231                 builder.append("&hours=").append(URLEncoder.encode(tdaf.getHours().toString(), "UTF-8"));
232                 builder.append("&startTime=").append(URLEncoder.encode(tdaf.getStartTime(), "UTF-8"));
233                 builder.append("&endTime=").append(URLEncoder.encode(tdaf.getEndTime(), "UTF-8"));
234             }
235             builder.append("&startDate=").append(URLEncoder.encode(tdaf.getStartDate(), "UTF-8"));
236             builder.append("&endDate=").append(URLEncoder.encode(tdaf.getEndDate(), "UTF-8"));
237             builder.append("&selectedAssignment=").append(URLEncoder.encode(tdaf.getSelectedAssignment(), "UTF-8"));
238             builder.append("&selectedEarnCode=").append(URLEncoder.encode(tdaf.getSelectedEarnCode(), "UTF-8"));
239             if (tdaf.getTkTimeBlockId() != null) {
240                 builder.append("&tkTimeBlockId=").append(URLEncoder.encode(tdaf.getTkTimeBlockId().toString(), "UTF-8"));
241             }
242         } catch (Exception e) {
243             LOG.error("Exception building Post String", e);
244         }
245 
246         return builder.toString();
247     }
248 }