BrightSide Workbench Full Report + Source Code
ProjectProvider.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 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 java.util.List;
22 import java.util.Map;
23 import org.turro.alliance.db.AlliancePU;
24 import org.turro.alliance.db.entities.AxProject;
25 import org.turro.jpa.Dao;
26 import org.turro.json.Jsons;
27 import org.turro.sql.SqlClause;
28 import org.turro.string.Strings;
29 import org.turro.util.Cached;
30 import org.turro.ws.WsMember;
31 import org.turro.ws.content.JsonServerProvider;
32 
37 public class ProjectProvider extends JsonServerProvider {
38 
39  private final List<String> banned;
40 
41  public ProjectProvider(WsMember member, String reason, Jsons criteria) {
42  super(member, reason, criteria);
43  banned = Banned.of(member).getBannedIds();
44  }
45 
46  @Override
47  protected Jsons loadData(WsMember member, String reason, Jsons criteria) {
48  Jsons projects = Jsons.array();
49  List<Long> ids = criteria.getArrayValues("categoryIds", Long.class);
50  String search = criteria.getString("search", "");
51  int phase = criteria.getInt("phase", -1);
52  boolean withBanned = criteria.getBoolean("withBanned", false);
53  SqlClause.select("p").from("AxProject p")
54  .startIf(ids != null && !ids.isEmpty())
55  .where().in("categoryId", ids)
56  .endIf()
57  .startIf(!Strings.isBlank(search))
58  .whereOrAnd().partial(search, "p.code", "p.name", "p.summary")
59  .endIf()
60  .startIf(phase > -1)
61  .whereOrAnd().equal("p.phaseIndex", phase)
62  .endIf()
63  .startIf(!withBanned)
64  .whereOrAnd().notIn("id", banned)
65  .endIf()
66  .orderBy("p.creation desc")
67  .start(criteria.getInt("curr", -1)).max(criteria.getInt("page", -1))
68  .dao(dao.get())
69  .resultList(AxProject.class).forEach(project -> {
70  projects.addValue(Jsons.read(project.toJson(Map.of("banned", withBanned && banned.contains(project.getId())))));
71  });
72  return projects;
73  }
74 
75  @Override
76  protected long count(WsMember member, String reason, Jsons criteria) {
77  List<Long> ids = criteria.getArrayValues("categoryIds", Long.class);
78  String search = criteria.getString("search", "");
79  int phase = criteria.getInt("phase", -1);
80  boolean withBanned = criteria.getBoolean("withBanned", false);
81  return SqlClause.select("count(p)").from("AxProject p")
82  .startIf(ids != null && !ids.isEmpty())
83  .where().in("categoryId", ids)
84  .endIf()
85  .startIf(!Strings.isBlank(search))
86  .whereOrAnd().partial(search, "p.code", "p.name", "p.summary")
87  .endIf()
88  .startIf(phase > -1)
89  .whereOrAnd().equal("p.phaseIndex", phase)
90  .endIf()
91  .startIf(!withBanned)
92  .whereOrAnd().notIn("id", banned)
93  .endIf()
94  .dao(dao.get())
95  .singleResult(Long.class, 0L);
96  }
97 
98  private final Cached<Dao> dao = Cached.instance(() -> new AlliancePU());
99 
100 }
List< String > getBannedIds()
Definition: Banned.java:36
static Banned of(WsMember member)
Definition: Banned.java:81
Jsons loadData(WsMember member, String reason, Jsons criteria)
ProjectProvider(WsMember member, String reason, Jsons criteria)
long count(WsMember member, String reason, Jsons criteria)