BrightSide Workbench Full Report + Source Code
ConvocationCtrl.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.www.convocation;
20 
21 import java.util.HashMap;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.turro.collections.parser.ParserException;
25 import org.turro.action.Actions;
26 import org.turro.action.Contacts;
27 import org.turro.action.ContentService;
28 import org.turro.annotation.ElephantPlugin;
29 import org.turro.auth.Authentication;
30 import org.turro.collections.KeyValueMap;
31 import org.turro.contacts.Contact;
32 import org.turro.contacts.Convocation;
33 import org.turro.contacts.db.ContactsPU;
34 import org.turro.elephant.context.Application;
35 import org.turro.elephant.context.ElephantContext;
36 import org.turro.elephant.context.IConstructor;
37 import org.turro.elephant.direct.AbstractDirectEntityCtrl;
38 import org.turro.elephant.direct.DirectContent;
39 import org.turro.elephant.direct.DirectContents;
40 import org.turro.elephant.security.IUser;
41 import org.turro.jpa.Dao;
42 import org.turro.marker.ElephantMarker;
43 import org.turro.marker.MarkerHelper;
44 import org.turro.plugin.contacts.IContact;
45 import org.turro.zul.convocation.ConvocationSender;
46 
51 @ElephantPlugin(label="convocation-ctrl")
52 @DirectContent(identifier="convocation-action")
54 
55  public ConvocationCtrl() {
56  super("convocation", "convocation");
57  }
58 
59  public boolean isRestricted() {
60  return true;
61  }
62 
63  @Override
64  protected String getIdentifier() {
66  }
67 
68  @Override
69  protected void prepareCleanMarker(ElephantMarker em, KeyValueMap kvm) {
70  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
71  }
72 
73  @Override
74  protected void prepareMarker(ElephantMarker marker) {
75  // Compatibility with ConvocationContentIterator isRestricted()
76  marker.put("entiter", this);
77  }
78 
79  @Override
80  protected void doExecute(IConstructor constructor, KeyValueMap map) {
81  String type = map.get("type");
82  if("attended".equals(type)) {
83  if(Application.getApplication().isInRole("convocation:validate")) {
84  Convocation convocation = getDao().find(Convocation.class, map.get("id"));
85  IContact contact = Contacts.getContactByEmail(map.get("contact"));
86  if(convocation != null && contact != null && contact.isValid()) {
87  convocation.attended(contact);
88  } else {
89  Logger.getLogger(ConvocationCtrl.class.getName()).log(Level.SEVERE,
90  ElephantContext.logMsg("Validating Convocation: {0} - Contact: {1}"),
91  new Object[] {
92  (convocation == null ? "null:" + map.get("id") : convocation.getId() + " " + convocation.getName()),
93  (contact == null || !contact.isValid() ? "null:" + map.get("contact") : contact.getId() + " " + contact.getName())
94  });
95  }
96  } else {
98  Logger.getLogger(ConvocationCtrl.class.getName()).log(Level.SEVERE,
99  ElephantContext.logMsg("No permissions: {1}"),
100  new Object[] {
101  (contact == null || !contact.isValid() ? "null:" + map.get("contact") : contact.getId() + " " + contact.getName())
102  });
103  }
104  } if("willattend".equals(type)) {
105  IContact contact = Authentication.getIContact();
106  Convocation convocation = getDao().find(Convocation.class, map.get("id"));
107  convocation.addContact((Contact) contact.getContact());
108  convocation = getDao().saveObject(convocation);
109  new ConvocationSender(convocation, contact).send();
110  }
111  }
112 
113  /* URLs */
114 
115  public static String createRegistrationURL(IConstructor constructor, IContact contact, Convocation convocation) {
116  ConvocationCtrl cc = new ConvocationCtrl();
117  cc.setConstructor(constructor);
118  cc.setEntityPath(ContactsPU.getObjectPath(convocation));
119  return cc.createRegistration(constructor, contact, convocation);
120  }
121 
122  public static String createAttendantQrCodeURL(IConstructor constructor, IContact contact, Convocation convocation) {
123  ConvocationCtrl cc = new ConvocationCtrl();
124  cc.setConstructor(constructor);
125  cc.setEntityPath(ContactsPU.getObjectPath(convocation));
126  return cc.createAttendantQrCode(contact, convocation);
127  }
128 
129  public String createRegistration(IConstructor constructor, IContact contact, Convocation convocation) {
130  return createRightNowURL("type=willattend;id=" + convocation.getId());
131  }
132 
133  public String createAttendantQrCode(IContact contact, Convocation convocation) {
134  try {
135  KeyValueMap kvm = new KeyValueMap("");
136  kvm.put("type", "attended");
137  kvm.put("id", convocation.getId());
138  kvm.put("contact", contact.getConnector(IUser.CONNECTOR_EMAIL));
139  kvm.put(Actions.REDIR_PAR, ElephantContext.getServerBase("http") + getUserConvocation(convocation, contact));
140  String exacton = Actions.createAction(kvm, (int) convocation.getDaysLeft() + 1, false);
141  String url = ElephantContext.getServerBase("http") +
144  "?" + exacton;
145  HashMap map = new HashMap();
146  map.put("type", "qrcode");
147  map.put("data", url);
148  return ContentService.createURL(null, new KeyValueMap(map));
149  } catch (ParserException ex) {
150  Logger.getLogger(ConvocationCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
151  }
152  return "";
153  }
154 
155  private String getUserConvocation(Convocation convocation, IContact contact) {
156  return ElephantContext.getRootWebPath() + "/user/convocation?" +
157  MarkerHelper.setObfuscatedPars("item=" + convocation.getId() + ";contact=" + contact.getId());
158  }
159 
160  /* Dao */
161 
162  private Dao _dao;
163 
164  private Dao getDao() {
165  if(_dao == null) {
166  _dao = new ContactsPU();
167  }
168  return _dao;
169  }
170 
171 }
static String createAction(String email, String redir)
Definition: Actions.java:90
static IContact getContactByEmail(String email)
Definition: Contacts.java:83
static String createURL(IConstructor constructor, KeyValueMap values)
void addContact(Contact contact)
static String getObjectPath(Object object)
Definition: ContactsPU.java:68
static String getServerBase(String scheme)
static String getIdentifier(Class<? extends IDirectContent > control)
Object put(Object key, Object value)
static String setObfuscatedPars(String parameters)
static String createAttendantQrCodeURL(IConstructor constructor, IContact contact, Convocation convocation)
void prepareCleanMarker(ElephantMarker em, KeyValueMap kvm)
void prepareMarker(ElephantMarker marker)
String createAttendantQrCode(IContact contact, Convocation convocation)
static String createRegistrationURL(IConstructor constructor, IContact contact, Convocation convocation)
void doExecute(IConstructor constructor, KeyValueMap map)
String createRegistration(IConstructor constructor, IContact contact, Convocation convocation)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27