1 package com.dotmarketing.portlets.discountcode.struts; 2 3 import java.text.SimpleDateFormat ; 4 5 import javax.portlet.ActionRequest; 6 import javax.servlet.http.HttpServletRequest ; 7 8 import org.apache.struts.Globals; 9 import org.apache.struts.action.ActionErrors; 10 import org.apache.struts.action.ActionMapping; 11 import org.apache.struts.action.ActionMessage; 12 import org.apache.struts.validator.ValidatorForm; 13 14 import com.dotmarketing.util.UtilMethods; 15 import com.dotmarketing.util.WebKeys; 16 import com.liferay.portal.util.Constants; 17 import com.dotmarketing.portlets.discountcode.factories.DiscountCodeFactory; 18 import com.dotmarketing.portlets.discountcode.model.DiscountCode; 19 20 public class DiscountCodeForm extends ValidatorForm { 21 22 private static final long serialVersionUID = 1L; 23 24 private long inode; private int discountType; private String startDate; private String endDate; private String codeId; private String codeDescription; private boolean freeShipping; private boolean noBulkDisc; private float discountAmount; private int minOrder; private String orderby; 36 private String direction; 37 38 39 private String [] products; 40 41 public DiscountCodeForm() { 42 } 43 44 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { 45 if(request.getParameter("cmd")!=null && request.getParameter("cmd").equals(Constants.ADD)) { 46 return super.validate(mapping, request); 47 } 48 return null; 49 } 50 51 public String getCodeDescription() { 52 return codeDescription; 53 } 54 55 public void setCodeDescription(String codeDescription) { 56 this.codeDescription = codeDescription; 57 } 58 59 public String getCodeId() { 60 return codeId; 61 } 62 63 public void setCodeId(String codeId) { 64 this.codeId = codeId; 65 } 66 67 public float getDiscountAmount() { 68 return discountAmount; 69 } 70 71 public void setDiscountAmount(float discountAmount) { 72 this.discountAmount = discountAmount; 73 } 74 75 public int getDiscountType() { 76 return discountType; 77 } 78 79 public void setDiscountType(int discountType) { 80 this.discountType = discountType; 81 } 82 83 public String getEndDate() { 84 return endDate; 85 } 86 87 public void setEndDate(String endDate) { 88 this.endDate = endDate; 89 } 90 91 public boolean isFreeShipping() { 92 return freeShipping; 93 } 94 95 public void setFreeShipping(boolean freeShipping) { 96 this.freeShipping = freeShipping; 97 } 98 99 public long getInode() { 100 return inode; 101 } 102 103 public void setInode(long inode) { 104 this.inode = inode; 105 } 106 107 public int getMinOrder() { 108 return minOrder; 109 } 110 111 public void setMinOrder(int minOrder) { 112 this.minOrder = minOrder; 113 } 114 115 public boolean isNoBulkDisc() { 116 return noBulkDisc; 117 } 118 119 public void setNoBulkDisc(boolean noBulkDisc) { 120 this.noBulkDisc = noBulkDisc; 121 } 122 123 public String [] getProducts() { 124 return products; 125 } 126 127 public void setProducts(String [] products) { 128 this.products = products; 129 } 130 131 public String getStartDate() { 132 return startDate; 133 } 134 135 public void setStartDate(String startDate) { 136 this.startDate = startDate; 137 } 138 139 public String getDirection() { 140 return direction; 141 } 142 143 public void setDirection(String direction) { 144 this.direction = direction; 145 } 146 147 public String getOrderby() { 148 return orderby; 149 } 150 151 public void setOrderby(String orderby) { 152 this.orderby = orderby; 153 } 154 155 public ActionErrors validateEdit(ActionMapping mapping,ActionRequest req) 156 { 157 ActionErrors ae = new ActionErrors(); 158 SimpleDateFormat df = new SimpleDateFormat (WebKeys.DateFormats.SHORTDATE); 159 if(!UtilMethods.isSet(codeId)) 160 { 161 ae.add(Globals.ERROR_KEY,new ActionMessage("error.form.mandatory","Discount Code Id")); 162 } 163 if(!UtilMethods.isSet(codeDescription)) 164 { 165 ae.add(Globals.ERROR_KEY,new ActionMessage("error.form.mandatory","Code Description")); 166 } 167 if(UtilMethods.isSet(startDate)) 168 { 169 try 170 { 171 df.parse(startDate); 172 } 173 catch(Exception ex) 174 { 175 ae.add(Globals.ERROR_KEY,new ActionMessage("error.form.format","Start Date")); 176 } 177 } 178 if(UtilMethods.isSet(endDate)) 179 { 180 try 181 { 182 df.parse(endDate); 183 } 184 catch(Exception ex) 185 { 186 ae.add(Globals.ERROR_KEY,new ActionMessage("error.form.format","End Date")); 187 } 188 } 189 if (discountType == 0) 191 { 192 ae.add(Globals.ERROR_KEY,new ActionMessage("error.form.mandatory","Discount Type")); 193 } 194 195 try 197 { 198 float discountAmountAux = Float.parseFloat(req.getParameter("discountAmount")); 199 if (Integer.toString(discountType).equals(WebKeys.DISCOUNTCODE_PERCENTAGE) && 200 (discountAmountAux < 0 || 201 discountAmountAux > 100)) 202 { 203 ae.add(Globals.ERROR_KEY,new ActionMessage("error.form.format","Discount Amount")); 204 } 205 } 206 catch(Exception ex) 207 { 208 ae.add(Globals.ERROR_KEY,new ActionMessage("error.form.format","Discount Amount")); 209 } 210 try 212 { 213 int minOrderAux = Integer.parseInt(req.getParameter("minOrder")); 214 if (minOrderAux < 0) 215 { 216 ae.add(Globals.ERROR_KEY,new ActionMessage("error.form.format","Minimum Order")); 217 } 218 } 219 catch(Exception ex) 220 { 221 ae.add(Globals.ERROR_KEY,new ActionMessage("error.form.format","Minimum Order")); 222 } 223 224 225 DiscountCode discountCodeAux = DiscountCodeFactory.getDiscountCodeById(codeId); 227 if (discountCodeAux.getInode() != 0 && discountCodeAux.getInode() != inode) 228 { 229 ae.add(Globals.ERROR_KEY,new ActionMessage("error.form.unique","Discount Code ID")); 230 } 231 232 return ae; 233 } 234 235 236 } 237 | Popular Tags |