BrightSide Workbench Full Report + Source Code
AgreementsPlugin.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.agreements;
20 
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.turro.action.IAgreements;
26 import org.turro.action.MailSenders;
27 import org.turro.action.queue.NotificationCategory;
28 import org.turro.action.queue.NotificationRole;
29 import org.turro.annotation.ElephantPlugin;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.context.IConstructor;
32 import org.turro.elephant.entities.db.AgreementSignature;
33 import org.turro.plugin.contacts.IContact;
34 
39 @ElephantPlugin(label = "agreements")
40 public class AgreementsPlugin implements IAgreements {
41 
42  private AgreementsUtil agreements;
43 
44  @Override
45  public void setContact(IContact contact) {
46  agreements = new AgreementsUtil(contact);
47  }
48 
49  @Override
50  public void setContact(String idContact) {
51  agreements = new AgreementsUtil(idContact);
52  }
53 
54  @Override
55  public boolean isValid() {
56  return agreements.isValid();
57  }
58 
59  @Override
60  public boolean canNotify() {
61  return agreements.canNotify();
62  }
63 
64  @Override
65  public boolean canAccess() {
66  return agreements.canAccess();
67  }
68 
69  @Override
70  public boolean canAct(String action) {
71  return agreements.canAct(action);
72  }
73 
74  @Override
75  public boolean declinedNotifications() {
76  return agreements.declinedNotifications();
77  }
78 
79  @Override
80  public boolean declinedAccessing() {
81  return agreements.declinedAccessing();
82  }
83 
84  @Override
85  public void sendIfNecessary(IConstructor constructor) {
86  for(AgreementSignature as : agreements.getSignatures(null)) {
87  if(as.shouldSend() && as.getSignedDate() == null && as.getAgreement().isRequiredToNotify()) {
88  try {
89  Map args = new HashMap();
91  .setRoot("/agreements")
92  .addContact(agreements.getContact())
93  .put("signature", as)
94  .put("acceptUrl", AgreementAction.createURL(constructor, as, agreements.getContact(), true))
95  .put("declineUrl", AgreementAction.createURL(constructor, as, agreements.getContact(), false))
96  .sendTemplate("requiredSignature", as.getAgreement().getTitle() + " : " + ElephantContext.getSiteName());
97  agreements.markAsSent(as);
98  } catch (Exception ex) {
99  Logger.getLogger(AgreementsPlugin.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
100  }
101  }
102  }
103  }
104 
105  @Override
106  public String getPendingAccessURL() {
107  for(AgreementSignature as : agreements.getSignatures(null)) {
108  if(!as.doesAgree() && as.getAgreement().isRequiredToAccess()) {
109  return "/user/myagreements/?item=" + as.getId();
110  }
111  }
112  return null;
113  }
114 
115  @Override
116  public String getPendingActURL(String action) {
117  for(AgreementSignature as : agreements.getSignatures(action)) {
118  if(!as.doesAgree() && as.getAgreement().isRequiredToAction() && action.equals(as.getAgreement().getAction())) {
119  return "/user/myagreements/?item=" + as.getId();
120  }
121  }
122  return null;
123  }
124 
125  @Override
126  public boolean canSendEmails(NotificationCategory nc) {
127  return (NotificationRole.NOTIF_OBSERVER.equals(nc.getRole()) && agreements.canNotify()) ||
128  (NotificationRole.NOTIF_PARTICIPANT.equals(nc.getRole()) && !agreements.declinedNotifications());
129  }
130 
131 }
static IMailSender getPool()
static String createURL(IConstructor constructor, AgreementSignature signature, IContact contact, boolean accept)
boolean canSendEmails(NotificationCategory nc)
void sendIfNecessary(IConstructor constructor)
List< AgreementSignature > getSignatures(String action)
void markAsSent(AgreementSignature as)
T addContact(IContact contact)
IMailSender setRoot(String root)