BrightSide Workbench Full Report + Source Code
PollIterator.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.polls;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import org.turro.string.Strings;
24 import org.turro.attach.www.AttachCtrl;
25 import org.turro.auth.Authentication;
26 import org.turro.collections.CollectionHtmlIterator;
27 import org.turro.elephant.context.IConstructor;
28 import org.turro.elephant.db.ElephantPU;
29 import org.turro.elephant.entities.db.Poll;
30 import org.turro.elephant.entities.db.PollOption;
31 import org.turro.elephant.impl.repository.Repository;
32 import org.turro.elephant.impl.repository.RepositoryFile;
33 import org.turro.elephant.web.SocialImageMap;
34 import org.turro.elephant.web.SocialNet;
35 import org.turro.file.util.FileAttach;
36 import org.turro.mail.recipients.MailContact;
37 import org.turro.marker.ElephantMarker;
38 
43 public class PollIterator extends CollectionHtmlIterator<Poll, Long> {
44 
45  private String pubPath, actualParameters;
46  private boolean restricted, publicOnly, activeOnly;
47  private final PollsUtil pollsUtil;
48 
49  public PollIterator(IConstructor constructor, String pubPath) {
51  this.pubPath = pubPath;
52  pollsUtil = new PollsUtil(Authentication.getIContact());
53  }
54 
55  public boolean isPublicOnly() {
56  return publicOnly;
57  }
58 
59  public void setPublicOnly(boolean publicOnly) {
60  this.publicOnly = publicOnly;
61  }
62 
63  public boolean isActiveOnly() {
64  return activeOnly;
65  }
66 
67  public void setActiveOnly(boolean activeOnly) {
68  this.activeOnly = activeOnly;
69  }
70 
71  public boolean isRestricted() {
72  return restricted;
73  }
74 
75  public void setRestricted(boolean restricted) {
76  this.restricted = restricted;
77  }
78 
79  public int count() {
80  return getTotalCount();
81  }
82 
83  public Collection<Poll> items() {
84  return getTotalItems();
85  }
86 
87  public String getActualParameters() {
88  return Strings.isBlank(actualParameters) ? "?a=b" : actualParameters;
89  }
90 
91  public String getVoteURL(PollOption option, int vote) throws Exception {
92  return PollAction.createURL(option, Authentication.getIContact(), vote);
93  }
94 
95  public static String getOptionTemplate(Poll poll) {
96  if(!poll.isShowParticipants()) {
97  if(poll.isAllowNegatives()) {
98  return "voteOption";
99  } else {
100  return "likeOption";
101  }
102  } else {
103  if(poll.isAllowNegatives()) {
104  return "quadrantOption";
105  } else {
106  return "doodleOption";
107  }
108  }
109  }
110 
111  @Override
112  protected void renderSummary(ElephantMarker marker, Poll e, int page) {
113  if(e != null) {
114  marker.put("poll", e);
115  prepareValues(e, page, false);
116  }
117  marker.process("poll", getSummaryTemplate());
118  actualParameters = getPageLink(page);
119  }
120 
121  @Override
122  protected void renderItem(ElephantMarker marker, Poll e, int page) {
123  marker.put("poll", e);
124  prepareValues(e, page, false);
125  marker.process("poll", getFullTemplate());
126  actualParameters = getPageLink(page);
127  }
128 
129  @Override
130  protected Poll entity(Long value) {
131  return new ElephantPU().find(Poll.class, value);
132  }
133 
134  @Override
135  protected boolean isValid(Poll e) {
136  if(restricted && e != null) {
137  return e.isParticipant();
138  } else {
139  return super.isValid(e);
140  }
141  }
142 
143  private void prepareValues(Poll poll, int page, boolean asMail) {
144 
145  if(asMail && pubPath != null) {
146  marker.put("name", MailContact.createLink(poll.getTitle(), getItemLink(poll, page), false));
147  } else {
148  marker.put("name", poll.getTitle());
149  }
150  if(pubPath != null) {
151  marker.put("path", getItemLink(poll, page));
152  marker.put("all", getPageLink(page));
153  }
154  if(!Strings.isBlank(pubPath)) {
155  marker.put("readall", getItemLink(poll, page));
156  }
157 
158  AttachCtrl ac = new AttachCtrl(constructor);
159  ac.setEntityPath(ElephantPU.getObjectPath(poll));
160  ac.setPublicOnly(publicOnly);
161  marker.put("ac", ac);
162 
163  marker.put("control", this);
164  marker.put("template", getOptionTemplate(poll));
165  marker.put("utils", pollsUtil);
166 
167  }
168 
169  private String getItemLink(Poll poll, int page) {
170  return pubPath + "?item=" + poll.getId() + "&page=" + page;
171  }
172 
173  private String getPageLink(int page) {
174  return pubPath + "?page=" + page;
175  }
176 
177  @Override
178  protected String title(Poll e) {
179  return e.getTitle();
180  }
181 
182  @Override
183  protected Collection<String> metas(Poll e) {
184  ArrayList<RepositoryFile> files = new ArrayList<>();
185  String path = getItemLink(e, 0);
186  if(!SocialImageMap.hasImage(path)) {
189  files.addAll(repository.getRepositoryFiles("*_social.png,*_social.jpg"));
190  files.addAll(repository.getRepositoryFiles("*.png,*.jpg"));
191  }
192  SocialNet sn = new SocialNet(path, e.getTitle(), e.getText(), files);
193  return sn.getMetas();
194  }
195 
196 }
static String getObjectPath(Object object)
Definition: ElephantPU.java:63
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 String createURL(PollOption option, IContact contact, int vote)
Definition: PollAction.java:50
Collection< Poll > items()
static String getOptionTemplate(Poll poll)
String getVoteURL(PollOption option, int vote)
PollIterator(IConstructor constructor, String pubPath)
void setActiveOnly(boolean activeOnly)
void renderItem(ElephantMarker marker, Poll e, int page)
void setPublicOnly(boolean publicOnly)
void renderSummary(ElephantMarker marker, Poll e, int page)
Collection< String > metas(Poll e)
void setRestricted(boolean restricted)
static Set< Poll > getMyPolls(IContact contact)
Definition: PollsUtil.java:211