BrightSide Workbench Full Report + Source Code
TagProvider.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2024 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.alliance.provider;
20 
21 import org.turro.elephant.db.ElephantPU;
22 import org.turro.elephant.entities.db.Tag;
23 import org.turro.jpa.Dao;
24 import org.turro.json.Jsons;
25 import org.turro.sql.SqlClause;
26 import org.turro.string.Strings;
27 import org.turro.util.Cached;
28 import org.turro.ws.WsMember;
29 import org.turro.ws.content.JsonServerProvider;
30 
35 public class TagProvider extends JsonServerProvider {
36 
37  public TagProvider(WsMember member, String reason, Jsons criteria) {
38  super(member, reason, criteria);
39  }
40 
41  @Override
42  protected Jsons loadData(WsMember member, String reason, Jsons criteria) {
43  Jsons tags = Jsons.array();
44  String search = criteria.getString("search", "");
45  String root = criteria.getString("root", "");
46  String entityPath = criteria.getString("path", "");
47  SqlClause.select("t").from("Tag t")
48  .startIf(!Strings.isBlank(search))
49  .whereOrAnd().partial(search, "t.tagName")
50  .endIf()
51  .startIf(!Strings.isBlank(root))
52  .whereOrAnd().startsWith("t.entityPath", "/" + root + "/")
53  .endIf()
54  .startIf(!Strings.isBlank(entityPath))
55  .whereOrAnd().equal("t.entityPath", entityPath)
56  .endIf()
57  .orderBy("t.tagName")
58  .dao(dao.get())
59  .resultList(Tag.class).forEach(tag -> {
60  tags.addValue(Jsons.read(tag.toJson()));
61  });
62  return tags;
63  }
64 
65  @Override
66  protected long count(WsMember member, String reason, Jsons criteria) {
67  String search = criteria.getString("search", "");
68  String root = criteria.getString("root", "");
69  String entityPath = criteria.getString("path", "");
70  return SqlClause.select("count(t)").from("Tag t")
71  .startIf(!Strings.isBlank(search))
72  .whereOrAnd().partial(search, "t.tagName")
73  .endIf()
74  .startIf(!Strings.isBlank(root))
75  .whereOrAnd().startsWith("t.entityPath", "/" + root + "/")
76  .endIf()
77  .startIf(!Strings.isBlank(entityPath))
78  .whereOrAnd().equal("t.entityPath", entityPath)
79  .endIf()
80  .orderBy("t.tagName")
81  .dao(dao.get())
82  .singleResult(Long.class, 0L);
83  }
84 
85  private final Cached<Dao> dao = Cached.instance(() -> new ElephantPU());
86 
87 }
88 
TagProvider(WsMember member, String reason, Jsons criteria)
long count(WsMember member, String reason, Jsons criteria)
Jsons loadData(WsMember member, String reason, Jsons criteria)