BrightSide Workbench Full Report + Source Code
OldDirectoryContentIterator.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.content;
20 
21 import java.io.Writer;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import org.turro.string.Strings;
26 import org.turro.attach.www.AttachCtrl;
27 import org.turro.contacts.Contact;
28 import org.turro.contacts.db.ContactsPU;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.elephant.context.IConstructor;
31 import org.turro.elephant.db.WhereClause;
32 import org.turro.elephant.impl.repository.Repository;
33 import org.turro.elephant.impl.repository.RepositoryFile;
34 import org.turro.elephant.web.SocialImageMap;
35 import org.turro.elephant.web.SocialNet;
36 import org.turro.entities.Entities;
37 import org.turro.file.util.FileAttach;
38 import org.turro.jpa.content.EntityDaoContentIterator;
39 import org.turro.jpa.search.DaoHtmlSearch;
40 import org.turro.jpa.search.DaoSearchKey;
41 import org.turro.marker.ElephantMarker;
42 import org.turro.plugin.contacts.IContact;
43 import org.turro.polls.PollsCtrl;
44 import org.turro.www.commentit.CommentItCtrl;
45 import org.turro.www.describeit.DescribeItCtrl;
46 import org.turro.www.starit.StarItCtrl;
47 import org.turro.www.voteit.VoteItCtrl;
48 
53 public class OldDirectoryContentIterator extends EntityDaoContentIterator<Contact, String> {
54 
55  public OldDirectoryContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String contextPath) {
56  super(new ContactsPU(), constructor, writer, contact, mail, contextPath);
57  }
58 
59  @Override
61  WhereClause wc = new WhereClause();
62  wc.addClause("select distinct contact from Contact as contact");
63  wc.addClause("left outer join contact.syndications syndication");
64  wc.addClause("left outer join contact.connectors connector with connector.description like '%Directory%'");
65  addCriteria(wc);
66  wc.addClause("order by contact.name");
67  return wc;
68  }
69 
70  @Override
72  WhereClause wc = new WhereClause();
73  wc.addClause("select count(distinct contact) from Contact as contact");
74  wc.addClause("left outer join contact.syndications syndication");
75  wc.addClause("left outer join contact.connectors connector with connector.description like '%Directory%'");
76  addCriteria(wc);
77  return wc;
78  }
79 
80  @Override
81  protected void renderSummary(ElephantMarker marker, Contact e, int page) {
82  if(e != null) {
83  marker.put("contact", e);
84  prepareValues(e, page);
85  }
87  }
88 
89  @Override
90  protected void renderItem(ElephantMarker marker, Contact e, int page) {
91  marker.put("contact", e);
92  prepareValues(e, page);
94  }
95 
96  @Override
97  protected String entityRoot() {
98  return "contact";
99  }
100 
101  @Override
102  protected Contact entity(String value) {
103  WhereClause wc = new WhereClause();
104  wc.addClause("select distinct contact from Contact as contact");
105  wc.addClause("left outer join contact.syndications syndication");
106  wc.addClause("left outer join contact.connectors connector with connector.description like '%Directory%'");
107  addCriteria(wc);
108  wc.addClause("and contact.id = :id");
109  wc.addNamedValue("id", value);
110  return (Contact) dao.getSingleResultOrNull(wc);
111  }
112 
113  private void addCriteria(WhereClause wc) {
114  wc.addClause("where 1=1");
115  if(isSearchOption()) {
117  if(dhs != null) {
118  DaoSearchKey dsk = dhs.get("search-value");
119  if(dsk != null) {
120  dsk.applyToQuery(wc, Arrays.asList(new String[] { "contact.name", "connector.value" }), false);
121 // String searchValue = dsk.getValue();
122 // if(!Strings.isBlank(searchValue)) {
123 // wc.addClause("and contact.name like :search01");
124 // wc.addNamedValue("search01", "%" + searchValue + "%");
125 // }
126  }
127  }
128  }
129  if(isRestricted()) {
130  wc.addClause("and syndication.name = 'directory'");
131  if(!getContact().getPermissions().isInRole("directory:show")) {
132  wc.addClause("and 2=1");
133  }
134  }
135  }
136 
137  @Override
138  protected boolean isValid(Contact e) {
139  return super.isValid(e);
140  }
141 
142  private void prepareValues(final Contact e, int page) {
143  prepareControls(e, page);
144  }
145 
146  @Override
147  protected String title(Contact e) {
148  if(!Strings.isBlank(e.getName())) {
149  return e.getName();
150  }
151  return null;
152  }
153 
154  @Override
155  protected Collection<String> metas(Contact e) {
156  ArrayList<RepositoryFile> files = new ArrayList<>();
157  String path = getItemLink(e);
158  if(!SocialImageMap.hasImage(path)) {
161  files.addAll(repository.getRepositoryFiles("*_social.png,*_social.jpg"));
162  files.addAll(repository.getRepositoryFiles("*.png,*.jpg"));
163  }
164  SocialNet sn;
165  sn = new SocialNet(path, e.getName(), e.getName(), files);
166  return sn.getMetas();
167  }
168 
169  @Override
170  protected String getTemplateRoot() {
171  return isMail() ? "content/newsletter/sections/directory" : "directory";
172  }
173 
174  @Override
175  protected Object doVotesCtrl(Contact e) {
178  }
179 
180  @Override
181  protected Object doInterestCtrl(Contact e) {
184  }
185 
186  @Override
187  protected Object doCommentsCtrl(Contact e) {
190  }
191 
192  @Override
193  protected Object doAttachmentsCtrl(Contact e) {
196  }
197 
198  @Override
199  protected Object doFilesCtrl(Contact e) {
202  }
203 
204  @Override
205  protected Object doDescriptionsCtrl(Contact e) {
208  }
209 
210  @Override
211  protected Object doPollsCtrl(Contact e) {
214  }
215 
216  @Override
217  protected String getItemLink(Contact e) {
218  return doItemLink(e, e.getId(), true);
219  }
220 
221  @Override
222  protected String getReadAllLink() {
223  String path = getContextPath();
224  if(Strings.isBlank(path)) {
225  path = ElephantContext.getEntityWebContext("/directory");
226  }
227  if(Strings.isBlank(path)) {
228  return getRestrictedLink();
229  }
230  return path;
231  }
232 
233  @Override
234  protected String getRestrictedLink() {
235  return "/user/directory";
236  }
237 
238 }
void renderItem(ElephantMarker marker, Contact e, int page)
void renderSummary(ElephantMarker marker, Contact e, int page)
OldDirectoryContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String contextPath)
static String getObjectPath(Object object)
Definition: ContactsPU.java:68
static String getContextVariable(IConstructor constructor)
static String getEntityWebContext(String path)
void addNamedValue(String name, Object value)
static boolean hasImage(String url)
static IElephantEntity getController(String path)
Definition: Entities.java:78
Repository getPublishableRepository(IConstructor constructor)
Definition: FileAttach.java:47
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
String doItemLink(E entity, ID id, boolean obfuscated)
static DaoHtmlSearch getInstance(IConstructor constructor, String context)
boolean applyToQuery(WhereClause wc, List< String > fields, boolean withSynonyms)
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)
Object configureCtrl(Object ctrl, IContact contact)