Example usage for org.joda.time.format DateTimeFormat forPattern

List of usage examples for org.joda.time.format DateTimeFormat forPattern

Introduction

In this page you can find the example usage for org.joda.time.format DateTimeFormat forPattern.

Prototype

public static DateTimeFormatter forPattern(String pattern) 

Source Link

Document

Factory to create a formatter from a pattern string.

Usage

From source file:com.gst.infrastructure.campaigns.sms.service.SmsCampaignWritePlatformServiceJpaImpl.java

License:Apache License

@Transactional
@Override/*ww  w. j a  v  a2  s . co m*/
public CommandProcessingResult closeSmsCampaign(Long campaignId, JsonCommand command) {

    final AppUser currentUser = this.context.authenticatedUser();
    this.smsCampaignValidator.validateClosedDate(command.json());

    final SmsCampaign smsCampaign = this.smsCampaignRepository.findOne(campaignId);
    if (smsCampaign == null) {
        throw new SmsCampaignNotFound(campaignId);
    }

    final Locale locale = command.extractLocale();
    final DateTimeFormatter fmt = DateTimeFormat.forPattern(command.dateFormat()).withLocale(locale);
    final LocalDate closureDate = command.localDateValueOfParameterNamed("closureDate");

    smsCampaign.close(currentUser, fmt, closureDate);

    this.smsCampaignRepository.saveAndFlush(smsCampaign);
    //        this.serviceui.sendMessagesToGateway();

    return new CommandProcessingResultBuilder() //
            .withCommandId(command.commandId()) //
            .withEntityId(smsCampaign.getId()) //
            .build();
}

From source file:com.gst.infrastructure.campaigns.sms.service.SmsCampaignWritePlatformServiceJpaImpl.java

License:Apache License

@Transactional
@Override// w  w w  . ja va2 s.  com
public CommandProcessingResult reactivateSmsCampaign(final Long campaignId, JsonCommand command) {

    this.smsCampaignValidator.validateActivation(command.json());

    final AppUser currentUser = this.context.authenticatedUser();

    final SmsCampaign smsCampaign = this.smsCampaignRepository.findOne(campaignId);

    if (smsCampaign == null) {
        throw new SmsCampaignNotFound(campaignId);
    }

    final Locale locale = command.extractLocale();
    final DateTimeFormatter fmt = DateTimeFormat.forPattern(command.dateFormat()).withLocale(locale);
    final LocalDate reactivationDate = command.localDateValueOfParameterNamed("activationDate");
    smsCampaign.reactivate(currentUser, fmt, reactivationDate);
    if (smsCampaign.isDirect()) {
        insertDirectCampaignIntoSmsOutboundTable(smsCampaign);
    } else if (smsCampaign.isSchedule()) {

        /**
         * if recurrence start date is in the future calculate next trigger
         * date if not use recurrence start date us next trigger date when
         * activating
         */
        LocalDate nextTriggerDate = null;
        if (smsCampaign.getRecurrenceStartDateTime().isBefore(tenantDateTime())) {
            nextTriggerDate = CalendarUtils.getNextRecurringDate(smsCampaign.getRecurrence(),
                    smsCampaign.getRecurrenceStartDate(), DateUtils.getLocalDateOfTenant());
        } else {
            nextTriggerDate = smsCampaign.getRecurrenceStartDate();
        }
        // to get time of tenant
        final LocalDateTime getTime = smsCampaign.getRecurrenceStartDateTime();

        final String dateString = nextTriggerDate.toString() + " " + getTime.getHourOfDay() + ":"
                + getTime.getMinuteOfHour() + ":" + getTime.getSecondOfMinute();
        final DateTimeFormatter simpleDateFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        final LocalDateTime nextTriggerDateWithTime = LocalDateTime.parse(dateString, simpleDateFormat);

        smsCampaign.setNextTriggerDate(nextTriggerDateWithTime.toDate());
    }
    this.smsCampaignRepository.saveAndFlush(smsCampaign);

    return new CommandProcessingResultBuilder() //
            .withEntityId(smsCampaign.getId()) //
            .build();

}

From source file:com.gst.infrastructure.core.api.JsonCommand.java

License:Apache License

