BrightSide Workbench Full Report + Source Code
Skills.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.skills;
20 
21 import java.util.Arrays;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.stream.Collectors;
26 import org.turro.string.Strings;
27 import org.turro.elephant.db.ElephantPU;
28 import org.turro.elephant.db.WhereClause;
29 import org.turro.elephant.entities.db.Skill;
30 import org.turro.elephant.entities.db.SkillType;
31 import org.turro.jpa.Dao;
32 import org.turro.sql.SqlClause;
33 
38 public class Skills {
39 
40  public static List<Skill> getRootSkillsFrom(String rootFilter, SkillType... types) {
41  return getSkillsFrom(rootFilter == null ? "*" : rootFilter, types);
42  }
43 
44  public static List<SkillRoot> getSkillsAsRootFrom(String entityPath, SkillType... types) {
45  return getSkillsFrom(entityPath, types).stream().map(s -> new SkillRoot(s)).collect(Collectors.toList());
46  }
47 
48  public static List<Skill> getSkillsFrom(String entityPath, SkillType... types) {
49  WhereClause wc = new WhereClause();
50  wc.addClause("select s from Skill s");
51  wc.addClause("where s.entityPath = :path");
52  wc.addNamedValue("path", entityPath);
53  if(types != null) {
54  wc.addClause("and s.type in (:types)");
55  wc.addNamedValue("types", Arrays.asList(types));
56  }
57  return new ElephantPU().getResultList(Skill.class, wc);
58  }
59 
60  public static List<SkillRoot> getAvailables(String rootFilter, SkillType... types) {
61  return getAvailables(rootFilter, false, types);
62  }
63 
64  public static List<SkillRoot> getValidateds(String rootFilter, SkillType... types) {
65  return getAvailables(rootFilter, true, types);
66  }
67 
68  public static List<SkillRoot> getAvailables(String rootFilter, boolean validated, SkillType... types) {
69  WhereClause wc = new WhereClause();
70  wc.addClause("select new org.turro.skills.SkillRoot(s.skill, s.type, count(s))");
71  wc.addClause("from Skill s");
72  wc.addClause(validated ? "where s.validated = TRUE" : "where 1=1");
73  if(!Strings.isBlank(rootFilter)) {
74  wc.addClause("and s.entityPath like :root");
75  wc.addNamedValue("root", "/" + rootFilter + "/%");
76  }
77  if(types != null) {
78  wc.addClause("and s.type in (:types)");
79  wc.addNamedValue("types", Arrays.asList(types));
80  }
81  wc.addClause("group by s.skill, s.type");
82  return new ElephantPU().getResultList(SkillRoot.class, wc);
83  }
84 
85  public static void addSkillFor(String entityPath, Skill skill) {
86  Dao dao = new ElephantPU();
87  skill.setEntityPath(entityPath);
88  dao.saveObject(skill);
89  }
90 
91  public static void setSkillsFor(String entityPath, Set<Skill> skills, SkillType... types) {
92  Dao dao = new ElephantPU();
93  removeSkillsFor(dao, entityPath, types);
94  skills.forEach(s -> s.setEntityPath(entityPath));
95  dao.saveCollection(skills);
96  }
97 
98  public static void removeSkillsFor(String entityPath, SkillType... types) {
99  removeSkillsFor(new ElephantPU(), entityPath, types);
100  }
101 
102  public static void removeSkillsFor(Dao dao, String entityPath, SkillType... types) {
103  WhereClause wc = new WhereClause();
104  wc.addClause("delete from Skill s");
105  wc.addClause("where s.entityPath = :path");
106  wc.addNamedValue("path", entityPath);
107  if(types != null) {
108  wc.addClause("and s.type in (:types)");
109  wc.addNamedValue("types", Arrays.asList(types));
110  }
111  dao.executeUpdate(wc);
112  }
113 
114  public static void removeSkills(String entityPath) {
115  Dao dao = new ElephantPU();
116  WhereClause wc = new WhereClause();
117  wc.addClause("delete from Skill");
118  wc.addClause("where entityPath = :path");
119  wc.addNamedValue("path", entityPath);
120  dao.executeUpdate(wc);
121  }
122 
123  /* Utils */
124 
125  public static Set<String> getAllPaths(String root) {
126  return new HashSet<>(SqlClause.select("distinct s.entityPath").from("Skill s")
127  .where().startsWith("s.entityPath", "/" + root + "/")
128  .dao(new ElephantPU())
129  .resultList(String.class));
130  }
131 
132  private Skills() {}
133 
134 }
void addNamedValue(String name, Object value)
void setEntityPath(String entityPath)
Definition: Skill.java:55
int executeUpdate(String query)
Definition: Dao.java:463
void saveCollection(Collection objs)
Definition: Dao.java:144
static List< SkillRoot > getSkillsAsRootFrom(String entityPath, SkillType... types)
Definition: Skills.java:44
static List< SkillRoot > getAvailables(String rootFilter, SkillType... types)
Definition: Skills.java:60
static List< SkillRoot > getValidateds(String rootFilter, SkillType... types)
Definition: Skills.java:64
static void removeSkillsFor(String entityPath, SkillType... types)
Definition: Skills.java:98
static List< SkillRoot > getAvailables(String rootFilter, boolean validated, SkillType... types)
Definition: Skills.java:68
static Set< String > getAllPaths(String root)
Definition: Skills.java:125
static List< Skill > getRootSkillsFrom(String rootFilter, SkillType... types)
Definition: Skills.java:40
static void removeSkills(String entityPath)
Definition: Skills.java:114
static void setSkillsFor(String entityPath, Set< Skill > skills, SkillType... types)
Definition: Skills.java:91
static void removeSkillsFor(Dao dao, String entityPath, SkillType... types)
Definition: Skills.java:102
static List< Skill > getSkillsFrom(String entityPath, SkillType... types)
Definition: Skills.java:48
static void addSkillFor(String entityPath, Skill skill)
Definition: Skills.java:85