BrightSide Workbench Full Report + Source Code
ContactWrapper.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.io.File;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.function.Consumer;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import org.turro.action.queue.ConstraintKeys;
30 import org.turro.auth.Authentication;
31 import org.turro.contacts.Address;
32 import org.turro.contacts.Connector;
33 import org.turro.contacts.Contact;
34 import org.turro.contacts.FieldValue;
35 import org.turro.contacts.db.ContactsPU;
36 import org.turro.contacts.duplicates.DuplicatesModel;
37 import org.turro.contacts.util.PairedFile;
38 import org.turro.elephant.context.Application;
39 import org.turro.elephant.context.ElephantContext;
40 import org.turro.elephant.db.WhereClause;
41 import org.turro.i18n.I_;
42 import org.turro.jpa.Dao;
43 import org.turro.jpa.entity.DaoEntity;
44 import org.turro.mail.queue.QueueManager;
45 import org.turro.zkoss.dialog.Windows;
46 
51 public class ContactWrapper extends DaoEntity<Contact, String> {
52 
53  private Map<String, Connector> connector;
54  private Map<String, FieldValue> field;
55  private Map<String, Address> address;
56 
58  super(entity);
59  }
60 
61  public String getTabLabel() {
62  return entity.getName(); //getSelfLabel() + " #" + entity.getId();
63  }
64 
65  public String getSelfLabel() {
66  return I_.get("Contact");
67  }
68 
69  public String getTooltiptext() {
70  try {
71  return entity.getName();
72  } catch(Exception ex) {
73  return null;
74  }
75  }
76 
77  @Override
78  protected Dao createDao() {
79  return new ContactsPU();
80  }
81 
82  @Override
83  public boolean delete() {
84  entity.checkEmptyness();
85  if(super.delete()) {
88  return true;
89  }
90  return false;
91  }
92 
93  @Override
94  public Contact save() {
95  entity.checkEmptyness();
96  try {
97  entity = super.save();
99  } catch (Exception ex) {
100  Logger.getLogger(ContactWrapper.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
101  }
102  return entity;
103  }
104 
105  @Override
106  protected boolean shouldLog() {
107  return true;
108  }
109 
110  public Map<String, Address> getAddress() {
111  if(address == null) {
112  address = new HashMap<>();
113  for(Address a : entity.getAddresses()) {
114  address.put(a.getDescription(), a);
115  }
116  }
117  return address;
118  }
119 
120  public Map<String, Connector> getConnector() {
121  if(connector == null) {
122  connector = new HashMap<>();
123  for(Connector c : entity.getConnectors()) {
124  connector.put(c.getDescription(), c);
125  }
126  }
127  return connector;
128  }
129 
130  public Map<String, FieldValue> getField() {
131  if(field == null) {
132  field = new HashMap<>();
133  for(FieldValue fv : entity.getFieldValues()) {
134  field.put(fv.getFieldDef().getTabKey() + "," + fv.getFieldDef().getLabelKey(), fv);
135  }
136  }
137  return field;
138  }
139 
140  public boolean isSelf() {
141  return Authentication.isLogged(entity.getIContact());
142  }
143 
144  public boolean isInBusiness() {
145  return entity.isInBusiness(Authentication.getIContact());
146  }
147 
148  public String getPublishable() {
149  return "/_internal/files/contact/" + entity.getId();
150  }
151 
152  public String getPrivate() {
153  return "/WEB-INF/files/contact/" + entity.getId();
154  }
155 
156  public String getFace() {
157  return getPublishable() + "/profile/face.png";
158  }
159 
160  public List<String> getFiles(String folder) {
161  File root = new File(ElephantContext.getRealPath(folder));
162  List<String> files = new ArrayList<>();
163  if(root.exists()) {
164  for(File f : root.listFiles()) {
165  if(f.isFile()) {
166  files.add(folder + "/" + f.getName());
167  }
168  }
169  }
170  return files;
171  }
172 
173  public List<PairedFile> getImageFiles(String folder) {
174  File root = new File(ElephantContext.getRealPath(folder));
175  List<PairedFile> files = new ArrayList<>();
176  if(root.exists()) {
177  for(File f : root.listFiles()) {
178  if(f.isFile()) {
179  files.add(new PairedFile(folder, f.getName(), folder + "/thumbs", ".png"));
180  }
181  }
182  }
183  return files;
184  }
185 
186  public static void clearRelations(Dao dao, Contact contact) {
187  WhereClause wc = new WhereClause();
188  wc.addClause("delete from BusinessRelation as relation");
189  wc.addClause("where relation.contact = :contact or relation.business = :contact");
190  wc.addNamedValue("contact", contact);
191  dao.executeUpdate(wc);
192  }
193 
194  public static void saveAnyway(Contact contact, Consumer<Contact> onSave) {
195  DuplicatesModel dupmodel = new DuplicatesModel(contact);
196  if(dupmodel.isEmpty()) {
197  onSave.accept(contact);
198  } else {
199  Windows.title(I_.get("Duplicates"))
200  .width("70%")
201  .height("60%")
202  .addComponent("/contact/duplicates/duplicates.zul",
203  Map.of("contact", contact, "dupmodel", dupmodel, "onSave", onSave))
204  .closeable()
205  .show();
206  }
207  }
208 
209 }
210 
static ConstraintKeys from(IContact contact)
static boolean isLogged(IContact contact)
static void clearRelations(Dao dao, Contact contact)
List< PairedFile > getImageFiles(String folder)
Map< String, Connector > getConnector()
List< String > getFiles(String folder)
Map< String, FieldValue > getField()
static void saveAnyway(Contact contact, Consumer< Contact > onSave)
void addNamedValue(String name, Object value)
static String get(String msg)
Definition: I_.java:41
int executeUpdate(String query)
Definition: Dao.java:463
void subscribeDefaults(ConstraintKeys keys)
static Windows title(String title)
Definition: Windows.java:138
Windows addComponent(HtmlBasedComponent component)
Definition: Windows.java:97
Windows width(String width)
Definition: Windows.java:61
Windows height(String height)
Definition: Windows.java:66