19 package org.turro.tags;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Optional;
28 import java.util.stream.Collectors;
29 import org.turro.string.Strings;
30 import org.turro.action.Plugins;
31 import org.turro.elephant.db.ElephantPU;
32 import org.turro.elephant.db.WhereClause;
33 import org.turro.elephant.entities.db.Tag;
34 import org.turro.elephant.entities.db.TagPK;
35 import org.turro.entities.Entities;
36 import org.turro.jpa.Dao;
37 import org.turro.path.Path;
38 import org.turro.sql.SqlClause;
39 import org.turro.util.PhraseBuilder;
50 wc.
addClause(
"select new org.turro.tags.TagItem(t.tagName, count(t))");
52 wc.
addClause(
"where t.entityPath like '/contact/%'");
53 if(!Strings.isBlank(root)) {
54 wc.
addClause(
"and t.entityPath like concat('/', :root, '/%')");
57 if(!Strings.isBlank(value)) {
58 wc.
addLikeFields(
new String[]{
"t.tagName" }, value.toString());
62 return new TagSet(dao.getResultList(wc, max));
72 wc.
addClause(
"select new org.turro.tags.TagItem(t.tagName, count(t))");
74 if(!Strings.isBlank(root)) {
75 wc.
addClause(
"where t.entityPath like concat('/', :root, '/%')");
79 return new TagSet(dao.getResultList(wc));
89 for(String tag : tagNames) {
91 if(finalSet !=
null) {
92 finalSet.retainAll(
set);
97 return Optional.ofNullable(finalSet).orElseGet(() ->
TagSet.
empty());
102 wc.
addClause(
"select new org.turro.tags.TagItem(t.tagName, count(t))");
104 wc.
addClause(
"join Tag t1 on t.entityPath = t1.entityPath");
108 if(!Strings.isBlank(root)) {
109 wc.
addClause(
"and t.entityPath like concat('/', :root, '/%')");
113 return new TagSet(dao.getResultList(wc));
117 return getFrom(entityPath).stream().map(s ->
new TagItem(s.getTagName(), 0)).collect(Collectors.toList());
120 public static List<Tag>
getFrom(String entityPath) {
124 wc.
addClause(
"where t.entityPath = :path");
127 return dao.getResultList(wc);
134 wc.
addClause(
"where t.entityPath like :path");
137 return dao.getResultList(wc);
143 " select count(*) from Tag " +
144 " where tagName = ?",
145 new Object[] { tagName }
149 public static void setTags(Object entity, String tags) {
153 public static void setTags(String entityPath, String tags) {
154 if(Strings.isBlank(tags)) {
157 setTags(entityPath, Arrays.asList(tags.split(
"\\s*,\\s*")));
161 public static void setTags(Object entity, Collection<String> tags) {
165 public static void setTags(String entityPath, Collection<String> tags) {
166 if(tags ==
null || tags.isEmpty()) {
170 for(String tag : tags) {
176 public static boolean hasTag(Object entity, String tagName) {
180 public static boolean hasTag(String entityPath, String tagName) {
184 public static boolean hasTag(
Dao dao, String entityPath, String tagName) {
185 return getTag(dao, entityPath, tagName) !=
null;
188 public static void addTag(Object entity, String tagName) {
192 public static void addTag(String entityPath, String tagName) {
194 if(!
hasTag(dao, entityPath, tagName)) {
198 if(!tag.
isEmpty()) dao.saveObject(tag);
202 public static void changeTag(String entityPath, String oldName, String newName) {
208 wc.
addClause(
"where entityPath = :path");
215 public static void removeTag(Object entity, String tagName) {
219 public static void removeTag(String entityPath, String tagName) {
221 Tag tag =
getTag(dao, entityPath, tagName);
231 return dao.find(
Tag.class, tagPk);
239 return getEntityPaths(dao, root,
set).stream().map(s -> Path.getIdentifier(s)).collect(Collectors.toList());
247 List<String> tags =
set.stream().map(t -> t.getTagName()).collect(Collectors.toList());
248 if(!tags.isEmpty()) {
250 wc.
addClause(
"select distinct t.entityPath from Tag t");
252 if(!Strings.isBlank(root)) {
253 wc.
addClause(
"and t.entityPath like concat('/', :root, '/%')");
257 for(String tag : tags) {
259 wc.
addClause(
"select t2.entityPath from Tag t2");
260 wc.
addClause(
"where t2.entityPath = t.entityPath");
261 wc.
addClause(
"and t2.tagName = :tag" + count);
266 return dao.getResultList(String.class, wc);
268 return Collections.EMPTY_LIST;
272 return getEntityPaths(dao, root, sets).stream().map(s -> Path.getIdentifier(s)).collect(Collectors.toList());
281 wc.
addClause(
"select distinct t.entityPath from Tag t");
283 if(!Strings.isBlank(root)) {
284 wc.
addClause(
"and t.entityPath like concat('/', :root, '/%')");
288 return dao.getResultList(String.class, wc);
295 for(Set<TagItem>
set : sets) {
298 List<String> tags =
set.stream().map(t -> t.getTagName()).collect(Collectors.toList());
299 if(!tags.isEmpty()) {
300 for(String tag : tags) {
302 wc.
addClause(
"select t2.entityPath from Tag t2");
303 wc.
addClause(
"where t2.entityPath = t.entityPath");
304 wc.
addClause(
"and t2.tagName = :tag" + count);
318 return getCommonEntityPaths(source, rootTarget).stream().map(s -> Path.getIdentifier(s)).collect(Collectors.toList());
322 return getCommonEntityPaths(source, rootTarget).stream().map(s -> Path.getLongIdentifier(s)).collect(Collectors.toList());
330 return getCommonEntityPaths(dao, source, rootTarget).stream().map(s -> Path.getIdentifier(s)).collect(Collectors.toList());
334 return getCommonEntityPaths(dao, source, rootTarget).stream().map(s -> Path.getLongIdentifier(s)).collect(Collectors.toList());
339 wc.
addClause(
"select distinct t.entityPath from Tag t");
340 wc.
addClause(
"where t.entityPath like concat('/', :target, '/%')");
343 wc.
addClause(
"select t2.entityPath from Tag t2");
344 wc.
addClause(
"where t2.entityPath = :source");
346 wc.
addClause(
"and t2.tagName = t.tagName");
348 return dao.getResultList(String.class, wc);
358 wc.
addClause(
"select new org.turro.tags.TagItem(t.tagName, count(t))");
360 wc.
addClause(
"where t.entityPath = :path");
363 return new TagSet(dao.getResultList(wc));
374 wc.
addClause(
"where entityPath = :path");
379 public static void removeTags(String root, List<String> tags) {
383 wc.
addClause(
"where entityPath like :path");
384 wc.
addIn(
"and",
"tagName", tags);
393 wc.
addClause(
"where entityPath like :path");
402 public static Collection<String>
tagChoices(String entityPath) {
404 return dao.getResultList(
405 " select distinct tagName from Tag " +
406 " where entityPath <> ?",
407 new Object[] { entityPath }
412 if(entity ==
null)
return null;
424 PhraseBuilder pb =
new PhraseBuilder();
425 for(String tag :
getTags(entityPath).getTagNames()) {
427 pb.addPendingSeparator(
",");
429 return pb.toString();
435 return new HashSet<>(SqlClause.select(
"distinct t.entityPath").from(
"Tag t")
436 .where().startsWith(
"t.entityPath",
"/" + root +
"/")
438 .resultList(String.class));
static< T > T loadImplementation(Class< T > jclass)
void addClause(String clause)
void addLikeFields(String[] fields, String value)
void addIn(String operator, String field, List values)
void addNamedValue(String name, Object value)
void setEntityPath(String entityPath)
void setTagName(String tagName)
void setTagName(String tagName)
void setEntityPath(String entityPath)
static IElephantEntity getController(String path)
void deleteObject(Object obj)
int executeUpdate(String query)
Object getSingleResult(WhereClause wc)