Search in sources :

Example 11 with Calendar

use of com.ibm.icu.util.Calendar in project ofbiz-framework by apache.

the class SetCalendar method exec.

@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
    Object newValue = null;
    if (this.scriptlet != null) {
        try {
            newValue = this.scriptlet.executeScript(methodContext.getEnvMap());
        } catch (Exception exc) {
            Debug.logWarning(exc, "Error evaluating scriptlet [" + this.scriptlet + "]: " + exc, module);
        }
    } else if (!this.fromFma.isEmpty()) {
        newValue = this.fromFma.get(methodContext.getEnvMap());
    } else if (!this.valueFse.isEmpty()) {
        newValue = this.valueFse.expand(methodContext.getEnvMap());
    }
    if (ObjectType.isEmpty(newValue) && !this.defaultFse.isEmpty()) {
        newValue = this.defaultFse.expand(methodContext.getEnvMap());
    }
    if (!setIfNull && newValue == null) {
        return true;
    }
    Locale locale = null;
    TimeZone timeZone = null;
    Timestamp fromStamp = null;
    int years = 0;
    int months = 0;
    int days = 0;
    int hours = 0;
    int minutes = 0;
    int seconds = 0;
    int millis = 0;
    try {
        if (!this.localeFse.isEmpty()) {
            locale = (Locale) ObjectType.simpleTypeConvert(this.localeFse.expand(methodContext.getEnvMap()), "Locale", null, null);
        }
        if (locale == null) {
            locale = methodContext.getLocale();
        }
        if (locale == null) {
            locale = Locale.getDefault();
        }
        if (!this.timeZoneFse.isEmpty()) {
            timeZone = (TimeZone) ObjectType.simpleTypeConvert(this.timeZoneFse.expand(methodContext.getEnvMap()), "TimeZone", null, null);
        }
        if (timeZone == null) {
            timeZone = methodContext.getTimeZone();
        }
        if (timeZone == null) {
            timeZone = TimeZone.getDefault();
        }
        fromStamp = (Timestamp) MiniLangUtil.convertType(newValue, java.sql.Timestamp.class, locale, timeZone, UtilDateTime.getDateTimeFormat());
        if (!this.yearsFse.isEmpty()) {
            years = parseInt(this.yearsFse.expandString(methodContext.getEnvMap()));
        }
        if (!this.monthsFse.isEmpty()) {
            months = parseInt(this.monthsFse.expandString(methodContext.getEnvMap()));
        }
        if (!this.daysFse.isEmpty()) {
            days = parseInt(this.daysFse.expandString(methodContext.getEnvMap()));
        }
        if (!this.hoursFse.isEmpty()) {
            hours = parseInt(this.hoursFse.expandString(methodContext.getEnvMap()));
        }
        if (!this.minutesFse.isEmpty()) {
            minutes = parseInt(this.minutesFse.expandString(methodContext.getEnvMap()));
        }
        if (!this.secondsFse.isEmpty()) {
            seconds = parseInt(this.secondsFse.expandString(methodContext.getEnvMap()));
        }
        if (!this.millisFse.isEmpty()) {
            millis = parseInt(this.millisFse.expandString(methodContext.getEnvMap()));
        }
    } catch (Exception e) {
        throw new MiniLangRuntimeException("Exception thrown while parsing attributes: " + e.getMessage(), this);
    }
    Calendar cal = UtilDateTime.toCalendar(fromStamp, timeZone, locale);
    cal.add(Calendar.MILLISECOND, millis);
    cal.add(Calendar.SECOND, seconds);
    cal.add(Calendar.MINUTE, minutes);
    cal.add(Calendar.HOUR, hours);
    cal.add(Calendar.DAY_OF_MONTH, days);
    cal.add(Calendar.MONTH, months);
    cal.add(Calendar.YEAR, years);
    Timestamp toStamp = new Timestamp(cal.getTimeInMillis());
    if (!periodAlignStart.isEmpty()) {
        String period = periodAlignStart.expandString(methodContext.getEnvMap());
        if ("day".equals(period)) {
            toStamp = UtilDateTime.getDayStart(toStamp, 0, timeZone, locale);
        } else if ("week".equals(period)) {
            toStamp = UtilDateTime.getWeekStart(toStamp, 0, timeZone, locale);
        } else if ("month".equals(period)) {
            toStamp = UtilDateTime.getMonthStart(toStamp, 0, timeZone, locale);
        } else if ("year".equals(period)) {
            toStamp = UtilDateTime.getYearStart(toStamp, 0, timeZone, locale);
        } else {
            throw new MiniLangRuntimeException("Invalid period-align-start attribute value: " + period, this);
        }
    } else if (!periodAlignEnd.isEmpty()) {
        String period = periodAlignEnd.expandString(methodContext.getEnvMap());
        if ("day".equals(period)) {
            toStamp = UtilDateTime.getDayEnd(toStamp, timeZone, locale);
        } else if ("week".equals(period)) {
            toStamp = UtilDateTime.getWeekEnd(toStamp, timeZone, locale);
        } else if ("month".equals(period)) {
            toStamp = UtilDateTime.getMonthEnd(toStamp, timeZone, locale);
        } else if ("year".equals(period)) {
            toStamp = UtilDateTime.getYearEnd(toStamp, timeZone, locale);
        } else {
            throw new MiniLangRuntimeException("Invalid period-align-end attribute value: " + period, this);
        }
    }
    this.fieldFma.put(methodContext.getEnvMap(), toStamp);
    return true;
}
Also used : Locale(java.util.Locale) TimeZone(java.util.TimeZone) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) Calendar(com.ibm.icu.util.Calendar) Timestamp(java.sql.Timestamp) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) MiniLangException(org.apache.ofbiz.minilang.MiniLangException)

