Example usage for org.joda.time LocalDateTime parse

List of usage examples for org.joda.time LocalDateTime parse

Introduction

In this page you can find the example usage for org.joda.time LocalDateTime parse.

Prototype

public static LocalDateTime parse(String str, DateTimeFormatter formatter) 

Source Link

Document

Parses a LocalDateTime from the specified string using a formatter.

Usage

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

License:Apache License

private void updateTriggerDates(Long campaignId) {
    final SmsCampaign smsCampaign = this.smsCampaignRepository.findOne(campaignId);
    if (smsCampaign == null) {
        throw new SmsCampaignNotFound(campaignId);
    }/*from   w ww  . j  ava  2  s. c  o m*/
    LocalDateTime nextTriggerDate = smsCampaign.getNextTriggerDate();
    smsCampaign.setLastTriggerDate(nextTriggerDate.toDate());
    // calculate new trigger date and insert into next trigger date

    /**
     * next run time has to be in the future if not calculate a new future
     * date
     */
    LocalDate nextRuntime = CalendarUtils.getNextRecurringDate(smsCampaign.getRecurrence(),
            smsCampaign.getNextTriggerDate().toLocalDate(), nextTriggerDate.toLocalDate());
    if (nextRuntime.isBefore(DateUtils.getLocalDateOfTenant())) { // means
                                                                  // next
                                                                  // run
                                                                  // time is
                                                                  // in the
                                                                  // past
                                                                  // calculate
                                                                  // a new
                                                                  // future
                                                                  // date
        nextRuntime = CalendarUtils.getNextRecurringDate(smsCampaign.getRecurrence(),
                smsCampaign.getNextTriggerDate().toLocalDate(), DateUtils.getLocalDateOfTenant());
    }
    final LocalDateTime getTime = smsCampaign.getRecurrenceStartDateTime();
    final String dateString = nextRuntime.toString() + " " + getTime.getHourOfDay() + ":"
            + getTime.getMinuteOfHour() + ":" + getTime.getSecondOfMinute();
    final DateTimeFormatter simpleDateFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    final LocalDateTime newTriggerDateWithTime = LocalDateTime.parse(dateString, simpleDateFormat);

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

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

License:Apache License

@Transactional
@Override/*w  ww.  j  a  va  2 s  . c om*/
public CommandProcessingResult activateSmsCampaign(Long campaignId, JsonCommand command) {
    final AppUser currentUser = this.context.authenticatedUser();

    this.smsCampaignValidator.validateActivation(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 activationDate = command.localDateValueOfParameterNamed("activationDate");

    smsCampaign.activate(currentUser, fmt, activationDate);

    this.smsCampaignRepository.saveAndFlush(smsCampaign);

    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);
    }

    /*
     * if campaign is direct insert campaign message into sms outbound table
     * else if its a schedule create a job process for it
     */
    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//from w  w w  .  jav a  2  s.c om
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 va 2s. 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 LocalDateTime extractLocalTimeNamed(final String parameterName, final JsonObject element,
        final String timeFormat, final Locale clientApplicationLocale,
        final Set<String> parametersPassedInCommand) {
    LocalDateTime value = null;/* w w  w.  j ava  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.reportmailingjob.domain.ReportMailingJob.java

License:Apache License

/** 
 * create a new instance of the ReportmailingJob for a new entry 
 * //from  w w  w .ja va 2 s .c  om
 * @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 av a 2s . 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.validation.ReportMailingJobValidator.java

License:Apache License

/** 
 * validate the request to create a new report mailing job 
 * /*from   w  w  w.j  a va  2  s  .  com*/
 * @param jsonCommand -- the JSON command object (instance of the JsonCommand class)
 * @return None
 **/
public void validateCreateRequest(final JsonCommand jsonCommand) {
    final String jsonString = jsonCommand.json();
    final JsonElement jsonElement = jsonCommand.parsedJson();

    if (StringUtils.isBlank(jsonString)) {
        throw new InvalidJsonException();
    }

    final Type typeToken = new TypeToken<Map<String, Object>>() {
    }.getType();
    this.fromJsonHelper.checkForUnsupportedParameters(typeToken, jsonString,
            ReportMailingJobConstants.CREATE_REQUEST_PARAMETERS);

    final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
    final DataValidatorBuilder dataValidatorBuilder = new DataValidatorBuilder(dataValidationErrors)
            .resource(StringUtils.lowerCase(ReportMailingJobConstants.REPORT_MAILING_JOB_RESOURCE_NAME));

    final String name = this.fromJsonHelper.extractStringNamed(ReportMailingJobConstants.NAME_PARAM_NAME,
            jsonElement);
    dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.NAME_PARAM_NAME).value(name).notBlank()
            .notExceedingLengthOf(100);

    final String startDateTime = this.fromJsonHelper
            .extractStringNamed(ReportMailingJobConstants.START_DATE_TIME_PARAM_NAME, jsonElement);
    dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.START_DATE_TIME_PARAM_NAME)
            .value(startDateTime).notBlank();

    final Integer stretchyReportId = this.fromJsonHelper.extractIntegerWithLocaleNamed(
            ReportMailingJobConstants.STRETCHY_REPORT_ID_PARAM_NAME, jsonElement);
    dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.STRETCHY_REPORT_ID_PARAM_NAME)
            .value(stretchyReportId).notNull().integerGreaterThanZero();

    final String emailRecipients = this.fromJsonHelper
            .extractStringNamed(ReportMailingJobConstants.EMAIL_RECIPIENTS_PARAM_NAME, jsonElement);
    dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.EMAIL_RECIPIENTS_PARAM_NAME)
            .value(emailRecipients).notBlank();

    final String emailSubject = this.fromJsonHelper
            .extractStringNamed(ReportMailingJobConstants.EMAIL_SUBJECT_PARAM_NAME, jsonElement);
    dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.EMAIL_SUBJECT_PARAM_NAME)
            .value(emailSubject).notBlank().notExceedingLengthOf(100);

    final String emailMessage = this.fromJsonHelper
            .extractStringNamed(ReportMailingJobConstants.EMAIL_MESSAGE_PARAM_NAME, jsonElement);
    dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.EMAIL_MESSAGE_PARAM_NAME)
            .value(emailMessage).notBlank();

    if (this.fromJsonHelper.parameterExists(ReportMailingJobConstants.IS_ACTIVE_PARAM_NAME, jsonElement)) {
        final Boolean isActive = this.fromJsonHelper
                .extractBooleanNamed(ReportMailingJobConstants.IS_ACTIVE_PARAM_NAME, jsonElement);
        dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.IS_ACTIVE_PARAM_NAME).value(isActive)
                .notNull();
    }

    final Integer emailAttachmentFileFormatId = this.fromJsonHelper.extractIntegerSansLocaleNamed(
            ReportMailingJobConstants.EMAIL_ATTACHMENT_FILE_FORMAT_ID_PARAM_NAME, jsonElement);
    dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.EMAIL_ATTACHMENT_FILE_FORMAT_ID_PARAM_NAME)
            .value(emailAttachmentFileFormatId).notNull();

    if (emailAttachmentFileFormatId != null) {
        dataValidatorBuilder.reset()
                .parameter(ReportMailingJobConstants.EMAIL_ATTACHMENT_FILE_FORMAT_ID_PARAM_NAME)
                .value(emailAttachmentFileFormatId)
                .isOneOfTheseValues(ReportMailingJobEmailAttachmentFileFormat.validIds());
    }

    final String dateFormat = jsonCommand.dateFormat();
    dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.DATE_FORMAT_PARAM_NAME).value(dateFormat)
            .notBlank();

    if (StringUtils.isNotEmpty(dateFormat)) {

        try {
            final DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(dateFormat)
                    .withLocale(jsonCommand.extractLocale());

            // try to parse the date time string
            LocalDateTime.parse(startDateTime, dateTimeFormatter);
        }

        catch (IllegalArgumentException ex) {
            dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.DATE_FORMAT_PARAM_NAME)
                    .value(dateFormat).failWithCode("invalid.date.format");
        }
    }

    throwExceptionIfValidationWarningsExist(dataValidationErrors);
}

