19 package org.turro.related;
21 import java.util.Date;
22 import java.util.HashSet;
23 import java.util.List;
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;
38 return add(origin, entityPath, description);
42 return add(entityPath, destination, description);
46 return remove(origin, entityPath);
50 return remove(entityPath, destination);
54 return exists(origin, entityPath);
58 return exists(entityPath, destination);
62 return SqlClause.select(
"p").from(
"Related p")
63 .where().equal(
"destination", entityPath)
69 return SqlClause.select(
"p").from(
"Related p")
70 .where().equal(
"origin", entityPath)
77 public Related add(String origin, String destination, String description) {
78 if(!
exists(origin, destination)) {
85 return dao.get().saveObject(re);
91 public Relateds remove(String origin, String destination) {
92 SqlClause.delete(
"Related")
93 .where().equal(
"origin", origin)
94 .and().equal(
"destination", destination)
101 SqlClause.delete(
"Related")
102 .where().equal(
"origin", removePath)
103 .or().equal(
"destination", removePath)
109 public boolean exists(String origin, String destination) {
110 return SqlClause.count().from(
"Related")
111 .where().equal(
"origin", origin)
112 .and().equal(
"destination", destination)
114 .singleResult(Long.class) > 0;
118 return SqlClause.count().from(
"Related")
119 .where().equal(
"origin", existPath)
120 .or().equal(
"destination", existPath)
122 .singleResult(Long.class) > 0;
126 Set<String> all =
new HashSet<>(
127 SqlClause.select(
"distinct origin").from(
"Related")
128 .where().startsWith(
"origin",
"/" + root +
"/")
130 .resultList(String.class));
132 SqlClause.select(
"distinct destination").from(
"Related")
133 .where().startsWith(
"destination",
"/" + root +
"/")
135 .resultList(String.class));
141 private Cached<Dao> dao = Cached.instance(() ->
new ElephantPU());
153 private final String entityPath;
155 private Relateds(String entityPath) {
156 this.entityPath = entityPath;