BrightSide Workbench Full Report + Source Code
ProductIterator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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.financials.product.www;
20 
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.List;
25 import org.turro.string.Strings;
26 import org.turro.attach.www.AttachCtrl;
27 import org.turro.describeit.DescribeItUtil;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.context.IConstructor;
30 import org.turro.elephant.db.WhereClause;
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.financials.cart.ShopContext;
37 import org.turro.financials.cart.ShoppingCart;
38 import org.turro.financials.db.FinancialsPU;
39 import org.turro.financials.entity.Product;
40 import org.turro.groupit.GroupItUtil;
41 import org.turro.jpa.DaoUtil;
42 import org.turro.jpa.iterator.DaoHtmlIterator;
43 import org.turro.jpa.search.DaoHtmlSearch;
44 import org.turro.jpa.search.DaoSearchKey;
45 import org.turro.mail.recipients.MailContact;
46 import org.turro.marker.ElephantMarker;
47 import org.turro.www.commentit.CommentItCtrl;
48 import org.turro.www.describeit.DescribeItCtrl;
49 import org.turro.www.starit.StarItCtrl;
50 import org.turro.www.voteit.VoteItCtrl;
51 
56 @Deprecated
57 public class ProductIterator extends DaoHtmlIterator<Product, Long> {
58 
59  private String context, actualParameters, uniqueId;
60  private boolean exact, allowInterest, allowComments, allowVotes, allowGroups,
61  showDescriptions, showAttachments, onlyPromoted, publishable;
62 
63  public ProductIterator(IConstructor constructor, String context, String uniqueId) {
64  super(new FinancialsPU(), new ElephantMarker(constructor));
65  this.context = context;
66  this.uniqueId = uniqueId;
68  if(dhs != null) {
69  setSearchEngine(dhs);
70  }
71  }
72 
73  public boolean isExact() {
74  return exact;
75  }
76 
77  public void setExact(boolean exact) {
78  this.exact = exact;
79  }
80 
81  public void setAllowInterest(boolean allowInterest) {
82  this.allowInterest = allowInterest;
83  }
84 
85  public void setAllowComments(boolean allowComments) {
86  this.allowComments = allowComments;
87  }
88 
89  public void setAllowVotes(boolean allowVotes) {
90  this.allowVotes = allowVotes;
91  }
92 
93  public void setAllowGroups(boolean allowGroups) {
94  this.allowGroups = allowGroups;
95  }
96 
97  public boolean isShowAttachments() {
98  return showAttachments;
99  }
100 
101  public void setShowAttachments(boolean showAttachments) {
102  this.showAttachments = showAttachments;
103  }
104 
105  public boolean isShowDescriptions() {
106  return showDescriptions;
107  }
108 
109  public void setShowDescriptions(boolean showDescriptions) {
110  this.showDescriptions = showDescriptions;
111  }
112 
113  public boolean isOnlyPromoted() {
114  return onlyPromoted;
115  }
116 
117  public void setOnlyPromoted(boolean onlyPromoted) {
118  this.onlyPromoted = onlyPromoted;
119  }
120 
121  public boolean isPublishable() {
122  return publishable;
123  }
124 
125  public void setPublishable(boolean publishable) {
126  this.publishable = publishable;
127  }
128 
129  @Override
131  WhereClause wc = new WhereClause();
132  wc.addClause("select distinct p from Product as p");
133  addCriteria(wc);
134  wc.addClause("order by p.description");
135  return wc;
136  }
137 
138  @Override
140  WhereClause wc = new WhereClause();
141  wc.addClause("select count(distinct p) from Product as p");
142  addCriteria(wc);
143  return wc;
144  }
145 
146  public String getActualParameters() {
147  return Strings.isBlank(actualParameters) ? "?a=b" : actualParameters;
148  }
149 
150  @Override
151  protected void renderSummary(ElephantMarker marker, Product e, int page) {
152  prepareValues(e, page, false);
153  marker.process("product", getSummaryTemplate());
154  actualParameters = getPageLink(page);
155  }
156 
157  @Override
158  protected void renderItem(ElephantMarker marker, Product e, int page) {
159  prepareValues(e, page, false);
160  marker.process("product", getFullTemplate());
161  actualParameters = getPageLink(page);
162  }
163 
164  @Override
165  protected String entityRoot() {
166  return "product";
167  }
168 
169  @Override
170  protected Product entity(Long value) {
171  return dao.find(Product.class, value);
172  }
173 
174  private void addCriteria(WhereClause wc) {
175  boolean searching = false;
176  wc.addClause("where p.publishable = :publishable");
177  wc.addNamedValue("publishable", publishable);
178  if(isSearchOption()) {
180  if(dhs != null) {
181  DaoSearchKey dsk = dhs.get("search-value");
182  if(dsk != null) {
183  searching = dsk.applyToQuery(wc, Arrays.asList("p.description", "p.keywords"), true);
184  }
185  }
186  }
187  if(allowGroups) {
188  if(!searching) {
189  String selectedGroup = GroupItUtil.getSelectedGroup(constructor, ElephantContext.getRootWebPath() + context);
190  String selectedCategory = GroupItUtil.getCategory(constructor, ElephantContext.getRootWebPath() + context);
191  if(!Strings.isBlank(selectedGroup)) {
192  List<String> list = (List<String>) GroupItUtil.getPathsFromGroup("/product", selectedGroup, selectedCategory, exact);
193  if(list != null && !list.isEmpty()) {
194  wc.addClause("and p.id in (" + DaoUtil.getInClauseFromPaths(list, false) + ")");
195  } else {
196  wc.addClause("and 1=2");
197  }
198  }
199  } else {
200  String selectedCategory = GroupItUtil.getCategory(constructor, ElephantContext.getRootWebPath() + context);
201  if(!Strings.isBlank(selectedCategory)) {
202  List<String> list = (List<String>) GroupItUtil.getPathsFromCategory("/product", selectedCategory);
203  if(list != null && !list.isEmpty()) {
204  wc.addClause("and p.id in (" + DaoUtil.getInClauseFromPaths(list, false) + ")");
205  } else {
206  wc.addClause("and 1=2");
207  }
208  }
209  }
210  }
211  if(onlyPromoted) {
212  wc.addClause("and p.promoted = :promoted");
213  wc.addNamedValue("promoted", true);
214  }
215  }
216 
217  private void prepareValues(Product product, int page, boolean asMail) {
218  if(product == null) return;
219 
220  marker.put("product", product);
221 
222  if(asMail && context != null) {
223  marker.put("description", MailContact.createLink(product.getDescription(), getItemLink(product, page), false));
224  } else {
225  marker.put("description", product.getDescription());
226  }
227  if(context != null) {
228  marker.put("path", getItemLink(product, page));
229  marker.put("all", getPageLink(page));
230  }
231  if(!Strings.isBlank(context)) {
232  marker.put("readall", getItemLink(product, page));
233  }
234 
235  if(allowVotes) {
236  VoteItCtrl vic = new VoteItCtrl(constructor);
237  vic.setEntityPath(FinancialsPU.getObjectPath(product));
238  marker.put("vic", vic);
239  }
240  if(allowInterest) {
241  StarItCtrl sic = new StarItCtrl(constructor);
242  sic.setEntityPath(FinancialsPU.getObjectPath(product));
243  marker.put("sic", sic);
244  }
245  if(allowComments) {
246  CommentItCtrl cic = new CommentItCtrl(constructor);
247  cic.setEntityPath(FinancialsPU.getObjectPath(product));
248  marker.put("cic", cic);
249  }
250  if(showAttachments) {
251  AttachCtrl ac = new AttachCtrl(constructor);
252  ac.setPublicOnly(true);
253  ac.setEntityPath(FinancialsPU.getObjectPath(product));
254  marker.put("ac", ac);
255  }
256  if(showDescriptions) {
257  DescribeItCtrl dic = new DescribeItCtrl(constructor);
258  dic.setEntityPath(FinancialsPU.getObjectPath(product));
259  marker.put("dic", dic);
260  }
261 
262  FileAttach files = new FileAttach(FinancialsPU.getObjectPath(product));
263  marker.put("files", files);
264 
265  if(ShopContext.getInstance().isValid()) {
266  marker.put("cart", getCartExacton(product));
267  marker.put("stock", getCartStock(product));
268  marker.put("action", ElephantContext.getRootWebPath() + ShopContext.getInstance().getCartPath());
269  }
270  }
271 
272  private String getItemLink(Product product, int page) {
273  return ElephantContext.getRootWebPath() + context + "?item=" + product.getId() + "&page=" + page;
274  }
275 
276  private String getPageLink(int page) {
277  return ElephantContext.getRootWebPath() + context + "?page=" + page;
278  }
279 
280  private String getCartExacton(Product product) {
281  return ShoppingCart.getAddToCart(product.getId() + "", "1", product.getProductPrice() + "");
282  }
283 
284  private Double getCartStock(Product product) {
285  if(product.isService()) {
286  return 0.0;
287  }
288  return ShoppingCart.getStockForProduct(constructor, product);
289  }
290 
291  @Override
292  protected String title(Product e) {
293  return e.getDescription();
294  }
295 
296  @Override
297  protected Collection<String> metas(Product e) {
298  ArrayList<RepositoryFile> files = new ArrayList<>();
299  String path = getItemLink(e, 0);
300  if(!SocialImageMap.hasImage(path)) {
303  files.addAll(repository.getRepositoryFiles("*_social.png,*_social.jpg"));
304  files.addAll(repository.getRepositoryFiles("*.png,*.jpg"));
305  }
307  if(desc == null) desc = e.getKeywords();
308  SocialNet sn = new SocialNet(path, e.getDescription(), desc, files);
309  return sn.getMetas();
310  }
311 
312 }
static String descriptionString(String id, Object entity)
static String getContextVariable(IConstructor constructor)
void addNamedValue(String name, Object value)
static boolean hasImage(String url)
Repository getPublishableRepository(IConstructor constructor)
Definition: FileAttach.java:47
static String getObjectPath(Object object)
void renderSummary(ElephantMarker marker, Product e, int page)
void setShowDescriptions(boolean showDescriptions)
void renderItem(ElephantMarker marker, Product e, int page)
ProductIterator(IConstructor constructor, String context, String uniqueId)
void setSearchEngine(DaoHtmlSearch searchEngine)
static DaoHtmlSearch getInstance(IConstructor constructor, String context)
boolean applyToQuery(WhereClause wc, List< String > fields, boolean withSynonyms)
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)