BrightSide Workbench Full Report + Source Code
FieldItUtil.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.fieldit;
20 
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.List;
24 import org.turro.string.Strings;
25 import org.turro.contacts.FieldIt;
26 import org.turro.contacts.ValueIt;
27 import org.turro.contacts.db.ContactsPU;
28 import org.turro.entities.Entities;
29 import org.turro.i18n.I_;
30 import org.turro.jpa.Dao;
31 import org.turro.path.Path;
32 
37 public class FieldItUtil {
38 
39  public static FieldItSet fields(Object entity) {
40  String path = Entities.getController(entity).getPath();
41  if(path != null) {
42  return fields(path);
43  }
44  return new FieldItSet();
45  }
46 
47  public static FieldItSet fields(String path) {
48  Dao dao = new ContactsPU();
49  List l = dao.getResultList(
50  " select v from FieldIt v " +
51  " where path = ?",
52  new Object[] { path }
53  );
54  FieldItSet fis = new FieldItSet();
55  fis.addAll(l);
56  return fis;
57  }
58 
59  public static ValueItSet values(Object entity) {
60  String path = Entities.getController(entity).getPath();
61  if(path != null) {
62  return values(path);
63  }
64  return new ValueItSet();
65  }
66 
67  public static ValueItSet values(String path) {
68  Dao dao = new ContactsPU();
69  List l = dao.getResultList(
70  " select v from ValueIt v " +
71  " where path = ?",
72  new Object[] { path }
73  );
74  ValueItSet vis = new ValueItSet();
75  vis.addAll(l);
76  return vis;
77  }
78 
79  public static ValueIt value(FieldIt field, String path) {
80  Dao dao = new ContactsPU();
82  " select v from ValueIt v " +
83  " where path = ? and v.fieldIt = ? ",
84  new Object[] { path, field }
85  );
86  if(vi == null) {
87  vi = field.createValue();
88  vi.setPath(path);
89  }
90  return vi;
91  }
92 
93  public static ValueIt value(String field, String path) {
94  Dao dao = new ContactsPU();
96  " select v from ValueIt v " +
97  " where path = ? and v.fieldIt.name = ? ",
98  new Object[] { path, field }
99  );
100  return vi;
101  }
102 
103  public static String getLabel(Class javaClass) {
104  if(String.class.equals(javaClass)) {
105  return I_.get("String");
106  } else if(Long.class.equals(javaClass)) {
107  return I_.get("Long integer");
108  } else if(Double.class.equals(javaClass)) {
109  return I_.get("Double");
110  } else if(Date.class.equals(javaClass)) {
111  return I_.get("Date");
112  } else if(Boolean.class.equals(javaClass)) {
113  return I_.get("Boolean");
114  } else if(StringList.class.equals(javaClass)) {
115  return I_.get("String list");
116  } else if(AutoFillList.class.equals(javaClass)) {
117  return I_.get("Auto fill list");
118  }
119  return null;
120  }
121 
122  public static void delete(FieldIt field) {
123  Dao dao = new ContactsPU();
124  dao.executeUpdate("delete from ValueIt where fieldIt = ?", new Object[] { field });
125  dao.deleteObject(field);
126  }
127 
128  public static void saveValues(Collection<ValueIt> values) {
129  Dao dao = new ContactsPU();
130  for(ValueIt v : values) {
131  if(v.isValid()) {
132  dao.saveObject(v);
133  } else if(!Strings.isBlank(v.getId())) {
134  dao.deleteObject(v);
135  }
136  }
137  }
138 
139  //TODO: Related entities
140  public static Collection<String> related(String path) {
141  Path p = new Path(path);
142  Dao dao = new ContactsPU();
143  return dao.getResultList(
144  " select v.path from ValueIt v " +
145  " where path <> ? " +
146  " and path like ? " +
147  " and exists ( " +
148  " select v2 from " +
149  " )",
150  new Object[] { path, p.getRoot() + "/" }
151  );
152  }
153 
154  //TODO: Search on entities
155  public static Collection<String> search(String path, String value) {
156  Path p = new Path(path);
157  Dao dao = new ContactsPU();
158  return dao.getResultList(
159  " select v.path from ValueIt v " +
160  " where path like ? " +
161  " and v.value like ? ",
162  new Object[] { p.getRoot() + "/", value }
163  );
164  }
165 
166  public static Object getFieldValue(String label, String entityPath) {
167  ValueIt value = FieldItUtil.value(label, entityPath);
168  if(value != null) {
169  Object v = value.getObjectValue();
170  return v == null ? "" : v;
171  }
172  return "";
173  }
174 
175  private FieldItUtil() {
176  }
177 }
static IElephantEntity getController(String path)
Definition: Entities.java:78
static ValueItSet values(Object entity)
static ValueIt value(FieldIt field, String path)
static FieldItSet fields(Object entity)
static Collection< String > related(String path)
static ValueIt value(String field, String path)
static FieldItSet fields(String path)
static void saveValues(Collection< ValueIt > values)
static Object getFieldValue(String label, String entityPath)
static ValueItSet values(String path)
static Collection< String > search(String path, String value)
static String getLabel(Class javaClass)
static String get(String msg)
Definition: I_.java:41
void deleteObject(Object obj)
Definition: Dao.java:162
int executeUpdate(String query)
Definition: Dao.java:463
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419