BrightSide Workbench Full Report + Source Code
ContactComposer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 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.form;
20 
21 import java.awt.image.RenderedImage;
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import java.util.stream.Collectors;
30 import org.turro.action.Actions;
31 import org.turro.action.Contacts;
32 import org.turro.action.Plugins;
33 import org.turro.activity.ParticipationCatchers;
34 import org.turro.command.Command;
35 import org.turro.command.Context;
36 import org.turro.contacts.Contact;
37 import org.turro.contacts.db.ContactsPU;
38 import org.turro.contacts.profile.EditProfileControl;
39 import org.turro.contacts.social.SocialGroupValue;
40 import org.turro.contacts.social.SocialGroupedListbox;
41 import org.turro.contacts.zul.fields.FieldValueGrid;
42 import org.turro.contacts.zul.menu.ContactMenu;
43 import org.turro.elephant.context.Application;
44 import org.turro.elephant.context.ElephantContext;
45 import org.turro.elephant.db.WhereClause;
46 import org.turro.elephant.zkoss.ModalWindow;
47 import org.turro.file.util.FileAttach;
48 import org.turro.i18n.I_;
49 import org.turro.jpa.Dao;
50 import org.turro.jpa.composer.EntityComposer;
51 import org.turro.jpa.entity.DaoEntity;
52 import org.turro.mail.sender.MailPool;
53 import org.turro.plugin.IPlugin;
54 import org.turro.plugin.contacts.IContact;
55 import org.turro.security.SocialGroups;
56 import org.turro.tags.Tags;
57 import org.turro.upload.Medias;
58 import org.turro.util.IdGenerator;
59 import org.turro.zkoss.locale.Currencies;
60 import org.turro.zkoss.locale.CurrencyListbox;
61 import org.turro.zkoss.locale.LocaleListbox;
62 import org.turro.zul.frame.Framework;
63 import org.turro.zul.frame.ScrollTabpanel;
64 import org.zkoss.image.AImage;
65 import org.zkoss.lang.Strings;
66 import org.zkoss.util.media.Media;
67 import org.zkoss.zk.ui.Component;
68 import org.zkoss.zk.ui.event.SelectEvent;
69 import org.zkoss.zk.ui.event.UploadEvent;
70 import org.zkoss.zk.ui.select.annotation.Listen;
71 import org.zkoss.zk.ui.select.annotation.Wire;
72 import org.zkoss.zk.ui.util.Clients;
73 import org.zkoss.zul.Image;
74 import org.zkoss.zul.Listitem;
75 import org.zkoss.zul.Tab;
76 import org.zkoss.zul.Tabpanels;
77 import org.zkoss.zul.Tabs;
78 
83 public class ContactComposer extends EntityComposer<Contact, String> {
84 
85  private FileAttach fileAttach;
86  private boolean isNew = false;
87 
88  @Wire("#detail #localebox") private LocaleListbox localebox;
89  @Wire("#detail #currencybox") private CurrencyListbox currencybox;
90  @Wire("#detail #socialgroups") private SocialGroupedListbox socialgroups;
91  @Wire("#detail #face") private Image face;
92  @Wire("#detail #qrcode") private Image qrcode;
93  @Wire("#contabs") private Tabs contabs;
94  @Wire("#conpanels") private Tabpanels conpanels;
95 
96  @Listen("onSelect = #detail #localebox")
97  public void onLocale() {
98  entity.setLocale(localebox.getObjectValue());
99  currencybox.setObjectValue(Currencies.getDefault(entity.getLocale()));
100  entity.setCurrency(currencybox.getObjectValue());
101  }
102 
103  @Listen("onSelect = #detail #socialgroups")
104  public void onSocialGroup(SelectEvent event) {
105  if(event.getReference() instanceof Listitem) {
106  Collection<SocialGroupValue> sgs = socialgroups.getSelected();
108  List.of(entity),
109  new HashSet<>(sgs.stream().map(sgv -> sgv.getKey()).collect(Collectors.toList())),
110  null);
111  }
112  }
113 
114  @Listen("onUpload = #detail #uploadface")
115  public void onEvent(UploadEvent t) throws Exception {
116  File newFile = new File(ElephantContext.getRealPath(fileAttach.getPublishable() + "/profile/face.png"));
117  if(!newFile.getParentFile().exists()) {
118  newFile.getParentFile().mkdirs();
119  }
120  Media media = t.getMedia();
121  if(media != null) {
122  Medias.toFile(media, newFile, Medias.scaling(600));
123  }
124  if(newFile.exists()) {
125  try {
126  face.setContent(new AImage(newFile.getAbsolutePath()));
127  } catch (IOException ex) {
128  Logger.getLogger(EditProfileControl.class.getName()).log(Level.SEVERE, null, ex);
129  }
130  } else {
131  face.setSrc(null);
132  }
133  }
134 
135  @Listen("onClick = #subscriptions")
136  public void onSubscriptions() {
138  }
139 
140  @Listen("onClick = #organigram")
141  public void onOrganigram() {
143  }
144 
145  @Listen("onClick = #welcome")
146  public void onWelcome() {
148  .setRoot("/sign")
149  .put("contact", entity)
150  .put("link", ElephantContext.getServerUrl("http") + "?" +
151  Actions.createAction(entity.getConnectorMap().get(IContact.CONNECTOR_EMAIL).getValue(), "/user/changepass"))
152  .silentSendTemplate("welcome", I_.get("Welcome") + " : " + ElephantContext.getSiteName());
153  Clients.showNotification(I_.get("Welcome was sent..."));
154  }
155 
156  @Override
157  protected String getAttributeName() {
158  return "contact";
159  }
160 
161  @Override
162  protected Contact getEntityInstance(String id) {
163  if(id == null) {
164  entity = new Contact();
165  entity.setUserMenu(true);
166  } else {
167  entity = new ContactsPU().find(Contact.class, id);
168  }
169  return entity;
170  }
171 
172  @Override
173  protected DaoEntity getWrapperInstance(Component comp) {
177  return w;
178  }
179 
180  @Override
181  protected void doOnSave() {
182  entity.checkEmptyness();
183  ContactWrapper.saveAnyway(entity, contact -> {
184  ContactComposer.super.doOnSave();
185  SocialGroups.checkInheritance(List.of(contact), wrapper.getDao());
186  });
187  }
188 
189  @Override
190  protected void doOnDelete() {
191  ContactMenu.showEntityModal(entity.getId(), "contactId", "/contact/delContact.zul", new Command() {
192  @Override
193  public Object execute(Context context) {
194  ModalWindow mw = (ModalWindow) context.get("win");
195  if(mw.getResult() == 1) {
196  ContactComposer.super.doOnDelete();
197  }
198  return true;
199  }
200  });
201  }
202 
203  @Override
204  protected void afterSave() {
205  super.afterSave();
207  if(isNew && entity != null) {
208  ContactMenu.showContact(entity.getId());
209  }
210  }
211 
212  @Override
213  public void doFinally() throws Exception {
214  super.doFinally();
216  isNew = Strings.isBlank(entity.getId());
217  localebox.sort();
218  fileAttach = new FileAttach(ContactsPU.getObjectPath(entity));
219  String faceFile = fileAttach.getPublicFile("/profile/face.png", false);
220  if(!Strings.isBlank(faceFile)) {
221  face.setSrc(faceFile + "?" + IdGenerator.generate());
222  }
223  if(!isNew) {
224  IPlugin vcard = Plugins.loadImplementation(IPlugin.class, "vcard");
225  if(vcard != null) {
226  vcard.setData("contact", entity);
227  qrcode.setContent((RenderedImage) vcard.getData("qrcode"));
228  }
229  for(String tabKey : getTabKeys()) {
230  Tab tab = new Tab(I_.get(tabKey));
231  contabs.appendChild(tab);
232 
233  ScrollTabpanel panel = new ScrollTabpanel();
234  conpanels.appendChild(panel);
235 
236  FieldValueGrid grid = new FieldValueGrid(tabKey);
237  panel.appendScrollableChild(grid);
238  grid.setContact(entity);
239  }
240  }
241  }
242 
243  @Override
244  protected boolean shouldBeSaved() {
245  return !entity.isEmpty() && (Strings.isBlank(entity.getId())) || super.shouldBeSaved();
246  }
247 
248  @Override
249  protected boolean inSaveRole() {
250  return Application.getApplication().isInRole("contact:edit");
251  }
252 
253  @Override
254  protected boolean inDeleteRole() {
255  return Application.getApplication().isInRole("contact:delete");
256  }
257 
258  private List<String> getTabKeys() {
259  List names = getTagNames();
260  if(names.isEmpty()) return names;
261  Dao dao = new ContactsPU();
262  WhereClause wc = new WhereClause();
263  wc.addClause("select distinct fieldDef.tabKey");
264  wc.addClause("from FieldDef as fieldDef");
265  wc.addClause("where fieldDef.tagDiscriminator in (:tags)");
266  wc.addClause("order by fieldDef.tabKey");
267  wc.addNamedValue("tags", getTagNames());
268  return (List<String>) dao.getResultList(wc);
269  }
270 
271  private List<String> getTagNames() {
272  return Tags.getTags(entity).stream().map(t -> t.getTagName()).collect(Collectors.toList());
273  }
274 
275 }
static String createAction(String email, String redir)
Definition: Actions.java:90
static IContact getContact(Object object)
Definition: Contacts.java:109
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
static String getObjectPath(Object object)
Definition: ContactsPU.java:68
DaoEntity getWrapperInstance(Component comp)
static void saveAnyway(Contact contact, Consumer< Contact > onSave)
static void showOrganigram(Contact contact)
static void showSubscriptions(Contact contact)
static void showContact(Contact contact)
static String getServerUrl(String scheme)
void addNamedValue(String name, Object value)
String getPublicFile(String file)
Definition: FileAttach.java:59
static String get(String msg)
Definition: I_.java:41
IMailSender addContact(IContact contact)
static void showEntityModal(Object id, String attribute, String include, Command command)
static void absoluteSyndicate(List< Contact > contacts, Set< String > socialGroupIds, Dao dao)
static void checkInheritance(List< Contact > contacts, Dao dao)
static TagSet getTags(Object entity)
Definition: Tags.java:351
static void toFile(Media media, File file)
Definition: Medias.java:66
static Consumer< File > scaling(double scale)
Definition: Medias.java:102
static Framework getCurrent()
Definition: Framework.java:203
void setSelectedLabel(String text)
Definition: Framework.java:103
void setSelectedTooltiptext(String text)
Definition: Framework.java:107
void appendScrollableChild(Component child)
IMailSender put(Object key, Object value)
void silentSendTemplate(String template, String subject)
IMailSender setRoot(String root)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27
Object getData(String id)
void setData(String id, Object data)