BrightSide Workbench Full Report + Source Code
ValueIt.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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.contacts;
20 
21 import java.util.Date;
22 import javax.persistence.Column;
23 import javax.persistence.Entity;
24 import javax.persistence.GeneratedValue;
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.fieldit.StringList;
33 import org.turro.i18n.I_;
34 import org.turro.reflection.Reflections;
35 
40 @Entity
41 @org.hibernate.annotations.GenericGenerator(name = "hibernate-uuid", strategy = "uuid")
42 public class ValueIt implements java.io.Serializable {
43 
44  @Id
45  @GeneratedValue(generator = "hibernate-uuid")
46  @Column(name="IDENTIFIER")
47  private String id;
48 
49  @Column(name="VALUEIT_VALUE")
50  private String value;
51 
52  @ManyToOne
53  private FieldIt fieldIt;
54 
55  @Column(name="VALUEIT_PATH")
56  private String path;
57 
58  public String getId() {
59  return id;
60  }
61 
62  public void setId(String id) {
63  this.id = id;
64  }
65 
66  public String getValue() {
67  return value;
68  }
69 
70  public void setValue(String value) {
71  this.value = value;
72  }
73 
74  public FieldIt getFieldIt() {
75  return fieldIt;
76  }
77 
78  public void setFieldIt(FieldIt fieldIt) {
79  this.fieldIt = fieldIt;
80  }
81 
82  public String getPath() {
83  return path;
84  }
85 
86  public void setPath(String path) {
87  this.path = path;
88  }
89 
90  /* Value Helper */
91 
92  public boolean isValid() {
93  return fieldIt != null && !Strings.isBlank(value);
94  }
95 
96  public void setObjectValue(Object obj) {
97  if(obj instanceof StringList) {
98  value = ((StringList) obj).getValue();
99  } else {
100  value = ObjectString.formatNativeObject(obj, false);
101  }
102  }
103 
104  public Object getObjectValue() {
105  if(Reflections.of(fieldIt.getJavaClass()).canCast(StringList.class)) {
106  return value;
107  } else {
108  return ObjectString.parseNativeString(value, fieldIt.getJavaClass(), false);
109  }
110  }
111 
112  public String getObjectString() {
113  return getObjectString(true);
114  }
115 
116  public String getObjectString(boolean label) {
117  Object obj = getObjectValue();
118  if(obj instanceof Boolean) {
119  if(label) {
120  return BooleanFormats.format((Boolean) obj);
121  } else {
122  return ((Boolean) obj) ? I_.get(fieldIt.getName()) : "";
123  }
124  } else if(obj instanceof Number) {
125  return DecimalFormats.format((Number) obj);
126  } else if(obj instanceof Date) {
127  return DateFormats.format((Date) obj, true);
128  } else if(obj instanceof String) {
129  return (String) obj;
130  } else if(obj instanceof StringList) {
131  return ((StringList) obj).getValue();
132  }
133  return obj == null ? "" : obj.toString();
134  }
135 
136  public String getLabeledObjectString() {
137  Object obj = getObjectValue();
138  if(obj instanceof Boolean) {
139  return ((Boolean) obj) ? I_.get(fieldIt.getName()) : "";
140  } else if(obj instanceof Number) {
141  return I_.get(fieldIt.getName()) + ": " + DecimalFormats.format((Number) obj);
142  } else if(obj instanceof Date) {
143  return I_.get(fieldIt.getName()) + ": " + DateFormats.format((Date) obj, true);
144  } else if(obj instanceof String) {
145  return I_.get(fieldIt.getName()) + ": " + (String) obj;
146  } else if(obj instanceof StringList) {
147  return I_.get(fieldIt.getName()) + ": " + ((StringList) obj).getValue();
148  }
149  return obj == null ? "" : I_.get(fieldIt.getName()) + ": " + obj.toString();
150  }
151 
152 }
void setObjectValue(Object obj)
Definition: ValueIt.java:96
void setPath(String path)
Definition: ValueIt.java:86
void setId(String id)
Definition: ValueIt.java:62
String getObjectString(boolean label)
Definition: ValueIt.java:116
void setValue(String value)
Definition: ValueIt.java:70
void setFieldIt(FieldIt fieldIt)
Definition: ValueIt.java:78
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