BrightSide Workbench Full Report + Source Code
ContactIterator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.www;
20 
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.List;
26 import org.turro.string.Strings;
27 import org.turro.contacts.Connector;
28 import org.turro.contacts.Contact;
29 import org.turro.contacts.db.ContactsPU;
30 import org.turro.contacts.form.ContactWrapper;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.elephant.context.IConstructor;
33 import org.turro.elephant.db.WhereClause;
34 import org.turro.elephant.impl.repository.Repository;
35 import org.turro.elephant.impl.repository.RepositoryFile;
36 import org.turro.elephant.web.SocialImageMap;
37 import org.turro.elephant.web.SocialNet;
38 import org.turro.file.util.FileAttach;
39 import org.turro.jpa.iterator.DaoHtmlIterator;
40 import org.turro.jpa.search.DaoHtmlSearch;
41 import org.turro.jpa.search.DaoSearchKey;
42 import org.turro.mail.recipients.MailContact;
43 import org.turro.marker.ElephantMarker;
44 import org.turro.tags.TagCloud;
45 
50 public class ContactIterator extends DaoHtmlIterator<Contact, String> {
51 
52  private String pubPath, syndicationId, actualParameters, uniqueId;
53  private HashMap<String, String> connectors;
54  private List<String> groupings;
55 
56  public ContactIterator(IConstructor constructor, String pubPath, String uniqueId) {
57  super(new ContactsPU(), new ElephantMarker(constructor));
58  this.pubPath = pubPath;
59  this.uniqueId = uniqueId;
60  connectors = new HashMap<>();
61  groupings = new ArrayList<>();
62  }
63 
64  public void setSyndicationId(String syndicationId) {
65  this.syndicationId = syndicationId;
66  }
67 
68  public void setConnector(String connector, String values) {
69  connectors.put(connector, values);
70  }
71 
72  public void setGroupings(List groupings) {
73  this.groupings.addAll(groupings);
74  }
75 
76  @Override
77  public void load() {
78  super.load();
79  TagCloud.toggleTags(constructor, "contact");
80  }
81 
82  @Override
84  WhereClause wc = new WhereClause();
85  wc.addClause("select distinct c from Contact as c");
86  wc.addClause("left join c.syndications as s");
87  wc.addClause("left join c.connectors as o");
88  wc.addClause("left outer join c.comments as m with m.comment like '#cv%'");
89  addCriteria(wc);
90  wc.addClause("order by c.name");
91  return wc;
92  }
93 
94  @Override
96  WhereClause wc = new WhereClause();
97  wc.addClause("select count(distinct c) from Contact as c");
98  wc.addClause("left join c.syndications as s");
99  wc.addClause("left join c.connectors as o");
100  wc.addClause("left outer join c.comments as m with m.comment like '#cv%'");
101  addCriteria(wc);
102  return wc;
103  }
104 
105  public String getActualParameters() {
106  return Strings.isBlank(actualParameters) ? "?a=b" : actualParameters;
107  }
108 
109  @Override
110  protected void renderSummary(ElephantMarker marker, Contact e, int page) {
111  if(e != null) {
112  prepareValues(e, page, false);
113  }
114  marker.put("tags", TagCloud.getTags(constructor, "contact"));
115  marker.process("contact", getSummaryTemplate());
116  actualParameters = getPageLink(page);
117  }
118 
119  @Override
120  protected void renderItem(ElephantMarker marker, Contact e, int page) {
121  if(!Strings.isBlank(e.getName())) {
123  "jQuery('title').html('" + e.getName().replace("'", "\\'") + "');"
124  );
125  }
126  prepareValues(e, page, false);
127  marker.put("tags", TagCloud.getTags(constructor, "contact"));
128  marker.process("contact", getFullTemplate());
129  actualParameters = getPageLink(page);
130  }
131 
132  @Override
133  protected String entityRoot() {
134  return "contact";
135  }
136 
137  @Override
138  protected Contact entity(String value) {
139  return dao.find(Contact.class, value);
140  }
141 
142  private void addCriteria(WhereClause wc) {
143  if(!Strings.isBlank(syndicationId)) {
144  wc.addClause("where s.name = :syndication");
145  wc.addNamedValue("syndication", syndicationId);
146  } else if(connectors.isEmpty() && groupings.isEmpty()) {
147  wc.addClause("where 1=2");
148  } else {
149  wc.addClause("where 1=1");
150  }
151  if(!Strings.isBlank(pubPath) && isSearchOption()) {
152  DaoHtmlSearch dhs = DaoHtmlSearch.getInstance(constructor, ElephantContext.getContextVariable(constructor));
153  if(dhs != null) {
154  DaoSearchKey dsk = dhs.get("search-value");
155  if(dsk != null) {
156  dsk.applyToQuery(wc, Arrays.asList(new String[] { "c.name", "o.value", "m.comment" }), false);
157 // String searchValue = dsk.getValue();
158 // if(!Strings.isBlank(searchValue)) {
159 // wc.addClause("and (c.name like :search01 or o.value like :search01)");
160 // wc.addNamedValue("search01", "%" + searchValue + "%");
161 // }
162  }
163  }
164  }
165  if(!connectors.isEmpty()) {
166  for(String key : connectors.keySet()) {
167  wc.addClause("and o.description = :keyconn");
168  wc.addNamedValue("keyconn", key);
169  wc.addClause("and o.value in :valueconn");
170  wc.addNamedValue("valueconn", Arrays.asList(connectors.get(key).split(",")));
171  }
172  }
173  if(!groupings.isEmpty()) {
174  wc.addClause("and c.grouping in (:groupings)");
175  wc.addNamedValue("groupings", groupings);
176  }
177  if(TagCloud.hasSelected(constructor, "contact")) {
178  wc.addIn("and", "c.id", TagCloud.getIdentifiersAsLong(constructor, "contact"));
179  }
180  }
181 
182  public void prepareValues(Contact contact, int page, boolean asMail) {
183 
184  ContactWrapper cw = new ContactWrapper(contact);
185 
186  marker.put("contact", contact);
187 
188  if(asMail && pubPath != null) {
189  marker.put("name", MailContact.createLink(contact.getName(), getItemLink(contact, page), false));
190  } else {
191  if(pubPath != null) {
192  marker.put("path", getItemLink(contact, page));
193  marker.put("all", getPageLink(page));
194  }
195  marker.put("name", contact.getName());
196  }
197 
198  marker.put("face", ElephantContext.getRootWebPath() + cw.getPublishable() + "/profile/face.png");
199  marker.put("cv", contact.getComment(marker.getConstructor(), "#cv"));
200 
201  Connector conn = contact.getConnectorMap().get("Web");
202  if(conn != null) {
203  marker.put("web", conn.getValue());
204  }
205 
206  if(!Strings.isBlank(pubPath)) {
207  marker.put("readall", getItemLink(contact, page));
208  }
209 
210  marker.put("files", new FileAttach(ContactsPU.getObjectPath(contact)));
211 
212  }
213 
214  public String getItemLink(Contact contact, int page) {
215  return pubPath + "?item=" + contact.getId() + "&page=" + page;
216  }
217 
218  public String getPageLink(int page) {
219  return pubPath + "?page=" + page;
220  }
221 
222  @Override
223  protected String title(Contact e) {
224  return e.getName();
225  }
226 
227  @Override
228  protected Collection<String> metas(Contact e) {
229  ArrayList<RepositoryFile> files = new ArrayList<>();
230  String path = getItemLink(e, 0);
231  if(!SocialImageMap.hasImage(path)) {
234  files.addAll(repository.getRepositoryFiles("/profile", "*.png,*.jpg"));
235  files.addAll(repository.getRepositoryFiles("*_social.png,*_social.jpg"));
236  files.addAll(repository.getRepositoryFiles("*.png,*.jpg"));
237  }
238  SocialNet sn = new SocialNet(path, e.getName(), e.getComment(constructor, "#cv"), files);
239  return sn.getMetas();
240  }
241 
242 }
Map< String, Connector > getConnectorMap()
Definition: Contact.java:499
String getComment(IConstructor constructor, String key)
Definition: Contact.java:519
static String getObjectPath(Object object)
Definition: ContactsPU.java:68
void renderSummary(ElephantMarker marker, Contact e, int page)
ContactIterator(IConstructor constructor, String pubPath, String uniqueId)
void renderItem(ElephantMarker marker, Contact e, int page)
void setSyndicationId(String syndicationId)
void prepareValues(Contact contact, int page, boolean asMail)
String getItemLink(Contact contact, int page)
Collection< String > metas(Contact e)
void setConnector(String connector, String values)
void addIn(String operator, String field, List values)
void addNamedValue(String name, Object value)
static boolean hasImage(String url)
Repository getPublishableRepository(IConstructor constructor)
Definition: FileAttach.java:47
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)
static void toggleTags(IConstructor constructor, String root)
Definition: TagCloud.java:45
static TagSet getTags(IConstructor constructor, String root)
Definition: TagCloud.java:36
void addOnLoadedJavaScript(String script)