Search in sources :

Example 56 with Calendar

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

the class TimeDurationTests method assertDurationLoop.

private static TimeDuration assertDurationLoop(String label, Calendar right, int years, int months, int days, int hours, int minutes, int seconds, int milliseconds, TimeDuration lastString, boolean isNegative) {
    StringBuilder sb = new StringBuilder();
    sb.append(years != 0 ? years : "");
    sb.append(':').append(months != 0 ? months : "");
    sb.append(':').append(days != 0 ? days : "");
    sb.append(':').append(hours != 0 ? hours : "");
    sb.append(':').append(minutes != 0 ? minutes : "");
    sb.append(':').append(seconds != 0 ? seconds : "");
    sb.append(':').append(milliseconds != 0 ? milliseconds : "");
    String durationString = years + ":" + months + ":" + days + ":" + hours + ":" + minutes + ":" + seconds + ":" + milliseconds;
    TimeDuration stringDuration = TimeDuration.parseDuration(sb.toString());
    right.setTimeInMillis(0);
    if (years != 0) {
        right.set(Calendar.YEAR, 1970 + Math.abs(years));
    }
    if (months != 0) {
        right.set(Calendar.MONTH, Math.abs(months));
    }
    right.set(Calendar.DAY_OF_MONTH, Math.abs(days) + 1);
    if (hours != 0) {
        right.set(Calendar.HOUR, Math.abs(hours));
    }
    if (minutes != 0) {
        right.set(Calendar.MINUTE, Math.abs(minutes));
    }
    if (seconds != 0) {
        right.set(Calendar.SECOND, Math.abs(seconds));
    }
    if (milliseconds != 0) {
        right.set(Calendar.MILLISECOND, Math.abs(milliseconds));
    }
    TimeDuration calDuration = isNegative ? new TimeDuration(right, zero) : new TimeDuration(zero, right);
    assertDurationFields(label + "(parseString[0])", years, months, days, hours, minutes, seconds, milliseconds, durationString, TimeDuration.parseDuration(durationString), isNegative, false);
    assertDurationFields(label + "(parseString)", years, months, days, hours, minutes, seconds, milliseconds, durationString, stringDuration, isNegative, false);
    assertDurationFields(label + "(cal)", years, months, days, hours, minutes, seconds, milliseconds, durationString, calDuration, isNegative, false);
    Calendar added = calDuration.addToCalendar((Calendar) zero.clone());
    TimeDuration addDuration = new TimeDuration(zero, added);
    assertDurationFields(label + "(cal[add])", years, months, days, hours, minutes, seconds, milliseconds, durationString, addDuration, isNegative, false);
    assertEquals(label + ".compareTo(string, cal)", 0, doCompare(stringDuration, calDuration));
    assertEquals(label + ".compareTo(string, string)", 0, doCompare(stringDuration, stringDuration));
    assertEquals(label + ".compareTo(cal, cal)", 0, doCompare(calDuration, calDuration));
    assertEquals(label + ".compareTo(cal, string)", 0, doCompare(calDuration, stringDuration));
    assertEquals(label + ".equals(cal, cal)", calDuration, calDuration);
    assertEquals(label + ".equals(cal, string)", calDuration, stringDuration);
    assertEquals(label + ".equals(string, cal)", stringDuration, calDuration);
    assertEquals(label + ".equals(string, string)", stringDuration, stringDuration);
    assertFalse(label + ".not-equals(string, this)", stringDuration.equals(TimeDurationTests.class));
    if (lastString != null) {
        assertFalse(label + ".not-equals(string, lastString)", stringDuration.equals(lastString));
    }
    return stringDuration;
}
Also used : Calendar(com.ibm.icu.util.Calendar) TimeDuration(org.apache.ofbiz.base.util.TimeDuration)

Example 57 with Calendar

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

the class TimeDurationTests method assertDuration.

public static void assertDuration(String label, int years, int months, int days, int hours, int minutes, int seconds, int milliseconds) {
    TimeDuration lastString = null;
    Calendar right = (Calendar) zero.clone();
    for (int i = 1; i < 12; i++) {
        lastString = assertDurationLoop(i + " " + label, right, i * years, i * months, i * days, i * hours, i * minutes, i * seconds, i * milliseconds, lastString, false);
    }
    lastString = null;
    for (int i = -2; i > -12; i--) {
        lastString = assertDurationLoop(i + " " + label, right, i * years, i * months, i * days, i * hours, i * minutes, i * seconds, i * milliseconds, lastString, true);
    }
}
Also used : Calendar(com.ibm.icu.util.Calendar) TimeDuration(org.apache.ofbiz.base.util.TimeDuration)

Example 58 with Calendar

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

the class MacroFormRenderer method renderDateTimeField.

