BrightSide Workbench Full Report + Source Code
Secs.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.security;
20 
21 import java.util.List;
22 import java.util.Optional;
23 import java.util.Set;
24 import org.turro.string.Strings;
25 import org.turro.contacts.db.ContactsPU;
26 import org.turro.elephant.db.WhereClause;
27 import org.turro.jpa.Dao;
28 
33 public class Secs {
34 
35  public static SecSet search(String root, String value, int max) {
36  Dao dao = new ContactsPU();
37  WhereClause wc = new WhereClause();
38  wc.addClause("select new org.turro.security.SecItem(s.name, count(s))");
39  wc.addClause("from Syndication s");
40  wc.addClause("where 1=1");
41  if(!Strings.isBlank(value)) {
42  wc.addLikeFields(new String[]{ "s.name" }, value.toString());
43  }
44  wc.addClause("group by s.name");
45  wc.addClause("order by s.name");
46  return new SecSet(dao.getResultList(wc, max));
47  }
48 
49  public static SecSet getAvailables() {
50  Dao dao = new ContactsPU();
51  WhereClause wc = new WhereClause();
52  wc.addClause("select new org.turro.security.SecItem(s.name, count(s))");
53  wc.addClause("from Syndication s");
54  wc.addClause("where 1=1");
55  wc.addClause("group by s.name");
56  return new SecSet(dao.getResultList(wc));
57  }
58 
59  public static SecSet getSiblings(Set<String> secNames) {
60  Dao dao = new ContactsPU();
61  SecSet finalSet = null;
62  for(String sec : secNames) {
63  SecSet set = getSiblings(dao, sec);
64  if(finalSet != null) {
65  finalSet.retainAll(set);
66  } else {
67  finalSet = set;
68  }
69  }
70  return Optional.ofNullable(finalSet).orElseGet(() -> SecSet.empty());
71  }
72 
73  private static SecSet getSiblings(Dao dao, String secName) {
74  WhereClause wc = new WhereClause();
75  wc.addClause("select new org.turro.security.SecItem(s.name, count(s))");
76  wc.addClause("from Syndication s");
77  wc.addClause("join Syndication s1 on s.contact = s1.contact");
78  wc.addClause("where s.name <> :sec");
79  wc.addClause("and s1.name = :sec");
80  wc.addNamedValue("sec", secName);
81  wc.addClause("group by s.name");
82  return new SecSet(dao.getResultList(wc));
83  }
84 
85  public static List<String> getIdentifiers(Dao dao, Set<SecItem> sets) {
86  WhereClause wc = new WhereClause();
87  wc.addClause("select distinct s.contact.id from Syndication s");
88  wc.addClause("where 1=1");
89  int count = 0;
90  for(SecItem sec : sets) {
91  wc.addClause("and exists (");
92  wc.addClause("select s.name from Syndication s2");
93  wc.addClause("where s2.contact = s.contact");
94  wc.addClause("and s2.name = :name" + count);
95  wc.addNamedValue("name" + count, sec.getSecName());
96  wc.addClause(")");
97  count++;
98  }
99  return dao.getResultList(String.class, wc);
100  }
101 
102  public static void removeSyndication(Dao dao, String syndication) {
103  WhereClause wc = new WhereClause();
104  wc.addClause("delete from Syndication s");
105  wc.addClause("where s.name = :name");
106  wc.addNamedValue("name", syndication);
107  dao.executeUpdate(wc);
108  }
109 
110  private Secs() {}
111 
112 }
void addLikeFields(String[] fields, String value)
void addNamedValue(String name, Object value)
int executeUpdate(String query)
Definition: Dao.java:463
static SecSet empty()
Definition: SecSet.java:121
static void removeSyndication(Dao dao, String syndication)
Definition: Secs.java:102
static SecSet search(String root, String value, int max)
Definition: Secs.java:35
static SecSet getSiblings(Set< String > secNames)
Definition: Secs.java:59
static SecSet getAvailables()
Definition: Secs.java:49
static List< String > getIdentifiers(Dao dao, Set< SecItem > sets)
Definition: Secs.java:85