public boolean isChangeInTimeParameterNamed(final String parameterName, final Date existingValue,
        final String timeFormat) {
    LocalDateTime time = null;/*w  w w.j a v  a2 s  .  c o m*/
    if (existingValue != null) {
        DateTimeFormatter timeFormtter = DateTimeFormat.forPattern(timeFormat);
        time = LocalDateTime.parse(existingValue.toString(), timeFormtter);
    }
    return isChangeInLocalTimeParameterNamed(parameterName, time);
}

From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java

License:Apache License

public MonthDay extractMonthDayNamed(final String parameterName, final JsonObject element,
        final String dateFormat, final Locale clientApplicationLocale) {
    MonthDay value = null;/*from   ww w  .  j a v a 2  s. c  om*/
    if (element.isJsonObject()) {
        final JsonObject object = element.getAsJsonObject();

        if (object.has(parameterName) && object.get(parameterName).isJsonPrimitive()) {

            final JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive();
            final String valueAsString = primitive.getAsString();
            if (StringUtils.isNotBlank(valueAsString)) {
                try {
                    final DateTimeFormatter formatter = DateTimeFormat.forPattern(dateFormat)
                            .withLocale(clientApplicationLocale);
                    value = MonthDay.parse(valueAsString.toLowerCase(clientApplicationLocale), formatter);
                } catch (final IllegalArgumentException e) {
                    final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
                    final ApiParameterError error = ApiParameterError.parameterError(
                            "validation.msg.invalid.month.day",
                            "The parameter " + parameterName + " is invalid based on the monthDayFormat: '"
                                    + dateFormat + "' and locale: '" + clientApplicationLocale + "' provided:",
                            parameterName, valueAsString, dateFormat);
                    dataValidationErrors.add(error);

                    throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
                            "Validation errors exist.", dataValidationErrors);
                }
            }
        }

    }
    return value;
}

From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java

License:Apache License

public LocalDateTime extractLocalTimeNamed(final String parameterName, final JsonObject element,
        final String timeFormat, final Locale clientApplicationLocale,
        final Set<String> parametersPassedInCommand) {
    LocalDateTime value = null;/*from www.  j a  v  a  2  s . c o m*/
    String timeValueAsString = null;
    if (element.isJsonObject()) {
        final JsonObject object = element.getAsJsonObject();
        if (object.has(parameterName) && object.get(parameterName).isJsonPrimitive()) {
            parametersPassedInCommand.add(parameterName);

            try {
                DateTimeFormatter timeFormtter = DateTimeFormat.forPattern(timeFormat);
                final JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive();
                timeValueAsString = primitive.getAsString();
                if (StringUtils.isNotBlank(timeValueAsString)) {
                    value = LocalDateTime.parse(timeValueAsString, timeFormtter);
                }
            } catch (IllegalArgumentException e) {
                final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
                final String defaultMessage = new StringBuilder(
                        "The parameter '" + timeValueAsString + "' is not in correct format.").toString();
                final ApiParameterError error = ApiParameterError
                        .parameterError("validation.msg.invalid.TimeFormat", defaultMessage, parameterName);
                dataValidationErrors.add(error);
                throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
                        "Validation errors exist.", dataValidationErrors);
            }

        }
    }
    return value;
}

From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java

License:Apache License

public static LocalDateTime convertDateTimeFrom(final String dateTimeAsString, final String parameterName,
        final String dateTimeFormat, final Locale clientApplicationLocale) {

    validateDateFormatAndLocale(parameterName, dateTimeFormat, clientApplicationLocale);
    LocalDateTime eventLocalDateTime = null;
    if (StringUtils.isNotBlank(dateTimeAsString)) {
        try {/* www .  jav  a2  s .  c  om*/
            eventLocalDateTime = DateTimeFormat.forPattern(dateTimeFormat).withLocale(clientApplicationLocale)
                    .parseLocalDateTime(dateTimeAsString.toLowerCase(clientApplicationLocale));
        } catch (final IllegalArgumentException e) {
            final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
            final ApiParameterError error = ApiParameterError.parameterError(
                    "validation.msg.invalid.dateFormat.format",
                    "The parameter " + parameterName + " is invalid based on the dateFormat: '" + dateTimeFormat
                            + "' and locale: '" + clientApplicationLocale + "' provided:",
                    parameterName, eventLocalDateTime, dateTimeFormat);
            dataValidationErrors.add(error);

            throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
                    "Validation errors exist.", dataValidationErrors);
        }
    }

    return eventLocalDateTime;
}

