BrightSide Workbench Full Report + Source Code
ProjectJsonIterator.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.client;
20 
21 import java.io.Writer;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Objects;
25 import javax.json.JsonObject;
26 import org.turro.alliance.content.control.ProjectAction;
27 import org.turro.alliance.content.control.ProjectCtrl;
28 import org.turro.alliance.service.AxConstants;
29 import org.turro.alliance.service.AxP2PServer;
30 import org.turro.collections.Item;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.elephant.context.IConstructor;
33 import org.turro.elephant.impl.repository.RepositoryFile;
34 import org.turro.elephant.web.SocialNet;
35 import org.turro.entities.Entities;
36 import org.turro.jpa.search.DaoSearchKey;
37 import org.turro.json.JsonSearch;
38 import org.turro.json.Jsons;
39 import org.turro.marker.ElephantMarker;
40 import org.turro.plugin.contacts.IContact;
41 import org.turro.string.Strings;
42 import org.turro.util.Converter;
43 import org.turro.ws.WsServer;
44 import org.turro.ws.content.iterator.JsonHtmlClientIterator;
45 import org.turro.ws.content.search.JsonHtmlSearch;
46 import org.turro.ws.service.WsConstants;
47 import org.turro.ws.service.member.Servers;
48 import org.turro.ws.service.p2p.P2Ps;
49 
54 public class ProjectJsonIterator extends JsonHtmlClientIterator {
55 
56  public ProjectJsonIterator(IConstructor constructor, IContact contact, WsServer server, Writer writer, boolean mail) {
57  super(constructor, contact, server, writer, mail);
58  }
59 
60  @Override
61  protected String entityRoot() {
62  // c for client?
63  return "caxproject";
64  }
65 
66  @Override
67  protected Jsons entity(String value) {
68  return items.getStructure().getArrayValues(JsonObject.class).stream()
69  .filter(item -> item.getString("id").equals(value.replaceAll("_", "#")))
70  .map(item -> Jsons.read(item.toString()))
71  .findFirst().orElse(null);
72  }
73 
74  @Override
75  protected void renderSummary(ElephantMarker marker, Jsons e, int page) {
76  if(e != null) {
77  ProjectCtrl pctrl = new ProjectCtrl();
78  pctrl.setServer(getServer());
79  pctrl.setAxId(e.getString("id"));
81  marker.put("banctrl", pctrl);
82  marker.put("itemLink", getItemLink(e));
83  marker.put("project", JsonSearch.of(e).silent().root());
84  marker.put("server", getServer());
85  }
86  marker.process(getTemplateRoot(), getSummaryTemplate(), getWriter());
87  }
88 
89  @Override
90  protected void renderItem(ElephantMarker marker, Jsons e, int page) {
91  Jsons criteria = Jsons.object();
92  criteria.add("projectId", e.context().read("$.projectId.entityId", String.class));
93  Jsons project = P2Ps.getP2PData(getServer().getServerDomain(),
94  e.context().read("$.projectId.memberId", Long.class),
96  if(project != null) {
97  Jsons memberInfo = Servers.getData(getServer(), WsConstants.CLIENT_INFO);
98  ProjectCtrl banctrl = new ProjectCtrl();
99  banctrl.setServer(getServer());
100  banctrl.setAxId(e.getString("id"));
102  marker.put("banctrl", banctrl);
103  ProjectCtrl offctrl = new ProjectCtrl();
104  offctrl.setServer(getServer());
105  offctrl.setAxId(e.getString("id"));
107  offctrl.setParticiationLabel("Offerer");
108  marker.put("offctrl", offctrl);
109  ProjectCtrl benctrl = new ProjectCtrl();
110  benctrl.setServer(getServer());
111  benctrl.setAxId(e.getString("id"));
113  benctrl.setParticiationLabel("Beneficiary");
114  marker.put("benctrl", benctrl);
115  ProjectCtrl conctrl = new ProjectCtrl();
116  conctrl.setServer(getServer());
117  conctrl.setAxId(e.getString("id"));
119  conctrl.setParticiationLabel("Consortium");
120  marker.put("conctrl", conctrl);
121  marker.put("itemLink", getItemLink(e));
122  marker.put("dossier", JsonSearch.of(project).silent().root());
123  criteria = Jsons.object();
124  criteria.add("projectId", e.context().read("$.projectId.entityId", String.class));
125  Jsons attachments = P2Ps.getP2PData(getServer().getServerDomain(),
126  e.context().read("$.projectId.memberId", Long.class),
128  marker.put("attachments", JsonSearch.of(attachments).silent().root());
129  marker.put("server", getServer());
130  marker.put("domain", getServer().getServerDomain());
131  marker.put("service", getServer().getService());
132  marker.put("member", e.context().read("$.projectId.memberId", Long.class));
133  marker.put("serverFace", getServer().getContact().getFaces().getUrl());
134  marker.put("axid", e.getString("id"));
135  marker.put("selfMember", memberInfo.getLong("id"));
136  if(Objects.equals(marker.get("member"), marker.get("selfMember"))) {
137  marker.put("internalLink", Entities.getController("/dossier/" + e.context().read("$.projectId.entityId")).getEntityUrl());
138  }
139  }
140  marker.process(getTemplateRoot(), getFullTemplate(), getWriter());
141  }
142 
143  @Override
144  protected String title(Jsons e) {
145  return Strings.isBlank(e.getString("name"), null);
146  }
147 
148  @Override
149  protected Collection<String> metas(Jsons e) {
150  ArrayList<RepositoryFile> files = new ArrayList<>();
151  String path = getItemLink(e);
152  SocialNet sn = new SocialNet(path, e.getString("name"), e.getString("summary", "*"), files);
153  return sn.getMetas();
154  }
155 
156  @Override
157  protected String getTemplateRoot() {
158  return isMail() ? "content/newsletter/sections/alliance/client/projects" : "alliance/client/project";
159  }
160 
161  @Override
162  protected String getItemLink(Jsons e) {
163  return constructor.getCurrentContext().getFullPath() +
164  "?item=" + e.getString("id").replaceAll("#", "_");
165  }
166 
167  @Override
168  protected String reason() {
169  return AxConstants.PROJECT_ITERATOR;
170  }
171 
172  @Override
173  protected Jsons criteria() {
174  Jsons criteria = Jsons.object();
175  criteria.add("withBanned", contact.isAdmin());
176  Item<Jsons, Long> selected = getNavigationTree().getSelectedItem();
177  if(selected != null) {
178  criteria.addArray("categoryIds");
179  selected.selfAndChildrenIds().forEach(id -> {
180  criteria.add(id);
181  });
182  }
183  if(isSearchOption()) {
184  criteria.toRoot();
185  JsonHtmlSearch dhs = JsonHtmlSearch.getInstance(constructor, ElephantContext.getContextVariable(constructor));
186  if(dhs != null) {
187  DaoSearchKey dsk = dhs.get("search-value");
188  criteria.add("search", dsk.getValue());
189  dsk = dhs.get("phase");
190  criteria.add("phase", Converter.STANDARD.convert(Strings.isBlank(dsk.getValue(), "-1"), Integer.class, -1));
191  }
192  }
193  return criteria;
194  }
195 
196 }
void renderSummary(ElephantMarker marker, Jsons e, int page)
ProjectJsonIterator(IConstructor constructor, IContact contact, WsServer server, Writer writer, boolean mail)
void renderItem(ElephantMarker marker, Jsons e, int page)
void setParticiationLabel(String particiationLabel)
static String getContextVariable(IConstructor constructor)
static IElephantEntity getController(String path)
Definition: Entities.java:78
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)