Search in sources :

Example 26 with GenericEntityException

use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.

the class ProductEvents method updateProductQuickAdminShipping.

public static String updateProductQuickAdminShipping(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
    GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
    String variantProductId = request.getParameter("productId0");
    boolean applyToAll = (request.getParameter("applyToAll") != null);
    try {
        boolean beganTransaction = TransactionUtil.begin();
        try {
            // check for variantProductId - this will mean that we have multiple ship info to update
            if (variantProductId == null) {
                // only single product to update
                String productId = request.getParameter("productId");
                GenericValue product = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
                product.set("lastModifiedDate", nowTimestamp);
                product.setString("lastModifiedByUserLogin", userLogin.getString("userLoginId"));
                try {
                    product.set("productHeight", parseBigDecimalForEntity(request.getParameter("productHeight")));
                    product.set("productWidth", parseBigDecimalForEntity(request.getParameter("productWidth")));
                    product.set("productDepth", parseBigDecimalForEntity(request.getParameter("productDepth")));
                    product.set("productWeight", parseBigDecimalForEntity(request.getParameter("weight")));
                    // default unit settings for shipping parameters
                    product.set("heightUomId", "LEN_in");
                    product.set("widthUomId", "LEN_in");
                    product.set("depthUomId", "LEN_in");
                    product.set("weightUomId", "WT_oz");
                    BigDecimal floz = parseBigDecimalForEntity(request.getParameter("~floz"));
                    BigDecimal ml = parseBigDecimalForEntity(request.getParameter("~ml"));
                    BigDecimal ntwt = parseBigDecimalForEntity(request.getParameter("~ntwt"));
                    BigDecimal grams = parseBigDecimalForEntity(request.getParameter("~grams"));
                    List<GenericValue> currentProductFeatureAndAppls = EntityQuery.use(delegator).from("ProductFeatureAndAppl").where("productId", productId, "productFeatureApplTypeId", "STANDARD_FEATURE").filterByDate().queryList();
                    setOrCreateProdFeature(delegator, productId, currentProductFeatureAndAppls, "VLIQ_ozUS", "AMOUNT", floz);
                    setOrCreateProdFeature(delegator, productId, currentProductFeatureAndAppls, "VLIQ_ml", "AMOUNT", ml);
                    setOrCreateProdFeature(delegator, productId, currentProductFeatureAndAppls, "WT_g", "AMOUNT", grams);
                    setOrCreateProdFeature(delegator, productId, currentProductFeatureAndAppls, "WT_oz", "AMOUNT", ntwt);
                    product.store();
                } catch (NumberFormatException nfe) {
                    String errMsg = "Shipping Dimensions and Weights must be numbers.";
                    request.setAttribute("_ERROR_MESSAGE_", errMsg);
                    Debug.logError(nfe, errMsg, module);
                    return "error";
                }
            } else {
                // multiple products, so use a numeric suffix to get them all
                int prodIdx = 0;
                int attribIdx = 0;
                String productId = variantProductId;
                do {
                    GenericValue product = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
                    try {
                        product.set("productHeight", parseBigDecimalForEntity(request.getParameter("productHeight" + attribIdx)));
                        product.set("productWidth", parseBigDecimalForEntity(request.getParameter("productWidth" + attribIdx)));
                        product.set("productDepth", parseBigDecimalForEntity(request.getParameter("productDepth" + attribIdx)));
                        product.set("productWeight", parseBigDecimalForEntity(request.getParameter("weight" + attribIdx)));
                        BigDecimal floz = parseBigDecimalForEntity(request.getParameter("~floz" + attribIdx));
                        BigDecimal ml = parseBigDecimalForEntity(request.getParameter("~ml" + attribIdx));
                        BigDecimal ntwt = parseBigDecimalForEntity(request.getParameter("~ntwt" + attribIdx));
                        BigDecimal grams = parseBigDecimalForEntity(request.getParameter("~grams" + attribIdx));
                        List<GenericValue> currentProductFeatureAndAppls = EntityQuery.use(delegator).from("ProductFeatureAndAppl").where("productId", productId, "productFeatureApplTypeId", "STANDARD_FEATURE").filterByDate().queryList();
                        setOrCreateProdFeature(delegator, productId, currentProductFeatureAndAppls, "VLIQ_ozUS", "AMOUNT", floz);
                        setOrCreateProdFeature(delegator, productId, currentProductFeatureAndAppls, "VLIQ_ml", "AMOUNT", ml);
                        setOrCreateProdFeature(delegator, productId, currentProductFeatureAndAppls, "WT_g", "AMOUNT", grams);
                        setOrCreateProdFeature(delegator, productId, currentProductFeatureAndAppls, "WT_oz", "AMOUNT", ntwt);
                        product.store();
                    } catch (NumberFormatException nfe) {
                        String errMsg = "Shipping Dimensions and Weights must be numbers.";
                        request.setAttribute("_ERROR_MESSAGE_", errMsg);
                        Debug.logError(nfe, errMsg, module);
                        return "error";
                    }
                    prodIdx++;
                    if (!applyToAll) {
                        attribIdx = prodIdx;
                    }
                    productId = request.getParameter("productId" + prodIdx);
                } while (productId != null);
            }
            TransactionUtil.commit(beganTransaction);
        } catch (GenericEntityException e) {
            String errMsg = "Error updating quick admin shipping settings: " + e.toString();
            Debug.logError(e, errMsg, module);
            request.setAttribute("_ERROR_MESSAGE_", errMsg);
            TransactionUtil.rollback(beganTransaction, errMsg, e);
            return "error";
        }
    } catch (GenericTransactionException gte) {
        String errMsg = "Error updating quick admin shipping settings: " + gte.toString();
        Debug.logError(gte, errMsg, module);
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    return "success";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal)

Example 27 with GenericEntityException

use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.

the class ProductEvents method updateProductCategoryMember.

public static String updateProductCategoryMember(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    String productId = request.getParameter("productId");
    String productCategoryId = request.getParameter("productCategoryId");
    String thruDate = request.getParameter("thruDate");
    if ((thruDate == null) || (thruDate.trim().length() == 0)) {
        thruDate = UtilDateTime.nowTimestamp().toString();
    }
    try {
        List<GenericValue> prodCatMembs = EntityQuery.use(delegator).from("ProductCategoryMember").where("productCategoryId", productCategoryId, "productId", productId).filterByDate().queryList();
        if (prodCatMembs.size() > 0) {
            // there is one to modify
            GenericValue prodCatMemb = prodCatMembs.get(0);
            prodCatMemb.setString("thruDate", thruDate);
            prodCatMemb.store();
        }
    } catch (GenericEntityException e) {
        String errMsg = "Error adding to category: " + e.toString();
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    return "success";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 28 with GenericEntityException

use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.

the class ProductEvents method removeProductFromComparisonList.

public static String removeProductFromComparisonList(HttpServletRequest request, HttpServletResponse response) {
    HttpSession session = request.getSession();
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    String productId = request.getParameter("productId");
    GenericValue product = null;
    if (UtilValidate.isNotEmpty(productId)) {
        try {
            product = ProductWorker.findProduct(delegator, productId);
        } catch (GenericEntityException e) {
            productId = null;
            Debug.logError(e, module);
        }
    }
    if (product == null) {
        String errMsg = UtilProperties.getMessage(resource, "productevents.product_with_id_not_found", UtilMisc.toMap("productId", productId), UtilHttp.getLocale(request));
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    List<GenericValue> compareList = getProductCompareList(request);
    Iterator<GenericValue> it = compareList.iterator();
    while (it.hasNext()) {
        GenericValue compProduct = it.next();
        if (product.getString("productId").equals(compProduct.getString("productId"))) {
            it.remove();
            break;
        }
    }
    session.setAttribute("productCompareList", compareList);
    String productName = ProductContentWrapper.getProductContentAsText(product, "PRODUCT_NAME", request, "html");
    String eventMsg = UtilProperties.getMessage("ProductUiLabels", "ProductRemoveFromCompareListSuccess", UtilMisc.toMap("name", productName), UtilHttp.getLocale(request));
    request.setAttribute("_EVENT_MESSAGE_", eventMsg);
    return "success";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) HttpSession(javax.servlet.http.HttpSession) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 29 with GenericEntityException

use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.

the class ProductEvents method updateAllKeywords.

/**
 * Updates/adds keywords for all products
 *
 * @param request HTTPRequest object for the current request
 * @param response HTTPResponse object for the current request
 * @return String specifying the exit status of this event
 */
public static String updateAllKeywords(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    Security security = (Security) request.getAttribute("security");
    Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
    String updateMode = "CREATE";
    String errMsg = null;
    String doAll = request.getParameter("doAll");
    // check permissions before moving on...
    if (!security.hasEntityPermission("CATALOG", "_" + updateMode, request.getSession())) {
        Map<String, String> messageMap = UtilMisc.toMap("updateMode", updateMode);
        errMsg = UtilProperties.getMessage(resource, "productevents.not_sufficient_permissions", messageMap, UtilHttp.getLocale(request));
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    EntityCondition condition = null;
    if (!"Y".equals(doAll)) {
        List<EntityCondition> condList = new LinkedList<>();
        condList.add(EntityCondition.makeCondition(EntityCondition.makeCondition("autoCreateKeywords", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("autoCreateKeywords", EntityOperator.NOT_EQUAL, "N")));
        if ("true".equals(EntityUtilProperties.getPropertyValue("prodsearch", "index.ignore.variants", delegator))) {
            condList.add(EntityCondition.makeCondition(EntityCondition.makeCondition("isVariant", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("isVariant", EntityOperator.NOT_EQUAL, "Y")));
        }
        if ("true".equals(EntityUtilProperties.getPropertyValue("prodsearch", "index.ignore.discontinued.sales", delegator))) {
            condList.add(EntityCondition.makeCondition(EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.GREATER_THAN_EQUAL_TO, nowTimestamp)));
        }
        condition = EntityCondition.makeCondition(condList, EntityOperator.AND);
    } else {
        condition = EntityCondition.makeCondition(EntityCondition.makeCondition("autoCreateKeywords", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("autoCreateKeywords", EntityOperator.NOT_EQUAL, "N"));
    }
    int numProds = 0;
    int errProds = 0;
    boolean beganTx = false;
    try {
        // begin the transaction
        beganTx = TransactionUtil.begin(7200);
    } catch (GenericTransactionException gte) {
        Debug.logError(gte, "Unable to begin transaction", module);
    }
    try (EntityListIterator entityListIterator = EntityQuery.use(delegator).from("Product").where(condition).queryIterator()) {
        try {
            if (Debug.infoOn()) {
                long count = EntityQuery.use(delegator).from("Product").where(condition).queryCount();
                Debug.logInfo("========== Found " + count + " products to index ==========", module);
            }
        } catch (GenericEntityException gee) {
            Debug.logWarning(gee, gee.getMessage(), module);
            Map<String, String> messageMap = UtilMisc.toMap("gee", gee.toString());
            errMsg = UtilProperties.getMessage(resource, "productevents.error_getting_product_list", messageMap, UtilHttp.getLocale(request));
            request.setAttribute("_ERROR_MESSAGE_", errMsg);
            return "error";
        }
        GenericValue product;
        while ((product = entityListIterator.next()) != null) {
            try {
                KeywordIndex.indexKeywords(product, "Y".equals(doAll));
            } catch (GenericEntityException e) {
                Debug.logWarning("[ProductEvents.updateAllKeywords] Could not create product-keyword (write error); message: " + e.getMessage(), module);
                errProds++;
            }
            numProds++;
            if (numProds % 500 == 0) {
                Debug.logInfo("Keywords indexed for " + numProds + " so far", module);
            }
        }
    } catch (GenericEntityException e) {
        try {
            TransactionUtil.rollback(beganTx, e.getMessage(), e);
        } catch (GenericTransactionException e1) {
            Debug.logError(e1, module);
        }
        return "error";
    } catch (Throwable t) {
        Debug.logError(t, module);
        request.setAttribute("_ERROR_MESSAGE_", t.getMessage());
        try {
            TransactionUtil.rollback(beganTx, t.getMessage(), t);
        } catch (GenericTransactionException e2) {
            Debug.logError(e2, module);
        }
        return "error";
    }
    // commit the transaction
    try {
        TransactionUtil.commit(beganTx);
    } catch (GenericTransactionException e) {
        Debug.logError(e, module);
    }
    if (errProds == 0) {
        Map<String, String> messageMap = UtilMisc.toMap("numProds", Integer.toString(numProds));
        errMsg = UtilProperties.getMessage(resource, "productevents.keyword_creation_complete_for_products", messageMap, UtilHttp.getLocale(request));
        request.setAttribute("_EVENT_MESSAGE_", errMsg);
        return "success";
    } else {
        Map<String, String> messageMap = UtilMisc.toMap("numProds", Integer.toString(numProds));
        messageMap.put("errProds", Integer.toString(errProds));
        errMsg = UtilProperties.getMessage(resource, "productevents.keyword_creation_complete_for_products_with_errors", messageMap, UtilHttp.getLocale(request));
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) Security(org.apache.ofbiz.security.Security) Timestamp(java.sql.Timestamp) LinkedList(java.util.LinkedList) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 30 with GenericEntityException

use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.

the class ProductEvents method updateProductQuickAdminSelFeat.

public static String updateProductQuickAdminSelFeat(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
    String productId = request.getParameter("productId");
    String variantProductId = request.getParameter("productId0");
    String useImagesProdId = request.getParameter("useImages");
    String productFeatureTypeId = request.getParameter("productFeatureTypeId");
    if (UtilValidate.isEmpty(productFeatureTypeId)) {
        String errMsg = "Error: please select a ProductFeature Type to add or update variant features.";
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    try {
        boolean beganTransaction = TransactionUtil.begin();
        try {
            GenericValue productFeatureType = EntityQuery.use(delegator).from("ProductFeatureType").where("productFeatureTypeId", productFeatureTypeId).queryOne();
            if (productFeatureType == null) {
                String errMsg = "Error: the ProductFeature Type specified was not valid and one is require to add or update variant features.";
                request.setAttribute("_ERROR_MESSAGE_", errMsg);
                return "error";
            }
            // check for variantProductId - this will mean that we have multiple variants to update
            if (variantProductId != null) {
                // multiple products, so use a numeric suffix to get them all
                int attribIdx = 0;
                GenericValue product = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
                do {
                    GenericValue variantProduct = EntityQuery.use(delegator).from("Product").where("productId", variantProductId).queryOne();
                    String description = request.getParameter("description" + attribIdx);
                    // blank means null, which means delete the feature application
                    if ((description != null) && (description.trim().length() < 1)) {
                        description = null;
                    }
                    Set<String> variantDescRemoveToRemoveOnVirtual = new HashSet<>();
                    checkUpdateFeatureApplByDescription(variantProductId, variantProduct, description, productFeatureTypeId, productFeatureType, "STANDARD_FEATURE", nowTimestamp, delegator, null, variantDescRemoveToRemoveOnVirtual);
                    checkUpdateFeatureApplByDescription(productId, product, description, productFeatureTypeId, productFeatureType, "SELECTABLE_FEATURE", nowTimestamp, delegator, variantDescRemoveToRemoveOnVirtual, null);
                    // update image urls
                    if ((useImagesProdId != null) && (useImagesProdId.equals(variantProductId))) {
                        product.set("smallImageUrl", variantProduct.getString("smallImageUrl"));
                        product.set("mediumImageUrl", variantProduct.getString("mediumImageUrl"));
                        product.set("largeImageUrl", null);
                        product.set("detailImageUrl", null);
                        product.store();
                    }
                    attribIdx++;
                    variantProductId = request.getParameter("productId" + attribIdx);
                } while (variantProductId != null);
            }
            TransactionUtil.commit(beganTransaction);
        } catch (GenericEntityException e) {
            String errMsg = "Error updating quick admin selectable feature settings: " + e.toString();
            Debug.logError(e, errMsg, module);
            request.setAttribute("_ERROR_MESSAGE_", errMsg);
            TransactionUtil.rollback(beganTransaction, errMsg, e);
            return "error";
        }
    } catch (GenericTransactionException gte) {
        String errMsg = "Error updating quick admin selectable feature settings: " + gte.toString();
        Debug.logError(gte, errMsg, module);
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    return "success";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) Timestamp(java.sql.Timestamp) HashSet(java.util.HashSet)

Aggregations

GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)913 GenericValue (org.apache.ofbiz.entity.GenericValue)847 Delegator (org.apache.ofbiz.entity.Delegator)599 Locale (java.util.Locale)384 HashMap (java.util.HashMap)336 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)270 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)259 LinkedList (java.util.LinkedList)231 BigDecimal (java.math.BigDecimal)213 Timestamp (java.sql.Timestamp)171 Map (java.util.Map)109 GeneralException (org.apache.ofbiz.base.util.GeneralException)95 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)78 IOException (java.io.IOException)75 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)57 Security (org.apache.ofbiz.security.Security)54 ArrayList (java.util.ArrayList)48 EntityExpr (org.apache.ofbiz.entity.condition.EntityExpr)47 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)39 LinkedHashMap (java.util.LinkedHashMap)37