BrightSide Workbench Full Report + Source Code
ProjectContentIterator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.alliance.content;
20 
21 import java.io.Writer;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import org.apache.commons.lang3.ObjectUtils;
25 import org.turro.action.Interceptors;
26 import org.turro.string.Strings;
27 import org.turro.alliance.db.AlliancePU;
28 import org.turro.alliance.db.entities.AxProject;
29 import org.turro.alliance.db.entities.ProcedenceId;
30 import org.turro.alliance.www.CategoriesTree;
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.content.EntityDaoContentIterator;
40 import org.turro.jpa.search.DaoHtmlSearch;
41 import org.turro.jpa.search.DaoSearchKey;
42 import org.turro.marker.ElephantMarker;
43 import org.turro.plugin.contacts.IContact;
44 import org.turro.tags.TagCloud;
45 import org.turro.util.Arrays;
46 import org.turro.ws.WsMember;
47 
52 public class ProjectContentIterator extends EntityDaoContentIterator<AxProject, ProcedenceId> {
53 
54  private long lastCategoryId = -1;
55 
56  public ProjectContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath) {
57  super(new AlliancePU(), constructor, writer, contact, mail, pubPath);
58  }
59 
60  public String getProjectUrl(WsMember member, AxProject project) {
61  if(ObjectUtils.allNotNull(member, project)) {
62  if(member.getMemberId() == project.getProjectId().getMemberId()) {
63  return Interceptors.parameters("project", project.getProjectId().getEntityId())
64  .url(member.getMemberUrl())
65  .encoded();
66  } else {
67  return Interceptors.parameters("axproject", project.getId().replaceAll("#", "_"))
69  .url(member.getMemberUrl())
70  .encoded();
71  }
72  }
73  return "#";
74  }
75 
76  @Override
78  WhereClause wc = new WhereClause();
79  wc.addClause("select distinct project from AxProject as project");
80  wc.addClause("join AxCategory as category on project.categoryId = category.categoryId");
81  wc.addClause("left outer join project.participations participant");
82  wc.addClause("where 1=1");
83  addCriteria(wc, true);
84  wc.addClause("order by project.creation desc");
85  return wc;
86  }
87 
88  @Override
90  WhereClause wc = new WhereClause();
91  wc.addClause("select count(distinct project) from AxProject as project");
92  wc.addClause("join AxCategory as category on project.categoryId = category.categoryId");
93  wc.addClause("left outer join project.participations participant");
94  wc.addClause("where 1=1");
95  addCriteria(wc, true);
96  wc.addClause("order by project.creation desc");
97  return wc;
98  }
99 
100  @Override
101  protected void renderSummary(ElephantMarker marker, AxProject e, int page) {
102  if(e != null) {
103  marker.put("project", e);
104  if(e.getCategory() != null) {
105  marker.put("newCategory", e.getCategory().getCategoryId() != lastCategoryId);
106  lastCategoryId = e.getCategory().getCategoryId();
107  } else {
108  lastCategoryId = 0;
109  }
110  prepareValues(e, page);
111  marker.put("restricted", isRestricted());
112  }
114  }
115 
116  @Override
117  protected void renderItem(ElephantMarker marker, AxProject e, int page) {
118  marker.put("project", e);
119  prepareValues(e, page);
120  marker.put("restricted", isRestricted());
122  }
123 
124  @Override
125  protected String entityRoot() {
126  return "axproject";
127  }
128 
129  @Override
130  protected AxProject entity(ProcedenceId value) {
131  WhereClause wc = new WhereClause();
132  wc.addClause("select distinct project from AxProject as project");
133  wc.addClause("join AxCategory as category on project.categoryId = category.categoryId");
134  wc.addClause("left outer join project.participations participant");
135  wc.addClause("where 1=1");
136  addCriteria(wc, true);
137  wc.addClause("and project.projectId = :id");
138  wc.addNamedValue("id", value);
139  return (AxProject) dao.getSingleResultOrNull(wc);
140  }
141 
142  @Override
144  ProcedenceId identifier = getIDFromURL("/axproject");
145  if(identifier == null) {
146  identifier = super.getIdentifier();
147  }
148  return identifier;
149  }
150 
151  private void addCriteria(WhereClause wc, boolean filterPhase) {
152  if(getNavigationTree() != null) {
153  String selected = ((CategoriesTree) getNavigationTree()).getSelectedItem();
154  if(!Strings.isBlank(selected)) {
155  wc.addClause("and concat(category.uniquePath, '/') like :category");
156  wc.addNamedValue("category", selected + "/%");
157  }
158  }
159  if(isSearchOption()) {
160  DaoHtmlSearch dhs = DaoHtmlSearch.getInstance(constructor, ElephantContext.getContextVariable(constructor));
161  if(dhs != null) {
162  DaoSearchKey dsk = dhs.get("search-value");
163  if(dsk != null) {
164  dsk.applyToQuery(wc, Arrays.objects("project.name"), true);
165  }
166  }
167  if(filterPhase) {
168  PhaseHtmlFilter phf = PhaseHtmlFilter.getInstance(constructor, ElephantContext.getContextVariable(constructor));
169  if(phf != null && !phf.isEmpty()) {
170  int phase = Integer.valueOf(phf.getPhase());
171  if(phase > -1) {
172  String par = "phase" + wc.getUniqueSuffix();
173  wc.addClause("and project.phaseIndex = :" + par);
174  wc.addNamedValue(par, phase);
175  }
176  }
177  }
178  }
179  if(TagCloud.hasSelected(constructor, "axproject")) {
180  wc.addIn("and", "project.id", TagCloud.getIdentifiers(constructor, "axproject"));
181  }
182  }
183 
184  private void prepareValues(final AxProject project, int page) {
185  prepareControls(project, page);
186  }
187 
188  @Override
189  protected String title(AxProject e) {
190  if(!Strings.isBlank(e.getName())) {
191  return e.getName();
192  }
193  return null;
194  }
195 
196  @Override
197  protected Collection<String> metas(AxProject e) {
198  ArrayList<RepositoryFile> files = new ArrayList<>();
199  String path = getItemLink(e);
200  if(!SocialImageMap.hasImage(path)) {
203  files.addAll(repository.getRepositoryFiles("*_social.png,*_social.jpg"));
204  files.addAll(repository.getRepositoryFiles("*.png,*.jpg"));
205  }
206  SocialNet sn = new SocialNet(path, e.getName(), e.getSummary(), files);
207  return sn.getMetas();
208  }
209 
210  @Override
211  protected String getTemplateRoot() {
212  return isMail() ? "content/newsletter/sections/alliance/projects" : "alliance/project";
213  }
214 
215  @Override
216  protected Object doVotesCtrl(AxProject e) {
217  return null;
218  }
219 
220  @Override
221  protected Object doInterestCtrl(AxProject e) {
222  return null;
223  }
224 
225  @Override
226  protected Object doCommentsCtrl(AxProject e) {
227  return null;
228  }
229 
230  @Override
231  protected Object doAttachmentsCtrl(AxProject e) {
232  return null;
233  }
234 
235  @Override
236  protected Object doFilesCtrl(AxProject e) {
237  return null;
238  }
239 
240  @Override
241  protected Object doDescriptionsCtrl(AxProject e) {
242  return null;
243  }
244 
245  @Override
246  protected Object doPollsCtrl(AxProject e) {
247  return null;
248  }
249 
250  @Override
251  protected String getItemLink(AxProject project) {
252  return doItemLink(project, project.getProjectId(), true);
253  }
254 
255  @Override
256  protected String getReadAllLink() {
257  String path = getContextPath();
258  if(Strings.isBlank(path)) {
259  path = ElephantContext.getEntityWebContext("/axproject");
260  }
261  if(Strings.isBlank(path)) {
262  return getRestrictedLink();
263  }
264  return path;
265  }
266 
267  @Override
268  protected String getRestrictedLink() {
269  return "/user/alliance/projects";
270  }
271 
272  @Override
273  protected String stringId(ProcedenceId value) {
274  return value.stringify();
275  }
276 
277  @Override
278  protected ProcedenceId parseId(String value) {
279  return ProcedenceId.from(value);
280  }
281 
282 }
static Parameters parameters(String root)
Parameters add(String key, Object value)
Definition: Parameters.java:48
ProjectContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath)
void renderSummary(ElephantMarker marker, AxProject e, int page)
String getProjectUrl(WsMember member, AxProject project)
void renderItem(ElephantMarker marker, AxProject e, int page)
static String getObjectPath(Object object)
Definition: AlliancePU.java:60
static ProcedenceId from(String id)
static String getEntityWebContext(String path)
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
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
String doItemLink(E entity, ID id, boolean obfuscated)
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)