BrightSide Workbench Full Report + Source Code
UniquePath.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.forum.db;
20 
21 import org.turro.elephant.db.ElephantPU;
22 import org.turro.elephant.entities.db.Post;
23 import org.turro.elephant.entities.db.Topic;
24 import org.turro.jpa.Dao;
25 import org.turro.jpa.query.JpaCriteria;
26 import org.turro.jpa.query.JpaCriteriaUpdate;
27 import org.turro.jpa.query.JpaQuery;
28 
33 public class UniquePath {
34 
35  public static void normalizeUniquePaths(Topic topic) {
36  Dao dao = new ElephantPU();
37  normalizeUniquePaths(topic, new JpaCriteria(dao), dao, new ElephantPU());
38  }
39 
40  public static void normalizeUniquePaths(Post post) {
41  Dao dao = new ElephantPU();
42  normalizeUniquePaths(post, new JpaCriteria(dao), dao, new ElephantPU());
43  }
44 
45  public static void normalizeUniquePaths(Topic topic, final JpaCriteria criteria, Dao dao, Dao execute) {
46  JpaCriteriaUpdate<Topic> jud = criteria.createCriteriaUpdate(Topic.class);
47  execute.executeUpdate(jud.set("uniquePath", topic.getEntityPath() + ElephantPU.getObjectPath(topic))
48  .where(criteria.equal(jud.field("id"), topic.getId())));
49  topic.getPosts(dao).forEach(p -> normalizeUniquePaths(p, criteria, dao, execute));
50  }
51 
52  public static void normalizeUniquePaths(Post post, final JpaCriteria criteria, Dao dao, Dao execute) {
53  JpaCriteriaUpdate<Post> jud = criteria.createCriteriaUpdate(Post.class);
54  execute.executeUpdate(jud.set("uniquePath", post.getCurrentParent().getUniquePath() + ElephantPU.getObjectPath(post))
55  .where(criteria.equal(jud.field("id"), post.getId())));
56  post.getPosts(dao).forEach(p -> normalizeUniquePaths(p, criteria, dao, execute));
57  }
58 
59  public static void normalizeUniquePaths() {
60  Dao dao = new ElephantPU(); //query
61  Dao execute = new ElephantPU(); //update within query
62  JpaCriteria criteria = new JpaCriteria(dao);
63  JpaQuery<Topic> jqc = criteria.query(Topic.class);
64  dao.getResultList(jqc.select()).forEach((topic) -> {
65  normalizeUniquePaths(topic, criteria, dao, execute);
66  });
67  }
68 
69 }
static String getObjectPath(Object object)
Definition: ElephantPU.java:63
static void normalizeUniquePaths(Post post)
Definition: UniquePath.java:40
static void normalizeUniquePaths(Post post, final JpaCriteria criteria, Dao dao, Dao execute)
Definition: UniquePath.java:52
static void normalizeUniquePaths()
Definition: UniquePath.java:59
static void normalizeUniquePaths(Topic topic)
Definition: UniquePath.java:35
static void normalizeUniquePaths(Topic topic, final JpaCriteria criteria, Dao dao, Dao execute)
Definition: UniquePath.java:45
int executeUpdate(String query)
Definition: Dao.java:463
Predicate equal(Expression<?> x, Expression<?> y)
JpaQuery< E > select()
Definition: JpaQuery.java:42