Created
August 20, 2013 12:14
-
-
Save davelnewton/6280618 to your computer and use it in GitHub Desktop.
Omg. How not to do it, followed by some sanitization. With emphasis on the sanity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BrandAction extends AbstractProductListAction { | |
private static final long serialVersionUID = 3647582541701870610L; | |
protected CatalogBrowsingFacade catalogBrowsingFacade; | |
protected RedirectHandler redirectHandler; | |
private static final String BRAND_CONSTANT="BRAND"; | |
private FormatStrategy formatStrategy; | |
@Override | |
public String execute() { | |
if (searchModel.getFreeTextCount() == 2) { | |
searchModel.setFreeTextCount(0); | |
} else { | |
searchModel.setFreeText(null); | |
((AbstractProductListWebModel)searchModel).setSearchText(null); | |
} | |
String key = null; | |
HttpServletRequest request = ServletActionContext.getRequest(); | |
Map params = request.getParameterMap(); | |
Iterator iter = params.keySet().iterator(); | |
String value = null; | |
int priceCount = 0; | |
int stockCount = 0; | |
StringBuffer parameters = new StringBuffer("?"); | |
while (iter.hasNext()) { | |
key = (String) iter.next(); | |
value = ((String[]) params.get(key))[0]; | |
parameters.append(key); | |
parameters.append('='); | |
parameters.append(value); | |
parameters.append('&'); | |
} | |
String returnURL = request.getRequestURL().toString() + "?" + key + "=" + value; | |
if (key == null && value == null) { | |
returnURL = request.getRequestURL().toString(); | |
} | |
searchModel.setJobListReturnTo(returnURL); | |
homeModel.setHomeRedirectUrl(returnURL); | |
searchModel.setBackToSearchUrl(request.getRequestURI().toString() + parameters.substring(0,parameters.length()-1)); | |
String brandCode = ((BrandWebModel)model).getBrandCode(); | |
if (homeModel.getSuccessMsg() != null) { | |
addActionMessage(homeModel.getSuccessMsg()); | |
homeModel.setSuccessMsg(null); | |
} | |
if(searchModel.getSuccessMsg() != null) { | |
addActionMessage(searchModel.getSuccessMsg()); | |
searchModel.setSuccessMsg(null); | |
} | |
model.setSearchText(searchModel.getFreeText()); | |
final SearchResultData searchResult = catalogBrowsingFacade.searchByBrand(brandCode); | |
if (configurationContext.getPropertyAsBoolean("hide.brand.LHN.austria")) { | |
List<Refinement> refinementList = new ArrayList<Refinement>(); | |
if (searchResult.getRefinements() != null) { | |
for (Refinement refineResult : searchResult.getRefinements()) { | |
if (!BRAND_CONSTANT.equals(refineResult.getType().toString())) { | |
refinementList.add(refineResult); | |
} | |
} | |
searchResult.setRefinements(refinementList); | |
} | |
} | |
if (searchResult.getProducts() != null) { | |
if (searchResult.getProducts().size() == 1) { | |
searchModel.setBackToSearchUrl(null); | |
} | |
} | |
if (model.getPersonalDetails() != null) { | |
if (model.getPersonalDetails().getPriceVisibility().getCode().equalsIgnoreCase("AllPrices")) { | |
if (searchResult!= null && searchResult.getProducts()!=null) { | |
int count=searchResult.getProducts().size(); | |
if (count>0) { | |
for (int j = 0; j < count; j++) { | |
searchResult.getProducts().get(j).setPriceVisibilityALL(true); | |
} | |
} | |
} | |
} | |
} | |
if (searchResult != null && searchResult.getProducts() != null) { | |
List<ProductData> productCurrentList = new ArrayList<ProductData>(); | |
List<ProductData> productNewList = new ArrayList<ProductData>(); | |
productCurrentList=searchResult.getProducts(); | |
for (ProductData product : productCurrentList) { | |
if (product.getLifeCyle() != null) { | |
if (!product.getLifeCyle().equals("Inactive")) { | |
productNewList.add(product); | |
} | |
} | |
} | |
searchResult.setProducts(productNewList); | |
} | |
if (searchResult != null && searchResult.getProducts() != null) { | |
for (ProductData prodData : searchResult.getProducts()) { | |
if (prodData.getStockData() != null && prodData.getStockData().isPriceServiceDown()) { | |
priceCount++; | |
} | |
if (prodData.getStockData() != null && prodData.getStockData().isStockServiceDown()) { | |
stockCount++; | |
} | |
} | |
if ((searchResult.getProducts().size() == priceCount) && priceCount != 0) { | |
priceCount = 0; | |
addActionError(getText("error.price.display.price.notretrived")); | |
} | |
if ((searchResult.getProducts().size() == stockCount) && stockCount != 0) { | |
stockCount = 0; | |
addActionError(getText("error.price.display.stock.price.notretrived")); | |
} | |
} | |
processResult(searchResult); | |
searchModel.setSearchResult(searchResult); | |
calculateTDK(); | |
BrandData brandData = catalogBrowsingFacade.getBrand(brandCode); | |
if (brandData != null && brandData.getCode() != null) { | |
((BrandWebModel)model).setLargePictureURL(brandData.getLargePictureUrl()); | |
((BrandWebModel)model).setDescription(brandData.getDescription()); | |
((BrandWebModel)model).setName(brandData.getName()); | |
} | |
if (searchResult.getRedirectPage() != null) { | |
String redirectTo = redirectHandler.handleResultPageCode(searchResult.getRedirectPage()); | |
if (redirectTo.equalsIgnoreCase("productPage")) { | |
return redirectTo; | |
} else { | |
return Action.SUCCESS; | |
} | |
} | |
if (configurationContext.getPropertyAsBoolean("redirectToNo.searchresult.page")) { | |
if (searchResult == null || searchResult.getProducts() == null || (searchResult.getProducts()!=null && searchResult.getProducts().isEmpty())) { | |
return "noProductsFound"; | |
} | |
} | |
return Action.SUCCESS; | |
} | |
public void setCatalogBrowsingFacade(CatalogBrowsingFacade catalogBrowsingFacade) { | |
this.catalogBrowsingFacade = catalogBrowsingFacade; | |
} | |
public void setFormatStrategy(final FormatStrategy formatStrategy) { | |
this.formatStrategy = formatStrategy; | |
} | |
public void setRedirectHandler(RedirectHandler redirectHandler) { | |
this.redirectHandler = redirectHandler; | |
} | |
protected HomeWebModel homeModel; | |
public HomeWebModel getHomeModel() { | |
return homeModel; | |
} | |
public void setHomeModel(final HomeWebModel homeModel) { | |
this.homeModel = homeModel; | |
} | |
protected SearchWebModel searchModel; | |
public SearchWebModel getSearchModel() { | |
return searchModel; | |
} | |
public void setSearchModel(SearchWebModel searchModel) { | |
this.searchModel = searchModel; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment