BrightSide Workbench Full Report + Source Code
ParticipationCtrl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.participation;
20 
21 import java.util.Collections;
22 import java.util.List;
23 import org.turro.string.Strings;
24 import org.turro.action.MailSenders;
25 import org.turro.annotation.ElephantPlugin;
26 import org.turro.assistant.ParticipationInfo;
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.AbstractDirectEntityCtrl;
31 import org.turro.elephant.direct.DirectContent;
32 import org.turro.entities.IElephantEntity;
33 import org.turro.marker.ElephantMarker;
34 import org.turro.plugin.contacts.ContactList;
35 import org.turro.plugin.contacts.IContact;
36 
41 @ElephantPlugin(label="participation-ctrl")
42 @DirectContent(identifier="participation-action")
44 
45  private ParticipationReason reason;
46 
47  public ParticipationCtrl() {
48  super("widgets/participation", "participation");
49  setNeedsUser(true);
50  }
51 
52  @Override
53  public void setReason(ParticipationReason reason) {
54  this.reason = reason;
55  setTemplate(reason.toString().replaceAll("_", "-").toLowerCase());
56  }
57 
58  @Override
60  ContactList list = new ContactList();
61  for(IEntityParticipation ep : new ParticipationInfo(entityPath, reason).getParticipations()) {
62  list.add(ep.getContact());
63  }
64  return list;
65  }
66 
67  @Override
68  public List<IEntityParticipation> getParticipations() {
69  return new ParticipationInfo(entityPath, reason).getParticipations();
70  }
71 
72  @Override
73  public List<IEntityParticipation> getCrossParticipations() {
74  return new ParticipationInfo(entityPath, reason).getCrossParticipations();
75  }
76 
77  @Override
78  protected void prepareCleanMarker(ElephantMarker marker, KeyValueMap map) {
79  marker.putAll(map);
80  marker.put("info", new ParticipationInfo(map.get("entityPath"),
81  map.get("participatorPath"), ParticipationReason.valueOf((String) marker.get("reason"))));
82  marker.put("checkLink", getAjaxUrl((String) marker.get("domid"), map));
83  }
84 
85  /* IEntityCtrl */
86  @Override
87  protected void prepareMarker(ElephantMarker marker) {
88  addContainerId(marker, true);
90  if(contact.isWebUser()) {
91  String participatorPath = "/contact/" + contact.getId();
92  marker.put("info", new ParticipationInfo(entityPath, participatorPath, reason));
93  KeyValueMap kvm = new KeyValueMap(Collections.EMPTY_MAP);
94  kvm.put("type", "click");
95  kvm.put("entityPath", entityPath);
96  kvm.put("participatorPath", participatorPath);
97  kvm.put("reason", reason.toString());
98  marker.put("checkLink", getAjaxUrl((String) marker.get("containerId"), kvm));
99  }
100  }
101 
102  /* IDirectContent */
103 
104  @Override
105  protected String getIdentifier() {
106  return ParticipationCtrl.class.getAnnotation(DirectContent.class).identifier();
107  }
108 
109  @Override
110  protected void doExecute(IConstructor constructor, KeyValueMap map) {
111  String type = map.get("type");
112  if("click".equals(type)) {
113  processClick(constructor, map);
114  }
115  }
116 
117  private void processClick(IConstructor constructor, KeyValueMap map) {
118  ParticipationReason reason = ParticipationReason.valueOf(map.get("reason"));
119  if(reason != null) {
120  setReason(reason);
121  ParticipationInfo info = new ParticipationInfo(map.get("entityPath"), map.get("participatorPath"), reason);
122  boolean selected = info.toggle();
123  if(selected && reason.isMailableOnSelect()) {
124  IElephantEntity participator = info.getParticipator(),
125  entity = info.getEntity();
126  String subject = participator.getName() + " " + info.getReasonStr() + " " + entity.getName();
127  String description = entity.getDescription();
128  if(Strings.isBlank(description)) description = "";
131  .send(subject, subject + "\n\n" + description);
132  } else if(reason.isMailableOnDeselect()) {
133  IElephantEntity participator = info.getParticipator(),
134  entity = info.getEntity();
135  String subject = participator.getName() + " NOT " + info.getReasonStr() + " " + entity.getName();
136  String description = entity.getDescription();
137  if(Strings.isBlank(description)) description = "";
138  MailSenders.getPool()
139  .addAdministrators()
140  .send(subject, subject + "\n\n" + description);
141  }
142  }
143  writeMarkerToResponse(constructor, map);
144  }
145 
146 }
static IMailSender getPool()
List< IEntityParticipation > getParticipations()
List< IEntityParticipation > getCrossParticipations()
Object put(Object key, Object value)
List< IEntityParticipation > getParticipations()
void setReason(ParticipationReason reason)
List< IEntityParticipation > getCrossParticipations()
void doExecute(IConstructor constructor, KeyValueMap map)
void prepareCleanMarker(ElephantMarker marker, KeyValueMap map)