BrightSide Workbench Full Report + Source Code
elephant-alliance/src/main/java/org/turro/alliance/content/ChallengeContentIterator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2024 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.alliance.db.AlliancePU;
27 import org.turro.alliance.db.entities.AxChallenge;
28 import org.turro.alliance.db.entities.ProcedenceId;
29 import org.turro.alliance.www.CategoriesTree;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.context.IConstructor;
32 import org.turro.elephant.db.WhereClause;
33 import org.turro.elephant.impl.repository.Repository;
34 import org.turro.elephant.impl.repository.RepositoryFile;
35 import org.turro.elephant.web.SocialImageMap;
36 import org.turro.elephant.web.SocialNet;
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.string.Strings;
44 import org.turro.tags.TagCloud;
45 import org.turro.util.Arrays;
46 import org.turro.ws.WsMember;
47 
52 public class ChallengeContentIterator extends EntityDaoContentIterator<AxChallenge, ProcedenceId> {
53 
54  private long lastCategoryId = -1;
55 
56  public ChallengeContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath) {
57  super(new AlliancePU(), constructor, writer, contact, mail, pubPath);
58  }
59 
60  public String getChallengeUrl(WsMember member, AxChallenge challenge) {
61  if(ObjectUtils.allNotNull(member, challenge)) {
62  if(member.getMemberId() == challenge.getChallengeId().getMemberId()) {
63  return Interceptors.parameters("challenge", challenge.getChallengeId().getEntityId())
64  .url(member.getMemberUrl())
65  .encoded();
66  } else {
67  return Interceptors.parameters("axchallenge", challenge.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 challenge from AxChallenge as challenge");
80  wc.addClause("join AxTalentCategory as category on challenge.categoryId = category.categoryId");
81  wc.addClause("left outer join challenge.responses response");
82  wc.addClause("where 1=1");
83  addCriteria(wc, true);
84  wc.addClause("order by challenge.creation desc");
85  return wc;
86  }
87 
88  @Override
90  WhereClause wc = new WhereClause();
91  wc.addClause("select count(distinct challenge) from AxChallenge as challenge");
92  wc.addClause("join AxTalentCategory as category on challenge.categoryId = category.categoryId");
93  wc.addClause("left outer join challenge.responses response");
94  wc.addClause("where 1=1");
95  addCriteria(wc, true);
96  wc.addClause("order by challenge.creation desc");
97  return wc;
98  }
99 
100  @Override
101  protected void renderSummary(ElephantMarker marker, AxChallenge e, int page) {
102  if(e != null) {
103  marker.put("challenge", 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, AxChallenge e, int page) {
118  marker.put("challenge", e);
119  prepareValues(e, page);
120  marker.put("restricted", isRestricted());
122  }
123 
124  @Override
125  protected String entityRoot() {
126  return "axchallenge";
127  }
128 
129  @Override
130  protected AxChallenge entity(ProcedenceId value) {
131  WhereClause wc = new WhereClause();
132  wc.addClause("select distinct challenge from AxChallenge as challenge");
133  wc.addClause("join AxTalentCategory as category on challenge.categoryId = category.categoryId");
134  wc.addClause("left outer join challenge.responses response");
135  wc.addClause("where 1=1");
136  addCriteria(wc, true);
137  wc.addClause("and challenge.challengeId = :id");
138  wc.addNamedValue("id", value);
140  }
141 
142  @Override
144  ProcedenceId identifier = getIDFromURL("/axchallenge");
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("challenge.name"), true);
165  }
166  }
167  }
168  if(TagCloud.hasSelected(constructor, "axchallenge")) {
169  wc.addIn("and", "challenge.id", TagCloud.getIdentifiers(constructor, "axchallenge"));
170  }
171  }
172 
173  private void prepareValues(final AxChallenge challenge, int page) {
174  prepareControls(challenge, page);
175  }
176 
177  @Override
178  protected String title(AxChallenge e) {
179  if(!Strings.isBlank(e.getName())) {
180  return e.getName();
181  }
182  return null;
183  }
184 
185  @Override
186  protected Collection<String> metas(AxChallenge e) {
187  ArrayList<RepositoryFile> files = new ArrayList<>();
188  String path = getItemLink(e);
189  if(!SocialImageMap.hasImage(path)) {
192  files.addAll(repository.getRepositoryFiles("*_social.png,*_social.jpg"));
193  files.addAll(repository.getRepositoryFiles("*.png,*.jpg"));
194  }
195  SocialNet sn = new SocialNet(path, e.getName(), e.getQuestion(), files);
196  return sn.getMetas();
197  }
198 
199  @Override
200  protected String getTemplateRoot() {
201  return isMail() ? "content/newsletter/sections/alliance/challenges" : "alliance/challenge";
202  }
203 
204  @Override
205  protected Object doVotesCtrl(AxChallenge e) {
206  return null;
207  }
208 
209  @Override
210  protected Object doInterestCtrl(AxChallenge e) {
211  return null;
212  }
213 
214  @Override
215  protected Object doCommentsCtrl(AxChallenge e) {
216  return null;
217  }
218 
219  @Override
220  protected Object doAttachmentsCtrl(AxChallenge e) {
221  return null;
222  }
223 
224  @Override
225  protected Object doFilesCtrl(AxChallenge e) {
226  return null;
227  }
228 
229  @Override
230  protected Object doDescriptionsCtrl(AxChallenge e) {
231  return null;
232  }
233 
234  @Override
235  protected Object doPollsCtrl(AxChallenge e) {
236  return null;
237  }
238 
239  @Override
240  protected String getItemLink(AxChallenge challenge) {
241  return doItemLink(challenge, challenge.getChallengeId(), true);
242  }
243 
244  @Override
245  protected String getReadAllLink() {
246  String path = getContextPath();
247  if(Strings.isBlank(path)) {
248  path = ElephantContext.getEntityWebContext("/axchallenge");
249  }
250  if(Strings.isBlank(path)) {
251  return getRestrictedLink();
252  }
253  return path;
254  }
255 
256  @Override
257  protected String getRestrictedLink() {
258  return "/user/alliance/challenges";
259  }
260 
261  @Override
262  protected String stringId(ProcedenceId value) {
263  return value.stringify();
264  }
265 
266  @Override
267  protected ProcedenceId parseId(String value) {
268  return ProcedenceId.from(value);
269  }
270 
271 }
static Parameters parameters(String root)
Parameters add(String key, Object value)
Definition: Parameters.java:48
ChallengeContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath)
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)