Search in sources :

Example 61 with GenericValue

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

the class PromoServices method importPromoCodeEmailsFromFile.

public static Map<String, Object> importPromoCodeEmailsFromFile(DispatchContext dctx, Map<String, ? extends Object> context) {
    LocalDispatcher dispatcher = dctx.getDispatcher();
    String productPromoCodeId = (String) context.get("productPromoCodeId");
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    ByteBuffer bytebufferwrapper = (ByteBuffer) context.get("uploadedFile");
    if (bytebufferwrapper == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductPromoCodeImportUploadedFileNotValid", locale));
    }
    byte[] wrapper = bytebufferwrapper.array();
    // read the bytes into a reader
    BufferedReader reader = new BufferedReader(new StringReader(new String(wrapper, UtilIO.getUtf8())));
    List<Object> errors = new LinkedList<>();
    int lines = 0;
    String line;
    // read the uploaded file and process each line
    try {
        while ((line = reader.readLine()) != null) {
            if (line.length() > 0 && !line.startsWith("#")) {
                if (UtilValidate.isEmail(line)) {
                    // valid email address
                    Map<String, Object> result = dispatcher.runSync("createProductPromoCodeEmail", UtilMisc.<String, Object>toMap("productPromoCodeId", productPromoCodeId, "emailAddress", line, "userLogin", userLogin));
                    if (result != null && ServiceUtil.isError(result)) {
                        errors.add(line + ": " + ServiceUtil.getErrorMessage(result));
                    }
                } else {
                    // not valid ignore and notify
                    errors.add(line + ": is not a valid email address");
                }
                ++lines;
            }
        }
    } catch (IOException | GenericServiceException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            Debug.logError(e, module);
        }
    }
    // return errors or success
    if (errors.size() > 0) {
        return ServiceUtil.returnError(errors);
    } else if (lines == 0) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductPromoCodeImportEmptyFile", locale));
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) LinkedList(java.util.LinkedList) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 62 with GenericValue

use of org.apache.ofbiz.entity.GenericValue 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 63 with GenericValue

use of org.apache.ofbiz.entity.GenericValue 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 64 with GenericValue

use of org.apache.ofbiz.entity.GenericValue 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)

Example 65 with GenericValue

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

the class SubscriptionServices method processExtendSubscriptionByProduct.

public static Map<String, Object> processExtendSubscriptionByProduct(DispatchContext dctx, Map<String, ? extends Object> context) throws GenericServiceException {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    String productId = (String) context.get("productId");
    Integer qty = (Integer) context.get("quantity");
    Locale locale = (Locale) context.get("locale");
    if (qty == null) {
        qty = Integer.valueOf(1);
    }
    Timestamp orderCreatedDate = (Timestamp) context.get("orderCreatedDate");
    if (orderCreatedDate == null) {
        orderCreatedDate = UtilDateTime.nowTimestamp();
    }
    try {
        List<GenericValue> productSubscriptionResourceList = EntityQuery.use(delegator).from("ProductSubscriptionResource").where("productId", productId).cache(true).filterByDate(orderCreatedDate, "fromDate", "thruDate", "purchaseFromDate", "purchaseThruDate").queryList();
        if (productSubscriptionResourceList.size() == 0) {
            Debug.logError("No ProductSubscriptionResource found for productId: " + productId, module);
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductSubscriptionResourceNotFound", UtilMisc.toMap("productId", productId), locale));
        }
        for (GenericValue productSubscriptionResource : productSubscriptionResourceList) {
            Long useTime = productSubscriptionResource.getLong("useTime");
            Integer newUseTime = Integer.valueOf(0);
            if (useTime != null) {
                newUseTime = Integer.valueOf(useTime.intValue() * qty.intValue());
            }
            Map<String, Object> subContext = UtilMisc.makeMapWritable(context);
            subContext.put("useTime", newUseTime);
            subContext.put("useTimeUomId", productSubscriptionResource.get("useTimeUomId"));
            subContext.put("useRoleTypeId", productSubscriptionResource.get("useRoleTypeId"));
            subContext.put("subscriptionResourceId", productSubscriptionResource.get("subscriptionResourceId"));
            subContext.put("automaticExtend", productSubscriptionResource.get("automaticExtend"));
            subContext.put("canclAutmExtTime", productSubscriptionResource.get("canclAutmExtTime"));
            subContext.put("canclAutmExtTimeUomId", productSubscriptionResource.get("canclAutmExtTimeUomId"));
            subContext.put("gracePeriodOnExpiry", productSubscriptionResource.get("gracePeriodOnExpiry"));
            subContext.put("gracePeriodOnExpiryUomId", productSubscriptionResource.get("gracePeriodOnExpiryUomId"));
            Map<String, Object> ctx = dctx.getModelService("processExtendSubscription").makeValid(subContext, ModelService.IN_PARAM);
            Map<String, Object> processExtendSubscriptionResult = dispatcher.runSync("processExtendSubscription", ctx);
            if (ServiceUtil.isError(processExtendSubscriptionResult)) {
                return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductSubscriptionByProductError", UtilMisc.toMap("productId", productId), locale), null, null, processExtendSubscriptionResult);
            }
        }
    } catch (GenericEntityException e) {
        Debug.logError(e, e.toString(), module);
        return ServiceUtil.returnError(e.toString());
    }
    return ServiceUtil.returnSuccess();
}
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) Timestamp(java.sql.Timestamp)

Aggregations

GenericValue (org.apache.ofbiz.entity.GenericValue)1422 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)871 Delegator (org.apache.ofbiz.entity.Delegator)721 Locale (java.util.Locale)505 HashMap (java.util.HashMap)463 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)370 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)356 BigDecimal (java.math.BigDecimal)338 LinkedList (java.util.LinkedList)312 Timestamp (java.sql.Timestamp)202 GeneralException (org.apache.ofbiz.base.util.GeneralException)168 Map (java.util.Map)155 IOException (java.io.IOException)116 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)97 HttpSession (javax.servlet.http.HttpSession)89 ArrayList (java.util.ArrayList)69 Security (org.apache.ofbiz.security.Security)69 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)59 List (java.util.List)56 EntityExpr (org.apache.ofbiz.entity.condition.EntityExpr)52