public void renderDateTimeField(Appendable writer, Map<String, Object> context, DateTimeField dateTimeField) throws IOException {
    ModelFormField modelFormField = dateTimeField.getModelFormField();
    String paramName = modelFormField.getParameterName(context);
    String defaultDateTimeString = dateTimeField.getDefaultDateTimeString(context);
    String className = "";
    String alert = "false";
    String name = "";
    String formattedMask = "";
    String event = modelFormField.getEvent();
    String action = modelFormField.getAction(context);
    if (UtilValidate.isNotEmpty(modelFormField.getWidgetStyle())) {
        className = modelFormField.getWidgetStyle();
        if (modelFormField.shouldBeRed(context)) {
            alert = "true";
        }
    }
    boolean useTimeDropDown = "time-dropdown".equals(dateTimeField.getInputMethod());
    String stepString = dateTimeField.getStep();
    int step = 1;
    StringBuilder timeValues = new StringBuilder();
    if (useTimeDropDown && UtilValidate.isNotEmpty(step)) {
        try {
            step = Integer.parseInt(stepString);
        } catch (IllegalArgumentException e) {
            Debug.logWarning("Invalid value for step property for field[" + paramName + "] with input-method=\"time-dropdown\" " + " Found Value [" + stepString + "]  " + e.getMessage(), module);
        }
        timeValues.append("[");
        for (int i = 0; i <= 59; ) {
            if (i != 0) {
                timeValues.append(", ");
            }
            timeValues.append(i);
            i += step;
        }
        timeValues.append("]");
    }
    Map<String, String> uiLabelMap = UtilGenerics.checkMap(context.get("uiLabelMap"));
    if (uiLabelMap == null) {
        Debug.logWarning("Could not find uiLabelMap in context", module);
    }
    String localizedInputTitle = "", localizedIconTitle = "";
    // whether the date field is short form, yyyy-mm-dd
    boolean shortDateInput = ("date".equals(dateTimeField.getType()) || useTimeDropDown ? true : false);
    if (useTimeDropDown) {
        name = UtilHttp.makeCompositeParam(paramName, "date");
    } else {
        name = paramName;
    }
    // the default values for a timestamp
    int size = 25;
    int maxlength = 30;
    if (shortDateInput) {
        size = maxlength = 10;
        if (uiLabelMap != null) {
            localizedInputTitle = uiLabelMap.get("CommonFormatDate");
        }
    } else if ("time".equals(dateTimeField.getType())) {
        size = maxlength = 8;
        if (uiLabelMap != null) {
            localizedInputTitle = uiLabelMap.get("CommonFormatTime");
        }
    } else {
        if (uiLabelMap != null) {
            localizedInputTitle = uiLabelMap.get("CommonFormatDateTime");
        }
    }
    /*
         * FIXME: Using a builder here is a hack. Replace the builder with appropriate code.
         */
    ModelFormFieldBuilder builder = new ModelFormFieldBuilder(modelFormField);
    boolean memEncodeOutput = modelFormField.getEncodeOutput();
    if (useTimeDropDown) {
        // If time-dropdown deactivate encodingOutput for found hour and minutes
        // FIXME: Encoding should be controlled by the renderer, not by the model.
        builder.setEncodeOutput(false);
    }
    // FIXME: modelFormField.getEntry ignores shortDateInput when converting Date objects to Strings.
    if (useTimeDropDown) {
        builder.setEncodeOutput(memEncodeOutput);
    }
    modelFormField = builder.build();
    String contextValue = modelFormField.getEntry(context, dateTimeField.getDefaultValue(context));
    String value = contextValue;
    if (UtilValidate.isNotEmpty(value)) {
        if (value.length() > maxlength) {
            value = value.substring(0, maxlength);
        }
    }
    String id = modelFormField.getCurrentContainerId(context);
    ModelForm modelForm = modelFormField.getModelForm();
    String formName = FormRenderer.getCurrentFormName(modelForm, context);
    String timeDropdown = dateTimeField.getInputMethod();
    String timeDropdownParamName = "";
    String classString = "";
    boolean isTwelveHour = false;
    String timeHourName = "";
    int hour2 = 0, hour1 = 0, minutes = 0;
    String timeMinutesName = "";
    String amSelected = "", pmSelected = "", ampmName = "";
    String compositeType = "";
    // search for a localized label for the icon
    if (uiLabelMap != null) {
        localizedIconTitle = uiLabelMap.get("CommonViewCalendar");
    }
    if (!"time".equals(dateTimeField.getType())) {
        String tempParamName;
        if (useTimeDropDown) {
            tempParamName = UtilHttp.makeCompositeParam(paramName, "date");
        } else {
            tempParamName = paramName;
        }
        timeDropdownParamName = tempParamName;
        defaultDateTimeString = UtilHttp.encodeBlanks(modelFormField.getEntry(context, defaultDateTimeString));
    }
    // dropdowns
    if (useTimeDropDown) {
        className = modelFormField.getWidgetStyle();
        classString = (className != null ? className : "");
        isTwelveHour = "12".equals(dateTimeField.getClock());
        // set the Calendar to the default time of the form or now()
        Calendar cal = null;
        try {
            Timestamp defaultTimestamp = Timestamp.valueOf(contextValue);
            cal = Calendar.getInstance();
            cal.setTime(defaultTimestamp);
        } catch (IllegalArgumentException e) {
            Debug.logWarning("Form widget field [" + paramName + "] with input-method=\"time-dropdown\" was not able to understand the default time [" + defaultDateTimeString + "]. The parsing error was: " + e.getMessage(), module);
        }
        timeHourName = UtilHttp.makeCompositeParam(paramName, "hour");
        if (cal != null) {
            int hour = cal.get(Calendar.HOUR_OF_DAY);
            hour2 = hour;
            if (hour == 0) {
                hour = 12;
            }
            if (hour > 12) {
                hour -= 12;
            }
            hour1 = hour;
            minutes = cal.get(Calendar.MINUTE);
        }
        timeMinutesName = UtilHttp.makeCompositeParam(paramName, "minutes");
        compositeType = UtilHttp.makeCompositeParam(paramName, "compositeType");
        // if 12 hour clock, write the AM/PM selector
        if (isTwelveHour) {
            amSelected = ((cal != null && cal.get(Calendar.AM_PM) == Calendar.AM) ? "selected" : "");
            pmSelected = ((cal != null && cal.get(Calendar.AM_PM) == Calendar.PM) ? "selected" : "");
            ampmName = UtilHttp.makeCompositeParam(paramName, "ampm");
        }
    }
    // check for required field style on single forms
    if ("single".equals(modelFormField.getModelForm().getType()) && modelFormField.getRequiredField()) {
        String requiredStyle = modelFormField.getRequiredFieldStyle();
        if (UtilValidate.isEmpty(requiredStyle)) {
            requiredStyle = "required";
        }
        if (UtilValidate.isEmpty(className)) {
            className = requiredStyle;
        } else {
            className = requiredStyle + " " + className;
        }
    }
    String mask = dateTimeField.getMask();
    if ("Y".equals(mask)) {
        if ("date".equals(dateTimeField.getType())) {
            formattedMask = "9999-99-99";
        } else if ("time".equals(dateTimeField.getType())) {
            formattedMask = "99:99:99";
        } else if ("timestamp".equals(dateTimeField.getType())) {
            formattedMask = "9999-99-99 99:99:99";
        }
    }
    String tabindex = modelFormField.getTabindex();
    StringWriter sr = new StringWriter();
    sr.append("<@renderDateTimeField ");
    sr.append("name=\"");
    sr.append(name);
    sr.append("\" className=\"");
    sr.append(className);
    sr.append("\" alert=\"");
    sr.append(alert);
    sr.append("\" value=\"");
    sr.append(value);
    sr.append("\" title=\"");
    sr.append(localizedInputTitle);
    sr.append("\" size=\"");
    sr.append(Integer.toString(size));
    sr.append("\" maxlength=\"");
    sr.append(Integer.toString(maxlength));
    sr.append("\" step=\"");
    sr.append(Integer.toString(step));
    sr.append("\" timeValues=\"");
    sr.append(timeValues.toString());
    sr.append("\" id=\"");
    sr.append(id);
    sr.append("\" event=\"");
    sr.append(event);
    sr.append("\" action=\"");
    sr.append(action);
    sr.append("\" dateType=\"");
    sr.append(dateTimeField.getType());
    sr.append("\" shortDateInput=");
    sr.append(Boolean.toString(shortDateInput));
    sr.append(" timeDropdownParamName=\"");
    sr.append(timeDropdownParamName);
    sr.append("\" defaultDateTimeString=\"");
    sr.append(defaultDateTimeString);
    sr.append("\" localizedIconTitle=\"");
    sr.append(localizedIconTitle);
    sr.append("\" timeDropdown=\"");
    sr.append(timeDropdown);
    sr.append("\" timeHourName=\"");
    sr.append(timeHourName);
    sr.append("\" classString=\"");
    sr.append(classString);
    sr.append("\" hour1=");
    sr.append(Integer.toString(hour1));
    sr.append(" hour2=");
    sr.append(Integer.toString(hour2));
    sr.append(" timeMinutesName=\"");
    sr.append(timeMinutesName);
    sr.append("\" minutes=");
    sr.append(Integer.toString(minutes));
    sr.append(" isTwelveHour=");
    sr.append(Boolean.toString(isTwelveHour));
    sr.append(" ampmName=\"");
    sr.append(ampmName);
    sr.append("\" amSelected=\"");
    sr.append(amSelected);
    sr.append("\" pmSelected=\"");
    sr.append(pmSelected);
    sr.append("\" compositeType=\"");
    sr.append(compositeType);
    sr.append("\" formName=\"");
    sr.append(formName);
    sr.append("\" mask=\"");
    sr.append(formattedMask);
    sr.append("\" tabindex=\"");
    sr.append(tabindex);
    sr.append("\" />");
    executeMacro(writer, sr.toString());
    this.addAsterisks(writer, context, modelFormField);
    this.appendTooltip(writer, context, modelFormField);
}
Also used : StringWriter(java.io.StringWriter) ModelFormField(org.apache.ofbiz.widget.model.ModelFormField) Calendar(com.ibm.icu.util.Calendar) ModelFormFieldBuilder(org.apache.ofbiz.widget.model.ModelFormFieldBuilder) Timestamp(java.sql.Timestamp) ModelForm(org.apache.ofbiz.widget.model.ModelForm)

