Example usage for javax.swing.text DateFormatter stringToValue

List of usage examples for javax.swing.text DateFormatter stringToValue

Introduction

In this page you can find the example usage for javax.swing.text DateFormatter stringToValue.

Prototype

public Object stringToValue(String text) throws ParseException 

Source Link

Document

Returns the Object representation of the String text.

Usage

From source file:com.mirth.connect.plugins.messagepruner.MessagePrunerService.java

private Trigger createTrigger(Properties properties) throws ParseException {
    Trigger trigger = null;/*from  ww  w .j a v  a2 s  .c  o  m*/
    String interval = PropertyLoader.getProperty(properties, "interval");

    if (interval.equals("hourly"))
        trigger = TriggerUtils.makeHourlyTrigger();
    else {
        SimpleDateFormat timeDateFormat = new SimpleDateFormat("hh:mm aa");
        DateFormatter timeFormatter = new DateFormatter(timeDateFormat);

        String time = PropertyLoader.getProperty(properties, "time");
        Date timeDate = (Date) timeFormatter.stringToValue(time);
        Calendar timeCalendar = Calendar.getInstance();
        timeCalendar.setTime(timeDate);

        if (interval.equals("daily")) {
            trigger = TriggerUtils.makeDailyTrigger(timeCalendar.get(Calendar.HOUR_OF_DAY),
                    timeCalendar.get(Calendar.MINUTE));
        } else if (interval.equals("weekly")) {
            SimpleDateFormat dayDateFormat = new SimpleDateFormat("EEEEEEEE");
            DateFormatter dayFormatter = new DateFormatter(dayDateFormat);

            String dayOfWeek = PropertyLoader.getProperty(properties, "dayOfWeek");
            Date dayDate = (Date) dayFormatter.stringToValue(dayOfWeek);
            Calendar dayCalendar = Calendar.getInstance();
            dayCalendar.setTime(dayDate);

            trigger = TriggerUtils.makeWeeklyTrigger(dayCalendar.get(Calendar.DAY_OF_WEEK),
                    timeCalendar.get(Calendar.HOUR_OF_DAY), timeCalendar.get(Calendar.MINUTE));
        } else if (interval.equals("monthly")) {
            SimpleDateFormat dayDateFormat = new SimpleDateFormat("DD");
            DateFormatter dayFormatter = new DateFormatter(dayDateFormat);

            String dayOfMonth = PropertyLoader.getProperty(properties, "dayOfMonth");
            Date dayDate = (Date) dayFormatter.stringToValue(dayOfMonth);
            Calendar dayCalendar = Calendar.getInstance();
            dayCalendar.setTime(dayDate);

            trigger = TriggerUtils.makeMonthlyTrigger(dayCalendar.get(Calendar.DAY_OF_MONTH),
                    timeCalendar.get(Calendar.HOUR_OF_DAY), timeCalendar.get(Calendar.MINUTE));
        }
    }

    trigger.setStartTime(new Date());
    trigger.setName("prunerTrigger");
    trigger.setJobName("prunerJob");
    return trigger;
}

From source file:com.mirth.connect.plugins.datapruner.DataPrunerService.java

