BrightSide Workbench Full Report + Source Code
Grouped.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.ArrayList;
23 import java.util.Collection;
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.elephant.db.WhereClause;
32 import org.turro.entities.Entities;
33 import org.turro.jpa.Dao;
34 
39 @Entity
40 @org.hibernate.annotations.GenericGenerator(name = "hibernate-uuid", strategy = "uuid")
41 public class Grouped implements Serializable {
42 
43  @Id
44  @GeneratedValue(generator = "hibernate-uuid")
45  @Column(name="IDENTIFIER")
46  private String id;
47 
48  @ManyToOne
49  private GroupIt parent;
50 
51  @Column(name="GROUPED_PATH")
52  private String path;
53 
54  public String getId() {
55  return id;
56  }
57 
58  public void setId(String id) {
59  this.id = id;
60  }
61 
62  public GroupIt getParent() {
63  return parent;
64  }
65 
66  public void setParent(GroupIt parent) {
67  this.parent = parent;
68  }
69 
70  public String getPath() {
71  return path;
72  }
73 
74  public void setPath(String path) {
75  this.path = path;
76  }
77 
78  /* Helpers */
79 
80  private transient String category;
81 
82  public String getCategory() {
83  if(category == null && parent != null) {
84  category = parent.getCategory();
85  }
86  return category;
87  }
88 
89  public void setCategory(String category) {
90  this.category = category;
91  }
92 
93  public boolean isValid() {
94  return parent != null &&
95  !Strings.isBlank(path);
96  }
97 
98  public static Collection<Grouped> groups(Object entity, String category) {
99  String path = Entities.getController(entity).getPath();
100  if(path != null) {
101  return groups(path, category);
102  }
103  return new ArrayList<>();
104  }
105 
106  public static Collection<Grouped> groups(String path, String category) {
107  Dao dao = new ContactsPU();
108  return dao.getResultList(
109  " select g from Grouped g " +
110  " where g.parent.category = ? " +
111  " and g.path = ? " +
112  " order by g.parent.category, g.parent.name ",
113  new Object[] { category, path }
114  );
115  }
116 
117  public static Collection<Grouped> groups(Object entity) {
118  String path = Entities.getController(entity).getPath();
119  if(path != null) {
120  return groups(path);
121  }
122  return new ArrayList<>();
123  }
124 
125  public static Collection<Grouped> groups(String path) {
126  Dao dao = new ContactsPU();
127  return dao.getResultList(
128  " select g from Grouped g " +
129  " where g.path = ? " +
130  " order by g.parent.category, g.parent.name ",
131  new Object[] { path }
132  );
133  }
134 
135  public static Collection<String> paths(String path, String selectedGroup, String selectedCategory, boolean exact) {
136  Dao dao = new ContactsPU();
137  WhereClause wc = new WhereClause();
138  wc.addClause("select g.path from Grouped g");
139  wc.addClause("where g.path like :path");
140  wc.addNamedValue("path", path + "/%");
141  if(Strings.isBlank(selectedGroup)) {
142  selectedGroup = "";
143  }
144  if(exact) {
145  wc.addClause("and g.parent.pathName = :pathName01");
146  wc.addNamedValue("pathName01", selectedGroup);
147  } else {
148  wc.addClause("and (g.parent.pathName = :pathName01 or g.parent.pathName like :pathName02)");
149  wc.addNamedValue("pathName01", selectedGroup);
150  wc.addNamedValue("pathName02", selectedGroup + "%");
151  }
152  wc.addClause("and g.parent.category = :category");
153  wc.addNamedValue("category", selectedCategory);
154  return dao.getResultList(wc);
155  }
156 
157  public static Collection<String> pathsFromCategory(String path, String category) {
158  Dao dao = new ContactsPU();
159  return dao.getResultList(
160  " select g.path from Grouped g " +
161  " where g.path like ? " +
162  " and g.parent.category = ?",
163  new Object[] { path + "/%", category }
164  );
165  }
166 
167 }
168 
void setCategory(String category)
Definition: Grouped.java:89
void setPath(String path)
Definition: Grouped.java:74
static Collection< Grouped > groups(Object entity)
Definition: Grouped.java:117
static Collection< String > paths(String path, String selectedGroup, String selectedCategory, boolean exact)
Definition: Grouped.java:135
static Collection< Grouped > groups(String path, String category)
Definition: Grouped.java:106
static Collection< Grouped > groups(Object entity, String category)
Definition: Grouped.java:98
void setParent(GroupIt parent)
Definition: Grouped.java:66
static Collection< String > pathsFromCategory(String path, String category)
Definition: Grouped.java:157
static Collection< Grouped > groups(String path)
Definition: Grouped.java:125
void setId(String id)
Definition: Grouped.java:58
void addNamedValue(String name, Object value)
static IElephantEntity getController(String path)
Definition: Entities.java:78