BrightSide Workbench Full Report + Source Code
WorthDefinition.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 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.dossier.entity;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Objects;
24 import java.util.Set;
25 import java.util.TreeSet;
26 import javax.persistence.Column;
27 import javax.persistence.Entity;
28 import javax.persistence.GeneratedValue;
29 import javax.persistence.GenerationType;
30 import javax.persistence.Id;
31 import javax.persistence.Lob;
32 import javax.persistence.ManyToOne;
33 import org.turro.string.ObjectString;
34 import org.turro.string.Strings;
35 import org.turro.dossier.www.WorthPair;
36 import org.turro.jpa.entity.IDaoEntity;
37 
42 @Entity
43 public class WorthDefinition implements java.io.Serializable, IDaoEntity {
44 
45  @Id
46  @GeneratedValue(strategy=GenerationType.IDENTITY)
47  @Column(name="IDENTIFIER")
48  private Long id;
49 
50  private String title, possibleValues, colors, grouping;
51 
52  @Lob
53  @Column(length=4096)
54  private String description;
55 
56  @ManyToOne
57  private Category category;
58 
59  public Long getId() {
60  return id;
61  }
62 
63  public void setId(Long id) {
64  this.id = id;
65  }
66 
67  public String getTitle() {
68  return title;
69  }
70 
71  public void setTitle(String title) {
72  this.title = title;
73  }
74 
75  public String getPossibleValues() {
76  return possibleValues;
77  }
78 
79  public void setPossibleValues(String possibleValues) {
80  this.possibleValues = possibleValues;
81  }
82 
83  public String getColors() {
84  return colors;
85  }
86 
87  public void setColors(String colors) {
88  this.colors = colors;
89  }
90 
91  public String getGrouping() {
92  return grouping;
93  }
94 
95  public void setGrouping(String grouping) {
96  this.grouping = grouping;
97  }
98 
99  public String getDescription() {
100  return description;
101  }
102 
103  public void setDescription(String description) {
104  this.description = description;
105  }
106 
108  return category;
109  }
110 
111  public void setCategory(Category category) {
112  this.category = category;
113  }
114 
115  public Dossier getDossier() {
116  return _dossier;
117  }
118 
119  public void setDossier(Dossier _dossier) {
120  this._dossier = _dossier;
121  }
122 
123  /* Value helpers */
124 
125  transient List<WorthValue> _values;
126  transient Dossier _dossier;
127 
128  public void assignDossier(Dossier dossier) {
129  _dossier = dossier;
130  }
131 
133  return _dossier;
134  }
135 
136  public double getWorthTotal() {
137  double total = 0.0;
138  for(WorthValue v : getWorthValues()) {
139  total += v.getValue();
140  }
141  return total;
142  }
143 
144  public List<WorthValue> getWorthValues() {
145  if(_values == null) {
146  _values = new ArrayList<>();
147  for(WorthValue v : _dossier.getWorths()) {
148  if(Objects.equals(v.getWorthDefinition().getId(), id)) {
149  _values.add(v);
150  }
151  }
152  }
153  return _values;
154  }
155 
156  public Double getWorthAvg() {
157  if(getWorthValues().size() > 0) {
158  Double total = 0.0;
159  for(WorthValue v : getWorthValues()) {
160  total += v.getValue();
161  }
162  return total / getWorthValues().size();
163  }
164  return 0.0;
165  }
166 
168  Double avg = getWorthAvg();
169  Set<WorthPair> set = getRangeValues();
170  if(set.size() > 0) {
171  for(WorthPair wp : set) {
172  if(wp.getValue() >= avg) {
173  return wp;
174  }
175  }
176  }
177  return null;
178  }
179 
180  /* IDaoEntity */
181 
182  @Override
183  public Object entityId() {
184  return id;
185  }
186 
187  @Override
188  public boolean isEmpty() {
189  return Strings.isBlank(title) || Strings.isBlank(possibleValues);
190  }
191 
192  /* Helpers */
193 
194  public List<WorthPair> getPairValues() {
195  List<WorthPair> list = new ArrayList<>();
196  String[] sa = possibleValues.split(",");
197  String[] co = colors.split(",");
198  if(sa.length == co.length) {
199  for(int i = 0; i < sa.length; i++) {
200  list.add(new WorthPair((Double) ObjectString.parseString(sa[i], Double.class, true), co[i]));
201  }
202  }
203  return list;
204  }
205 
206  public Set<WorthPair> getRangeValues() {
207  TreeSet<WorthPair> set = new TreeSet<>();
208  List<WorthPair> pairs = getPairValues();
209  Double diff = null;
210  WorthPair prev = null;
211  for(WorthPair wp : pairs) {
212  if(prev != null) {
213  diff = diff == null ? wp.getValue() - prev.getValue() : Math.min(wp.getValue() - prev.getValue(), diff);
214  }
215  prev = wp;
216  }
217  for(WorthPair wp : pairs) {
218  set.add(new WorthPair(wp.getValue() + (diff / 2.0), wp.getColor()));
219  }
220 
221 // if(pairs.size() > 1) {
222 // WorthPair prev = pairs.get(0);
223 // WorthPair last = pairs.get(pairs.size() - 1);
224 // for(int i = 1; i < pairs.size(); i++) {
225 // WorthPair current = pairs.get(i);
226 // Double diff = current.getValue() - prev.getValue();
227 // set.add(new WorthPair((diff / 3.0) + prev.getValue(), prev.getColor()));
228 // prev = current;
229 // }
230 // set.add(new WorthPair(last.getValue(), last.getColor()));
231 // }
232  return set;
233  }
234 
235 }
Set< WorthValue > getWorths()
Definition: Dossier.java:243
void setPossibleValues(String possibleValues)