private Trigger createTrigger(Properties properties) throws ParseException {
    String interval = PropertyLoader.getProperty(properties, "interval");

    if (interval.equals("disabled")) {
        return null;
    }/*  ww  w  . j a  v  a  2s  .  c o  m*/

    ScheduleBuilder<?> schedule = null;

    if (interval.equals("hourly")) {
        schedule = cronSchedule("0 0 * * * ?");
    } else {
        SimpleDateFormat timeDateFormat = new SimpleDateFormat("hh:mm aa");
        DateFormatter timeFormatter = new DateFormatter(timeDateFormat);

        String time = PropertyLoader.getProperty(properties, "time");
        Date timeDate = (Date) timeFormatter.stringToValue(time);
        Calendar timeCalendar = Calendar.getInstance();
        timeCalendar.setTime(timeDate);

        if (interval.equals("daily")) {
            schedule = dailyAtHourAndMinute(timeCalendar.get(Calendar.HOUR_OF_DAY),
                    timeCalendar.get(Calendar.MINUTE));
        } else if (interval.equals("weekly")) {
            SimpleDateFormat dayDateFormat = new SimpleDateFormat("EEEEEEEE");
            DateFormatter dayFormatter = new DateFormatter(dayDateFormat);

            String dayOfWeek = PropertyLoader.getProperty(properties, "dayOfWeek");
            Date dayDate = (Date) dayFormatter.stringToValue(dayOfWeek);
            Calendar dayCalendar = Calendar.getInstance();
            dayCalendar.setTime(dayDate);

            schedule = weeklyOnDayAndHourAndMinute(dayCalendar.get(Calendar.DAY_OF_WEEK),
                    timeCalendar.get(Calendar.HOUR_OF_DAY), timeCalendar.get(Calendar.MINUTE));
        } else if (interval.equals("monthly")) {
            SimpleDateFormat dayDateFormat = new SimpleDateFormat("DD");
            DateFormatter dayFormatter = new DateFormatter(dayDateFormat);

            String dayOfMonth = PropertyLoader.getProperty(properties, "dayOfMonth");
            Date dayDate = (Date) dayFormatter.stringToValue(dayOfMonth);
            Calendar dayCalendar = Calendar.getInstance();
            dayCalendar.setTime(dayDate);

            schedule = monthlyOnDayAndHourAndMinute(dayCalendar.get(Calendar.DAY_OF_MONTH),
                    timeCalendar.get(Calendar.HOUR_OF_DAY), timeCalendar.get(Calendar.MINUTE));
        } else {
            logger.error("Invalid pruner interval: " + interval);
            return null;
        }
    }

    return newTrigger()// @formatter:off
            .withIdentity(PRUNER_TRIGGER_KEY).withSchedule(schedule).startNow().build(); // @formatter:on
}

From source file:com.mirth.connect.server.migration.Migrate3_3_0.java

