BrightSide Workbench Full Report + Source Code
SkillTags.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.skills.tags;
20 
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Optional;
25 import java.util.Set;
26 import java.util.stream.Collectors;
27 import org.turro.string.Strings;
28 import org.turro.elephant.db.ElephantPU;
29 import org.turro.elephant.db.WhereClause;
30 import org.turro.elephant.entities.db.SkillType;
31 import org.turro.entities.Entities;
32 import org.turro.jpa.Dao;
33 import org.turro.path.Path;
34 import org.turro.util.Arrays;
35 
40 public class SkillTags {
41 
42  public static SkillSet getAvailables(String root) {
43  Dao dao = new ElephantPU();
44  WhereClause wc = new WhereClause();
45  wc.addClause("select new org.turro.skills.tags.SkillItem(t.skill, t.type, count(t))");
46  wc.addClause("from Skill t");
47  wc.addIn("where", "t.type", Arrays.asList(SkillType.values()));
48  if(!Strings.isBlank(root)) {
49  wc.addClause("and t.entityPath like concat('/', :root, '/%')");
50  wc.addNamedValue("root", root);
51  }
52  wc.addClause("group by t.skill, t.type");
53  return new SkillSet(dao.getResultList(wc));
54  }
55 
56  public static SkillSet getAvailables(String root, boolean user) {
57  Dao dao = new ElephantPU();
58  WhereClause wc = new WhereClause();
59  wc.addClause("select new org.turro.skills.tags.SkillItem(t.skill, t.type, count(t))");
60  wc.addClause("from Skill t");
61  wc.addIn("where", "t.type", new ArrayList(user ? SkillType.getNaturals() : SkillType.getJuridicals()));
62  if(!Strings.isBlank(root)) {
63  wc.addClause("and t.entityPath like concat('/', :root, '/%')");
64  wc.addNamedValue("root", root);
65  }
66  wc.addClause("group by t.skill, t.type");
67  return new SkillSet(dao.getResultList(wc));
68  }
69 
70  public static SkillSet getSiblings(String root, Set<String> skillNames) {
71  Dao dao = new ElephantPU();
72  SkillSet finalSet = null;
73  for(String tag : skillNames) {
74  SkillSet set = getSiblings(dao, root, tag);
75  if(finalSet != null) {
76  finalSet.retainAll(set);
77  } else {
78  finalSet = set;
79  }
80  }
81  return Optional.ofNullable(finalSet).orElseGet(() -> SkillSet.empty());
82  }
83 
84  private static SkillSet getSiblings(Dao dao, String root, String skillName) {
85  WhereClause wc = new WhereClause();
86  wc.addClause("select new org.turro.skills.tags.SkillItem(t.skill, t.type, count(t))");
87  wc.addClause("from Skill t");
88  wc.addClause("join Skill t1 on t.entityPath = t1.entityPath");
89  wc.addClause("where t.skill <> :skill");
90  wc.addClause("and t1.skill = :skill");
91  wc.addNamedValue("skill", skillName);
92  if(!Strings.isBlank(root)) {
93  wc.addClause("and t.entityPath like concat('/', :root, '/%')");
94  wc.addNamedValue("root", root);
95  }
96  wc.addClause("group by t.skill, t.type");
97  return new SkillSet(dao.getResultList(wc));
98  }
99 
100  public static List<String> getIdentifiers(String root, Set<SkillItem> set) {
101  return getIdentifiers(new ElephantPU(), root, set);
102  }
103 
104  public static List<String> getIdentifiers(Dao dao, String root, Set<SkillItem> set) {
105  return getEntityPaths(dao, root, set).stream().map(s -> Path.getIdentifier(s)).collect(Collectors.toList());
106  }
107 
108  public static List<String> getEntityPaths(String root, Set<SkillItem> set) {
109  return getEntityPaths(new ElephantPU(), root, set);
110  }
111 
112  public static List<String> getEntityPaths(Dao dao, String root, Set<SkillItem> set) {
113  List<String> skills = set.stream().map(t -> t.getSkillName()).collect(Collectors.toList());
114  if(!skills.isEmpty()) {
115  WhereClause wc = new WhereClause();
116  wc.addClause("select distinct t.entityPath from Skill t");
117  wc.addClause("where 1=1");
118  if(!Strings.isBlank(root)) {
119  wc.addClause("and t.entityPath like concat('/', :root, '/%')");
120  wc.addNamedValue("root", root);
121  }
122  int count = 0;
123  for(String skill : skills) {
124  wc.addClause("and exists (");
125  wc.addClause("select t2.entityPath from Skill t2");
126  wc.addClause("where t2.entityPath = t.entityPath");
127  wc.addClause("and t2.skill = :skill" + count);
128  wc.addNamedValue("skill" + count, skill);
129  wc.addClause(")");
130  count++;
131  }
132  return dao.getResultList(String.class, wc);
133  }
134  return Collections.EMPTY_LIST;
135  }
136 
137  public static SkillSet getSkills(Object entity) {
138  return getSkills(Entities.getController(entity).getPath());
139  }
140 
141  public static SkillSet getSkills(String entityPath) {
142  Dao dao = new ElephantPU();
143  WhereClause wc = new WhereClause();
144  wc.addClause("select new org.turro.skils.tags.SkillItem(t.skill, t.type, count(t))");
145  wc.addClause("from Skill t");
146  wc.addClause("where t.entityPath = :path");
147  wc.addNamedValue("path", entityPath);
148  wc.addClause("group by t.skill, t.type");
149  return new SkillSet(dao.getResultList(wc));
150  }
151 
152  private SkillTags() {}
153 
154 }
void addIn(String operator, String field, List values)
void addNamedValue(String name, Object value)
static IElephantEntity getController(String path)
Definition: Entities.java:78
static SkillSet getSkills(String entityPath)
Definition: SkillTags.java:141
static SkillSet getSkills(Object entity)
Definition: SkillTags.java:137
static List< String > getEntityPaths(Dao dao, String root, Set< SkillItem > set)
Definition: SkillTags.java:112
static List< String > getEntityPaths(String root, Set< SkillItem > set)
Definition: SkillTags.java:108
static SkillSet getAvailables(String root, boolean user)
Definition: SkillTags.java:56
static List< String > getIdentifiers(String root, Set< SkillItem > set)
Definition: SkillTags.java:100
static SkillSet getSiblings(String root, Set< String > skillNames)
Definition: SkillTags.java:70
static SkillSet getAvailables(String root)
Definition: SkillTags.java:42
static List< String > getIdentifiers(Dao dao, String root, Set< SkillItem > set)
Definition: SkillTags.java:104
static Set< SkillType > getJuridicals()
Definition: SkillType.java:74
static Set< SkillType > getNaturals()
Definition: SkillType.java:70