BrightSide Workbench Full Report + Source Code
ResourceAptitude.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.resource;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
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.Resource;
31 import org.turro.util.PhraseBuilder;
32 
37 public class ResourceAptitude {
38 
39  private Resource resource;
40  private AptitudeDegree aptitudeDegree;
41 
42  public ResourceAptitude(Resource resource, AptitudeDegree aptitudeDegree) {
43  this.resource = resource;
44  this.aptitudeDegree = aptitudeDegree;
45  }
46 
47  public Resource getResource() {
48  return resource;
49  }
50 
52  return aptitudeDegree;
53  }
54 
55  public String getName() {
56  PhraseBuilder pb = new PhraseBuilder();
57  if(resource != null) {
58  pb.addWord(resource.getName());
59  }
60  pb.addPendingSeparator(" -");
61  if(aptitudeDegree != null) {
62  pb.addWord(aptitudeDegree.getFullName());
63  }
64  return pb.toString();
65  }
66 
67  @Override
68  public String toString() {
69  return getName();
70  }
71 
72  public double getCost() {
73  return resource.getCostHour() + aptitudeDegree.getCost();
74  }
75 
76  public double getPrice() {
77  return resource.getPriceHour() + aptitudeDegree.getPrice();
78  }
79 
80  public double getMarketPrice() {
81  return resource.getMarketPrice() + aptitudeDegree.getMarketPrice();
82  }
83 
84  public static Collection<ResourceAptitude> load(String value, boolean onlyActive, int nRows) {
85  ArrayList<ResourceAptitude> result = new ArrayList<ResourceAptitude>();
86  EntityManager em = new ErpPU().getEntityManager();
87  try {
88  WhereClause wc = SQLHelper.getWhereClause(new String[]{
89  "res.name"
90  }, value);
91  Query q = em.createQuery(
92  "select res from Resource res " +
93  "where res.active = :active " +
94  wc.getClause() +
95  " order by res.name"
96  );
97  wc.addNamedValue("active", onlyActive);
98  q.setMaxResults(nRows);
99  wc.setNamedParameters(q);
100  List<Resource> l = q.getResultList();
101  for(Resource hr : l) {
102  for(AptitudeDegree ad : hr.getAptitudeDegrees()) {
103  result.add(new ResourceAptitude(hr, ad));
104  }
105  }
106  } finally {
107  em.close();
108  }
109  return result;
110  }
111 
112  public static ResourceAptitude get(String text) {
113  String s[] = text.split(" - ");
114  if(s.length > 1) {
115  String name = s.length == 3 ? s[0] + " - " + s[1] : s[0];
116  String aptitude = s.length == 3 ? s[2] : s[1];
117  EntityManager em = new ErpPU().getEntityManager();
118  try {
119  Query q = em.createQuery(
120  " select res from Resource res " +
121  " where res.name = :name");
122  q.setParameter("name", name);
123  q.setMaxResults(1);
124  Resource hr = (Resource) q.getSingleResult();
125  for(AptitudeDegree ad : hr.getAptitudeDegrees()) {
126  if(ad.getFullName().equals(aptitude)) {
127  return new ResourceAptitude(hr, ad);
128  }
129  }
130  } catch(Exception ex) {
131  return null;
132  } finally {
133  em.close();
134  }
135  }
136  return null;
137  }
138 
139 }
static WhereClause getWhereClause(String[] fields, String value)
Definition: SQLHelper.java:64
void addNamedValue(String name, Object value)
Set< AptitudeDegree > getAptitudeDegrees()
Definition: Resource.java:98
ResourceAptitude(Resource resource, AptitudeDegree aptitudeDegree)
static Collection< ResourceAptitude > load(String value, boolean onlyActive, int nRows)
EntityManager getEntityManager()
Definition: Dao.java:202