Example 59 with Calendar

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

the class PaymentGatewayServices method testCaptureWithReAuth.

public static Map<String, Object> testCaptureWithReAuth(DispatchContext dctx, Map<String, ? extends Object> context) {
    Locale locale = (Locale) context.get("locale");
    GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
    GenericValue authTransaction = (GenericValue) context.get("authTrans");
    Debug.logInfo("Test Capture with 2 minute delay failure/re-auth process", module);
    if (authTransaction == null) {
        authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference);
    }
    if (authTransaction == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentCannotBeCaptured", locale));
    }
    Timestamp txStamp = authTransaction.getTimestamp("transactionDate");
    Timestamp nowStamp = UtilDateTime.nowTimestamp();
    Map<String, Object> result = ServiceUtil.returnSuccess();
    result.put("captureAmount", context.get("captureAmount"));
    result.put("captureRefNum", UtilDateTime.nowAsString());
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(txStamp.getTime());
    cal.add(Calendar.MINUTE, 2);
    Timestamp twoMinAfter = new Timestamp(cal.getTimeInMillis());
    if (Debug.infoOn()) {
        Debug.logInfo("Re-Auth Capture Test : Tx Date - " + txStamp + " : 2 Min - " + twoMinAfter + " : Now - " + nowStamp, module);
    }
    if (nowStamp.after(twoMinAfter)) {
        result.put("captureResult", Boolean.FALSE);
    } else {
        result.put("captureResult", Boolean.TRUE);
        result.put("captureFlag", "C");
        result.put("captureMessage", UtilProperties.getMessage(resource, "AccountingPaymentTestCaptureWithReauth", locale));
    }
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Calendar(com.ibm.icu.util.Calendar) Timestamp(java.sql.Timestamp)

