Search in sources :

Example 41 with GenericEntityException

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

the class ProductWorker method getAverageProductRating.

public static BigDecimal getAverageProductRating(GenericValue product, List<GenericValue> reviews, String productStoreId) {
    if (product == null) {
        Debug.logWarning("Invalid product entity passed; unable to obtain valid product rating", module);
        return BigDecimal.ZERO;
    }
    BigDecimal productRating = BigDecimal.ZERO;
    BigDecimal productEntityRating = product.getBigDecimal("productRating");
    String entityFieldType = product.getString("ratingTypeEnum");
    // null check
    if (productEntityRating == null) {
        productEntityRating = BigDecimal.ZERO;
    }
    if (entityFieldType == null) {
        entityFieldType = "";
    }
    if ("PRDR_FLAT".equals(entityFieldType)) {
        productRating = productEntityRating;
    } else {
        // get the product rating from the ProductReview entity; limit by product store if ID is passed
        Map<String, String> reviewByAnd = UtilMisc.toMap("statusId", "PRR_APPROVED");
        if (productStoreId != null) {
            reviewByAnd.put("productStoreId", productStoreId);
        }
        // lookup the reviews if we didn't pass them in
        if (reviews == null) {
            try {
                reviews = product.getRelated("ProductReview", reviewByAnd, UtilMisc.toList("-postedDateTime"), true);
            } catch (GenericEntityException e) {
                Debug.logError(e, module);
            }
        }
        // tally the average
        BigDecimal ratingTally = BigDecimal.ZERO;
        BigDecimal numRatings = BigDecimal.ZERO;
        if (reviews != null) {
            for (GenericValue productReview : reviews) {
                BigDecimal rating = productReview.getBigDecimal("productRating");
                if (rating != null) {
                    ratingTally = ratingTally.add(rating);
                    numRatings = numRatings.add(BigDecimal.ONE);
                }
            }
        }
        if (ratingTally.compareTo(BigDecimal.ZERO) > 0 && numRatings.compareTo(BigDecimal.ZERO) > 0) {
            productRating = ratingTally.divide(numRatings, generalRounding);
        }
        if ("PRDR_MIN".equals(entityFieldType)) {
            // check for min
            if (productEntityRating.compareTo(productRating) > 0) {
                productRating = productEntityRating;
            }
        } else if ("PRDR_MAX".equals(entityFieldType)) {
            // check for max
            if (productRating.compareTo(productEntityRating) > 0) {
                productRating = productEntityRating;
            }
        }
    }
    return productRating;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) BigDecimal(java.math.BigDecimal)

Example 42 with GenericEntityException

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

the class ProductWorker method isPhysical.

