BrightSide Workbench Full Report + Source Code
EditDataControl.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.directory;
20 
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import org.apache.commons.io.input.ReaderInputStream;
28 import org.apache.commons.mail.EmailException;
29 import org.turro.auth.Authentication;
30 import org.turro.contacts.Contact;
31 import org.turro.contacts.comment.AboutVM;
32 import org.turro.contacts.db.ContactsPU;
33 import org.turro.contacts.form.ContactWrapper;
34 import org.turro.elephant.TemplateControl;
35 import org.turro.elephant.context.Application;
36 import org.turro.elephant.context.ElephantContext;
37 import org.turro.file.util.FileAttach;
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.turro.util.IdGenerator;
43 import org.zkoss.image.AImage;
44 import org.zkoss.lang.Strings;
45 import org.zkoss.util.media.Media;
46 import org.zkoss.zk.ui.event.UploadEvent;
47 import org.zkoss.zk.ui.select.annotation.Listen;
48 import org.zkoss.zk.ui.select.annotation.Wire;
49 import org.zkoss.zul.Checkbox;
50 import org.zkoss.zul.Image;
51 import org.zkoss.zul.Textbox;
52 
57 @Deprecated
58 public class EditDataControl extends TemplateControl {
59 
60  public static final String
61  EMAIL_DIRECTORY = "EmailDirectory",
62  PHONE_DIRECTORY = "PhoneDirectory",
63  BUSINESS_DIRECTORY = "BusinessDirectory",
64  POSITION_DIRECTORY = "PositionDirectory",
65  WEB_DIRECTORY = "WebDirectory",
66  NETWORK_DIRECTORY = "NetworkDirectory";
67 
68  private IContact contact;
69  private String path;
70 
71  @Wire("#email") private Textbox email;
72  @Wire("#phone") private Textbox phone;
73  @Wire("#business") private Textbox business;
74  @Wire("#position") private Textbox position;
75  @Wire("#web") private Textbox web;
76  @Wire("#network") private Textbox network;
77  @Wire("#directory") private Checkbox directory;
78  @Wire("#face") private Image face;
79 
80  @Listen("onChange=#email")
81  public void onEmail() {
82  contact.setConnector(EMAIL_DIRECTORY, email.getValue());
83  }
84 
85  @Listen("onChange=#phone")
86  public void onPhone() {
87  contact.setConnector(PHONE_DIRECTORY, phone.getValue());
88  }
89 
90  @Listen("onChange=#business")
91  public void onBusiness() {
92  contact.setConnector(BUSINESS_DIRECTORY, business.getValue());
93  }
94 
95  @Listen("onChange=#position")
96  public void onPosition() {
97  contact.setConnector(POSITION_DIRECTORY, position.getValue());
98  }
99 
100  @Listen("onChange=#web")
101  public void onWeb() {
102  contact.setConnector(WEB_DIRECTORY, web.getValue());
103  }
104 
105  @Listen("onChange=#network")
106  public void onNetwork() {
107  contact.setConnector(NETWORK_DIRECTORY, network.getValue());
108  }
109 
110  @Listen("onCheck = #directory")
111  public void onDirectory() throws IOException {
112  if(!Authentication.isBehaving()) {
113  // Might cause problems when BEHAVE_AS
114  Authentication.reauthenticate();
115  }
116  Application.getApplication().sendRedirect(path);
117  }
118 
119  @Listen("onUpload = #uploadface")
120  public void onEvent(UploadEvent t) throws Exception {
121  FileAttach fileAttach = new FileAttach(ContactsPU.getObjectPath(contact.getContact()));
122  File newFile = new File(ElephantContext.getRealPath(fileAttach.getPublishable() + "/profile/face.png"));
123  if(!newFile.getParentFile().exists()) {
124  newFile.getParentFile().mkdirs();
125  }
126  try (FileOutputStream fos = new FileOutputStream(newFile)) {
127  Media media = t.getMedia();
128  if(media != null) {
129  if(media.inMemory()) {
130  fos.write(media.isBinary() ? media.getByteData() : media.getStringData().getBytes());
131  } else {
132  byte[] buffer = new byte[102400];
133  try (InputStream is = media.isBinary() ? media.getStreamData() : new ReaderInputStream(media.getReaderData())) {
134  int r;
135  while((r = is.read(buffer)) != -1) {
136  fos.write(buffer, 0, r);
137  }
138  }
139  }
140  }
141  }
142  String faceFile = fileAttach.getPublicFile("/profile/face.png", false);
143  if(!Strings.isBlank(faceFile)) {
144  face.setContent(new AImage(ElephantContext.getRealPath(faceFile)));
145  } else {
146  face.setSrc(null);
147  }
148  }
149 
150  @Listen("onClick=#save")
151  public void onSave() {
152  if(contact.isWebUser()) {
153  new ContactWrapper((Contact) contact.getContact()).save();
154  Application.getApplication().sendRedirect(path); // + MarkerHelper.setObfuscatedPars("item=" + contact.getId()));
155  sendNotification((Contact) contact.getContact());
156  }
157  }
158 
159  public String getPath() {
160  return path;
161  }
162 
163  public void setPath(String path) {
164  this.path = path;
165  }
166 
167  @Override
168  protected void doFinally() {
169  super.doFinally();
170  contact = Authentication.getIContact();
171  initComponents();
172  }
173 
174  private void initComponents() {
175  if(contact != null && contact.isWebUser()) {
176  email.setValue(contact.getConnector(EMAIL_DIRECTORY));
177  phone.setValue(contact.getConnector(PHONE_DIRECTORY));
178  business.setValue(contact.getConnector(BUSINESS_DIRECTORY));
179  position.setValue(contact.getConnector(POSITION_DIRECTORY));
180  web.setValue(contact.getConnector(WEB_DIRECTORY));
181  network.setValue(contact.getConnector(NETWORK_DIRECTORY));
182  FileAttach fileAttach = new FileAttach(ContactsPU.getObjectPath(contact.getContact()));
183  String faceFile = fileAttach.getPublicFile("/profile/face.png", false);
184  if(!Strings.isBlank(faceFile)) {
185  face.setSrc(faceFile + "?" + IdGenerator.generate());
186  }
187  } else {
188  Application.getApplication().sendRedirect("/user");
189  }
190  }
191 
192  private void sendNotification(Contact c) {
193  try {
194  FileAttach fileAttach = new FileAttach(ContactsPU.getObjectPath(c));
195  String faceFile = fileAttach.getPublicFile("/profile/face.png", false);
196  new MailPool()
197  .addAdministrators()
198  .addBySyndication(SecurityGroup.CONTACTS_ADMIN)
199  .setRoot("/directory")
200  .put("about", c)
201  .put("face", faceFile)
202  .sendTemplate("change-notification",
203  I_.get("Modified") + ": " + I_.get("Directory"));
204  } catch (EmailException ex) {
205  Logger.getLogger(AboutVM.class.getName()).log(Level.SEVERE, null, ex);
206  }
207  }
208 
209 }