BrightSide Workbench Full Report + Source Code
PollContentIterator.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.content;
20 
21 import java.io.Writer;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import org.turro.string.Strings;
25 import org.turro.attach.www.AttachCtrl;
26 import org.turro.auth.Authentication;
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.jpa.content.EntityItemContentIterator;
37 import org.turro.marker.ElephantMarker;
38 import org.turro.plugin.contacts.IContact;
39 import org.turro.polls.PollAction;
40 import org.turro.polls.PollsUtil;
41 
46 public class PollContentIterator extends EntityItemContentIterator<Poll, Long> {
47 
48  private boolean restricted, publicOnly, activeOnly;
49  private final PollsUtil pollsUtil;
50 
51  public PollContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean active, boolean restricted, int page, boolean mail, String pubPath) {
52  super(new ArrayList(PollsUtil.getPolls(contact, active, restricted, page)), constructor, writer, contact, mail, pubPath);
53  setPage(page);
54  activeOnly = active;
55  pollsUtil = new PollsUtil(contact);
56  }
57 
58  public boolean isPublicOnly() {
59  return publicOnly;
60  }
61 
62  public void setPublicOnly(boolean publicOnly) {
63  this.publicOnly = publicOnly;
64  }
65 
66  public boolean isActiveOnly() {
67  return activeOnly;
68  }
69 
70  public boolean isRestricted() {
71  return restricted;
72  }
73 
74  public String getVoteURL(PollOption option, int vote) throws Exception {
75  return PollAction.createURL(option, Authentication.getIContact(), vote);
76  }
77 
78  public static String getOptionTemplate(Poll poll) {
79  if(!poll.isShowParticipants()) {
80  if(poll.isAllowNegatives()) {
81  return "voteOption";
82  } else {
83  return "likeOption";
84  }
85  } else {
86  if(poll.isAllowNegatives()) {
87  return "quadrantOption";
88  } else {
89  return "doodleOption";
90  }
91  }
92  }
93 
94  @Override
95  protected void renderSummary(ElephantMarker marker, Poll e, int page) {
96  if(e != null) {
97  marker.put("poll", e);
98  prepareValues(e, page);
99  }
101  }
102 
103  @Override
104  protected void renderItem(ElephantMarker marker, Poll e, int page) {
105  marker.put("poll", e);
106  prepareValues(e, page);
108  }
109 
110  @Override
111  protected Poll entity(Long value) {
112  return new ElephantPU().find(Poll.class, value);
113  }
114 
115  @Override
116  protected boolean isValid(Poll e) {
117  if(restricted && e != null) {
118  return e.isParticipant();
119  } else {
120  return super.isValid(e);
121  }
122  }
123 
124  private void prepareValues(Poll poll, int page) {
125  prepareControls(poll, page);
126 
127  marker.put("template", getOptionTemplate(poll));
128  marker.put("utils", pollsUtil);
129  }
130 
131  @Override
132  protected String title(Poll e) {
133  return e.getTitle();
134  }
135 
136  @Override
137  protected Collection<String> metas(Poll e) {
138  ArrayList<RepositoryFile> files = new ArrayList<>();
139  String path = getItemLink(e);
140  if(!SocialImageMap.hasImage(path)) {
143  files.addAll(repository.getRepositoryFiles("*_social.png,*_social.jpg"));
144  files.addAll(repository.getRepositoryFiles("*.png,*.jpg"));
145  }
146  SocialNet sn = new SocialNet(path, e.getTitle(), e.getText(), files);
147  return sn.getMetas();
148  }
149 
150  @Override
151  protected String getTemplateRoot() {
152  return isMail() ? "content/newsletter/sections/polls" : "poll";
153  }
154 
155  @Override
156  protected Object doVotesCtrl(Poll e) {
157  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
158  }
159 
160  @Override
161  protected Object doInterestCtrl(Poll e) {
162  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
163  }
164 
165  @Override
166  protected Object doCommentsCtrl(Poll e) {
167  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
168  }
169 
170  @Override
171  protected Object doAttachmentsCtrl(Poll e) {
174  ac.setPublicOnly(publicOnly);
175  return ac;
176  }
177 
178  @Override
179  protected Object doFilesCtrl(Poll e) {
180  return new FileAttach(ElephantPU.getObjectPath(e));
181  }
182 
183  @Override
184  protected Object doDescriptionsCtrl(Poll e) {
185  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
186  }
187 
188  @Override
189  protected Object doPollsCtrl(Poll e) {
190  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
191  }
192 
193  @Override
194  protected String getItemLink(Poll e) {
195  return getReadAllLink() + "?item=" + e.getId();
196  }
197 
198  @Override
199  protected String getReadAllLink() {
200  if(Strings.isBlank(getContextPath())) {
201  return getRestrictedLink();
202  }
203  return getContextPath();
204  }
205 
206  @Override
207  protected String getRestrictedLink() {
208  return "/user/mypolls";
209  }
210 
211 }
void setPublicOnly(boolean publicOnly)
Definition: AttachCtrl.java:77
void setEntityPath(String entityPath)
Definition: AttachCtrl.java:45
String getVoteURL(PollOption option, int vote)
void renderItem(ElephantMarker marker, Poll e, int page)
PollContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean active, boolean restricted, int page, boolean mail, String pubPath)
void renderSummary(ElephantMarker marker, Poll e, int page)
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
static List< Poll > getPolls(String entityPath)
Definition: PollsUtil.java:148