public static boolean isPhysical(GenericValue product) {
    boolean isPhysical = false;
    if (product != null) {
        GenericValue productType = null;
        try {
            productType = product.getRelatedOne("ProductType", true);
        } catch (GenericEntityException e) {
            Debug.logWarning(e.getMessage(), module);
        }
        String isPhysicalValue = (productType != null ? productType.getString("isPhysical") : null);
        isPhysical = isPhysicalValue != null && "Y".equalsIgnoreCase(isPhysicalValue);
    }
    return isPhysical;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 43 with GenericEntityException

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

the class PromoServices method createProductPromoCodeSet.

public static Map<String, Object> createProductPromoCodeSet(DispatchContext dctx, Map<String, ? extends Object> context) {
    Locale locale = (Locale) context.get("locale");
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    Long quantity = (Long) context.get("quantity");
    int codeLength = (Integer) context.get("codeLength");
    String promoCodeLayout = (String) context.get("promoCodeLayout");
    // For PromoCodes we give the option not to use chars that are easy to mix up like 0<>O, 1<>I, ...
    boolean useSmartLayout = false;
    boolean useNormalLayout = false;
    if ("smart".equals(promoCodeLayout)) {
        useSmartLayout = true;
    } else if ("normal".equals(promoCodeLayout)) {
        useNormalLayout = true;
    }
    String newPromoCodeId = "";
    StringBuilder bankOfNumbers = new StringBuilder();
    bankOfNumbers.append(UtilProperties.getMessage(resource, "ProductPromoCodesCreated", locale));
    for (long i = 0; i < quantity; i++) {
        Map<String, Object> createProductPromoCodeMap = null;
        boolean foundUniqueNewCode = false;
        long count = 0;
        while (!foundUniqueNewCode) {
            if (useSmartLayout) {
                newPromoCodeId = RandomStringUtils.random(codeLength, smartChars);
            } else if (useNormalLayout) {
                newPromoCodeId = RandomStringUtils.randomAlphanumeric(codeLength);
            }
            GenericValue existingPromoCode = null;
            try {
                existingPromoCode = EntityQuery.use(delegator).from("ProductPromoCode").where("productPromoCodeId", newPromoCodeId).cache().queryOne();
            } catch (GenericEntityException e) {
                Debug.logWarning("Could not find ProductPromoCode for just generated ID: " + newPromoCodeId, module);
            }
            if (existingPromoCode == null) {
                foundUniqueNewCode = true;
            }
            count++;
            if (count > 999999) {
                return ServiceUtil.returnError("Unable to locate unique PromoCode! Length [" + codeLength + "]");
            }
        }
        try {
            Map<String, Object> newContext = dctx.makeValidContext("createProductPromoCode", ModelService.IN_PARAM, context);
            newContext.put("productPromoCodeId", newPromoCodeId);
            createProductPromoCodeMap = dispatcher.runSync("createProductPromoCode", newContext);
        } catch (GenericServiceException err) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductPromoCodeCannotBeCreated", locale), null, null, null);
        }
        if (ServiceUtil.isError(createProductPromoCodeMap)) {
            // what to do here? try again?
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductPromoCodeCannotBeCreated", locale), null, null, createProductPromoCodeMap);
        }
        bankOfNumbers.append((String) createProductPromoCodeMap.get("productPromoCodeId"));
        bankOfNumbers.append(",");
    }
    return ServiceUtil.returnSuccess(bankOfNumbers.toString());
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 44 with GenericEntityException

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

the class PromoServices method purgeOldStoreAutoPromos.

public static Map<String, Object> purgeOldStoreAutoPromos(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    String productStoreId = (String) context.get("productStoreId");
    Locale locale = (Locale) context.get("locale");
    Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
    List<EntityCondition> condList = new LinkedList<>();
    if (UtilValidate.isEmpty(productStoreId)) {
        condList.add(EntityCondition.makeCondition("productStoreId", EntityOperator.EQUALS, productStoreId));
    }
    condList.add(EntityCondition.makeCondition("userEntered", EntityOperator.EQUALS, "Y"));
    condList.add(EntityCondition.makeCondition("thruDate", EntityOperator.NOT_EQUAL, null));
    condList.add(EntityCondition.makeCondition("thruDate", EntityOperator.LESS_THAN, nowTimestamp));
    try (EntityListIterator eli = EntityQuery.use(delegator).from("ProductStorePromoAndAppl").where(condList).queryIterator()) {
        GenericValue productStorePromoAndAppl = null;
        while ((productStorePromoAndAppl = eli.next()) != null) {
            GenericValue productStorePromo = delegator.makeValue("ProductStorePromoAppl");
            productStorePromo.setAllFields(productStorePromoAndAppl, true, null, null);
            productStorePromo.remove();
        }
    } catch (GenericEntityException e) {
        Debug.logError(e, "Error removing expired ProductStorePromo records: " + e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductPromoCodeCannotBeRemoved", UtilMisc.toMap("errorString", e.toString()), locale));
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) Timestamp(java.sql.Timestamp) LinkedList(java.util.LinkedList)

Example 45 with GenericEntityException

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

the class SubscriptionServices method runServiceOnSubscriptionExpiry.

public static Map<String, Object> runServiceOnSubscriptionExpiry(DispatchContext dctx, Map<String, ? extends Object> context) {
    LocalDispatcher dispatcher = dctx.getDispatcher();
    Delegator delegator = dctx.getDelegator();
    Locale locale = (Locale) context.get("locale");
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Map<String, Object> result = new HashMap<>();
    Map<String, Object> expiryMap = new HashMap<>();
    String gracePeriodOnExpiry = null;
    String gracePeriodOnExpiryUomId = null;
    String subscriptionId = null;
    Timestamp expirationCompletedDate = null;
    try {
        EntityCondition cond1 = EntityCondition.makeCondition("automaticExtend", EntityOperator.EQUALS, "N");
        EntityCondition cond2 = EntityCondition.makeCondition("automaticExtend", EntityOperator.EQUALS, null);
        EntityCondition cond = EntityCondition.makeCondition(UtilMisc.toList(cond1, cond2), EntityOperator.OR);
        List<GenericValue> subscriptionList = null;
        subscriptionList = EntityQuery.use(delegator).from("Subscription").where(cond).queryList();
        if (subscriptionList != null) {
            for (GenericValue subscription : subscriptionList) {
                expirationCompletedDate = subscription.getTimestamp("expirationCompletedDate");
                if (expirationCompletedDate == null) {
                    Calendar currentDate = Calendar.getInstance();
                    currentDate.setTime(UtilDateTime.nowTimestamp());
                    // check if the thruDate + grace period (if provided) is earlier than today's date
                    Calendar endDateSubscription = Calendar.getInstance();
                    int field = Calendar.MONTH;
                    String subscriptionResourceId = subscription.getString("subscriptionResourceId");
                    GenericValue subscriptionResource = null;
                    subscriptionResource = EntityQuery.use(delegator).from("SubscriptionResource").where("subscriptionResourceId", subscriptionResourceId).queryOne();
                    subscriptionId = subscription.getString("subscriptionId");
                    gracePeriodOnExpiry = subscription.getString("gracePeriodOnExpiry");
                    gracePeriodOnExpiryUomId = subscription.getString("gracePeriodOnExpiryUomId");
                    String serviceNameOnExpiry = subscriptionResource.getString("serviceNameOnExpiry");
                    endDateSubscription.setTime(subscription.getTimestamp("thruDate"));
                    if (gracePeriodOnExpiry != null && gracePeriodOnExpiryUomId != null) {
                        if ("TF_day".equals(gracePeriodOnExpiryUomId)) {
                            field = Calendar.DAY_OF_YEAR;
                        } else if ("TF_wk".equals(gracePeriodOnExpiryUomId)) {
                            field = Calendar.WEEK_OF_YEAR;
                        } else if ("TF_mon".equals(gracePeriodOnExpiryUomId)) {
                            field = Calendar.MONTH;
                        } else if ("TF_yr".equals(gracePeriodOnExpiryUomId)) {
                            field = Calendar.YEAR;
                        } else {
                            Debug.logWarning("Don't know anything about gracePeriodOnExpiryUomId [" + gracePeriodOnExpiryUomId + "], defaulting to month", module);
                        }
                        endDateSubscription.add(field, Integer.parseInt(gracePeriodOnExpiry));
                    }
                    if ((currentDate.after(endDateSubscription) || currentDate.equals(endDateSubscription)) && serviceNameOnExpiry != null) {
                        if (userLogin != null) {
                            expiryMap.put("userLogin", userLogin);
                        }
                        if (subscriptionId != null) {
                            expiryMap.put("subscriptionId", subscriptionId);
                        }
                        result = dispatcher.runSync(serviceNameOnExpiry, expiryMap);
                        if (ServiceUtil.isSuccess(result)) {
                            subscription.set("expirationCompletedDate", UtilDateTime.nowTimestamp());
                            delegator.store(subscription);
                            Debug.logInfo("Subscription expired successfully for subscription ID:" + subscriptionId, module);
                        } else if (ServiceUtil.isError(result)) {
                            result = null;
                            Debug.logError("Error expiring subscription while processing with subscriptionId: " + subscriptionId, module);
                        }
                        if (result != null && subscriptionId != null) {
                            Debug.logInfo("Service mentioned in serviceNameOnExpiry called with result: " + ServiceUtil.makeSuccessMessage(result, "", "", "", ""), module);
                        } else if (result == null && subscriptionId != null) {
                            Debug.logError("Subscription couldn't be expired for subscriptionId: " + subscriptionId, module);
                            return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "ProductSubscriptionCouldntBeExpired", UtilMisc.toMap("subscriptionId", subscriptionId), locale));
                        }
                    }
                }
            }
        }
    } catch (GenericServiceException e) {
        Debug.logError("Error while calling service specified in serviceNameOnExpiry", module);
        return ServiceUtil.returnError(e.toString());
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
    }
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) HashMap(java.util.HashMap) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) Calendar(com.ibm.icu.util.Calendar) Timestamp(java.sql.Timestamp) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

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