Example 60 with Calendar

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

the class PaymentGatewayServices method retryFailedAuthNsfs.

public static Map<String, Object> retryFailedAuthNsfs(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    // get the date/time for one week before now since we'll only retry once a week for NSFs
    Calendar calcCal = Calendar.getInstance();
    calcCal.setTimeInMillis(System.currentTimeMillis());
    calcCal.add(Calendar.WEEK_OF_YEAR, -1);
    Timestamp oneWeekAgo = new Timestamp(calcCal.getTimeInMillis());
    EntityQuery eq = EntityQuery.use(delegator).from("OrderPaymentPreference").where(EntityCondition.makeCondition("needsNsfRetry", EntityOperator.EQUALS, "Y"), EntityCondition.makeCondition(ModelEntity.STAMP_FIELD, EntityOperator.LESS_THAN_EQUAL_TO, oneWeekAgo)).orderBy("orderId");
    try (EntityListIterator eli = eq.queryIterator()) {
        List<String> processList = new LinkedList<>();
        if (eli != null) {
            Debug.logInfo("Processing failed order re-auth(s)", module);
            GenericValue value = null;
            while (((value = eli.next()) != null)) {
                String orderId = value.getString("orderId");
                if (!processList.contains(orderId)) {
                    // just try each order once
                    try {
                        // each re-try is independent of each other; if one fails it should not effect the others
                        dispatcher.runAsync("retryFailedOrderAuth", UtilMisc.<String, Object>toMap("orderId", orderId, "userLogin", userLogin));
                        processList.add(orderId);
                    } catch (GenericServiceException e) {
                        Debug.logError(e, module);
                    }
                }
            }
        }
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
    }
    return ServiceUtil.returnSuccess();
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) Calendar(com.ibm.icu.util.Calendar) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) EntityQuery(org.apache.ofbiz.entity.util.EntityQuery) Timestamp(java.sql.Timestamp) LinkedList(java.util.LinkedList)

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