BrightSide Workbench Full Report + Source Code
HumanResource.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.erp.entity;
19 
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.List;
24 import javax.persistence.*;
25 import org.amic.util.date.CheckDate;
26 import org.turro.financials.db.FinancialsPU;
27 import org.turro.financials.entity.Contract;
28 import org.turro.financials.entity.ContractParticipant;
29 import org.turro.hr.aptitude.AptituteDegreeAdapter;
30 import org.turro.hr.humanres.HumanResourceAptitude;
31 import org.turro.jpa.Dao;
32 import org.turro.jpa.entity.CachedEntity;
33 import org.turro.plugin.contacts.IContact;
34 
39 @Entity
40 public class HumanResource implements java.io.Serializable {
41 
42  @Id
43  @GeneratedValue(strategy=GenerationType.IDENTITY)
44  @Column(name="IDENTIFIER")
45  private long id;
46 
47  @Column(name="RESOURCE_NAME", unique = true, nullable = false)
48  private String name;
49 
50  private long idContract, idOperator;
51 
52  private boolean active;
53 
54  private double costHour, priceHour, marketPrice;
55 
56  @OneToMany(mappedBy = "humanResource", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
57  @OrderBy("defaultDegree DESC, initialDate")
58  private List<OwnedAptitude> ownedAptitudes = new ArrayList<OwnedAptitude>();
59 
60  public boolean isActive() {
61  return active;
62  }
63 
64  public void setActive(boolean active) {
65  this.active = active;
66  }
67 
68  public long getId() {
69  return id;
70  }
71 
72  public void setId(long id) {
73  this.id = id;
74  }
75 
76  public long getIdContract() {
77  return idContract;
78  }
79 
80  public void setIdContract(long idContract) {
81  this.idContract = idContract;
82  }
83 
84  public long getIdOperator() {
85  return idOperator;
86  }
87 
88  public void setIdOperator(long idOperator) {
89  this.idOperator = idOperator;
90  }
91 
92  public double getMarketPrice() {
93  return marketPrice;
94  }
95 
96  public void setMarketPrice(double marketPrice) {
97  this.marketPrice = marketPrice;
98  }
99 
100  public String getName() {
101  return name;
102  }
103 
104  public void setName(String name) {
105  this.name = name;
106  }
107 
108  public double getCostHour() {
109  return costHour;
110  }
111 
112  public void setCostHour(double costHour) {
113  this.costHour = costHour;
114  }
115 
116  public List<OwnedAptitude> getOwnedAptitudes() {
117  return ownedAptitudes;
118  }
119 
120  public void setOwnedAptitudes(List<OwnedAptitude> ownedAptitudes) {
121  this.ownedAptitudes = ownedAptitudes;
122  }
123 
124  public double getPriceHour() {
125  return priceHour;
126  }
127 
128  public void setPriceHour(double priceHour) {
129  this.priceHour = priceHour;
130  }
131 
134  private transient CachedEntity<Contract, Long> contract =
135  new CachedEntity<Contract, Long>() {
136  @Override
137  protected Dao createDao() {
138  return new FinancialsPU();
139  }
140  };
141 
143  return contract.getEntity(idContract);
144  }
145 
146  public void setContract(Contract contract) {
147  idContract = this.contract.setEntity(contract);
148  }
149 
150  private transient CachedEntity<ContractParticipant, Long> operator =
151  new CachedEntity<ContractParticipant, Long>() {
152  @Override
153  protected Dao createDao() {
154  return new FinancialsPU();
155  }
156  };
157 
159  return operator.getEntity(idOperator);
160  }
161 
162  public void setOperator(ContractParticipant operator) {
163  idOperator = this.operator.setEntity(operator);
164  }
165 
168  public boolean fits(IContact contact) {
169  Contract contract = getContract();
170  ContractParticipant participant = getOperator();
171  return contact != null &&
172  ((contract != null && contract.getContractor().equals(contact.getId())) ||
173  (participant != null && participant.getIdContact().equals(contact.getId())));
174  }
175 
176  public boolean fits(AptitudeDegree required, Date now) {
177  return required.isIn(new AptituteDegreeAdapter(getActiveAptitudes(now)));
178  }
179 
180  public void prepareForSaving() {
181  Contract c = getContract();
182  name = c.getIContractor().getName();
184  if(cp != null) {
185  name = cp.getName() + " - " + name;
186  }
187  }
188 
189  public Collection<OwnedAptitude> getActiveAptitudes(Date date) {
190  CheckDate cd = new CheckDate(date);
191  ArrayList<OwnedAptitude> soo = new ArrayList<OwnedAptitude>();
192  for(OwnedAptitude oo : getOwnedAptitudes()) {
193  if(cd.isDateActive(oo.getInitialDate(), oo.getFinalDate())) {
194  soo.add(oo);
195  }
196  }
197  return soo;
198  }
199 
200  public boolean isInternal() {
201  Contract c = getContract();
202  if(c != null) {
203  return c.getContractDefinition().getId() == 26;
204  }
205  return true;
206  }
207 
209  HumanResourceAptitude hra = null;
210  for(OwnedAptitude oo : getActiveAptitudes(date)) {
211  hra = new HumanResourceAptitude(this, false, oo.getAptitudeDegree());
212  if(oo.isDefaultDegree()) break;
213  }
214  return hra;
215  }
216 
217 }
boolean isIn(Collection< AptitudeDegree > aptitudeDegrees)
void setContract(Contract contract)
boolean fits(AptitudeDegree required, Date now)
void setOperator(ContractParticipant operator)
void setIdOperator(long idOperator)
boolean fits(IContact contact)
Collection< OwnedAptitude > getActiveAptitudes(Date date)
void setPriceHour(double priceHour)
List< OwnedAptitude > getOwnedAptitudes()
void setOwnedAptitudes(List< OwnedAptitude > ownedAptitudes)
void setIdContract(long idContract)
void setMarketPrice(double marketPrice)
HumanResourceAptitude getDefaultHumanResourceAptitude(Date date)
ContractDefinition getContractDefinition()
Definition: Contract.java:125