BrightSide Workbench Full Report + Source Code
TimeControlUtil.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.erp.time;
20 
21 import java.util.ArrayList;
22 import java.util.Date;
23 import java.util.List;
24 import org.amic.util.date.CheckDate;
25 import org.turro.elephant.db.WhereClause;
26 import org.turro.erp.db.ErpPU;
27 import org.turro.erp.entity.HumanResource;
28 import org.turro.erp.entity.Task;
29 import org.turro.erp.entity.TimeControl;
30 import org.turro.jpa.Dao;
31 
36 public class TimeControlUtil {
37 
38  private HumanResource humanResource;
39  private Task task;
40 
42  return humanResource;
43  }
44 
45  public void setHumanResource(HumanResource humanResource) {
46  this.humanResource = humanResource;
47  }
48 
49  public Task getTask() {
50  return task;
51  }
52 
53  public void setTask(Task task) {
54  this.task = task;
55  }
56 
57  public List<TimeControl> getPendingControl() {
58  if(humanResource != null) {
59  WhereClause wc = new WhereClause();
60  wc.addClause("select tc from TimeControl as tc");
61  wc.addClause("where tc.humanResource = :humanResource");
62  wc.addNamedValue("humanResource", humanResource);
63  if(task != null) {
64  wc.addClause("and tc.task = :task");
65  wc.addNamedValue("task", task);
66  }
67  wc.addClause("and tc.endTime is null");
68  Dao dao = new ErpPU();
69  return dao.getResultList(wc);
70  }
71  return new ArrayList<TimeControl>();
72  }
73 
74  public TimeControl start() {
75  return start(null);
76  }
77 
78  public TimeControl start(Date date) {
79  TimeControl tc = new TimeControl();
80  tc.setHumanResource(humanResource);
81  tc.setTask(task);
82  if(date == null) {
83  tc.setStartTime(new Date());
84  } else {
85  tc.setStartTime(date);
86  tc.setStartManual(true);
87  }
88  Dao dao = new ErpPU();
89  tc = dao.saveObject(tc);
90  return tc;
91  }
92 
94  return end(tc, null);
95  }
96 
97  public TimeControl end(TimeControl tc, Date date) {
98  Dao dao = new ErpPU();
99  tc = dao.find(TimeControl.class, tc.getId());
100  if(date == null) {
101  tc.setEndTime(new Date());
102  } else {
103  tc.setEndTime(date);
104  tc.setEndManual(true);
105  }
106  tc = dao.saveObject(tc);
107  return tc;
108  }
109 
110  public List<TimeControl> getFromDate(Date date) {
111  CheckDate d = new CheckDate(date);
112  WhereClause wc = new WhereClause();
113  wc.addClause("select tc from TimeControl as tc");
114  wc.addClause("where (year(tc.startTime) = :year1 and month(tc.startTime) = :month1 and day(tc.startTime) = :day1)");
115  wc.addClause("or (year(tc.endTime) = :year2 and month(tc.endTime) = :month2 and day(tc.endTime) = :day2)");
116  wc.addNamedValue("year1", d.getYear());
117  wc.addNamedValue("month1", d.getMonth());
118  wc.addNamedValue("day1", d.getDay());
119  wc.addNamedValue("year2", d.getYear());
120  wc.addNamedValue("month2", d.getMonth());
121  wc.addNamedValue("day2", d.getDay());
122  Dao dao = new ErpPU();
123  return dao.getResultList(wc);
124  }
125 
126 }
void addNamedValue(String name, Object value)
void setEndManual(boolean endManual)
void setHumanResource(HumanResource humanResource)
void setStartTime(Date startTime)
void setStartManual(boolean startManual)
void setHumanResource(HumanResource humanResource)
TimeControl end(TimeControl tc)
List< TimeControl > getFromDate(Date date)
TimeControl end(TimeControl tc, Date date)
List< TimeControl > getPendingControl()