private void migrateDataPrunerConfiguration() {
    boolean enabled = true;
    String time = "";
    String interval = "";
    String dayOfWeek = "";
    String dayOfMonth = "1";

    ResultSet results = null;/*from  w ww  .j  a v  a 2  s . c  om*/
    PreparedStatement statement = null;
    Connection connection = null;

    try {
        connection = getConnection();
        try {
            statement = connection
                    .prepareStatement("SELECT NAME, VALUE FROM CONFIGURATION WHERE CATEGORY = 'Data Pruner'");

            results = statement.executeQuery();
            while (results.next()) {
                String name = results.getString(1);
                String value = results.getString(2);

                if (name.equals("interval")) {
                    interval = value;
                } else if (name.equals("time")) {
                    time = value;
                } else if (name.equals("dayOfWeek")) {
                    dayOfWeek = value;
                } else if (name.equals("dayOfMonth")) {
                    dayOfMonth = value;
                }
            }
        } catch (SQLException e) {
            logger.error("Failed to read Data Pruner configuration properties.", e);
        } finally {
            DbUtils.closeQuietly(statement);
            DbUtils.closeQuietly(results);
        }

        enabled = !interval.equals("disabled");
        String pollingType = "INTERVAL";
        String pollingHour = "12";
        String pollingMinute = "0";
        boolean weekly = !StringUtils.equals(interval, "monthly");
        boolean[] activeDays = new boolean[] { true, true, true, true, true, true, true, true };

        if (enabled && !StringUtils.equals(interval, "hourly")) {
            SimpleDateFormat timeDateFormat = new SimpleDateFormat("hh:mm aa");
            DateFormatter timeFormatter = new DateFormatter(timeDateFormat);
            Date timeDate = null;

            try {
                timeDate = (Date) timeFormatter.stringToValue(time);
                Calendar timeCalendar = Calendar.getInstance();
                timeCalendar.setTime(timeDate);

                pollingType = "TIME";
                pollingHour = String.valueOf(timeCalendar.get(Calendar.HOUR_OF_DAY));
                pollingMinute = String.valueOf(timeCalendar.get(Calendar.MINUTE));

                if (StringUtils.equals(interval, "weekly")) {
                    SimpleDateFormat dayDateFormat = new SimpleDateFormat("EEEEEEEE");
                    DateFormatter dayFormatter = new DateFormatter(dayDateFormat);

                    Date dayDate = (Date) dayFormatter.stringToValue(dayOfWeek);
                    Calendar dayCalendar = Calendar.getInstance();
                    dayCalendar.setTime(dayDate);

                    activeDays = new boolean[] { false, false, false, false, false, false, false, false };
                    activeDays[dayCalendar.get(Calendar.DAY_OF_WEEK)] = true;
                }
            } catch (Exception e) {
                logger.error("Failed to get Data Pruner time properties", e);
            }
        }

        DonkeyElement pollingProperties = new DonkeyElement(
                "<com.mirth.connect.donkey.model.channel.PollConnectorProperties/>");
        pollingProperties.setAttribute("version", "3.3.0");
        pollingProperties.addChildElementIfNotExists("pollingType", pollingType);
        pollingProperties.addChildElementIfNotExists("pollOnStart", "false");
        pollingProperties.addChildElementIfNotExists("pollingFrequency", "3600000");
        pollingProperties.addChildElementIfNotExists("pollingHour", pollingHour);
        pollingProperties.addChildElementIfNotExists("pollingMinute", pollingMinute);
        pollingProperties.addChildElementIfNotExists("cronJobs");

        DonkeyElement advancedProperties = pollingProperties
                .addChildElementIfNotExists("pollConnectorPropertiesAdvanced");
        advancedProperties.addChildElementIfNotExists("weekly", weekly ? "true" : "false");

        DonkeyElement inactiveDays = advancedProperties.addChildElementIfNotExists("inactiveDays");
        if (inactiveDays != null) {
            for (int index = 0; index < 8; ++index) {
                inactiveDays.addChildElement("boolean", activeDays[index] ? "false" : "true");
            }
        }

        advancedProperties.addChildElementIfNotExists("dayOfMonth", dayOfMonth);
        advancedProperties.addChildElementIfNotExists("allDay", "true");
        advancedProperties.addChildElementIfNotExists("startingHour", "8");
        advancedProperties.addChildElementIfNotExists("startingMinute", "0");
        advancedProperties.addChildElementIfNotExists("endingHour", "17");
        advancedProperties.addChildElementIfNotExists("endingMinute", "0");

        PreparedStatement inputStatement = null;
        try {
            inputStatement = connection
                    .prepareStatement("INSERT INTO CONFIGURATION (CATEGORY, NAME, VALUE) VALUES (?, ?, ?)");

            inputStatement.setString(1, "Data Pruner");
            inputStatement.setString(2, "pollingProperties");
            inputStatement.setString(3, pollingProperties.toXml());
            inputStatement.executeUpdate();
        } catch (Exception e) {
            logger.error("Failed to insert Data Pruner configuration pollingProperties.", e);
        } finally {
            DbUtils.closeQuietly(inputStatement);
        }

        PreparedStatement updateStatement = null;
        try {
            updateStatement = connection.prepareStatement(
                    "UPDATE CONFIGURATION SET NAME = ?, VALUE = ? WHERE CATEGORY = ? AND NAME = ?");
            updateStatement.setString(1, "enabled");
            updateStatement.setString(2, enabled ? "true" : "false");
            updateStatement.setString(3, "Data Pruner");
            updateStatement.setString(4, "interval");
            updateStatement.executeUpdate();
        } finally {
            DbUtils.closeQuietly(updateStatement);
        }

        PreparedStatement deleteStatement = null;
        try {
            deleteStatement = connection
                    .prepareStatement("DELETE FROM CONFIGURATION WHERE CATEGORY = ? AND NAME IN (?, ?, ?)");
            deleteStatement.setString(1, "Data Pruner");
            deleteStatement.setString(2, "time");
            deleteStatement.setString(3, "dayOfWeek");
            deleteStatement.setString(4, "dayOfMonth");
            deleteStatement.executeUpdate();
        } finally {
            DbUtils.closeQuietly(deleteStatement);
        }
    } catch (Exception e) {
        logger.error("Failed to modify Data Pruner configuration properties", e);
    }
}

