1 24 package org.ofbiz.product.product; 25 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import org.ofbiz.base.util.Debug; 30 import org.ofbiz.base.util.UtilDateTime; 31 import org.ofbiz.base.util.UtilMisc; 32 import org.ofbiz.base.util.UtilValidate; 33 import org.ofbiz.base.util.UtilProperties; 34 import org.ofbiz.base.util.UtilHttp; 35 import org.ofbiz.entity.GenericDelegator; 36 import org.ofbiz.entity.GenericEntityException; 37 import org.ofbiz.entity.GenericValue; 38 import org.ofbiz.entity.transaction.GenericTransactionException; 39 import org.ofbiz.entity.transaction.TransactionUtil; 40 import org.ofbiz.security.Security; 41 42 import java.util.Map ; 43 44 51 public class VariantEvents { 52 53 public static final String module = VariantEvents.class.getName(); 54 public static final String resource = "ProductUiLabels"; 55 56 61 public static String quickAddChosenVariant(HttpServletRequest request, HttpServletResponse response) { 62 String errMsg = ""; 63 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 64 Security security = (Security) request.getAttribute("security"); 65 66 String productId = request.getParameter("productId"); 67 String variantProductId = request.getParameter("variantProductId"); 68 String featureTypeSizeStr = request.getParameter("featureTypeSize"); 69 70 if (UtilValidate.isEmpty(productId)) { 71 errMsg = UtilProperties.getMessage(resource,"variantevents.productId_required_but_missing", UtilHttp.getLocale(request)); 72 request.setAttribute("_ERROR_MESSAGE_", errMsg); 73 return "error"; 74 } 75 76 if (UtilValidate.isEmpty(variantProductId)) { 77 errMsg = UtilProperties.getMessage(resource,"variantevents.variantProductId_required_but_missing_enter_an_id", UtilHttp.getLocale(request)); 78 request.setAttribute("_ERROR_MESSAGE_", errMsg); 79 return "error"; 80 } 81 82 int featureTypeSize = 0; 83 84 try { 85 featureTypeSize = Integer.parseInt(featureTypeSizeStr); 86 } catch (NumberFormatException e) { 87 Map messageMap = UtilMisc.toMap("featureTypeSizeStr", featureTypeSizeStr); 88 errMsg = UtilProperties.getMessage(resource,"variantevents.featureTypeSize_not_number", messageMap, UtilHttp.getLocale(request)); 89 request.setAttribute("_ERROR_MESSAGE_", errMsg); 90 return "error"; 91 } 92 93 try { 94 boolean beganTransacton = TransactionUtil.begin(); 95 96 try { 97 GenericValue product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId)); 99 if (product == null) { 100 Map messageMap = UtilMisc.toMap("productId", productId); 101 errMsg = UtilProperties.getMessage(resource,"variantevents.product_not_found_with_ID", messageMap, UtilHttp.getLocale(request)); 102 TransactionUtil.rollback(beganTransacton, errMsg, null); 103 request.setAttribute("_ERROR_MESSAGE_", errMsg); 104 return "error"; 105 } 106 107 GenericValue variantProduct = delegator.findByPrimaryKey("Product",UtilMisc.toMap("productId", variantProductId)); 109 if (variantProduct == null) { 110 variantProduct = GenericValue.create(product); 112 variantProduct.set("productId", variantProductId); 113 variantProduct.set("isVirtual", "N"); 114 variantProduct.set("isVariant", "Y"); 115 variantProduct.set("primaryProductCategoryId", null); 116 variantProduct.create(); 118 } else { 119 variantProduct.set("isVirtual", "N"); 121 variantProduct.set("isVariant", "Y"); 122 variantProduct.set("primaryProductCategoryId", null); 123 variantProduct.store(); 125 } 126 127 GenericValue productAssoc = delegator.makeValue("ProductAssoc", 129 UtilMisc.toMap("productId", productId, "productIdTo", variantProductId, 130 "productAssocTypeId", "PRODUCT_VARIANT", "fromDate", UtilDateTime.nowTimestamp())); 131 productAssoc.create(); 132 133 for (int i = 0; i < featureTypeSize; i++) { 135 String productFeatureId = request.getParameter("feature_" + i); 136 if (productFeatureId == null) { 137 Map messageMap = UtilMisc.toMap("i", Integer.toString(i)); 138 errMsg = UtilProperties.getMessage(resource,"variantevents.productFeatureId_for_feature_type_number_not_found", messageMap, UtilHttp.getLocale(request)); 139 TransactionUtil.rollback(beganTransacton, errMsg, null); 140 request.setAttribute("_ERROR_MESSAGE_", errMsg); 141 return "error"; 142 } 143 144 GenericValue productFeature = delegator.findByPrimaryKey("ProductFeature", UtilMisc.toMap("productFeatureId", productFeatureId)); 145 146 GenericValue productFeatureAppl = delegator.makeValue("ProductFeatureAppl", 147 UtilMisc.toMap("productId", variantProductId, "productFeatureId", productFeatureId, 148 "productFeatureApplTypeId", "STANDARD_FEATURE", "fromDate", UtilDateTime.nowTimestamp())); 149 150 if (productFeature != null) { 152 productFeatureAppl.set("sequenceNum", productFeature.get("defaultSequenceNum")); 153 } 154 155 productFeatureAppl.create(); 156 } 157 158 TransactionUtil.commit(beganTransacton); 159 } catch (GenericEntityException e) { 160 Map messageMap = UtilMisc.toMap("errMessage", e.toString()); 161 errMsg = UtilProperties.getMessage(resource,"variantevents.entity_error_quick_add_variant_data", messageMap, UtilHttp.getLocale(request)); 162 TransactionUtil.rollback(beganTransacton, errMsg, null); 163 Debug.logError(e, "Entity error creating quick add variant data", module); 164 request.setAttribute("_ERROR_MESSAGE_", errMsg); 165 return "error"; 166 } 167 } catch (GenericTransactionException e) { 168 Debug.logError(e, "Transaction error creating quick add variant data", module); 169 Map messageMap = UtilMisc.toMap("errMessage", e.toString()); 170 errMsg = UtilProperties.getMessage(resource,"variantevents.transaction_error_quick_add_variant_data", messageMap, UtilHttp.getLocale(request)); 171 request.setAttribute("_ERROR_MESSAGE_", errMsg); 172 return "error"; 173 } 174 175 Map messageMap = UtilMisc.toMap("variantProductId", variantProductId); 176 String sucMsg = UtilProperties.getMessage(resource,"variantevents.successfully_created_variant_product_with_id", messageMap, UtilHttp.getLocale(request)); 177 request.setAttribute("_EVENT_MESSAGE_", sucMsg); 178 return "success"; 179 } 180 } 181 | Popular Tags |