19 package org.turro.financials.product.www;
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;
59 private String context, actualParameters, uniqueId;
60 private boolean exact, allowInterest, allowComments, allowVotes, allowGroups,
61 showDescriptions, showAttachments, onlyPromoted, publishable;
65 this.context = context;
66 this.uniqueId = uniqueId;
82 this.allowInterest = allowInterest;
86 this.allowComments = allowComments;
90 this.allowVotes = allowVotes;
94 this.allowGroups = allowGroups;
98 return showAttachments;
102 this.showAttachments = showAttachments;
106 return showDescriptions;
110 this.showDescriptions = showDescriptions;
118 this.onlyPromoted = onlyPromoted;
126 this.publishable = publishable;
132 wc.
addClause(
"select distinct p from Product as p");
141 wc.
addClause(
"select count(distinct p) from Product as p");
147 return Strings.isBlank(actualParameters) ?
"?a=b" : actualParameters;
152 prepareValues(e, page,
false);
154 actualParameters = getPageLink(page);
159 prepareValues(e, page,
false);
161 actualParameters = getPageLink(page);
175 boolean searching =
false;
176 wc.
addClause(
"where p.publishable = :publishable");
183 searching = dsk.
applyToQuery(wc, Arrays.asList(
"p.description",
"p.keywords"),
true);
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) +
")");
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) +
")");
212 wc.
addClause(
"and p.promoted = :promoted");
217 private void prepareValues(Product product,
int page,
boolean asMail) {
218 if(product ==
null)
return;
222 if(asMail && context !=
null) {
223 marker.
put(
"description", MailContact.createLink(product.getDescription(), getItemLink(product, page),
false));
225 marker.
put(
"description", product.getDescription());
227 if(context !=
null) {
228 marker.
put(
"path", getItemLink(product, page));
231 if(!Strings.isBlank(context)) {
232 marker.
put(
"readall", getItemLink(product, page));
237 vic.setEntityPath(FinancialsPU.getObjectPath(product));
242 sic.setEntityPath(FinancialsPU.getObjectPath(product));
246 CommentItCtrl cic =
new CommentItCtrl(
constructor);
247 cic.setEntityPath(FinancialsPU.getObjectPath(product));
250 if(showAttachments) {
252 ac.setPublicOnly(
true);
253 ac.setEntityPath(FinancialsPU.getObjectPath(product));
256 if(showDescriptions) {
257 DescribeItCtrl dic =
new DescribeItCtrl(
constructor);
258 dic.setEntityPath(FinancialsPU.getObjectPath(product));
262 FileAttach files =
new FileAttach(FinancialsPU.getObjectPath(product));
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());
272 private String getItemLink(Product product,
int page) {
273 return ElephantContext.getRootWebPath() + context +
"?item=" + product.getId() +
"&page=" + page;
276 private String getPageLink(
int page) {
277 return ElephantContext.getRootWebPath() + context +
"?page=" + page;
280 private String getCartExacton(Product product) {
281 return ShoppingCart.getAddToCart(product.getId() +
"",
"1", product.getProductPrice() +
"");
284 private Double getCartStock(Product product) {
285 if(product.isService()) {
288 return ShoppingCart.getStockForProduct(
constructor, product);
298 ArrayList<RepositoryFile> files =
new ArrayList<>();
299 String path = getItemLink(e, 0);
static String descriptionString(String id, Object entity)
static final String DEFAULT_ID
static String getContextVariable(IConstructor constructor)
void addClause(String clause)
void addNamedValue(String name, Object value)
Set< RepositoryFile > getRepositoryFiles(String pattern)
static boolean hasImage(String url)
List< String > getMetas()
Repository getPublishableRepository(IConstructor constructor)
static String getObjectPath(Object object)
Collection< String > metas(Product e)
void setAllowVotes(boolean allowVotes)
void renderSummary(ElephantMarker marker, Product e, int page)
void setOnlyPromoted(boolean onlyPromoted)
WhereClause getWhereClause()
WhereClause getCountClause()
void setShowDescriptions(boolean showDescriptions)
void setAllowComments(boolean allowComments)
Product entity(Long value)
void setAllowGroups(boolean allowGroups)
void setShowAttachments(boolean showAttachments)
boolean isShowAttachments()
void setAllowInterest(boolean allowInterest)
void renderItem(ElephantMarker marker, Product e, int page)
String getActualParameters()
void setExact(boolean exact)
boolean isShowDescriptions()
void setPublishable(boolean publishable)
ProductIterator(IConstructor constructor, String context, String uniqueId)
String getSummaryTemplate()
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)