From source file:com.mirth.connect.model.ServerConfiguration.java

@Override
public void migrate3_3_0(DonkeyElement element) {
    DonkeyElement librariesElement = element.addChildElement("codeTemplateLibraries");
    DonkeyElement libraryElement = librariesElement.addChildElement("codeTemplateLibrary");
    libraryElement.setAttribute("version", "3.3.0");
    libraryElement.addChildElement("id", UUID.randomUUID().toString());
    libraryElement.addChildElement("name", "Library 1");
    libraryElement.addChildElement("revision", "1");
    try {/* w  ww .  j a v a  2 s .  co m*/
        libraryElement
                .addChildElementFromXml(ObjectXMLSerializer.getInstance().serialize(Calendar.getInstance()))
                .setNodeName("lastModified");
    } catch (DonkeyElementException e) {
        throw new SerializerException("Failed to migrate code template library last modified date.", e);
    }
    libraryElement.addChildElement("description",
            "This library was added upon migration to version 3.3.0. It includes all pre-existing\ncode templates, and is set to be included on all pre-existing and new channels.\n\nYou should create your own new libraries and assign code templates to them as you\nsee fit. You should also link libraries to specific channels, so that you're not\nnecessarily including all code templates on all channels all the time.");
    libraryElement.addChildElement("includeNewChannels", "true");
    libraryElement.addChildElement("enabledChannelIds");
    libraryElement.addChildElement("disabledChannelIds");
    try {
        libraryElement.addChildElementFromXml(element.removeChild("codeTemplates").toXml());
    } catch (DonkeyElementException e) {
        throw new SerializerException("Failed to migrate code templates.", e);
    }

    boolean enabled = true;
    String time = "";
    String interval = "";
    String dayOfWeek = "";
    String dayOfMonth = "1";

    DonkeyElement properties = null;
    DonkeyElement pluginProperties = element.getChildElement("pluginProperties");
    if (pluginProperties != null) {
        for (DonkeyElement childElement : pluginProperties.getChildElements()) {
            String entryKey = childElement.getChildElement("string").getTextContent();
            if (entryKey.equals("Data Pruner")) {
                properties = childElement.getChildElement("properties");

                for (DonkeyElement property : properties.getChildElements()) {
                    String propertyName = property.getAttributes().item(0).getTextContent();

                    if (propertyName.equals("dayOfWeek")) {
                        dayOfWeek = property.getTextContent();
                    } else if (propertyName.equals("dayOfMonth")) {
                        dayOfMonth = property.getTextContent();
                    } else if (propertyName.equals("interval")) {
                        interval = property.getTextContent();
                    } else if (propertyName.equals("time")) {
                        time = property.getTextContent();
                    }
                }
            }
        }

        enabled = !interval.equals("disabled");
        String pollingType = "INTERVAL";
        String pollingHour = "12";
        String pollingMinute = "0";
        boolean weekly = !StringUtils.equals(interval, "monthly");
        boolean[] activeDays = new boolean[] { true, true, true, true, true, true, true, true };

        if (enabled && !StringUtils.equals(interval, "hourly")) {
            SimpleDateFormat timeDateFormat = new SimpleDateFormat("hh:mm aa");
            DateFormatter timeFormatter = new DateFormatter(timeDateFormat);
            Date timeDate = null;

            try {
                timeDate = (Date) timeFormatter.stringToValue(time);
                Calendar timeCalendar = Calendar.getInstance();
                timeCalendar.setTime(timeDate);

                pollingType = "TIME";
                pollingHour = String.valueOf(timeCalendar.get(Calendar.HOUR_OF_DAY));
                pollingMinute = String.valueOf(timeCalendar.get(Calendar.MINUTE));

                if (StringUtils.equals(interval, "weekly")) {
                    SimpleDateFormat dayDateFormat = new SimpleDateFormat("EEEEEEEE");
                    DateFormatter dayFormatter = new DateFormatter(dayDateFormat);

                    Date dayDate = (Date) dayFormatter.stringToValue(dayOfWeek);
                    Calendar dayCalendar = Calendar.getInstance();
                    dayCalendar.setTime(dayDate);

                    activeDays = new boolean[] { false, false, false, false, false, false, false, false };
                    activeDays[dayCalendar.get(Calendar.DAY_OF_WEEK)] = true;
                }
            } catch (Exception e) {
            }
        }

        try {
            DonkeyElement pollingProperties = new DonkeyElement(
                    "<com.mirth.connect.donkey.model.channel.PollConnectorProperties/>");
            pollingProperties.setAttribute("version", "3.3.0");
            pollingProperties.addChildElementIfNotExists("pollingType", pollingType);
            pollingProperties.addChildElementIfNotExists("pollOnStart", "false");
            pollingProperties.addChildElementIfNotExists("pollingFrequency", "3600000");
            pollingProperties.addChildElementIfNotExists("pollingHour", pollingHour);
            pollingProperties.addChildElementIfNotExists("pollingMinute", pollingMinute);
            pollingProperties.addChildElementIfNotExists("cronJobs");

            DonkeyElement advancedProperties = pollingProperties
                    .addChildElementIfNotExists("pollConnectorPropertiesAdvanced");
            advancedProperties.addChildElementIfNotExists("weekly", weekly ? "true" : "false");

            DonkeyElement inactiveDays = advancedProperties.addChildElementIfNotExists("inactiveDays");
            if (inactiveDays != null) {
                for (int index = 0; index < 8; ++index) {
                    inactiveDays.addChildElement("boolean", activeDays[index] ? "false" : "true");
                }
            }

            advancedProperties.addChildElementIfNotExists("dayOfMonth", dayOfMonth);
            advancedProperties.addChildElementIfNotExists("allDay", "true");
            advancedProperties.addChildElementIfNotExists("startingHour", "8");
            advancedProperties.addChildElementIfNotExists("startingMinute", "0");
            advancedProperties.addChildElementIfNotExists("endingHour", "17");
            advancedProperties.addChildElementIfNotExists("endingMinute", "0");

            DonkeyElement prunerProperty = properties.addChildElementFromXml(
                    ObjectXMLSerializer.getInstance().serialize(pollingProperties.toXml()));
            prunerProperty.setAttribute("name", "pollingProperties");

            DonkeyElement enabledProperty = properties.addChildElement("property", Boolean.toString(enabled));
            enabledProperty.setAttribute("name", "enabled");
        } catch (Exception e) {
            throw new SerializerException("Failed to migrate Data Pruner properties.", e);
        }
    }
}

