BrightSide Workbench Full Report + Source Code
Relateds.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.related;
20 
21 import java.util.Date;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import org.turro.elephant.db.ElephantPU;
26 import org.turro.elephant.entities.db.Related;
27 import org.turro.jpa.Dao;
28 import org.turro.sql.SqlClause;
29 import org.turro.util.Cached;
30 
35 public class Relateds {
36 
37  public Related addOrigin(String origin, String description) {
38  return add(origin, entityPath, description);
39  }
40 
41  public Related addDestination(String destination, String description) {
42  return add(entityPath, destination, description);
43  }
44 
45  public Relateds removeOrigin(String origin) {
46  return remove(origin, entityPath);
47  }
48 
49  public Relateds removeDestination(String destination) {
50  return remove(entityPath, destination);
51  }
52 
53  public boolean existsOrigin(String origin) {
54  return exists(origin, entityPath);
55  }
56 
57  public boolean existsDestination(String destination) {
58  return exists(entityPath, destination);
59  }
60 
61  public List<Related> origins() {
62  return SqlClause.select("p").from("Related p")
63  .where().equal("destination", entityPath)
64  .dao(dao.get())
65  .resultList(Related.class);
66  }
67 
68  public List<Related> destinations() {
69  return SqlClause.select("p").from("Related p")
70  .where().equal("origin", entityPath)
71  .dao(dao.get())
72  .resultList(Related.class);
73  }
74 
75  /* Utils */
76 
77  public Related add(String origin, String destination, String description) {
78  if(!exists(origin, destination)) {
79  Related re = new Related();
80  re.setOrigin(origin);
81  re.setDestination(destination);
82  re.setDescription(description);
83  if(!re.isEmpty()) {
84  re.setCreation(new Date());
85  return dao.get().saveObject(re);
86  }
87  }
88  return null;
89  }
90 
91  public Relateds remove(String origin, String destination) {
92  SqlClause.delete("Related")
93  .where().equal("origin", origin)
94  .and().equal("destination", destination)
95  .dao(dao.get())
96  .execute();
97  return this;
98  }
99 
100  public Relateds removeAny(String removePath) {
101  SqlClause.delete("Related")
102  .where().equal("origin", removePath)
103  .or().equal("destination", removePath)
104  .dao(dao.get())
105  .execute();
106  return this;
107  }
108 
109  public boolean exists(String origin, String destination) {
110  return SqlClause.count().from("Related")
111  .where().equal("origin", origin)
112  .and().equal("destination", destination)
113  .dao(dao.get())
114  .singleResult(Long.class) > 0;
115  }
116 
117  public boolean existsAny(String existPath) {
118  return SqlClause.count().from("Related")
119  .where().equal("origin", existPath)
120  .or().equal("destination", existPath)
121  .dao(dao.get())
122  .singleResult(Long.class) > 0;
123  }
124 
125  public static Set<String> getAllPaths(String root) {
126  Set<String> all = new HashSet<>(
127  SqlClause.select("distinct origin").from("Related")
128  .where().startsWith("origin", "/" + root + "/")
129  .dao(new ElephantPU())
130  .resultList(String.class));
131  all.addAll(
132  SqlClause.select("distinct destination").from("Related")
133  .where().startsWith("destination", "/" + root + "/")
134  .dao(new ElephantPU())
135  .resultList(String.class));
136  return all;
137  }
138 
139  /* Dao */
140 
141  private Cached<Dao> dao = Cached.instance(() -> new ElephantPU());
142 
143  /* Factory */
144 
145  public static Relateds from(String entityPath) {
146  return new Relateds(entityPath);
147  }
148 
149  public static Relateds empty() {
150  return new Relateds(null);
151  }
152 
153  private final String entityPath;
154 
155  private Relateds(String entityPath) {
156  this.entityPath = entityPath;
157  }
158 
159 }
void setDestination(String destination)
Definition: Related.java:68
void setDescription(String description)
Definition: Related.java:60
Related addOrigin(String origin, String description)
Definition: Relateds.java:37
boolean existsDestination(String destination)
Definition: Relateds.java:57
boolean existsAny(String existPath)
Definition: Relateds.java:117
Relateds removeOrigin(String origin)
Definition: Relateds.java:45
boolean exists(String origin, String destination)
Definition: Relateds.java:109
Related add(String origin, String destination, String description)
Definition: Relateds.java:77
List< Related > origins()
Definition: Relateds.java:61
static Set< String > getAllPaths(String root)
Definition: Relateds.java:125
Relateds removeAny(String removePath)
Definition: Relateds.java:100
static Relateds from(String entityPath)
Definition: Relateds.java:145
static Relateds empty()
Definition: Relateds.java:149
List< Related > destinations()
Definition: Relateds.java:68
Related addDestination(String destination, String description)
Definition: Relateds.java:41
Relateds removeDestination(String destination)
Definition: Relateds.java:49
boolean existsOrigin(String origin)
Definition: Relateds.java:53