BrightSide Workbench Full Report + Source Code
HumanResourceAptitude.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.hr.humanres;
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.EntityManager;
25 import javax.persistence.Query;
26 import org.turro.elephant.db.SQLHelper;
27 import org.turro.elephant.db.WhereClause;
28 import org.turro.erp.db.ErpPU;
29 import org.turro.erp.entity.AptitudeDegree;
30 import org.turro.erp.entity.HumanResource;
31 import org.turro.erp.entity.OwnedAptitude;
32 import org.turro.util.PhraseBuilder;
33 
38 public class HumanResourceAptitude {
39 
40  private HumanResource humanResource;
41  private boolean canChange;
42  private AptitudeDegree aptitudeDegree;
43 
44  public HumanResourceAptitude(HumanResource humanResource, boolean canChange, AptitudeDegree aptitudeDegree) {
45  this.humanResource = humanResource;
46  this.canChange = canChange;
47  this.aptitudeDegree = aptitudeDegree;
48  }
49 
51  return humanResource;
52  }
53 
55  return aptitudeDegree;
56  }
57 
58  public String getRealName() {
59  PhraseBuilder pb = new PhraseBuilder();
60  if(humanResource != null) {
61  pb.addWord(humanResource.getName());
62  }
63  pb.addPendingSeparator(" -");
64  if(aptitudeDegree != null) {
65  pb.addWord(aptitudeDegree.getFullName());
66  }
67  return pb.toString();
68  }
69 
70  public String getName() {
71  PhraseBuilder pb = new PhraseBuilder();
72  if(humanResource != null) {
73  if(canChange) {
74  pb.addWord("Variable");
75  } else {
76  pb.addWord(humanResource.getName());
77  }
78  }
79  pb.addPendingSeparator(" -");
80  if(aptitudeDegree != null) {
81  pb.addWord(aptitudeDegree.getFullName());
82  }
83  return pb.toString();
84  }
85 
86  @Override
87  public String toString() {
88  return getName();
89  }
90 
91  public double getCost() {
92  return humanResource.getCostHour() + aptitudeDegree.getCost();
93  }
94 
95  public double getPrice() {
96  return humanResource.getPriceHour() + aptitudeDegree.getPrice();
97  }
98 
99  public double getMarketPrice() {
100  return humanResource.getMarketPrice() + aptitudeDegree.getMarketPrice();
101  }
102 
103  public static Collection<HumanResourceAptitude> load(Date date, String value, Collection<AptitudeDegree> required, boolean onlyActive, int nRows) {
104  ArrayList<HumanResourceAptitude> result = new ArrayList<HumanResourceAptitude>();
105  EntityManager em = new ErpPU().getEntityManager();
106  try {
107  WhereClause wc = SQLHelper.getWhereClause(new String[]{
108  "res.name"
109  }, value);
110  Query q = em.createQuery(
111  "select res from HumanResource res " +
112  "where res.active = :active " +
113  wc.getClause() +
114  " order by res.name"
115  );
116  wc.addNamedValue("active", onlyActive);
117  q.setMaxResults(nRows);
118  wc.setNamedParameters(q);
119  List<HumanResource> l = q.getResultList();
120  for(HumanResource hr : l) {
121  for(OwnedAptitude off : hr.getActiveAptitudes(date)) {
122  if(required.isEmpty() || off.getAptitudeDegree().isIn(required)) {
123  result.add(new HumanResourceAptitude(hr, false, off.getAptitudeDegree()));
124  }
125  }
126  }
127  } finally {
128  em.close();
129  }
130  return result;
131  }
132 
133  public static HumanResourceAptitude get(Date date, String text) {
134  String s[] = text.split(" - ");
135  if(s.length > 1) {
136  String name = s.length == 3 ? s[0] + " - " + s[1] : s[0];
137  String aptitude = s.length == 3 ? s[2] : s[1];
138  EntityManager em = new ErpPU().getEntityManager();
139  try {
140  Query q = em.createQuery(
141  " select res from HumanResource res " +
142  " where res.name = :name");
143  q.setParameter("name", name);
144  q.setMaxResults(1);
145  HumanResource hr = (HumanResource) q.getSingleResult();
146  for(OwnedAptitude off : hr.getActiveAptitudes(date)) {
147  if(off.getAptitudeDegree().getFullName().equals(aptitude)) {
148  return new HumanResourceAptitude(hr, false, off.getAptitudeDegree());
149  }
150  }
151  } catch(Exception ex) {
152  return null;
153  } finally {
154  em.close();
155  }
156  }
157  return null;
158  }
159 }
static WhereClause getWhereClause(String[] fields, String value)
Definition: SQLHelper.java:64
void addNamedValue(String name, Object value)
Collection< OwnedAptitude > getActiveAptitudes(Date date)
static Collection< HumanResourceAptitude > load(Date date, String value, Collection< AptitudeDegree > required, boolean onlyActive, int nRows)
HumanResourceAptitude(HumanResource humanResource, boolean canChange, AptitudeDegree aptitudeDegree)
EntityManager getEntityManager()
Definition: Dao.java:202