1 package org.paneris.jammyjoes.controller; 2 3 import java.util.List; 4 5 import org.melati.util.StringUtils; 6 import org.paneris.jammyjoes.model.Product; 7 8 public class SimilarSearchCriteria implements SearchCriteria { 9 10 Product similarToMe; 11 12 public SimilarSearchCriteria(Product similarToMe) { 13 this.similarToMe = similarToMe; 14 } 15 16 public void constrainOutOfStock(List constraints) { 17 constraints.add(q("stocklevel") + " > 0"); 18 } 19 20 public void constrainPrice(List constraints) { 21 constraints.add(q("retailpriceincvat") + " <= " + similarToMe.getRetailpriceincvat().doubleValue() * 1.1); 22 } 23 24 public void constrainType(List constraints) { 25 String type = similarToMe.getType().getTroid().toString(); 26 constraints.add("(" + q("type") + " = " + type + " OR " + q("type2") + " = " + type + ")"); 27 } 28 29 public void constrainId(List constraints) { 30 constraints.add(q("id") + " != " + similarToMe.getTroid()); 31 } 32 33 public String q(String name) { 34 StringBuffer b = new StringBuffer(); 35 StringUtils.appendQuoted(b, name, '"'); 36 return b.toString(); 37 } 38 39 public void constrain(List constraints) { 40 constrainOutOfStock(constraints); 41 constrainPrice(constraints); 42 constrainType(constraints); 43 constrainId(constraints); 44 constrainDicontinued(constraints); 45 } 46 47 public void constrainDicontinued(List constraints) { 48 constraints.add(q("status") + " != " + similarToMe.getJammyjoesDatabaseTables().getProductStatusTable().getDiscontinued().getTroid()); 49 } 50 51 public String order() { 52 return (q("retailpriceincvat")) + " desc"; 53 } 54 55 }