BrightSide Workbench Full Report + Source Code
CategoryResults.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.search;
19 
20 import java.util.stream.Stream;
21 import org.turro.dossier.db.DossierPU;
22 import org.turro.dossier.entity.Category;
23 import org.turro.dossier.entity.Dossier;
24 import org.turro.dossier.entity.ParticipantRole;
25 import org.turro.elephant.db.WhereClause;
26 import org.turro.jpa.Dao;
27 import org.turro.jpa.query.JpaCriteria;
28 import org.turro.jpa.query.JpaCriteriaUpdate;
29 import org.turro.jpa.query.JpaQuery;
30 import org.turro.util.Chars;
31 
36 public class CategoryResults {
37 
38  public CategoryResults() {
39  }
40 
41  public java.util.List getCategoryList() {
42  WhereClause wc = new WhereClause();
43  wc.addClause("select distinct category");
44  wc.addClause("from Category as category");
45  wc.addClause("order by category.fullDescription");
46  return new DossierPU().getResultList(wc);
47  }
48 
49  public java.util.List getCategoryRootList() {
50  WhereClause wc = new WhereClause();
51  wc.addClause("select distinct category");
52  wc.addClause("from Category as category");
53  wc.addClause("where parent is null");
54  wc.addClause("order by category.fullDescription");
55  return new DossierPU().getResultList(wc);
56  }
57 
58  public static void saveCategory(Category category) {
59  Dao dao = new DossierPU();
60  category.prepareSave();
61  dao.saveObject(category);
63  }
64 
65  public static void normalizeUniquePaths() {
66  Dao dao = new DossierPU(); //query
67  Dao execute = new DossierPU(); //update within query
68  JpaCriteria criteria = new JpaCriteria(dao);
69  JpaQuery<Category> jqc = criteria.query(Category.class);
70  dao.getResultList(jqc.select()).forEach((category) -> {
71  String uniquePath = "/c" + category.getId();
72  String fullDescription = category.getDescription();
73  Category c = category.getParent();
74  while(c != null) {
75  uniquePath = "/c" + c.getId() + uniquePath;
76  fullDescription = c.getDescription() + Chars.backward().spaced() + fullDescription;
77  c = c.getParent();
78  }
79  JpaCriteriaUpdate<Category> juc = criteria.createCriteriaUpdate(Category.class);
80  execute.executeUpdate(juc.set("uniquePath", uniquePath)
81  .set("fullDescription", fullDescription)
82  .where(criteria.equal(juc.field("id"), category.getId())));
83  });
84  JpaQuery<Dossier> jqd = criteria.query(Dossier.class);
85  try(Stream<Dossier> dossiers = dao.stream(Dossier.class, jqd.select())) {
86  dossiers.forEach((dossier) -> {
87  JpaCriteriaUpdate<Dossier> jud = criteria.createCriteriaUpdate(Dossier.class);
88  execute.executeUpdate(jud.set("uniquePath", dossier.getCategory().getUniquePath() + "/d" + dossier.getId())
89  .where(criteria.equal(jud.field("id"), dossier.getId())));
90  });
91  }
92  }
93 
94  public static void normalizeUniquePaths(Dossier dossier) {
95  Dao dao = new DossierPU();
96  JpaCriteria criteria = new JpaCriteria(dao);
97  JpaCriteriaUpdate<Dossier> jud = criteria.createCriteriaUpdate(Dossier.class);
98  dao.executeUpdate(jud.set("uniquePath", dossier.getCategory().getUniquePath() + "/d" + dossier.getId())
99  .where(criteria.equal(jud.field("id"), dossier.getId())));
100  }
101 
102  public static void addExistsCategoryAffiliance(WhereClause wc, String categoryIds, String dossierField) {
103  wc.addClause("and exists (select c from Category as c");
104  wc.addClause("where " + dossierField + ".uniquePath like concat(c.uniquePath, '/%')");
105  wc.addClause("and c.id in (" + categoryIds + "))");
106  }
107 
108  public static void addParticipantAffiliance(WhereClause wc, String sep, String idContact, String dossierField) {
109  wc.addClause(sep + " exists (");
110  wc.addClause("select cp from CategoryParticipant as cp");
111  wc.addClause("where cp.idContact = :idContact");
112  wc.addNamedValue("idContact", idContact);
113  wc.addClause("and (cp.role = :cprole1 or cp.role = :cprole2)");
116  wc.addClause("and (");
117  wc.addClause("cp.category.uniquePath = " + dossierField + ".category.uniquePath");
118  wc.addClause("or " + dossierField + ".category.uniquePath like concat(cp.category.uniquePath, '/%')");
119  wc.addClause("))");
120  }
121 
122  public static void addParticipantCategoryAffiliance(WhereClause wc, String sep, String idContact, String categoryField) {
123  wc.addClause(sep + " ( exists (");
124  wc.addClause("select cp from CategoryParticipant as cp");
125  wc.addClause("where cp.idContact = :idContact");
126  wc.addNamedValue("idContact", idContact);
127  wc.addClause("and (cp.role = :cprole1 or cp.role = :cprole2)");
130  wc.addClause("and (");
131  wc.addClause("cp.category.uniquePath = " + categoryField + ".uniquePath");
132  wc.addClause("or cp.category.uniquePath like concat(" + categoryField + ".uniquePath, '/%')");
133  wc.addClause("or " + categoryField + ".uniquePath like concat(cp.category.uniquePath, '/%')");
134  wc.addClause(")) or exists (");
135  wc.addClause("select dp from Participant as dp");
136  wc.addClause("where (");
137  wc.addClause("dp.dossier.category.uniquePath = " + categoryField + ".uniquePath");
138  wc.addClause("or dp.dossier.category.uniquePath like concat(" + categoryField + ".uniquePath, '/%')");
139  wc.addClause(")");
140  wc.addClause("and dp.idContact = :idContact");
141  wc.addNamedValue("idContact", idContact);
142  wc.addClause("and (dp.role = :dpdrole1 or dp.role = :dpdrole2)");
145  wc.addClause("))");
146  }
147 
148 }
static void normalizeUniquePaths(Dossier dossier)
static void addParticipantCategoryAffiliance(WhereClause wc, String sep, String idContact, String categoryField)
static void saveCategory(Category category)
static void addParticipantAffiliance(WhereClause wc, String sep, String idContact, String dossierField)
static void addExistsCategoryAffiliance(WhereClause wc, String categoryIds, String dossierField)
void addNamedValue(String name, Object value)
int executeUpdate(String query)
Definition: Dao.java:463
Predicate equal(Expression<?> x, Expression<?> y)
JpaQuery< E > select()
Definition: JpaQuery.java:42