BrightSide Workbench Full Report + Source Code
ProductContentIterator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.content;
20 
21 import java.io.Writer;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.List;
26 import org.turro.string.Strings;
27 import org.turro.attach.www.AttachCtrl;
28 import org.turro.describeit.DescribeItUtil;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.elephant.context.IConstructor;
31 import org.turro.elephant.db.WhereClause;
32 import org.turro.elephant.impl.repository.Repository;
33 import org.turro.elephant.impl.repository.RepositoryFile;
34 import org.turro.elephant.web.SocialImageMap;
35 import org.turro.elephant.web.SocialNet;
36 import org.turro.entities.Entities;
37 import org.turro.file.util.FileAttach;
38 import org.turro.financials.cart.ShopContext;
39 import org.turro.financials.cart.ShoppingCart;
40 import org.turro.financials.db.FinancialsPU;
41 import org.turro.financials.entity.Product;
42 import org.turro.groupit.GroupItUtil;
43 import org.turro.jpa.DaoUtil;
44 import org.turro.jpa.content.EntityDaoContentIterator;
45 import org.turro.jpa.search.DaoHtmlSearch;
46 import org.turro.jpa.search.DaoSearchKey;
47 import org.turro.marker.ElephantMarker;
48 import org.turro.plugin.contacts.IContact;
49 import org.turro.polls.PollsCtrl;
50 import org.turro.www.commentit.CommentItCtrl;
51 import org.turro.www.describeit.DescribeItCtrl;
52 import org.turro.www.starit.StarItCtrl;
53 import org.turro.www.voteit.VoteItCtrl;
54 
59 public class ProductContentIterator extends EntityDaoContentIterator<Product, Long> {
60 
61  private boolean exact, allowGroups, onlyPromoted, publishable, fullSearch;
62 
63  public ProductContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath) {
64  super(new FinancialsPU(), constructor, writer, contact, mail, pubPath);
65  }
66 
67  public boolean isExact() {
68  return exact;
69  }
70 
71  public void setExact(boolean exact) {
72  this.exact = exact;
73  }
74 
75  public void setAllowGroups(boolean allowGroups) {
76  this.allowGroups = allowGroups;
77  }
78 
79  public boolean isOnlyPromoted() {
80  return onlyPromoted;
81  }
82 
83  public void setOnlyPromoted(boolean onlyPromoted) {
84  this.onlyPromoted = onlyPromoted;
85  }
86 
87  public boolean isPublishable() {
88  return publishable;
89  }
90 
91  public void setPublishable(boolean publishable) {
92  this.publishable = publishable;
93  }
94 
95  public boolean isFullSearch() {
96  return fullSearch;
97  }
98 
99  public void setFullSearch(boolean fullSearch) {
100  this.fullSearch = fullSearch;
101  }
102 
103  @Override
105  WhereClause wc = new WhereClause();
106  wc.addClause("select distinct {p} from Product as p");
107  addCriteria(wc);
108  if(!wc.isUseNative()) wc.addClause("order by p.description");
109  wc.setResultClass(Product.class);
110  return wc;
111  }
112 
113  @Override
115  WhereClause wc = new WhereClause();
116  wc.addClause("select count({distinct p}) from Product as p");
117  addCriteria(wc);
118  return wc;
119  }
120 
121  @Override
122  protected void renderSummary(ElephantMarker marker, Product e, int page) {
123  if(e != null) {
124  marker.put("product", e);
125  prepareValues(e, page);
126  }
128  }
129 
130  @Override
131  protected void renderItem(ElephantMarker marker, Product e, int page) {
132  marker.put("product", e);
133  prepareValues(e, page);
135  }
136 
137  @Override
138  protected String entityRoot() {
139  return "product";
140  }
141 
142  @Override
143  protected Product entity(Long value) {
144  return dao.find(Product.class, value);
145  }
146 
147  @Override
148  protected boolean isValid(Product e) {
149  return super.isValid(e);
150  }
151 
152  private void addCriteria(WhereClause wc) {
153  boolean searching = false;
154  wc.addClause("where p.publishable = :publishable");
155  wc.addNamedValue("publishable", publishable);
156  wc.setUseNative(fullSearch);
157  if(isSearchOption()) {
159  if(dhs != null) {
160  DaoSearchKey dsk = dhs.get("search-value");
161  if(dsk != null) {
162  if(fullSearch) {
163  searching = dsk.applyToQueryFull(wc, "p.description, p.keywords", true);
164  } else {
165  searching = dsk.applyToQuery(wc, Arrays.asList("p.description", "p.keywords"), true);
166  }
167  }
168  }
169  }
170  if(allowGroups) {
171  if(!searching) {
172  String selectedGroup = GroupItUtil.getSelectedGroup(constructor, getContextPath());
173  String selectedCategory = GroupItUtil.getCategory(constructor, getContextPath());
174  if(!Strings.isBlank(selectedGroup)) {
175  List<String> list = (List<String>) GroupItUtil.getPathsFromGroup("/product", selectedGroup, selectedCategory, exact);
176  if(list != null && !list.isEmpty()) {
177  if(fullSearch) {
178  wc.addClause("and p.identifier in (" + DaoUtil.getInClauseFromPaths(list, false) + ")");
179  } else {
180  wc.addClause("and p.id in (" + DaoUtil.getInClauseFromPaths(list, false) + ")");
181  }
182  } else {
183  wc.addClause("and 1=2");
184  }
185  }
186  } else {
187  String selectedCategory = GroupItUtil.getCategory(constructor, getContextPath());
188  if(!Strings.isBlank(selectedCategory)) {
189  List<String> list = (List<String>) GroupItUtil.getPathsFromCategory("/product", selectedCategory);
190  if(list != null && !list.isEmpty()) {
191  if(fullSearch) {
192  wc.addClause("and p.identifier in (" + DaoUtil.getInClauseFromPaths(list, false) + ")");
193  } else {
194  wc.addClause("and p.id in (" + DaoUtil.getInClauseFromPaths(list, false) + ")");
195  }
196  } else {
197  wc.addClause("and 1=2");
198  }
199  }
200  }
201  }
202  if(onlyPromoted) {
203  wc.addClause("and p.promoted = :promoted");
204  wc.addNamedValue("promoted", true);
205  }
206  }
207 
208  private void prepareValues(Product product, int page) {
209  prepareControls(product, page);
210 
211  if(product == null) return;
212 
213  marker.put("product", product);
214 
215  if(ShopContext.getInstance().isValid()) {
216  marker.put("cart", getCartExacton(product));
217  marker.put("stock", getCartStock(product));
218  marker.put("action", ElephantContext.getRootWebPath() + ShopContext.getInstance().getCartPath());
219  }
220 
221  // Legacy
222  marker.put("description", product.getDescription());
223  if(!Strings.isBlank(getContextPath())) {
224  marker.put("path", getItemLink(product));
225  marker.put("all", getReadAllLink());
226  marker.put("readall", getItemLink(product));
227  }
228  }
229 
230  private String getCartExacton(Product product) {
231  return ShoppingCart.getAddToCart(product.getId() + "", "1", product.getProductPrice() + "");
232  }
233 
234  private Double getCartStock(Product product) {
235  if(product.isService()) {
236  return 0.0;
237  }
238  return ShoppingCart.getStockForProduct(constructor, product);
239  }
240 
241  @Override
242  protected String title(Product e) {
243  return e.getDescription();
244  }
245 
246  @Override
247  protected Collection<String> metas(Product e) {
248  ArrayList<RepositoryFile> files = new ArrayList<>();
249  String path = getItemLink(e);
250  if(!SocialImageMap.hasImage(path)) {
253  files.addAll(repository.getRepositoryFiles("*_social.png,*_social.jpg"));
254  files.addAll(repository.getRepositoryFiles("*.png,*.jpg"));
255  }
257  if(desc == null) desc = e.getKeywords();
258  SocialNet sn = new SocialNet(path, e.getDescription(), desc, files);
259  return sn.getMetas();
260  }
261 
262  @Override
263  protected String getTemplateRoot() {
264  return isMail() ? "content/newsletter/sections/products" : "product";
265  }
266 
267  @Override
268  protected Object doVotesCtrl(Product e) {
271  }
272 
273  @Override
274  protected Object doInterestCtrl(Product e) {
277  }
278 
279  @Override
280  protected Object doCommentsCtrl(Product e) {
283  }
284 
285  @Override
286  protected Object doAttachmentsCtrl(Product e) {
289  }
290 
291  @Override
292  protected Object doFilesCtrl(Product e) {
295  }
296 
297  @Override
298  protected Object doDescriptionsCtrl(Product e) {
301  }
302 
303  @Override
304  protected Object doPollsCtrl(Product e) {
307  }
308 
309  @Override
310  protected String getItemLink(Product e) {
311  return doItemLink(e, e.getId(), true);
312  }
313 
314  @Override
315  protected String getReadAllLink() {
316  String path = getContextPath();
317  if(Strings.isBlank(path)) {
318  return getRestrictedLink();
319  }
320  return path;
321  }
322 
323  @Override
324  protected String getRestrictedLink() {
325  return "/user/products";
326  }
327 
328 }
static String descriptionString(String id, Object entity)
static String getContextVariable(IConstructor constructor)
void setUseNative(boolean useNative)
void addNamedValue(String name, Object value)
void setResultClass(Class resultClass)
static boolean hasImage(String url)
static IElephantEntity getController(String path)
Definition: Entities.java:78
Repository getPublishableRepository(IConstructor constructor)
Definition: FileAttach.java:47
void renderItem(ElephantMarker marker, Product e, int page)
ProductContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath)
void renderSummary(ElephantMarker marker, Product e, int page)
static String getObjectPath(Object object)
String doItemLink(E entity, ID id, boolean obfuscated)
static DaoHtmlSearch getInstance(IConstructor constructor, String context)
boolean applyToQueryFull(WhereClause wc, String fields, boolean withSynonyms)
boolean applyToQuery(WhereClause wc, List< String > fields, boolean withSynonyms)
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)
Object configureCtrl(Object ctrl, IContact contact)