From source file:com.mirth.connect.client.ui.panels.connectors.PollingSettingsPanel.java

public void fillProperties(PollConnectorPropertiesInterface propertiesInterface) {
    PollConnectorProperties properties = null;

    if (propertiesInterface != null) {
        properties = propertiesInterface.getPollConnectorProperties();
    } else {//  w w w  .j a v  a 2s.  co  m
        properties = new PollConnectorProperties();
    }

    String selectedPollingType = (String) scheduleTypeComboBox.getSelectedItem();

    properties.setPollOnStart(yesStartPollRadioButton.isSelected());
    if (selectedPollingType.equals(PollingType.INTERVAL.getDisplayName())) {
        properties.setPollingType(PollingType.INTERVAL);

        String type = (String) pollingFrequencyTypeComboBox.getSelectedItem();
        int frequency = NumberUtils.toInt(pollingFrequencyField.getText(), 0);
        if (type.equals(POLLING_FREQUENCY_HOURS)) {
            frequency *= 3600000;
        } else if (type.equals(POLLING_FREQUENCY_MINUTES)) {
            frequency *= 60000;
        } else if (type.equals(POLLING_FREQUENCY_SECONDS)) {
            frequency *= 1000;
        }

        properties.setPollingFrequency(frequency);
    } else if (selectedPollingType.equals(PollingType.TIME.getDisplayName())) {
        properties.setPollingType(PollingType.TIME);

        try {
            SimpleDateFormat timeDateFormat = new SimpleDateFormat("hh:mm aa");
            DateFormatter timeFormatter = new DateFormatter(timeDateFormat);
            Date timeDate = (Date) timeFormatter.stringToValue(pollingTimePicker.getDate());
            Calendar timeCalendar = Calendar.getInstance();
            timeCalendar.setTime(timeDate);

            properties.setPollingHour(timeCalendar.get(Calendar.HOUR_OF_DAY));
            properties.setPollingMinute(timeCalendar.get(Calendar.MINUTE));
        } catch (ParseException e) {
            // Do nothing since they could be manually entering in the time...
        }
    } else if (selectedPollingType.equals(PollingType.CRON.getDisplayName())) {
        properties.setPollingType(PollingType.CRON);

        List<CronProperty> cronJobs = new ArrayList<CronProperty>();

        for (int rowCount = 0; rowCount < cronJobsTable.getRowCount(); rowCount++) {
            String description = (String) cronJobsTable.getValueAt(rowCount, 1);
            String expression = (String) cronJobsTable.getValueAt(rowCount, 0);

            if (StringUtils.isNotBlank(expression)) {
                cronJobs.add(new CronProperty(description, expression));
            }
        }

        properties.setCronJobs(cronJobs);
    }

    properties.setPollConnectorPropertiesAdvanced(cachedAdvancedConnectorProperties);

    selectedPollingType = null;
    if (properties != null) {
        nextFireTimeProperties = properties.clone();
    }
}

