BrightSide Workbench Full Report + Source Code
ProjectCtrl.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.content.control;
20 
21 import java.util.Collections;
22 import org.turro.action.Contacts;
23 import org.turro.action.MailSenders;
24 import org.turro.alliance.client.Projects;
25 import static org.turro.alliance.content.control.ProjectAction.TOGGLE_BAN;
26 import org.turro.alliance.service.AxConstants;
27 import org.turro.auth.Authentication;
28 import org.turro.collections.KeyValueMap;
29 import org.turro.elephant.context.IConstructor;
30 import org.turro.elephant.direct.AbstractDirectContentCtrl;
31 import org.turro.elephant.direct.DirectContent;
32 import org.turro.i18n.I_;
33 import org.turro.json.Jsons;
34 import org.turro.mail.json.JsonMailStructure;
35 import org.turro.marker.ElephantMarker;
36 import org.turro.plugin.contacts.CompoundId;
37 import org.turro.plugin.contacts.IContact;
38 import org.turro.ws.WsServer;
39 import org.turro.ws.WsServerPK;
40 import org.turro.ws.service.WsConstants;
41 import org.turro.ws.service.member.Servers;
42 import org.turro.ws.service.p2p.P2PServer;
43 import org.turro.ws.service.p2p.P2Ps;
44 
49 @DirectContent(identifier="axproject-action")
50 public class ProjectCtrl extends AbstractDirectContentCtrl {
51 
52  private WsServer server;
53  private ProjectAction action;
54  private String axId, particiationLabel;
55 
56  public ProjectCtrl() {
57  super("widgets/alliance/project");
58  setNeedsUser(true);
59  }
60 
61  public void setServer(WsServer server) {
62  this.server = server;
63  }
64 
65  public void setAction(ProjectAction action) {
66  this.action = action;
67  setTemplate(action.toString().replaceAll("_", "-").toLowerCase());
68  }
69 
70  public void setAxId(String axId) {
71  this.axId = axId;
72  }
73 
74  public void setParticiationLabel(String particiationLabel) {
75  this.particiationLabel = particiationLabel;
76  }
77 
78  @Override
79  protected void prepareCleanMarker(ElephantMarker marker, KeyValueMap map) {
80  marker.putAll(map);
81  marker.put("checkLink", getAjaxUrl((String) marker.get("domid"), map));
82  }
83 
84  /* IEntityCtrl */
85 
86  @Override
87  protected void prepareMarker(ElephantMarker marker) {
88  addContainerId(marker, true);
90  if(contact.isWebUser()) {
91  KeyValueMap kvm = new KeyValueMap(Collections.EMPTY_MAP);
92  kvm.put("type", "click");
93  kvm.put("serverDomain", server.getServerDomain());
94  kvm.put("service", server.getService());
95  kvm.put("action", action.name());
96  kvm.put("axId", axId);
97  switch(action) {
98  case TOGGLE_BAN -> {
99  marker.put("banned", Boolean.toString(isBanned()));
100  }
101  case PARTICIPATE -> {
102  marker.put("participationLabel", particiationLabel);
103  marker.put("participant", isParticipant(contact.getId()));
104  marker.put("requested", Boolean.toString(hasRequested(contact.getId())));
105  kvm.put("contactId", contact.getId());
106  kvm.put("as", particiationLabel);
107  }
108  }
109  marker.put("checkLink", getAjaxUrl((String) marker.get("containerId"), kvm));
110  }
111  }
112 
113  private boolean isBanned() {
114  Jsons response = Servers.getData(server, AxConstants.BAN_STATUS, Jsons.object().add("axId", axId));
115  if(!Jsons.isEmpty(response)) {
116  return response.getBoolean("banned");
117  } else {
118  return false;
119  }
120  }
121 
122  private boolean hasRequested(String contactId) {
123  return Projects.from(server).hasRequested(axId, contactId, particiationLabel).getBoolean("requested");
124  }
125 
126  private boolean isParticipant(String contactId) {
127  return Projects.from(server).status(axId, contactId, particiationLabel).getBoolean("participant");
128  }
129 
130  /* IDirectContent */
131 
132  @Override
133  protected String getIdentifier() {
134  return ProjectCtrl.class.getAnnotation(DirectContent.class).identifier();
135  }
136 
137  @Override
138  protected void doExecute(IConstructor constructor, KeyValueMap map) {
139  String type = map.get("type");
140  if("click".equals(type)) {
141  processClick(constructor, map);
142  }
143  }
144 
145  private void processClick(IConstructor constructor, KeyValueMap map) {
146  setAction(ProjectAction.valueOf(map.get("action")));
147  switch(action) {
148  case TOGGLE_BAN -> {
149  map.put("banned", Boolean.toString(toggleBanned(map)));
150  }
151  case PARTICIPATE -> {
152  map.put("requested", Boolean.toString(askParticipate(map)));
153  }
154  }
155  writeMarkerToResponse(constructor, map);
156  }
157 
158  public boolean toggleBanned(KeyValueMap map) {
159  WsServer server = Servers.getServer(WsServerPK.from(map.get("serverDomain"), map.get("service")));
160  Jsons response = Servers.getData(server, AxConstants.BAN_TOGGLE, Jsons.object().add("axId", map.get("axId")));
161  if(!Jsons.isEmpty(response)) {
162  return response.getBoolean("banned");
163  } else {
164  return false;
165  }
166  }
167 
168  public boolean askParticipate(KeyValueMap map) {
169  IContact contact = Contacts.getContactById(map.get("contactId"));
170  if(Contacts.isValid(contact)) {
171  IContact business = contact.getBusiness();
172  if(Contacts.isValid(business)) {
173  WsServer server = Servers.getServer(WsServerPK.from(map.get("serverDomain"), map.get("service")));
174  Jsons response = Servers.getData(server, AxConstants.AXPARTICIPATION_ASK, Jsons.object()
175  .add("as", map.get("as"))
176  .add("axId", map.get("axId"))
177  .add("contactId", contact.getId())
178  .add("contactName", contact.getName())
179  .add("contactFace", contact.getFaces().getUrl())
180  .add("companyId", business.getId())
181  .add("companyName", business.getName())
182  .add("companyFace", business.getFaces().getUrl())
183  );
184  if(!Jsons.isEmpty(response) && response.getBoolean("requested")) {
185  P2Ps.getP2PData(server.getServerDomain(), CompoundId.from(map.get("axId")).getMemberId(),
186  P2PServer.P2P_SERVICE, WsConstants.SEND_EMAIL, Jsons.object(Jsons.read(
187  JsonMailStructure.subject(I_.format("Alliance request from %s", server.getName()))
188  .message(I_.format("%s has requested to participate as %s in a project", contact.getName(), map.get("as")))
189  .administrators()
190  .toJson())));
193  .send(I_.format("Alliance request to %s", server.getName()),
194  I_.format("%s has requested to participate as %s in an outside project", contact.getName(), map.get("as")));
195  return true;
196  }
197  }
198  }
199  return false;
200  }
201 
202 }
static boolean isValid(IContact contact)
Definition: Contacts.java:52
static IContact getContactById(String id)
Definition: Contacts.java:72
static IMailSender getPool()
void prepareMarker(ElephantMarker marker)
void prepareCleanMarker(ElephantMarker marker, KeyValueMap map)
void setParticiationLabel(String particiationLabel)
void doExecute(IConstructor constructor, KeyValueMap map)
static String format(String msg, Object... arguments)
Definition: I_.java:49
Object put(Object key, Object value)
static CompoundId from(String id)
Definition: CompoundId.java:51
static WsServerPK from(String serverDomain, String service)
Definition: WsServerPK.java:33
String getServerDomain()
Definition: WsServer.java:52