19 package org.turro.contacts.comment;
22 import java.io.FileNotFoundException;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Date;
29 import java.util.List;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32 import java.util.stream.Collectors;
33 import org.turro.string.Strings;
34 import org.turro.url.UrlCompose;
35 import org.apache.commons.io.input.ReaderInputStream;
36 import org.apache.commons.mail.EmailException;
37 import org.turro.action.Contacts;
38 import org.turro.auth.Authentication;
39 import org.turro.contacts.Comment;
40 import org.turro.contacts.Contact;
41 import org.turro.contacts.db.ContactsPU;
42 import org.turro.contacts.util.ContactList;
43 import org.turro.elephant.context.Application;
44 import org.turro.elephant.context.ElephantContext;
45 import org.turro.elephant.security.IUser;
46 import org.turro.file.util.FileAttach;
47 import org.turro.i18n.I_;
48 import org.turro.mail.sender.MailPool;
49 import org.turro.security.SecurityGroup;
50 import org.turro.util.IdGenerator;
51 import org.zkoss.bind.BindContext;
52 import org.zkoss.bind.BindUtils;
53 import org.zkoss.bind.annotation.BindingParam;
54 import org.zkoss.bind.annotation.Command;
55 import org.zkoss.bind.annotation.ContextParam;
56 import org.zkoss.bind.annotation.ContextType;
57 import org.zkoss.bind.annotation.ExecutionArgParam;
58 import org.zkoss.bind.annotation.Init;
59 import org.zkoss.bind.annotation.NotifyChange;
60 import org.zkoss.util.media.Media;
61 import org.zkoss.zk.ui.event.UploadEvent;
62 import org.zkoss.zk.ui.util.Clients;
72 private List<String> langs;
73 private List<AboutWrapper> model;
76 public void init(@ExecutionArgParam(
"contact")
Contact contact) {
77 if(contact !=
null) this.contact = contact;
81 @NotifyChange({
"aboutModel",
"image",
"web",
"trade"})
83 public void setContact(@BindingParam(
"contact") Object contact) {
84 if(contact instanceof
Contact) {
85 this.contact = (
Contact) contact;
90 @NotifyChange({
"web"})
92 public void setWeb(@BindingParam(
"web") String web) {
93 if(!Strings.isBlank(web)) {
100 @NotifyChange({
"trade"})
102 public void setTrade(@BindingParam(
"trade") String trade) {
103 if(!Strings.isBlank(trade)) {
111 public void setImage(@ContextParam(ContextType.BIND_CONTEXT) BindContext ctx)
throws IOException {
112 UploadEvent
event = (UploadEvent) ctx.getTriggerEvent();
115 if(!newFile.getParentFile().exists()) {
116 newFile.getParentFile().mkdirs();
118 try (FileOutputStream fos =
new FileOutputStream(newFile)) {
119 Media media =
event.getMedia();
121 if(media.inMemory()) {
122 fos.write(media.isBinary() ? media.getByteData() : media.getStringData().getBytes());
124 byte[] buffer =
new byte[102400];
125 try (InputStream is = media.isBinary() ? media.getStreamData() :
new ReaderInputStream(media.getReaderData())) {
127 while((r = is.read(buffer)) != -1) {
128 fos.write(buffer, 0, r);
133 }
catch (FileNotFoundException ex) {
134 Logger.getLogger(
AboutVM.class.getName()).log(Level.SEVERE,
null, ex);
136 org.zkoss.lang.Threads.sleep(2000);
137 BindUtils.postNotifyChange(
null,
null,
AboutVM.this,
"image");
140 @NotifyChange({
"aboutModel",
"image",
"web",
"trade"})
144 if(contact !=
null) {
146 if(wrapper.isNewComment() && !wrapper.isEmpty()) {
150 contact.getComments().add(comment);
153 contact.checkEmptyness();
154 contact =
new ContactsPU().saveObject(contact);
156 sendNotification(contact);
157 Clients.showNotification(
I_.
get(
"Saved"));
173 String faceFile = fileAttach.
getPublicFile(
"/profile/face.png",
false);
174 if(!Strings.isBlank(faceFile)) {
175 return faceFile +
"?" + IdGenerator.generateHex();
195 langs =
new ArrayList<>();
204 list.
addAll(contact.getProfile()
205 .getCurrentCompanies().stream()
206 .filter(r -> r.getRelation().isResponsible())
207 .map(r -> (
Contact) r.getRelation().getBusiness())
208 .collect(Collectors.toList()));
209 if(!list.isEmpty()) {
217 private void resetAboutModel() {
224 model =
new ArrayList<>();
226 boolean done =
false;
227 for(
Comment comment : contact.getComments()) {
228 if(comment.getComment().startsWith(
"#cv" + lang)) {
229 AboutWrapper wrapper = new AboutWrapper(lang, chooseLabel(lang), comment);
243 private String chooseLabel(String lang) {
244 if(
"#".equals(lang)) {
245 return I_.
get(
"Default");
247 return I_.byKey(lang);
251 private void sendNotification(Contact c) {
253 FileAttach fileAttach =
new FileAttach(ContactsPU.getObjectPath(c));
254 String faceFile = fileAttach.getPublicFile(
"/profile/face.png",
false);
257 .addBySyndication(SecurityGroup.CONTACTS_ADMIN)
261 .put(
"face", faceFile)
262 .sendTemplate(
"change-notification",
263 I_.get(
"Modified") +
": " + I_.get(
"About"));
264 }
catch (EmailException ex) {
265 Logger.getLogger(AboutVM.class.getName()).log(Level.SEVERE,
null, ex);
static IContact getIContact()
abstract void sendRedirect(String uri)
boolean isInRole(String role)
static Application getApplication()
static String getRealPath(String path)
static String getSiteLocales()
String getPublicFile(String file)
static String get(String msg)
static final String CONNECTOR_TRADE