BrightSide Workbench Full Report + Source Code
GroupIt.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.io.Serializable;
22 import java.util.Collection;
23 import java.util.List;
24 import javax.persistence.Column;
25 import javax.persistence.Entity;
26 import javax.persistence.GeneratedValue;
27 import javax.persistence.Id;
28 import javax.persistence.ManyToOne;
29 import org.turro.string.Strings;
30 import org.turro.contacts.db.ContactsPU;
31 import org.turro.fieldit.FieldItSet;
32 import org.turro.fieldit.FieldItUtil;
33 import org.turro.jpa.Dao;
34 
39 @Entity
40 @org.hibernate.annotations.GenericGenerator(name = "hibernate-uuid", strategy = "uuid")
41 public class GroupIt implements Serializable {
42 
43  @Id
44  @GeneratedValue(generator = "hibernate-uuid")
45  @Column(name="IDENTIFIER")
46  private String id;
47 
48  private String category;
49 
50  @Column(name="GROUPIT_NAME")
51  private String name;
52 
53  private String pathName;
54 
55  @ManyToOne
56  private GroupIt parent;
57 
58  private boolean publishable;
59 
60  public String getId() {
61  return id;
62  }
63 
64  public void setId(String id) {
65  this.id = id;
66  }
67 
68  public String getCategory() {
69  return category;
70  }
71 
72  public void setCategory(String category) {
73  this.category = category;
74  }
75 
76  public String getName() {
77  return name;
78  }
79 
80  public void setName(String name) {
81  this.name = name;
82  }
83 
84  public String getPathName() {
85  return pathName;
86  }
87 
88  public void setPathName(String pathName) {
89  this.pathName = pathName;
90  }
91 
92  public GroupIt getParent() {
93  return parent;
94  }
95 
96  public void setParent(GroupIt parent) {
97  this.parent = parent;
98  }
99 
100  public boolean isPublishable() {
101  return publishable;
102  }
103 
104  public void setPublishable(boolean publishable) {
105  this.publishable = publishable;
106  }
107 
108  /* Helpers */
109 
110  public boolean isValid() {
111  return !Strings.isBlank(name) &&
112  !name.contains("/");
113  }
114 
115  public static Collection<String> categories() {
116  Dao dao = new ContactsPU();
117  return dao.getResultList(
118  " select distinct g.category from GroupIt g"
119  );
120  }
121 
122  public static Collection<GroupIt> roots(String category) {
123  Dao dao = new ContactsPU();
124  return dao.getResultList(
125  " select g from GroupIt g " +
126  " where g.category = ? " +
127  " and g.parent is null",
128  new Object[] { category }
129  );
130  }
131 
132  public static Collection<GroupIt> roots(String category, String entityPath) {
133  Dao dao = new ContactsPU();
134  return dao.getResultList(
135  " select g from GroupIt g " +
136  " where g.category = ? " +
137  " and g.parent is null " +
138  " and exists ( " +
139  " select gd from Grouped gd " +
140  " where (gd.parent.pathName like concat(g.pathName, '/%') " +
141  " or gd.parent.pathName = g.pathName) " +
142  " and gd.path like ? " +
143  " ) ",
144  new Object[] { category, entityPath + "/%" }
145  );
146  }
147 
148  public Collection<GroupIt> children() {
149  Dao dao = new ContactsPU();
150  return dao.getResultList(
151  " select g from GroupIt g " +
152  " where g.category = ? " +
153  " and g.parent = ?",
154  new Object[] { this.getCategory(), this }
155  );
156  }
157 
158  public Collection<GroupIt> children(String entityPath) {
159  Dao dao = new ContactsPU();
160  return dao.getResultList(
161  " select g from GroupIt g " +
162  " where g.category = ? " +
163  " and g.parent = ? " +
164  " and exists ( " +
165  " select gd from Grouped gd " +
166  " where (gd.parent.pathName like concat(g.pathName, '/%') " +
167  " or gd.parent.pathName = g.pathName) " +
168  " and gd.path like ? " +
169  " ) ",
170  new Object[] { this.getCategory(), this, entityPath + "/%" }
171  );
172  }
173 
174  public Collection<Grouped> entities() {
175  Dao dao = new ContactsPU();
176  return dao.getResultList(
177  " select g from Grouped g " +
178  " where g.parent.category = ? " +
179  " and g.parent = ?",
180  new Object[] { this.getCategory(), this }
181  );
182  }
183 
184  public Collection<Grouped> fullEntities() {
185  Dao dao = new ContactsPU();
186  return dao.getResultList(
187  " select g from Grouped g " +
188  " where g.parent.category = ? " +
189  " and (g.parent = ? or g.parent.path like ?)",
190  new Object[] { this.getCategory(), this, this.getPathName() + "/%" }
191  );
192  }
193 
194  public Collection<Grouped> fullEntities(String entityRoot) {
195  Dao dao = new ContactsPU();
196  return dao.getResultList(
197  " select g from Grouped g " +
198  " where g.parent.category = ? " +
199  " and (g.parent = ? or g.parent.path like ?) " +
200  " and g.path like ? ",
201  new Object[] { this.getCategory(), this,
202  this.getPathName() + "/%", entityRoot + "/%" }
203  );
204  }
205 
206  public static GroupIt normalizePaths(GroupIt group) {
207  Dao dao = new ContactsPU();
208  String d = "/" + group.getName();
209  GroupIt g = group.getParent();
210  while(g != null) {
211  d = "/" + g.getName() + d;
212  g = g.getParent();
213  }
214  group.setPathName(d);
215  return dao.saveObject(group);
216  }
217 
218  public static void normalizePaths(String oldPath) {
219  Dao dao = new ContactsPU();
220  for(GroupIt group : (List<GroupIt>) dao.getResultList(
221  "select g from GroupIt g where g.pathName like ?",
222  new Object[] { oldPath + "/%" })) {
223  String d = "/" + group.getName();
224  GroupIt g = group.getParent();
225  while(g != null) {
226  d = "/" + g.getName() + d;
227  g = g.getParent();
228  }
229  group.setPathName(d);
230  dao.saveObject(group);
231  }
232  }
233 
234  public static void delete(String path) {
235  Dao dao = new ContactsPU();
236  dao.executeUpdate(
237  "delete from Grouped where parent in (select gi from GroupIt gi where gi.pathName = ? or gi.pathName like ?)",
238  new Object[] { path, path + "/%" });
239  dao.executeUpdate(
240  "delete from GroupIt where pathName = ? or pathName like ?",
241  new Object[] { path, path + "/%" });
242  }
243 
244  public static GroupIt getOrCreate(String category, String node, GroupIt parent) {
245  Dao dao = new ContactsPU();
246  String parentPath = parent != null ? parent.getPathName() + "/" : "/";
248  "select g from GroupIt g where g.category = ? and g.pathName = ?",
249  new Object[] { category, parentPath + node });
250  if(gi == null) {
251  gi = new GroupIt();
252  gi.setCategory(category);
253  gi.setName(node);
254  gi.setParent(parent);
255  gi.setPublishable(true);
256  gi = gi.normalizePaths(gi);
257  }
258  return gi;
259  }
260 
261  /* Fields */
262 
263  public FieldItSet fields() {
265  GroupIt gi = getParent();
266  while(gi != null) {
267  fiu.addAll(FieldItUtil.fields(ContactsPU.getObjectPath(gi)));
268  gi = gi.getParent();
269  }
270  return fiu;
271  }
272 
273 }
274 
Collection< Grouped > entities()
Definition: GroupIt.java:174
static void normalizePaths(String oldPath)
Definition: GroupIt.java:218
void setId(String id)
Definition: GroupIt.java:64
Collection< GroupIt > children(String entityPath)
Definition: GroupIt.java:158
Collection< Grouped > fullEntities(String entityRoot)
Definition: GroupIt.java:194
Collection< GroupIt > children()
Definition: GroupIt.java:148
static GroupIt normalizePaths(GroupIt group)
Definition: GroupIt.java:206
static Collection< String > categories()
Definition: GroupIt.java:115
void setPublishable(boolean publishable)
Definition: GroupIt.java:104
void setParent(GroupIt parent)
Definition: GroupIt.java:96
void setName(String name)
Definition: GroupIt.java:80
Collection< Grouped > fullEntities()
Definition: GroupIt.java:184
void setCategory(String category)
Definition: GroupIt.java:72
static GroupIt getOrCreate(String category, String node, GroupIt parent)
Definition: GroupIt.java:244
static Collection< GroupIt > roots(String category, String entityPath)
Definition: GroupIt.java:132
static Collection< GroupIt > roots(String category)
Definition: GroupIt.java:122
void setPathName(String pathName)
Definition: GroupIt.java:88
static String getObjectPath(Object object)
Definition: ContactsPU.java:68
static FieldItSet fields(Object entity)
int executeUpdate(String query)
Definition: Dao.java:463
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419