Example 12 with Calendar

use of com.ibm.icu.util.Calendar in project ofbiz-framework by apache.

the class PaymentGatewayServices method checkAuthValidity.

public static boolean checkAuthValidity(GenericValue orderPaymentPreference, String paymentConfig) {
    Delegator delegator = orderPaymentPreference.getDelegator();
    Timestamp authTime = PaymentGatewayServices.getAuthTime(orderPaymentPreference);
    if (authTime == null) {
        return false;
    }
    String reauthDays = null;
    GenericValue paymentMethod = null;
    try {
        paymentMethod = orderPaymentPreference.getRelatedOne("PaymentMethod", false);
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
    }
    if (paymentMethod != null && "CREDIT_CARD".equals(paymentMethod.getString("paymentMethodTypeId"))) {
        GenericValue creditCard = null;
        try {
            creditCard = paymentMethod.getRelatedOne("CreditCard", false);
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
        }
        if (creditCard != null) {
            String cardType = creditCard.getString("cardType");
            // add more types as necessary -- maybe we should create seed data for credit card types??
            if ("CCT_DISCOVER".equals(cardType)) {
                reauthDays = EntityUtilProperties.getPropertyValue(paymentConfig, "payment.general.reauth.disc.days", "90", delegator);
            } else if ("CCT_AMERICANEXPRESS".equals(cardType)) {
                reauthDays = EntityUtilProperties.getPropertyValue(paymentConfig, "payment.general.reauth.amex.days", "30", delegator);
            } else if ("CCT_MASTERCARD".equals(cardType)) {
                reauthDays = EntityUtilProperties.getPropertyValue(paymentConfig, "payment.general.reauth.mc.days", "30", delegator);
            } else if ("CCT_VISA".equals(cardType)) {
                reauthDays = EntityUtilProperties.getPropertyValue(paymentConfig, "payment.general.reauth.visa.days", "7", delegator);
            } else {
                reauthDays = EntityUtilProperties.getPropertyValue(paymentConfig, "payment.general.reauth.other.days", "7", delegator);
            }
        }
    } else if (paymentMethod != null && "EXT_PAYPAL".equals(paymentMethod.get("paymentMethodTypeId"))) {
        reauthDays = EntityUtilProperties.getPropertyValue(paymentConfig, "payment.general.reauth.paypal.days", "3", delegator);
    }
    if (reauthDays != null) {
        int days = 0;
        try {
            days = Integer.parseInt(reauthDays);
        } catch (Exception e) {
            Debug.logError(e, module);
        }
        if (days > 0) {
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(authTime.getTime());
            cal.add(Calendar.DAY_OF_YEAR, days);
            Timestamp validTime = new Timestamp(cal.getTimeInMillis());
            Timestamp nowTime = UtilDateTime.nowTimestamp();
            if (nowTime.after(validTime)) {
                return false;
            }
        }
    }
    return true;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) Calendar(com.ibm.icu.util.Calendar) Timestamp(java.sql.Timestamp) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GeneralException(org.apache.ofbiz.base.util.GeneralException)

Example 13 with Calendar

use of com.ibm.icu.util.Calendar in project ofbiz-framework by apache.

the class AIMPaymentServices method ccRefund.

