BrightSide Workbench Full Report + Source Code
elephant-dossier/src/main/java/org/turro/dossier/entity/FieldValue.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.dossier.entity;
19 
20 import java.util.Date;
21 import javax.persistence.Column;
22 import javax.persistence.Entity;
23 import javax.persistence.GeneratedValue;
24 import javax.persistence.GenerationType;
25 import javax.persistence.Id;
26 import javax.persistence.ManyToOne;
27 import org.turro.string.ObjectString;
28 import org.turro.string.Strings;
29 import org.turro.elephant.util.BooleanFormats;
30 import org.turro.elephant.util.DateFormats;
31 import org.turro.elephant.util.DecimalFormats;
32 import org.turro.i18n.I_;
33 
38 @Entity
39 public class FieldValue implements java.io.Serializable {
40 
41  @Id
42  @GeneratedValue(strategy=GenerationType.IDENTITY)
43  @Column(name="IDENTIFIER")
44  private Long id;
45 
46  @Column(name="FIELD_VALUE")
47  private String value;
48 
49  @ManyToOne
50  private FieldDef fieldDef;
51 
52  @ManyToOne
53  private Dossier dossier;
54 
55  public Dossier getDossier() {
56  return dossier;
57  }
58 
59  public void setDossier(Dossier dossier) {
60  this.dossier = dossier;
61  }
62 
63  public FieldDef getFieldDef() {
64  return fieldDef;
65  }
66 
67  public void setFieldDef(FieldDef fieldDef) {
68  this.fieldDef = fieldDef;
69  }
70 
71  public Long getId() {
72  return id;
73  }
74 
75  public void setId(Long id) {
76  this.id = id;
77  }
78 
79  public String getValue() {
80  return value;
81  }
82 
83  public void setValue(String value) {
84  this.value = value;
85  }
86 
87  /* Value Helper */
88 
89  public boolean isEmpty() {
90  return Strings.isBlank(value);
91  }
92 
93  public void setObjectValue(Object obj) {
94  value = ObjectString.formatNativeObject(obj, false);
95  }
96 
97  public Object getObjectValue() {
98  return ObjectString.parseNativeString(value, fieldDef.getJavaClass(), false);
99  }
100 
101  public String getObjectString() {
102  return getObjectString(true);
103  }
104 
105  public String getObjectString(boolean label) {
106  Object obj = getObjectValue();
107  if(obj instanceof Boolean) {
108  if(label) {
109  return BooleanFormats.format((Boolean) obj);
110  } else {
111  return ((Boolean) obj) ? I_.get(fieldDef.getLabelKey()) : "";
112  }
113  } else if(obj instanceof Number) {
114  return DecimalFormats.format((Number) obj);
115  } else if(obj instanceof Date) {
116  return DateFormats.format((Date) obj, true);
117  } else if(obj instanceof String) {
118  return (String) obj;
119  }
120  return obj == null ? "" : obj.toString();
121  }
122 
123  public String getLabeledObjectString() {
124  Object obj = getObjectValue();
125  if(obj instanceof Boolean) {
126  return ((Boolean) obj) ? I_.get(fieldDef.getLabelKey()) : "";
127  } else if(obj instanceof Number) {
128  return I_.get(fieldDef.getLabelKey()) + ": " + DecimalFormats.format((Number) obj);
129  } else if(obj instanceof Date) {
130  return I_.get(fieldDef.getLabelKey()) + ": " + DateFormats.format((Date) obj, true);
131  } else if(obj instanceof String) {
132  return I_.get(fieldDef.getLabelKey()) + ": " + (String) obj;
133  }
134  return obj == null ? "" : I_.get(fieldDef.getLabelKey()) + ": " + obj.toString();
135  }
136 
137 }
static String format(boolean value)
static final String format(Date d, boolean dateOnly)
static String format(Number value, String pattern)
static String get(String msg)
Definition: I_.java:41