BrightSide Workbench Full Report + Source Code
EntityDaoContentIterator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.jpa.content;
20 
21 import java.io.Writer;
22 import java.nio.file.Paths;
23 import java.util.Optional;
24 import org.turro.string.Strings;
25 import org.turro.action.IEntityCtrl;
26 import org.turro.action.Plugins;
27 import org.turro.elephant.context.IConstructor;
28 import org.turro.entities.EmptyController;
29 import org.turro.entities.Entities;
30 import org.turro.entities.IElephantEntity;
31 import org.turro.jpa.Dao;
32 import org.turro.jpa.entity.EntityWebUrls;
33 import org.turro.jpa.iterator.DaoHtmlIterator;
34 import org.turro.marker.ElephantMarker;
35 import org.turro.marker.MarkerHelper;
36 import org.turro.parser.processor.MacroProcessorContext;
37 import org.turro.participation.ParticipationReason;
38 import org.turro.plugin.contacts.IContact;
39 
44 public abstract class EntityDaoContentIterator<E, ID> extends DaoHtmlIterator<E, ID> {
45 
46  private final String contextPath;
47  private final IContact contact;
48  private final boolean mail;
49  private SeenStatusCheck seenStatus = SeenStatusCheck.DONT_CHECK;
50 
52  Writer writer, IContact contact, boolean mail, String contextPath) {
53  super(dao, new ElephantMarker(constructor, mail), writer);
54  this.contextPath = Optional.ofNullable(contextPath)
55  .orElse(
56  Optional.ofNullable(constructor)
57  .map(cons -> cons.getCurrentContext())
58  .map(ctx -> ctx.getFullPath())
59  .filter(path -> !path.endsWith("/user"))
60  .orElse(null)
61  );
62  this.contact = contact;
63  this.mail = mail;
64  }
65 
66  public void setSeenStatus(SeenStatusCheck seenStatus) {
67  this.seenStatus = seenStatus;
68  }
69 
71  return seenStatus;
72  }
73 
74  public String getId() {
75  int hash = Paths.get(contextPath).hashCode();
76  return "UID_" + (hash < 0 ? -hash : hash);
77  }
78 
79  public String getContextPath() {
80  return contextPath;
81  }
82 
83  public IContact getContact() {
84  return contact;
85  }
86 
87  public boolean isMail() {
88  return mail;
89  }
90 
91  public Object getVotesCtrl(E e) {
92  if(isAllowVotes()) {
93  return doVotesCtrl(e);
94  }
95  return null;
96  }
97 
98  public Object getInterestCtrl(E e) {
99  if(isAllowInterest()) {
100  return doInterestCtrl(e);
101  }
102  return null;
103  }
104 
105  public Object getCommentsCtrl(E e) {
106  if(isAllowComments()) {
107  return doCommentsCtrl(e);
108  }
109  return null;
110  }
111 
112  public Object getAttachmentsCtrl(E e) {
113  if(isAllowAttachments()) {
114  return doAttachmentsCtrl(e);
115  }
116  return null;
117  }
118 
119  public Object getFilesCtrl(E e) {
120  if(isAllowFiles()) {
121  return doFilesCtrl(e);
122  }
123  return null;
124  }
125 
126  public Object getDescriptionsCtrl(E e) {
127  if(isAllowDescriptions()) {
128  return doDescriptionsCtrl(e);
129  }
130  return null;
131  }
132 
133  public Object getPollsCtrl(E e) {
134  if(isAllowPolls()) {
135  return doPollsCtrl(e);
136  }
137  return null;
138  }
139 
140  public Object getForumCtrl(E e) {
141  if(isAllowForum()) {
142  IEntityCtrl ecf = Plugins.loadImplementation(IEntityCtrl.class, "forum-ctrl");
143  if(ecf != null) {
146  ecf.setEntityPath(iee.getPath());
147  ecf.setTemplate("related");
148  marker.put("forum", ecf);
149  return iee.configureCtrl(iee, getContact());
150  }
151  }
152  return null;
153  }
154 
155  /* Macro helpers */
156 
157  public void assignDefaultsByName(String name) {
158  if(name.contains("summary")) {
159  setNavigatorTop(false);
160  setNavigatorBottom(false);
161  }
162  setAllowRanking(name.contains("ranking"));
163  setRestricted(name.contains("restricted"));
164  }
165 
167  setPage(context.getPage());
168  setNavigatorTop(context.isNavigation());
169  setNavigatorBottom(context.isNavigation());
170  setAllowRanking(context.isRanking());
171  setAllowMatching(context.isMatching());
172  setRestricted(context.isRestricted());
173  setSummaryTemplate(context.getTemplate());
174  }
175 
176  /* Participation helpers */
177 
178  public Object getParticipationCtrl(E e, ParticipationReason reason) {
179  return Entities.getController(e).getParticipationControl(constructor, contact, reason);
180  }
181 
182  public Object getSpamCtrl(E e) {
184  }
185 
186  public Object getFollowCtrl(E e) {
188  }
189 
190  public Object getSeenCtrl(E e) {
192  }
193 
194  public Object getLikeCtrl(E e) {
196  }
197 
198  public Object getApplyCtrl(E e) {
200  }
201 
202  public Object getCtrlByLabel(String ctrl, E e, IContact contact) {
204  if(ec != null) {
206  if(!Strings.isBlank(iee.getPath())) {
208  ec.setEntityPath(iee.getPath());
209  return iee.configureCtrl(ec, contact);
210  }
211  }
212  return null;
213  }
214 
215  protected void prepareControls(E entity, int page) {
216  marker.put("entiter", this);
217  if(!marker.contains("iee")) {
218  IElephantEntity iee = Entities.getController(entity);
219  if(!(iee instanceof EmptyController)) {
220  marker.put("iee", iee);
221  }
222  }
223  if(isMail()) {
224  marker.put("itemLink", "{liveref:" + getItemLink(entity) + "}");
225  marker.put("allLink", "{liveref:" + getReadAllLink() + "}");
226  marker.put("restrictedLink", "{liveref:" + getRestrictedLink() + "}");
227  } else {
228  marker.put("itemLink", getItemLink(entity));
229  marker.put("allLink", getReadAllLink());
230  marker.put("restrictedLink", getRestrictedLink());
231  }
232  }
233 
234  protected String doItemLink(E entity, ID id, boolean obfuscated) {
235  String ewu = EntityWebUrls.getUrlFromEntity(entity);
236  if(ewu != null) {
237  return getReadAllLink() + ewu;
238  } else {
239  if(obfuscated) {
240  return getReadAllLink() + "?" + MarkerHelper.setObfuscatedPars("item=" + stringId(id));
241  } else {
242  return getReadAllLink() + "?item=" + stringId(id);
243  }
244  }
245  }
246 
247  protected abstract String getTemplateRoot();
248  protected abstract Object doVotesCtrl(E e);
249  protected abstract Object doInterestCtrl(E e);
250  protected abstract Object doCommentsCtrl(E e);
251  protected abstract Object doAttachmentsCtrl(E e);
252  protected abstract Object doFilesCtrl(E e);
253  protected abstract Object doDescriptionsCtrl(E e);
254  protected abstract Object doPollsCtrl(E e);
255  protected abstract String getItemLink(E e);
256  protected abstract String getReadAllLink();
257  protected abstract String getRestrictedLink();
258 
259 }
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
static IElephantEntity getController(String path)
Definition: Entities.java:78
void assignDefaultsByMacro(MacroProcessorContext context)
Object getCtrlByLabel(String ctrl, E e, IContact contact)
Object getParticipationCtrl(E e, ParticipationReason reason)
EntityDaoContentIterator(Dao dao, IConstructor constructor, Writer writer, IContact contact, boolean mail, String contextPath)
String doItemLink(E entity, ID id, boolean obfuscated)
static String getUrlFromEntity(Object entity)
void setNavigatorBottom(boolean navigatorBottom)
void setAllowMatching(boolean allowMatching)
void setSummaryTemplate(String summaryTemplate)
void setNavigatorTop(boolean navigatorTop)
void setAllowRanking(boolean allowRanking)
Object put(Object key, Object value)
static String setObfuscatedPars(String parameters)
void setEntityPath(String entityPath)
void setConstructor(IConstructor constructor)
void setTemplate(String template)
Object getParticipationControl(IConstructor constructor, ParticipationReason reason)
Object configureCtrl(Object ctrl, IContact contact)