From source file:com.gst.infrastructure.core.service.DateUtils.java

License:Apache License

public static LocalDate parseLocalDate(final String stringDate, final String pattern) {

    try {//from   www.  j a  v a  2 s .  co m
        final DateTimeFormatter dateStringFormat = DateTimeFormat.forPattern(pattern);
        dateStringFormat.withZone(getDateTimeZoneOfTenant());
        final DateTime dateTime = dateStringFormat.parseDateTime(stringDate);
        return dateTime.toLocalDate();
    } catch (final IllegalArgumentException e) {
        final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
        final ApiParameterError error = ApiParameterError.parameterError("validation.msg.invalid.date.pattern",
                "The parameter date (" + stringDate + ") is invalid w.r.t. pattern " + pattern, "date",
                stringDate, pattern);
        dataValidationErrors.add(error);
        throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
                "Validation errors exist.", dataValidationErrors);
    }
}

From source file:com.gst.infrastructure.reportmailingjob.domain.ReportMailingJob.java

License:Apache License

/** 
 * create a new instance of the ReportmailingJob for a new entry 
 * /*from  w w  w .  ja  v a 2  s.c o m*/
 * @return ReportMailingJob object
 **/
public static ReportMailingJob newInstance(JsonCommand jsonCommand, final Report stretchyReport,
        final AppUser runAsUser) {
    final String name = jsonCommand.stringValueOfParameterNamed(ReportMailingJobConstants.NAME_PARAM_NAME);
    final String description = jsonCommand
            .stringValueOfParameterNamed(ReportMailingJobConstants.DESCRIPTION_PARAM_NAME);
    final String recurrence = jsonCommand
            .stringValueOfParameterNamed(ReportMailingJobConstants.RECURRENCE_PARAM_NAME);
    final boolean isActive = jsonCommand
            .booleanPrimitiveValueOfParameterNamed(ReportMailingJobConstants.IS_ACTIVE_PARAM_NAME);
    final String emailRecipients = jsonCommand
            .stringValueOfParameterNamed(ReportMailingJobConstants.EMAIL_RECIPIENTS_PARAM_NAME);
    final String emailSubject = jsonCommand
            .stringValueOfParameterNamed(ReportMailingJobConstants.EMAIL_SUBJECT_PARAM_NAME);
    final String emailMessage = jsonCommand
            .stringValueOfParameterNamed(ReportMailingJobConstants.EMAIL_MESSAGE_PARAM_NAME);
    final String stretchyReportParamMap = jsonCommand
            .stringValueOfParameterNamed(ReportMailingJobConstants.STRETCHY_REPORT_PARAM_MAP_PARAM_NAME);
    final Integer emailAttachmentFileFormatId = jsonCommand
            .integerValueOfParameterNamed(ReportMailingJobConstants.EMAIL_ATTACHMENT_FILE_FORMAT_ID_PARAM_NAME);
    final ReportMailingJobEmailAttachmentFileFormat emailAttachmentFileFormat = ReportMailingJobEmailAttachmentFileFormat
            .newInstance(emailAttachmentFileFormatId);
    LocalDateTime startDateTime = new LocalDateTime();

    if (jsonCommand.hasParameter(ReportMailingJobConstants.START_DATE_TIME_PARAM_NAME)) {
        final String startDateTimeString = jsonCommand
                .stringValueOfParameterNamed(ReportMailingJobConstants.START_DATE_TIME_PARAM_NAME);

        if (startDateTimeString != null) {
            final DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(jsonCommand.dateFormat())
                    .withLocale(jsonCommand.extractLocale());
            startDateTime = LocalDateTime.parse(startDateTimeString, dateTimeFormatter);
        }
    }

    return new ReportMailingJob(name, description, startDateTime, recurrence, emailRecipients, emailSubject,
            emailMessage, emailAttachmentFileFormat, stretchyReport, stretchyReportParamMap, null,
            startDateTime, null, null, null, isActive, false, runAsUser);
}

From source file:com.gst.infrastructure.reportmailingjob.domain.ReportMailingJob.java