From source file:com.gst.infrastructure.reportmailingjob.validation.ReportMailingJobValidator.java

License:Apache License

/** 
 * validate the request to update a report mailing job 
 * //from w  w w . j a  v  a2 s.  co m
 * @param jsonCommand -- the JSON command object (instance of the JsonCommand class)
 * @return None
 **/
public void validateUpdateRequest(final JsonCommand jsonCommand) {
    final String jsonString = jsonCommand.json();
    final JsonElement jsonElement = jsonCommand.parsedJson();

    if (StringUtils.isBlank(jsonString)) {
        throw new InvalidJsonException();
    }

    final Type typeToken = new TypeToken<Map<String, Object>>() {
    }.getType();
    this.fromJsonHelper.checkForUnsupportedParameters(typeToken, jsonString,
            ReportMailingJobConstants.UPDATE_REQUEST_PARAMETERS);

    final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
    final DataValidatorBuilder dataValidatorBuilder = new DataValidatorBuilder(dataValidationErrors)
            .resource(StringUtils.lowerCase(ReportMailingJobConstants.REPORT_MAILING_JOB_RESOURCE_NAME));

    if (this.fromJsonHelper.parameterExists(ReportMailingJobConstants.NAME_PARAM_NAME, jsonElement)) {
        final String name = this.fromJsonHelper.extractStringNamed(ReportMailingJobConstants.NAME_PARAM_NAME,
                jsonElement);
        dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.NAME_PARAM_NAME).value(name).notBlank()
                .notExceedingLengthOf(100);
    }

    if (this.fromJsonHelper.parameterExists(ReportMailingJobConstants.STRETCHY_REPORT_ID_PARAM_NAME,
            jsonElement)) {
        final Integer stretchyReportId = this.fromJsonHelper.extractIntegerWithLocaleNamed(
                ReportMailingJobConstants.STRETCHY_REPORT_ID_PARAM_NAME, jsonElement);
        dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.STRETCHY_REPORT_ID_PARAM_NAME)
                .value(stretchyReportId).notNull().integerGreaterThanZero();
    }

    if (this.fromJsonHelper.parameterExists(ReportMailingJobConstants.EMAIL_RECIPIENTS_PARAM_NAME,
            jsonElement)) {
        final String emailRecipients = this.fromJsonHelper
                .extractStringNamed(ReportMailingJobConstants.EMAIL_RECIPIENTS_PARAM_NAME, jsonElement);
        dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.EMAIL_RECIPIENTS_PARAM_NAME)
                .value(emailRecipients).notBlank();
    }

    if (this.fromJsonHelper.parameterExists(ReportMailingJobConstants.EMAIL_SUBJECT_PARAM_NAME, jsonElement)) {
        final String emailSubject = this.fromJsonHelper
                .extractStringNamed(ReportMailingJobConstants.EMAIL_SUBJECT_PARAM_NAME, jsonElement);
        dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.EMAIL_SUBJECT_PARAM_NAME)
                .value(emailSubject).notBlank().notExceedingLengthOf(100);
    }

    if (this.fromJsonHelper.parameterExists(ReportMailingJobConstants.EMAIL_MESSAGE_PARAM_NAME, jsonElement)) {
        final String emailMessage = this.fromJsonHelper
                .extractStringNamed(ReportMailingJobConstants.EMAIL_MESSAGE_PARAM_NAME, jsonElement);
        dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.EMAIL_MESSAGE_PARAM_NAME)
                .value(emailMessage).notBlank();
    }

    if (this.fromJsonHelper.parameterExists(ReportMailingJobConstants.IS_ACTIVE_PARAM_NAME, jsonElement)) {
        final Boolean isActive = this.fromJsonHelper
                .extractBooleanNamed(ReportMailingJobConstants.IS_ACTIVE_PARAM_NAME, jsonElement);
        dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.IS_ACTIVE_PARAM_NAME).value(isActive)
                .notNull();
    }

    if (this.fromJsonHelper.parameterExists(
            ReportMailingJobConstants.EMAIL_ATTACHMENT_FILE_FORMAT_ID_PARAM_NAME, jsonElement)) {
        final Integer emailAttachmentFileFormatId = this.fromJsonHelper.extractIntegerSansLocaleNamed(
                ReportMailingJobConstants.EMAIL_ATTACHMENT_FILE_FORMAT_ID_PARAM_NAME, jsonElement);
        dataValidatorBuilder.reset()
                .parameter(ReportMailingJobConstants.EMAIL_ATTACHMENT_FILE_FORMAT_ID_PARAM_NAME)
                .value(emailAttachmentFileFormatId).notNull();

        if (emailAttachmentFileFormatId != null) {
            dataValidatorBuilder.reset()
                    .parameter(ReportMailingJobConstants.EMAIL_ATTACHMENT_FILE_FORMAT_ID_PARAM_NAME)
                    .value(emailAttachmentFileFormatId)
                    .isOneOfTheseValues(ReportMailingJobEmailAttachmentFileFormat.validIds());
        }
    }

    if (this.fromJsonHelper.parameterExists(ReportMailingJobConstants.START_DATE_TIME_PARAM_NAME,
            jsonElement)) {
        final String dateFormat = jsonCommand.dateFormat();
        dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.DATE_FORMAT_PARAM_NAME)
                .value(dateFormat).notBlank();

        final String startDateTime = this.fromJsonHelper
                .extractStringNamed(ReportMailingJobConstants.START_DATE_TIME_PARAM_NAME, jsonElement);
        dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.START_DATE_TIME_PARAM_NAME)
                .value(startDateTime).notBlank();

        if (StringUtils.isNotEmpty(dateFormat)) {

            try {
                final DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(dateFormat)
                        .withLocale(jsonCommand.extractLocale());

                // try to parse the date time string
                LocalDateTime.parse(startDateTime, dateTimeFormatter);
            }

            catch (IllegalArgumentException ex) {
                dataValidatorBuilder.reset().parameter(ReportMailingJobConstants.DATE_FORMAT_PARAM_NAME)
                        .value(dateFormat).failWithCode("invalid.date.format");
            }
        }
    }

    throwExceptionIfValidationWarningsExist(dataValidationErrors);
}

From source file:com.miserablemind.api.consumer.tradeking.api.impl.response_entities.TKMarketStatusResponse.java

License:Open Source License

/**
 * Deserializer does not understand the milliseconds, need to do it manually here
 *
 * @param dateResponse a String date from Json
 *//*from  w  w  w  .j  a v a 2s  . c  om*/
@JsonSetter("date")
public void setDate(String dateResponse) {
    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
    this.date = LocalDateTime.parse(dateResponse, formatter);
}