BrightSide Workbench Full Report + Source Code
AboutVM.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.contacts.comment;
20 
21 import java.io.File;
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;
63 
68 @Deprecated
69 public class AboutVM {
70 
72  private List<String> langs;
73  private List<AboutWrapper> model;
74 
75  @Init
76  public void init(@ExecutionArgParam("contact") Contact contact) {
77  if(contact != null) this.contact = contact;
78  setContact(this.contact);
79  }
80 
81  @NotifyChange({"aboutModel", "image", "web", "trade"})
82  @Command
83  public void setContact(@BindingParam("contact") Object contact) {
84  if(contact instanceof Contact) {
85  this.contact = (Contact) contact;
86  }
87  model = null;
88  }
89 
90  @NotifyChange({"web"})
91  @Command
92  public void setWeb(@BindingParam("web") String web) {
93  if(!Strings.isBlank(web)) {
94  Contacts.getContact(contact).setConnector("Web", UrlCompose.fillWebGaps(web));
95  } else {
96  Contacts.getContact(contact).setConnector("Web", null);
97  }
98  }
99 
100  @NotifyChange({"trade"})
101  @Command
102  public void setTrade(@BindingParam("trade") String trade) {
103  if(!Strings.isBlank(trade)) {
105  } else {
107  }
108  }
109 
110  @Command
111  public void setImage(@ContextParam(ContextType.BIND_CONTEXT) BindContext ctx) throws IOException {
112  UploadEvent event = (UploadEvent) ctx.getTriggerEvent();
113  FileAttach fileAttach = new FileAttach(ContactsPU.getObjectPath(contact));
114  File newFile = new File(ElephantContext.getRealPath(fileAttach.getPublishable() + "/profile/face.png"));
115  if(!newFile.getParentFile().exists()) {
116  newFile.getParentFile().mkdirs();
117  }
118  try (FileOutputStream fos = new FileOutputStream(newFile)) {
119  Media media = event.getMedia();
120  if(media != null) {
121  if(media.inMemory()) {
122  fos.write(media.isBinary() ? media.getByteData() : media.getStringData().getBytes());
123  } else {
124  byte[] buffer = new byte[102400];
125  try (InputStream is = media.isBinary() ? media.getStreamData() : new ReaderInputStream(media.getReaderData())) {
126  int r;
127  while((r = is.read(buffer)) != -1) {
128  fos.write(buffer, 0, r);
129  }
130  }
131  }
132  }
133  } catch (FileNotFoundException ex) {
134  Logger.getLogger(AboutVM.class.getName()).log(Level.SEVERE, null, ex);
135  }
136  org.zkoss.lang.Threads.sleep(2000);
137  BindUtils.postNotifyChange(null, null, AboutVM.this, "image");
138  }
139 
140  @NotifyChange({"aboutModel", "image", "web", "trade"})
141  @Command
142  public void save() {
143  if(model != null) {
144  if(contact != null) {
145  for(AboutWrapper wrapper : model) {
146  if(wrapper.isNewComment() && !wrapper.isEmpty()) {
147  Comment comment = wrapper.getComment();
148  comment.setContact(contact);
149  comment.setModification(new Date());
150  contact.getComments().add(comment);
151  }
152  }
153  contact.checkEmptyness();
154  contact = new ContactsPU().saveObject(contact);
155  resetAboutModel();
156  sendNotification(contact);
157  Clients.showNotification(I_.get("Saved"));
158  }
159  }
160  }
161 
162  @Command("update")
163  public void update() {
164  model = null;
165  }
166 
167  public Contact getContact() {
168  return contact;
169  }
170 
171  public String getImage() {
172  FileAttach fileAttach = new FileAttach(ContactsPU.getObjectPath(contact));
173  String faceFile = fileAttach.getPublicFile("/profile/face.png", false);
174  if(!Strings.isBlank(faceFile)) {
175  return faceFile + "?" + IdGenerator.generateHex();
176  } else {
177  return null;
178  }
179  }
180 
181  public String getWeb() {
182  return Contacts.getContact(contact).getConnector("Web");
183  }
184 
185  public String getTrade() {
187  }
188 
189  public boolean isInRole(String role) {
190  return Application.getApplication().isInRole(role);
191  }
192 
193  public List<String> getLangModel() {
194  if(langs == null) {
195  langs = new ArrayList<>();
196  langs.add("#"); // default
197  langs.addAll(Arrays.asList(ElephantContext.getSiteLocales().split(" *, *")));
198  }
199  return langs;
200  }
201 
202  public List<Contact> getContactsModel() {
203  ContactList list = new ContactList();
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()) {
210  setContact(list.get(0));
211  } else {
213  }
214  return list;
215  }
216 
217  private void resetAboutModel() {
218  model = null;
219  getAboutModel();
220  }
221 
222  public List<AboutWrapper> getAboutModel() {
223  if(model == null) {
224  model = new ArrayList<>();
225  getLangModel().forEach((lang) -> {
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);
230  model.add(wrapper);
231  done = true;
232  }
233  }
234  if (!done) {
235  AboutWrapper wrapper = new AboutWrapper(lang, chooseLabel(lang), new Comment());
236  model.add(wrapper);
237  }
238  });
239  }
240  return model;
241  }
242 
243  private String chooseLabel(String lang) {
244  if("#".equals(lang)) {
245  return I_.get("Default");
246  } else {
247  return I_.byKey(lang);
248  }
249  }
250 
251  private void sendNotification(Contact c) {
252  try {
253  FileAttach fileAttach = new FileAttach(ContactsPU.getObjectPath(c));
254  String faceFile = fileAttach.getPublicFile("/profile/face.png", false);
255  new MailPool()
256  .addAdministrators()
257  .addBySyndication(SecurityGroup.CONTACTS_ADMIN)
258  .setRoot("/about")
259  .put("about", c)
260  .put("model", model)
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);
266  }
267  }
268 
269 }
static IContact getContact(Object object)
Definition: Contacts.java:109
static IContact getContactById(String id)
Definition: Contacts.java:72
void setContact(Contact contact)
Definition: Comment.java:93
void setModification(Date modification)
Definition: Comment.java:77
void init(@ExecutionArgParam("contact") Contact contact)
Definition: AboutVM.java:76
List< Contact > getContactsModel()
Definition: AboutVM.java:202
boolean isInRole(String role)
Definition: AboutVM.java:189
void setImage(@ContextParam(ContextType.BIND_CONTEXT) BindContext ctx)
Definition: AboutVM.java:111
void setContact(@BindingParam("contact") Object contact)
Definition: AboutVM.java:83
List< AboutWrapper > getAboutModel()
Definition: AboutVM.java:222
void setWeb(@BindingParam("web") String web)
Definition: AboutVM.java:92
void setTrade(@BindingParam("trade") String trade)
Definition: AboutVM.java:102
static String getObjectPath(Object object)
Definition: ContactsPU.java:68
abstract void sendRedirect(String uri)
String getPublicFile(String file)
Definition: FileAttach.java:59
static String get(String msg)
Definition: I_.java:41
static final String CONNECTOR_TRADE
Definition: IUser.java:28
void setConnector(String id, String value)