BrightSide Workbench Full Report + Source Code
ProjectGrantProvider.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.alliance.db.AlliancePU;
22 import org.turro.alliance.db.entities.AxProjectGrant;
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 ProjectGrantProvider extends JsonServerProvider {
36 
37  public ProjectGrantProvider(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 projectGrants = Jsons.array();
44  String search = criteria.getString("search", "");
45  boolean notSelf = criteria.getBoolean("notSelf", false);
46  SqlClause.select("p").from("AxProjectGrant p")
47  .startIf(!Strings.isBlank(search))
48  .whereOrAnd().partial(search, "p.title")
49  .endIf()
50  .startIf(notSelf)
51  .whereOrAnd().notEqual("projectGrantId.memberId", member.getMemberId())
52  .endIf()
53  .orderBy("p.endDate desc")
54  .start(criteria.getInt("curr", -1)).max(criteria.getInt("page", -1))
55  .dao(dao.get())
56  .resultList(AxProjectGrant.class).forEach(projectGrant -> {
57  projectGrants.addValue(Jsons.read(projectGrant.toJson()));
58  });
59  return projectGrants;
60  }
61 
62  @Override
63  protected long count(WsMember member, String reason, Jsons criteria) {
64  String search = criteria.getString("search", "");
65  boolean notSelf = criteria.getBoolean("notSelf", false);
66  return SqlClause.select("count(p)").from("AxProjectGrant p")
67  .startIf(!Strings.isBlank(search))
68  .whereOrAnd().partial(search, "p.title")
69  .endIf()
70  .startIf(notSelf)
71  .whereOrAnd().notEqual("projectGrantId.memberId", member.getMemberId())
72  .endIf()
73  .dao(dao.get())
74  .singleResult(Long.class, 0L);
75  }
76 
77  private final Cached<Dao> dao = Cached.instance(() -> new AlliancePU());
78 
79 }
ProjectGrantProvider(WsMember member, String reason, Jsons criteria)
Jsons loadData(WsMember member, String reason, Jsons criteria)
long count(WsMember member, String reason, Jsons criteria)