License:Apache License

/** 
 * Update the ReportMailingJob entity /*from   w  w w.  j  a  v  a2s .  c  om*/
 * 
 * @param jsonCommand JsonCommand object
 * @return map of string to object
 **/
public Map<String, Object> update(final JsonCommand jsonCommand) {
    final Map<String, Object> actualChanges = new LinkedHashMap<>();

    if (jsonCommand.isChangeInStringParameterNamed(ReportMailingJobConstants.NAME_PARAM_NAME, this.name)) {
        final String name = jsonCommand.stringValueOfParameterNamed(ReportMailingJobConstants.NAME_PARAM_NAME);
        actualChanges.put(ReportMailingJobConstants.NAME_PARAM_NAME, name);

        this.name = name;
    }

    if (jsonCommand.isChangeInStringParameterNamed(ReportMailingJobConstants.DESCRIPTION_PARAM_NAME,
            this.description)) {
        final String description = jsonCommand
                .stringValueOfParameterNamed(ReportMailingJobConstants.DESCRIPTION_PARAM_NAME);
        actualChanges.put(ReportMailingJobConstants.DESCRIPTION_PARAM_NAME, description);

        this.description = description;
    }

    if (jsonCommand.isChangeInStringParameterNamed(ReportMailingJobConstants.RECURRENCE_PARAM_NAME,
            this.recurrence)) {
        final String recurrence = jsonCommand
                .stringValueOfParameterNamed(ReportMailingJobConstants.RECURRENCE_PARAM_NAME);
        actualChanges.put(ReportMailingJobConstants.RECURRENCE_PARAM_NAME, recurrence);

        this.recurrence = recurrence;
    }

    if (jsonCommand.isChangeInBooleanParameterNamed(ReportMailingJobConstants.IS_ACTIVE_PARAM_NAME,
            this.isActive)) {
        final boolean isActive = jsonCommand
                .booleanPrimitiveValueOfParameterNamed(ReportMailingJobConstants.IS_ACTIVE_PARAM_NAME);
        actualChanges.put(ReportMailingJobConstants.IS_ACTIVE_PARAM_NAME, isActive);

        this.isActive = isActive;
    }

    if (jsonCommand.isChangeInStringParameterNamed(ReportMailingJobConstants.EMAIL_RECIPIENTS_PARAM_NAME,
            this.emailRecipients)) {
        final String emailRecipients = jsonCommand
                .stringValueOfParameterNamed(ReportMailingJobConstants.EMAIL_RECIPIENTS_PARAM_NAME);
        actualChanges.put(ReportMailingJobConstants.EMAIL_RECIPIENTS_PARAM_NAME, emailRecipients);

        this.emailRecipients = emailRecipients;
    }

    if (jsonCommand.isChangeInStringParameterNamed(ReportMailingJobConstants.EMAIL_SUBJECT_PARAM_NAME,
            this.emailSubject)) {
        final String emailSubject = jsonCommand
                .stringValueOfParameterNamed(ReportMailingJobConstants.EMAIL_SUBJECT_PARAM_NAME);
        actualChanges.put(ReportMailingJobConstants.EMAIL_SUBJECT_PARAM_NAME, emailSubject);

        this.emailSubject = emailSubject;
    }

    if (jsonCommand.isChangeInStringParameterNamed(ReportMailingJobConstants.EMAIL_MESSAGE_PARAM_NAME,
            this.emailMessage)) {
        final String emailMessage = jsonCommand
                .stringValueOfParameterNamed(ReportMailingJobConstants.EMAIL_MESSAGE_PARAM_NAME);
        actualChanges.put(ReportMailingJobConstants.EMAIL_MESSAGE_PARAM_NAME, emailMessage);

        this.emailMessage = emailMessage;
    }

    if (jsonCommand.isChangeInStringParameterNamed(
            ReportMailingJobConstants.STRETCHY_REPORT_PARAM_MAP_PARAM_NAME, this.stretchyReportParamMap)) {
        final String stretchyReportParamMap = jsonCommand
                .stringValueOfParameterNamed(ReportMailingJobConstants.STRETCHY_REPORT_PARAM_MAP_PARAM_NAME);
        actualChanges.put(ReportMailingJobConstants.STRETCHY_REPORT_PARAM_MAP_PARAM_NAME,
                stretchyReportParamMap);

        this.stretchyReportParamMap = stretchyReportParamMap;
    }

    final ReportMailingJobEmailAttachmentFileFormat emailAttachmentFileFormat = ReportMailingJobEmailAttachmentFileFormat
            .newInstance(this.emailAttachmentFileFormat);

    if (jsonCommand.isChangeInIntegerParameterNamed(
            ReportMailingJobConstants.EMAIL_ATTACHMENT_FILE_FORMAT_ID_PARAM_NAME,
            emailAttachmentFileFormat.getId())) {
        final Integer emailAttachmentFileFormatId = jsonCommand.integerValueOfParameterNamed(
                ReportMailingJobConstants.EMAIL_ATTACHMENT_FILE_FORMAT_ID_PARAM_NAME);
        actualChanges.put(ReportMailingJobConstants.EMAIL_ATTACHMENT_FILE_FORMAT_ID_PARAM_NAME,
                emailAttachmentFileFormatId);

        final ReportMailingJobEmailAttachmentFileFormat newEmailAttachmentFileFormat = ReportMailingJobEmailAttachmentFileFormat
                .newInstance(emailAttachmentFileFormatId);
        this.emailAttachmentFileFormat = newEmailAttachmentFileFormat.getValue();
    }

    final String newStartDateTimeString = jsonCommand
            .stringValueOfParameterNamed(ReportMailingJobConstants.START_DATE_TIME_PARAM_NAME);

    if (!StringUtils.isEmpty(newStartDateTimeString)) {
        final DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(jsonCommand.dateFormat())
                .withLocale(jsonCommand.extractLocale());
        final LocalDateTime newStartDateTime = LocalDateTime.parse(newStartDateTimeString, dateTimeFormatter);
        final LocalDateTime oldStartDateTime = (this.startDateTime != null)
                ? new LocalDateTime(this.startDateTime)
                : null;

        if ((oldStartDateTime != null) && !newStartDateTime.equals(oldStartDateTime)) {
            actualChanges.put(ReportMailingJobConstants.START_DATE_TIME_PARAM_NAME, newStartDateTimeString);

            this.startDateTime = newStartDateTime.toDate();
        }
    }

    Long currentStretchyReportId = null;

    if (this.stretchyReport != null) {
        currentStretchyReportId = this.stretchyReport.getId();
    }

    if (jsonCommand.isChangeInLongParameterNamed(ReportMailingJobConstants.STRETCHY_REPORT_ID_PARAM_NAME,
            currentStretchyReportId)) {
        final Long updatedStretchyReportId = jsonCommand
                .longValueOfParameterNamed(ReportMailingJobConstants.STRETCHY_REPORT_ID_PARAM_NAME);
        actualChanges.put(ReportMailingJobConstants.STRETCHY_REPORT_ID_PARAM_NAME, updatedStretchyReportId);
    }

    return actualChanges;
}

