BrightSide Workbench Full Report + Source Code
EditRelationControl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.contacts.profile;
20 
21 import java.util.Date;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.apache.commons.mail.EmailException;
25 import org.turro.action.Actions;
26 import org.turro.action.Contacts;
27 import org.turro.action.PushSenders;
28 import org.turro.auth.Authentication;
29 import org.turro.collections.KeyValueMap;
30 import org.turro.contacts.BusinessRelation;
31 import org.turro.contacts.Contact;
32 import org.turro.contacts.db.ContactsPU;
33 import org.turro.contacts.organigram.RelationType;
34 import org.turro.elephant.TemplateControl;
35 import org.turro.elephant.context.Application;
36 import org.turro.elephant.context.ElephantContext;
37 import org.turro.entities.EntityCombobox;
38 import org.turro.i18n.I_;
39 import org.turro.mail.sender.MailPool;
40 import org.turro.plugin.contacts.IContact;
41 import org.turro.security.SecurityGroup;
42 import org.zkoss.zk.ui.select.annotation.Listen;
43 import org.zkoss.zk.ui.select.annotation.Wire;
44 import org.zkoss.zul.Datebox;
45 
50 public class EditRelationControl extends TemplateControl {
51 
52  private BusinessRelation relation;
53  private String path;
54  private String type;
55 
56  @Wire("#business") private EntityCombobox business;
57  @Wire("#position") private PositionCombobox position;
58  @Wire("#start") private Datebox start;
59  @Wire("#end") private Datebox end;
60 
61  @Listen("onChange=#business")
62  public void onBusiness() {
63  relation.setBusiness((Contact) business.getObjectValue());
64  }
65 
66  @Listen("onChange=#position")
67  public void onPosition() {
68  relation.setDescription(position.getObjectValue());
69  }
70 
71  @Listen("onChange=#start")
72  public void onStart() {
73  relation.setStartDate(start.getValue());
74  }
75 
76  @Listen("onChange=#end")
77  public void onEnd() {
78  relation.setEndDate(end.getValue());
79  }
80 
81  @Listen("onClick=#save")
82  public void onSave() {
83  if(!relation.isEmpty()) {
84  new ContactsPU().saveObject(relation);
86  sendNotification(relation);
87  }
88  }
89 
90  @Listen("onClick=#cancel")
91  public void onCancel() {
93  }
94 
95  public String getPath() {
96  return path;
97  }
98 
99  public void setPath(String path) {
100  this.path = path;
101  }
102 
103  public String getType() {
104  return type;
105  }
106 
107  public void setType(String type) {
108  this.type = type;
109  }
110 
111  @Override
112  protected void doFinally() {
113  super.doFinally();
115  if(kvm != null) {
116  if(kvm.containsKey("relation")) {
117  relation = new ContactsPU().find(BusinessRelation.class, kvm.get("relation"));
118  } else if(kvm.containsKey("element")) {
119  IContact contact;
120  if(kvm != null && kvm.containsKey("contact")) {
121  contact = Contacts.getContactById(kvm.get("contact"));
122  } else {
123  contact = Authentication.getIContact();
124  }
125  relation = new BusinessRelation();
126  relation.setContact((Contact) contact.getContact());
127  relation.setRelationType(RelationType.REL_STAFF.getRelationKey());
128  }
129  }
130  initComponents();
131  }
132 
133  private void initComponents() {
134  if(relation != null) {
135  business.setRoot("contact");
136  business.setParameters("type=" + type);
137  business.setObjectValue(relation.getBusiness());
138  business.setReadonly(relation.isValidated());
139  position.setObjectValue(relation.getDescription());
140  correctDates(relation);
141  start.setValue(relation.getStartDate());
142  end.setValue(relation.getEndDate());
143  } else {
144  Application.getApplication().sendRedirect(path);
145  }
146  }
147 
148  private void sendNotification(BusinessRelation r) {
149  try {
150  new MailPool()
151  .addAdministrators()
152  .addBySyndication(SecurityGroup.CONTACTS_ADMIN)
153  .setRoot("/profile")
154  .put("relation", r)
155  .put("profile", new Profile(r.getContact()))
156  .sendTemplate("relation-notification",
157  I_.get("Modified") + ": " + I_.get("Relation"));
158  } catch (EmailException ex) {
159  Logger.getLogger(EditProfileControl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
160  }
161  PushSenders.getPool()
162  .addAdministrators()
163  .addBySyndication(SecurityGroup.CONTACTS_ADMIN)
164  .send(I_.get("Modified") + ": " + I_.get("Relation"),
165  r.getContact().getName() + " > " + r.getBusiness().getName());
166  }
167 
168  private void correctDates(BusinessRelation relation) {
169  if(relation.getStartDate() == null) {
170  if(relation.getEndDate() != null) {
171  relation.setStartDate(relation.getEndDate());
172  } else {
173  relation.setStartDate(new Date());
174  }
175  }
176  }
177 
178 }
179 
static KeyValueMap getRightNowAction(IConstructor constructor)
Definition: Actions.java:341
static IContact getContactById(String id)
Definition: Contacts.java:72
void setDescription(String description)
void setRelationType(String relationType)
abstract void sendRedirect(String uri)
void setParameters(KeyValueMap kvm)