BrightSide Workbench Full Report + Source Code
ProjectGrantList.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.client;
20 
21 import java.time.Instant;
22 import java.util.ArrayList;
23 import javax.json.JsonValue;
24 import org.turro.alliance.db.entities.AxProjectGrant;
25 import org.turro.alliance.db.entities.ProcedenceId;
26 import org.turro.alliance.service.AxConstants;
27 import org.turro.alliance.service.AxServer;
28 import org.turro.cache.ObjectCache;
29 import org.turro.dossier.db.DossierPU;
30 import org.turro.dossier.entity.ProjectGrant;
31 import org.turro.jpa.search.DaoHtmlSearch;
32 import org.turro.jpa.search.DaoSearchKey;
33 import org.turro.json.Jsons;
34 import org.turro.sql.SqlClause;
35 import org.turro.string.Strings;
36 import org.turro.time.Time;
37 import org.turro.util.Comparison;
38 import org.turro.ws.content.JsonProvider;
39 import org.turro.ws.service.member.Servers;
40 
45 public class ProjectGrantList extends ArrayList<AxProjectGrant> {
46 
47  public ProjectGrantList() {
48  addAll(ObjectCache.instance().sync("alliance-grants", ArrayList.class, () -> ProjectGrantList.getGrants()));
49  }
50 
52  return stream().filter(pg -> pg.getProjectGrantId().equals(id)).findFirst().orElse(null);
53  }
54 
55  public void doFilter(DaoHtmlSearch dhs) {
56  if(dhs != null) {
57  DaoSearchKey dsk = dhs.get("search-value");
58  if(dsk != null) {
59  String search = dsk.getValue();
60  removeIf(pg -> !Strings.findsIgnoreCase(pg.getTitle(), search));
61  }
62  dsk = dhs.get("minimis");
63  if(dsk != null) {
64  String minimis = dsk.getValue();
65  removeIf(pg -> {
66  if("minimis".equals(minimis)) {
67  return !pg.isMinimis();
68  } else if("nominimis".equals(minimis)) {
69  return pg.isMinimis();
70  } else {
71  return false;
72  }
73  });
74  }
75  dsk = dhs.get("grant");
76  if(dsk != null) {
77  String grant = dsk.getValue();
78  removeIf(pg -> {
79  if("subvention".equals(grant)) {
80  return !pg.isSubvention();
81  } else if("loan".equals(grant)) {
82  return !pg.isLoan();
83  } else if("combination".equals(grant)) {
84  return !pg.isLoan() && !pg.isSubvention();
85  } else {
86  return false;
87  }
88  });
89  }
90  dsk = dhs.get("collab");
91  if(dsk != null) {
92  String collab = dsk.getValue();
93  removeIf(pg -> {
94  if("collaboration".equals(collab)) {
95  return !pg.isCollaboration();
96  } else if("individual".equals(collab)) {
97  return pg.isCollaboration();
98  } else {
99  return false;
100  }
101  });
102  }
103  dsk = dhs.get("micro");
104  if(dsk != null) {
105  String micro = dsk.getValue();
106  removeIf(pg -> "true".equals(micro) && !pg.isMicro());
107  }
108  dsk = dhs.get("small");
109  if(dsk != null) {
110  String small = dsk.getValue();
111  removeIf(pg -> "true".equals(small) && !pg.isSmall());
112  }
113  dsk = dhs.get("medium");
114  if(dsk != null) {
115  String medium = dsk.getValue();
116  removeIf(pg -> "true".equals(medium) && !pg.isMedium());
117  }
118  dsk = dhs.get("big");
119  if(dsk != null) {
120  String big = dsk.getValue();
121  removeIf(pg -> "true".equals(big) && !pg.isBig());
122  }
123  dsk = dhs.get("hiring");
124  if(dsk != null) {
125  String hiring = dsk.getValue();
126  removeIf(pg -> "true".equals(hiring) && !pg.isHiring());
127  }
128  dsk = dhs.get("idi");
129  if(dsk != null) {
130  String idi = dsk.getValue();
131  removeIf(pg -> "true".equals(idi) && !pg.isIdi());
132  }
133  dsk = dhs.get("investment");
134  if(dsk != null) {
135  String investment = dsk.getValue();
136  removeIf(pg -> "true".equals(investment) && !pg.isInvestment());
137  }
138  dsk = dhs.get("start-up");
139  if(dsk != null) {
140  String startup = dsk.getValue();
141  removeIf(pg -> "true".equals(startup) && !pg.isStartUp());
142  }
143  dsk = dhs.get("deadline");
144  if(dsk != null) {
145  String deadline = dsk.getValue();
146  removeIf(pg -> {
147  if(pg.getEndDate() != null) {
148  if("active".equals(deadline)) {
149  return Time.from(pg.getEndDate()).isBefore(Instant.now());
150  } else if("ended".equals(deadline)) {
151  return Time.from(pg.getEndDate()).isAfter(Instant.now());
152  } else if("recurrent".equals(deadline)) {
153  return pg.isRecurrent();
154  }
155  }
156  return false;
157  });
158  }
159  }
160  }
161 
162  public static ArrayList<AxProjectGrant> getGrants() {
163  ArrayList<AxProjectGrant> list = new ArrayList<>();
164  Servers.getServers(AxServer.SERVER_SERVICE).forEach(server -> {
165  Jsons criteria = Jsons.object();
166  criteria.add(JsonProvider.JSON_ONLY_ITEMS, true);
167  criteria.add("notSelf", true);
168  Jsons data = Servers.getData(server, AxConstants.PROJECT_GRANT_ITERATOR, criteria);
169  Jsons items = data.getArray(JsonProvider.JSON_ITEMS);
170  for(JsonValue e : items.getStructure().asArray()) {
171  list.add(AxProjectGrant.fromJson(e));
172  }
173  });
174  list.addAll(SqlClause.select("pg").from("ProjectGrant pg")
175  .dao(new DossierPU())
176  .resultList(ProjectGrant.class)
177  .stream()
178  .map(pg -> AxProjectGrant.from(0, pg))
179  .toList());
180  list.sort((pg1, pg2) -> {
181  return Comparison.descendant().compare(pg1.getEndDate(), pg2.getEndDate()).get();
182  });
183  return list;
184  }
185 
186 }
AxProjectGrant getProjectGrant(ProcedenceId id)
static ArrayList< AxProjectGrant > getGrants()
static AxProjectGrant from(long memberId, ProjectGrant projectGrant)
static final String SERVER_SERVICE
Definition: AxServer.java:51