From source file:com.gst.infrastructure.reportmailingjob.service.ReportMailingJobWritePlatformServiceImpl.java

License:Apache License

/**
 * create the next recurring DateTime from recurrence pattern, start DateTime and current DateTime
 * //from w w w .  ja  v  a  2 s. c  om
 * @param recurrencePattern
 * @param startDateTime
 * @return DateTime object
 */
private DateTime createNextRecurringDateTime(final String recurrencePattern, final DateTime startDateTime) {
    DateTime nextRecurringDateTime = null;

    // the recurrence pattern/rule cannot be empty
    if (StringUtils.isNotBlank(recurrencePattern) && startDateTime != null) {
        final LocalDate nextDayLocalDate = startDateTime.plus(1).toLocalDate();
        final LocalDate nextRecurringLocalDate = CalendarUtils.getNextRecurringDate(recurrencePattern,
                startDateTime.toLocalDate(), nextDayLocalDate);
        final String nextDateTimeString = nextRecurringLocalDate + " " + startDateTime.getHourOfDay() + ":"
                + startDateTime.getMinuteOfHour() + ":" + startDateTime.getSecondOfMinute();
        final DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(DATETIME_FORMAT);

        nextRecurringDateTime = DateTime.parse(nextDateTimeString, dateTimeFormatter);
    }

    return nextRecurringDateTime;
}