BrightSide Workbench Full Report + Source Code
EditProfileControl.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.io.File;
22 import java.io.IOException;
23 import java.util.Date;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.apache.commons.mail.EmailException;
27 import org.turro.action.Actions;
28 import org.turro.action.Contacts;
29 import org.turro.action.MailSenders;
30 import org.turro.action.PushSenders;
31 import org.turro.auth.Authentication;
32 import org.turro.collections.KeyValueMap;
33 import org.turro.contacts.Contact;
34 import org.turro.contacts.JobBoard;
35 import org.turro.contacts.db.ContactsPU;
36 import org.turro.contacts.form.ContactWrapper;
37 import org.turro.elephant.TemplateControl;
38 import org.turro.elephant.context.Application;
39 import org.turro.elephant.context.ElephantContext;
40 import org.turro.file.util.FileAttach;
41 import org.turro.i18n.I_;
42 import org.turro.plugin.contacts.IContact;
43 import org.turro.security.SecurityGroup;
44 import org.turro.upload.Medias;
45 import org.turro.util.IdGenerator;
46 import org.zkoss.image.AImage;
47 import org.zkoss.lang.Strings;
48 import org.zkoss.util.media.Media;
49 import org.zkoss.zk.ui.event.UploadEvent;
50 import org.zkoss.zk.ui.select.annotation.Listen;
51 import org.zkoss.zk.ui.select.annotation.Wire;
52 import org.zkoss.zul.Checkbox;
53 import org.zkoss.zul.Div;
54 import org.zkoss.zul.Image;
55 import org.zkoss.zul.Listbox;
56 import org.zkoss.zul.Listitem;
57 import org.zkoss.zul.Textbox;
58 
63 public class EditProfileControl extends TemplateControl {
64 
65  private IContact contact;
66  private String path;
67 
68  @Wire("#name") private Textbox name;
69  @Wire("#phone") private Textbox phone;
70  @Wire("#web") private Textbox web;
71  @Wire("#network") private Textbox network;
72  @Wire("#face") private Image face;
73 
74  @Wire("#studentForm") private Div studentForm;
75  @Wire("#available") private Checkbox available;
76  @Wire("#online") private Checkbox online;
77  @Wire("#zipCode") private Textbox zipCode;
78  @Wire("#commute") private Listbox commute;
79 
80  @Listen("onChange=#name")
81  public void onName() {
82  contact.setName(name.getValue());
83  }
84 
85  @Listen("onChange=#phone")
86  public void onPhone() {
87  contact.setConnector(Profile.PHONE, phone.getValue());
88  }
89 
90  @Listen("onChange=#web")
91  public void onWeb() {
92  contact.setConnector(Profile.WEB, web.getValue());
93  }
94 
95  @Listen("onChange=#network")
96  public void onNetwork() {
97  contact.setConnector(Profile.NETWORK, network.getValue());
98  }
99 
100  @Listen("onUpload = #uploadface")
101  public void onEvent(UploadEvent t) throws Exception {
102  FileAttach fileAttach = new FileAttach(ContactsPU.getObjectPath(contact.getContact()));
103  File newFile = new File(ElephantContext.getRealPath(fileAttach.getPublishable() + "/profile/face.png"));
104  if(!newFile.getParentFile().exists()) {
105  newFile.getParentFile().mkdirs();
106  }
107  Media media = t.getMedia();
108  if(media != null) {
109  Medias.toFile(media, newFile, Medias.scaling(600));
110  }
111  if(newFile.exists()) {
112  try {
113  face.setContent(new AImage(newFile.getAbsolutePath()));
114  } catch (IOException ex) {
115  Logger.getLogger(EditProfileControl.class.getName()).log(Level.SEVERE, null, ex);
116  }
117  } else {
118  face.setSrc(null);
119  }
120  }
121 
122  @Listen("onClick=#save")
123  public void onSave() {
124  if(contact.isWebUser()) {
125  Contact c = (Contact) contact.getContact();
126  if(available.isChecked()) {
127  JobBoard board = c.getJobBoard();
128  if(board == null) {
129  board = new JobBoard();
130  board.setContact(c);
131  c.setJobBoard(board);
132  }
133  board.setAvailable(true);
134  board.setOnline(online.isChecked());
135  board.setZipCode(zipCode.getValue());
136  Listitem li = commute.getSelectedItem();
137  if(li != null) board.setCommuting(Integer.valueOf(li.getValue()));
138  board.setUpdateDate(new Date());
139  } else {
140  c.setJobBoard(null);
141  }
142  new ContactWrapper(c).save();
144  sendNotification((Contact) contact.getContact());
145  }
146  }
147 
148  @Listen("onClick=#cancel")
149  public void onCancel() {
151  }
152 
153  public String getPath() {
154  return path;
155  }
156 
157  public void setPath(String path) {
158  this.path = path;
159  }
160 
161  @Override
162  protected void doFinally() {
163  super.doFinally();
165  if(kvm != null && kvm.containsKey("contact")) {
166  contact = Contacts.getContactById(kvm.get("contact"));
167  } else {
168  contact = Authentication.getIContact();
169  }
170  initComponents();
171  }
172 
173  private void initComponents() {
174  if(contact != null && contact.isWebUser()) {
175  name.setValue(contact.getName());
176  name.setReadonly(ProfileContext.getAttribute("allow-name", true));
177  phone.setValue(contact.getConnector(Profile.PHONE));
178  web.setValue(contact.getConnector(Profile.WEB));
179  network.setValue(contact.getConnector(Profile.NETWORK));
180  FileAttach fileAttach = new FileAttach(ContactsPU.getObjectPath(contact.getContact()));
181  String faceFile = fileAttach.getPublicFile("/profile/face.png", false);
182  if(!Strings.isBlank(faceFile)) {
183  face.setSrc(faceFile + "?" + IdGenerator.generate());
184  }
185  if(contact.isStudent()) {
186  studentForm.setVisible(true);
187  JobBoard board = ((Contact) contact.getContact()).getJobBoard();
188  if(board != null) {
189  available.setChecked(true);
190  online.setChecked(board.isOnline());
191  zipCode.setValue(board.getZipCode());
192  for(Listitem li : commute.getItems()) {
193  if(li.getValue().equals("" + board.getCommuting())) {
194  commute.setSelectedItem(li);
195  }
196  }
197  }
198  } else {
199  studentForm.setVisible(false);
200  }
201  } else {
202  Application.getApplication().sendRedirect(path);
203  }
204  }
205 
206  private void sendNotification(Contact c) {
207  try {
208  MailSenders.getPool()
209  .addAdministrators()
210  .addBySyndication(SecurityGroup.CONTACTS_ADMIN)
211  .setRoot("/profile")
212  .put("profile", new Profile(c))
213  .sendTemplate("change-notification",
214  I_.get("Modified") + ": " + I_.get("Profile"));
215  } catch (EmailException ex) {
216  Logger.getLogger(EditProfileControl.class.getName()).log(Level.SEVERE, null, ex);
217  }
218  PushSenders.getPool()
219  .addAdministrators()
220  .addBySyndication(SecurityGroup.CONTACTS_ADMIN)
221  .send(I_.get("Modified") + ": " + I_.get("Profile"), c.getName());
222  }
223 
224 }
static KeyValueMap getRightNowAction(IConstructor constructor)
Definition: Actions.java:341
static IContact getContactById(String id)
Definition: Contacts.java:72
void setJobBoard(JobBoard jobBoard)
Definition: Contact.java:351
void setUpdateDate(Date updateDate)
Definition: JobBoard.java:96
void setAvailable(boolean available)
Definition: JobBoard.java:80
void setContact(Contact contact)
Definition: JobBoard.java:104
void setOnline(boolean online)
Definition: JobBoard.java:88
void setZipCode(String zipCode)
Definition: JobBoard.java:64
void setCommuting(int commuting)
Definition: JobBoard.java:72
static String getObjectPath(Object object)
Definition: ContactsPU.java:68
static String getAttribute(String attribute)
abstract void sendRedirect(String uri)
String getPublicFile(String file)
Definition: FileAttach.java:59
static void toFile(Media media, File file)
Definition: Medias.java:66
static Consumer< File > scaling(double scale)
Definition: Medias.java:102
void setConnector(String id, String value)