public static Map<String, Object> ccRefund(DispatchContext ctx, Map<String, Object> context) {
    Locale locale = (Locale) context.get("locale");
    Delegator delegator = ctx.getDelegator();
    GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
    GenericValue creditCard = null;
    try {
        creditCard = delegator.getRelatedOne("CreditCard", orderPaymentPreference, false);
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentUnableToGetCCInfo", locale));
    }
    GenericValue authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference);
    if (authTransaction == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentTransactionAuthorizationNotFoundCannotRefund", locale));
    }
    context.put("creditCard", creditCard);
    context.put("authTransaction", authTransaction);
    Map<String, Object> results = ServiceUtil.returnSuccess();
    Map<String, Object> request = new HashMap<String, Object>();
    Properties props = buildAIMProperties(context, delegator);
    buildMerchantInfo(context, props, request);
    buildGatewayResponeConfig(context, props, request);
    buildCustomerBillingInfo(context, props, request);
    buildEmailSettings(context, props, request);
    buildInvoiceInfo(context, props, request);
    props.put("transType", "CREDIT");
    props.put("cardtype", creditCard.get("cardType"));
    buildRefundTransaction(context, props, request);
    Map<String, Object> validateResults = validateRequest(context, props, request);
    String respMsg = (String) validateResults.get(ModelService.RESPONSE_MESSAGE);
    if (ModelService.RESPOND_ERROR.equals(respMsg)) {
        results.put(ModelService.ERROR_MESSAGE, UtilProperties.getMessage(resource, "AccountingValidationFailedInvalidValues", locale));
        return results;
    }
    Map<String, Object> reply = processCard(request, props, locale);
    results.putAll(processRefundTransResult(request, reply));
    boolean refundResult = ((Boolean) results.get("refundResult")).booleanValue();
    String refundFlag = (String) results.get("refundFlag");
    // determining what transactions can be voided and what can be refunded, so we'll have to try it with timestamps
    if (!refundResult && isVoidableResponse(refundFlag)) {
        boolean canDoVoid = false;
        if (VOIDABLE_RESPONSES_TIME_LIMIT.contains(refundFlag)) {
            // We are calculating the timestamp that is at the beginning of a time limit,
            // since we can safely assume that, within this time limit, an unsettled transaction
            // can still be considered valid
            Calendar startCalendar = UtilDateTime.toCalendar(UtilDateTime.nowTimestamp());
            startCalendar.add(Calendar.DATE, -TIME_LIMIT_VERIFICATION_DAYS);
            Timestamp startTimestamp = new java.sql.Timestamp(startCalendar.getTime().getTime());
            Timestamp authTimestamp = authTransaction.getTimestamp("transactionDate");
            if (startTimestamp.before(authTimestamp)) {
                canDoVoid = true;
            }
        } else {
            // Since there's no time limit to check, the voiding of the transaction will go
            // through as usual
            canDoVoid = true;
        }
        if (canDoVoid) {
            Debug.logWarning("Refund was unsuccessful; will now attempt a VOID transaction.", module);
            BigDecimal authAmountObj = authTransaction.getBigDecimal("amount");
            BigDecimal refundAmountObj = (BigDecimal) context.get("refundAmount");
            BigDecimal authAmount = authAmountObj != null ? authAmountObj : BigDecimal.ZERO;
            BigDecimal refundAmount = refundAmountObj != null ? refundAmountObj : BigDecimal.ZERO;
            if (authAmount.compareTo(refundAmount) == 0) {
                reply = voidTransaction(authTransaction, context, delegator);
                if (ServiceUtil.isError(reply)) {
                    return reply;
                }
                results = ServiceUtil.returnSuccess();
                results.putAll(processRefundTransResult(request, reply));
                return results;
            } else {
                // create a new auth-capture of the difference.
                return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingAuthorizeNetCannotPerformVoidTransaction", UtilMisc.toMap("authAmount", authAmount, "refundAmount", refundAmount), locale));
            }
        }
    }
    return results;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) Calendar(com.ibm.icu.util.Calendar) UtilProperties(org.apache.ofbiz.base.util.UtilProperties) EntityUtilProperties(org.apache.ofbiz.entity.util.EntityUtilProperties) Properties(java.util.Properties) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 14 with Calendar

use of com.ibm.icu.util.Calendar in project ofbiz-framework by apache.

the class ContentManagementServices method updateContentSubscription.

