19 package org.turro.www.directory;
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;
60 public static final String
62 PHONE_DIRECTORY =
"PhoneDirectory",
63 BUSINESS_DIRECTORY =
"BusinessDirectory",
64 POSITION_DIRECTORY =
"PositionDirectory",
65 WEB_DIRECTORY =
"WebDirectory",
66 NETWORK_DIRECTORY =
"NetworkDirectory";
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;
80 @Listen("onChange=#email")
81 public void onEmail() {
82 contact.setConnector(EMAIL_DIRECTORY, email.getValue());
85 @Listen("onChange=#phone")
86 public void onPhone() {
87 contact.setConnector(PHONE_DIRECTORY, phone.getValue());
90 @Listen("onChange=#business")
91 public void onBusiness() {
92 contact.setConnector(BUSINESS_DIRECTORY, business.getValue());
95 @Listen("onChange=#position")
96 public void onPosition() {
97 contact.setConnector(POSITION_DIRECTORY, position.getValue());
100 @Listen("onChange=#web")
101 public void onWeb() {
102 contact.setConnector(WEB_DIRECTORY, web.getValue());
105 @Listen("onChange=#network")
106 public void onNetwork() {
107 contact.setConnector(NETWORK_DIRECTORY, network.getValue());
110 @Listen("onCheck = #directory")
111 public void onDirectory() throws IOException {
112 if(!Authentication.isBehaving()) {
113 // Might cause problems when BEHAVE_AS
114 Authentication.reauthenticate();
116 Application.getApplication().sendRedirect(path);
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();
126 try (FileOutputStream fos = new FileOutputStream(newFile)) {
127 Media media = t.getMedia();
129 if(media.inMemory()) {
130 fos.write(media.isBinary() ? media.getByteData() : media.getStringData().getBytes());
132 byte[] buffer = new byte[102400];
133 try (InputStream is = media.isBinary() ? media.getStreamData() : new ReaderInputStream(media.getReaderData())) {
135 while((r = is.read(buffer)) != -1) {
136 fos.write(buffer, 0, r);
142 String faceFile = fileAttach.getPublicFile("/profile/face.png", false);
143 if(!Strings.isBlank(faceFile)) {
144 face.setContent(new AImage(ElephantContext.getRealPath(faceFile)));
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());
159 public String getPath() {
163 public void setPath(String path) {
168 protected void doFinally() {
170 contact = Authentication.getIContact();
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());
188 Application.getApplication().sendRedirect("/user");
192 private void sendNotification(Contact c) {
194 FileAttach fileAttach = new FileAttach(ContactsPU.getObjectPath(c));
195 String faceFile = fileAttach.getPublicFile("/profile/face.png", false);
198 .addBySyndication(SecurityGroup.CONTACTS_ADMIN)
199 .setRoot("/directory")
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);
static final String EMAIL_DIRECTORY