From source file:com.mirth.connect.client.ui.browsers.event.EventBrowser.java

private Calendar getCalendar(MirthDatePicker datePicker, MirthTimePicker timePicker) throws ParseException {
    DateFormatter timeFormatter = new DateFormatter(new SimpleDateFormat("hh:mm aa"));
    Date date = datePicker.getDate();
    String time = timePicker.getDate();

    if (date != null && time != null) {
        Calendar dateCalendar = Calendar.getInstance();
        Calendar timeCalendar = Calendar.getInstance();
        Calendar dateTimeCalendar = Calendar.getInstance();

        dateCalendar.setTime(date);//from w  w w .  j  a v  a2  s .  c  om
        timeCalendar.setTime((Date) timeFormatter.stringToValue(time));
        dateTimeCalendar.setTime(date);

        // Only set the time if the time picker is enabled. Otherwise, it will default to 00:00:00
        if (timePicker.isEnabled()) {
            dateTimeCalendar.set(Calendar.HOUR_OF_DAY, timeCalendar.get(Calendar.HOUR_OF_DAY));
            dateTimeCalendar.set(Calendar.MINUTE, timeCalendar.get(Calendar.MINUTE));
            dateTimeCalendar.set(Calendar.SECOND, timeCalendar.get(Calendar.SECOND));
        }

        return dateTimeCalendar;
    }

    return null;
}