public static Map<String, Object> updateContentSubscription(DispatchContext dctx, Map<String, ? extends Object> context) throws GenericServiceException {
    Map<String, Object> result = new HashMap<String, Object>();
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
    Map<String, Object> thisResult = new HashMap<String, Object>();
    String partyId = (String) context.get("partyId");
    String webPubPt = (String) context.get("contentId");
    String roleTypeId = (String) context.get("useRoleTypeId");
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Integer useTime = (Integer) context.get("useTime");
    String useTimeUomId = (String) context.get("useTimeUomId");
    boolean hasExistingContentRole = false;
    GenericValue contentRole = null;
    try {
        contentRole = EntityQuery.use(delegator).from("ContentRole").where("partyId", partyId, "contentId", webPubPt, "roleTypeId", roleTypeId).orderBy("fromDate DESC").cache().filterByDate().queryFirst();
        if (contentRole != null) {
            hasExistingContentRole = true;
        }
    } catch (GenericEntityException e) {
        return ServiceUtil.returnError(e.toString());
    }
    if (contentRole == null) {
        contentRole = delegator.makeValue("ContentRole");
        contentRole.set("contentId", webPubPt);
        contentRole.set("partyId", partyId);
        contentRole.set("roleTypeId", roleTypeId);
        contentRole.set("fromDate", nowTimestamp);
    }
    Timestamp thruDate = (Timestamp) contentRole.get("thruDate");
    if (thruDate == null) {
        // no thruDate? start with NOW
        thruDate = nowTimestamp;
    } else {
        // don't want to penalize for skipping time, in other words if they had a subscription last year for a month and buy another month, we want that second month to start now and not last year
        if (thruDate.before(nowTimestamp)) {
            thruDate = nowTimestamp;
        }
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(thruDate);
    int field = Calendar.MONTH;
    if ("TF_day".equals(useTimeUomId)) {
        field = Calendar.DAY_OF_YEAR;
    } else if ("TF_wk".equals(useTimeUomId)) {
        field = Calendar.WEEK_OF_YEAR;
    } else if ("TF_mon".equals(useTimeUomId)) {
        field = Calendar.MONTH;
    } else if ("TF_yr".equals(useTimeUomId)) {
        field = Calendar.YEAR;
    } else {
        Debug.logWarning("Don't know anything about useTimeUomId [" + useTimeUomId + "], defaulting to month", module);
    }
    calendar.add(field, useTime.intValue());
    thruDate = new Timestamp(calendar.getTimeInMillis());
    contentRole.set("thruDate", thruDate);
    try {
        if (hasExistingContentRole) {
            contentRole.store();
        } else {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("partyId", partyId);
            map.put("roleTypeId", roleTypeId);
            map.put("userLogin", userLogin);
            thisResult = dispatcher.runSync("ensurePartyRole", map);
            if (ServiceUtil.isError(thisResult)) {
                return ServiceUtil.returnError(ServiceUtil.getErrorMessage(thisResult));
            }
            contentRole.create();
        }
    } catch (GenericEntityException e) {
        return ServiceUtil.returnError(e.toString());
    }
    return result;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) HashMap(java.util.HashMap) Calendar(com.ibm.icu.util.Calendar) Timestamp(java.sql.Timestamp) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 15 with Calendar

use of com.ibm.icu.util.Calendar in project ofbiz-framework by apache.

the class UtilDateTime method getMonthEnd.

public static Timestamp getMonthEnd(Timestamp stamp, TimeZone timeZone, Locale locale) {
    Calendar tempCal = toCalendar(stamp, timeZone, locale);
    tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.getActualMaximum(Calendar.DAY_OF_MONTH), 0, 0, 0);
    return getDayEnd(new Timestamp(tempCal.getTimeInMillis()), timeZone, locale);
}
Also used : Calendar(com.ibm.icu.util.Calendar) Timestamp(java.sql.Timestamp)

Aggregations

Calendar (com.ibm.icu.util.Calendar)75 Timestamp (java.sql.Timestamp)37 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)24 GenericValue (org.apache.ofbiz.entity.GenericValue)24 Delegator (org.apache.ofbiz.entity.Delegator)17 Date (java.util.Date)14 HashMap (java.util.HashMap)12 Locale (java.util.Locale)12 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)11 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)11 GregorianCalendar (com.ibm.icu.util.GregorianCalendar)10 ArrayList (java.util.ArrayList)8 SimpleDateFormat (java.text.SimpleDateFormat)6 LinkedList (java.util.LinkedList)6 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)6 Map (java.util.Map)5 TimeDuration (org.apache.ofbiz.base.util.TimeDuration)5 BigDecimal (java.math.BigDecimal)4 Time (java.sql.Time)4 UtilDateTime (org.apache.ofbiz.base.util.UtilDateTime)4