Example usage for org.apache.commons.lang.time DateUtils parseDate

List of usage examples for org.apache.commons.lang.time DateUtils parseDate

Introduction

In this page you can find the example usage for org.apache.commons.lang.time DateUtils parseDate.

Prototype

public static Date parseDate(String str, String... parsePatterns) throws ParseException 

Source Link

Document

Parses a string representing a date by trying a variety of different parsers.

The parse will try each parse pattern in turn.

Usage

From source file:org.isatools.isatab.commandline.permission_manager.PermissionManager.java

/**
 * Creates a user from the options ({@link #createUserDefOptions()}) passed to the command line.
 *///  w  w w  .j  av  a2s  . c  o  m
public Person createNewUserFromOptions(String login, CommandLine cmdl) {
    String forename = cmdl.getOptionValue("n");
    String surname = cmdl.getOptionValue("s");
    String pwd = cmdl.getOptionValue("p");
    String affiliation = cmdl.getOptionValue("f");
    String addr = cmdl.getOptionValue("a");
    String email = cmdl.getOptionValue("e");
    String dates = cmdl.getOptionValue("d");
    Date date = null;
    if (dates != null) {
        try {
            date = DateUtils.parseDate(dates, VALID_DATE_FORMATS);
        } catch (ParseException ex) {
            throw new TabInvalidValueException("Date '" + dates + "' is invalid");
        }
    }

    String role = cmdl.getOptionValue("r");
    if (role != null && !"submitter".equalsIgnoreCase(role) && !"curator".equalsIgnoreCase(role)) {
        throw new TabInvalidValueException("role value: '" + role + "' is invalid");
    }
    Person result = new Person();

    result.setUserName(login);
    result.setFirstName(forename);
    result.setLastName(surname);
    if (pwd != null) {
        result.setPassword(StringEncryption.getInstance().encrypt(pwd));
    }
    result.setAffiliation(affiliation);
    result.setAddress(addr);
    result.setEmail(email);
    result.setJoinDate(date);

    if ("submitter".equalsIgnoreCase(role)) {
        result.setRole(UserRole.SUBMITTER);
    } else if ("curator".equalsIgnoreCase(role)) {
        result.setRole(UserRole.CURATOR);
    }

    return result;
}

From source file:org.isatools.isatab.isaconfigurator.validators.FieldValuesValidator.java

/**
 * Does the job for a single field./*from  w  w  w  . ja  v  a 2  s .c o  m*/
 */
private boolean validateSingleField(Record record, int icol, IsaTabConfigurationType cfg, FieldType cfgField) {
    String dataType = StringUtils.trimToNull(cfgField.getDataType());
    if (dataType == null || "string".equalsIgnoreCase(dataType)) {
        return true;
    }

    String value = StringUtils.trimToNull(record.getString(icol));
    if (value == null) {
        if (cfgField.getIsRequired()) {
            messages.add("Missing value for the required field '" + cfgField.getHeader() + "' in the file '"
                    + record.getParent().getFileId() + "'");
        }
        return true;
    }

    boolean isValidValue = true;

    if ("boolean".equalsIgnoreCase(dataType)) {
        isValidValue = "true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value);
    } else if ("date".equalsIgnoreCase(dataType)) {
        try {
            DateUtils.parseDate(value, DatePropertyMappingHelper.VALID_FORMATS);
        } catch (ParseException e) {
            isValidValue = false;
        }
    } else if ("integer".equalsIgnoreCase(dataType)) {
        try {
            Integer.parseInt(value);
        } catch (NumberFormatException e) {
            isValidValue = false;
        }
    } else if ("double".equalsIgnoreCase(dataType)) {
        try {
            Double.parseDouble(value);
        } catch (NumberFormatException e) {
            isValidValue = false;
        }
    } else if ("ontology-term".equalsIgnoreCase(dataType) || "Ontology term".equalsIgnoreCase(dataType)) {
        return true;
    } else {
        // TODO: list, OEs
        messages.add("Unknown data type '" + dataType + "' for field '" + cfgField.getHeader()
                + "' in the file '" + record.getParent().getFileId() + "'");
        // TODO: false?
        return false;
    }

    if (!isValidValue) {
        String header = cfgField.getHeader();
        log.debug("Invalid value '" + value + "' for type '" + dataType + "' of the field '" + header + "'");
        messages.add("Invalid values found in the field '" + header + "' in the file '"
                + record.getParent().getFileId());
    }

    return isValidValue;
}

From source file:org.isatools.tablib.mapping.properties.DatePropertyMappingHelper.java

/**
 * Simply returns the value found at the field {@link #getFieldIndex()}, after having trimmed it.
 *
 * @see org.isatools.tablib.mapping.properties.PropertyMappingHelper#mapProperty(int)
 *//*  www .j a  va  2 s . com*/
@Override
public Date mapProperty(int recordIndex) {
    SectionInstance sectionInstance = getSectionInstance();
    String tabValue = sectionInstance.getString(recordIndex, getFieldIndex());
    String dates = StringUtils.trimToNull(tabValue);
    if (dates == null) {
        return null;
    }
    Date result = null;
    try {
        result = DateUtils.parseDate(dates, VALID_FORMATS);
    } catch (ParseException e) {
        log.warn(i18n.msg("invalid_date_format", dates, sectionInstance.getField(getFieldIndex()).getId()));
    }
    return result;
}

From source file:org.jumpmind.symmetric.db.oracle.OracleSymmetricDialect.java

@Override
public boolean areDatabaseTransactionsPendingSince(long time) {
    String returnValue = platform.getSqlTemplate().queryForObject(SQL_SELECT_TRANSACTIONS, String.class);
    if (returnValue != null) {
        Date date;/*from  w  ww . jav  a  2 s .  com*/
        try {
            date = DateUtils.parseDate(returnValue, new String[] { "MM/dd/yy HH:mm:ss" });
            return date.getTime() < time;
        } catch (ParseException e) {
            log.error("", e);
            return true;
        }
    } else {
        return false;
    }

}

From source file:org.jumpmind.symmetric.test.SimpleIntegrationTest.java

@Test(timeout = 120000)
public void test11SuspendIgnorePushRemoteBatches() throws Exception {
    // test suspend / ignore with remote database specifying the suspends
    // and ignores
    logTestRunning();/*from w ww  .j av  a  2  s . c  o  m*/

    Date date = DateUtils.parseDate("2007-01-03", new String[] { "yyyy-MM-dd" });
    Order order = new Order("101", 100, null, date);
    order.getOrderDetails().add(new OrderDetail("101", 1, "STK", "110000065", 3, new BigDecimal("3.33")));

    assertNull(serverTestService.getOrder(order.getOrderId()));

    clientTestService.insertOrder(order);

    boolean pushedData = clientPush();

    logger.error("Done pushing data");

    assertTrue("Client data was not batched and pushed", pushedData);

    assertNotNull(serverTestService.getOrder(order.getOrderId()));

    IConfigurationService rootConfigurationService = getServer().getConfigurationService();
    IOutgoingBatchService clientOutgoingBatchService = getClient().getOutgoingBatchService();

    date = DateUtils.parseDate("2007-01-03", new String[] { "yyyy-MM-dd" });
    order = new Order("102", 100, null, date);
    order.getOrderDetails().add(new OrderDetail("102", 1, "STK", "110000065", 3, new BigDecimal("3.33")));

    assertNull(serverTestService.getOrder(order.getOrderId()));

    clientTestService.insertOrder(order);

    getClient().route();

    NodeChannel c = rootConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setSuspendEnabled(true);
    rootConfigurationService.saveNodeChannel(c, true);

    OutgoingBatches batches = clientOutgoingBatchService
            .getOutgoingBatches(TestConstants.TEST_ROOT_NODE.getNodeId(), false);

    assertEquals("There should be 1 outgoing batches.", 1, batches.getBatches().size());

    pushedData = clientPush();

    assertNull("The order record was synchronized when it should not have been",
            serverTestService.getOrder(order.getOrderId()));

    c = rootConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setIgnoreEnabled(true);
    rootConfigurationService.saveNodeChannel(c, true);

    clientPush();

    batches = clientOutgoingBatchService.getOutgoingBatches(TestConstants.TEST_ROOT_NODE.getNodeId(), false);

    assertEquals("There should be no outgoing batches", 1, batches.getBatches().size());

    assertNull("The order record was synchronized when it should not have been",
            serverTestService.getOrder(order.getOrderId()));

    // Cleanup!
    c = rootConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setSuspendEnabled(false);
    c.setIgnoreEnabled(false);
    rootConfigurationService.saveNodeChannel(c, true);

    clientPush();

    batches = clientOutgoingBatchService.getOutgoingBatches(TestConstants.TEST_ROOT_NODE.getNodeId(), false);

    assertEquals("There should be no outgoing batches", 0, batches.getBatches().size());

    assertNotNull("The order record was synchronized when it should not have been",
            serverTestService.getOrder(order.getOrderId()));
}

From source file:org.jumpmind.symmetric.test.SimpleIntegrationTest.java

@Test(timeout = 120000)
public void test12SuspendIgnorePushLocalBatches() throws Exception {

    // test suspend / ignore with local database specifying the suspends
    // and ignores
    logTestRunning();//w ww  .  j a  v a  2  s.c o m
    Date date = DateUtils.parseDate("2007-01-03", new String[] { "yyyy-MM-dd" });
    Order order = new Order("105", 100, null, date);
    order.getOrderDetails().add(new OrderDetail("105", 1, "STK", "110000065", 3, new BigDecimal("3.33")));

    assertNull(serverTestService.getOrder(order.getOrderId()));

    clientTestService.insertOrder(order);

    clientPush();

    assertNotNull(serverTestService.getOrder(order.getOrderId()));

    IConfigurationService clientConfigurationService = getClient().getConfigurationService();
    IOutgoingBatchService clientOutgoingBatchService = getClient().getOutgoingBatchService();

    NodeChannel c = clientConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setSuspendEnabled(true);
    clientConfigurationService.saveNodeChannel(c, true);

    order = new Order("106", 100, null, date);
    order.getOrderDetails().add(new OrderDetail("106", 1, "STK", "110000065", 3, new BigDecimal("3.33")));

    clientPush();

    OutgoingBatches batches = clientOutgoingBatchService
            .getOutgoingBatches(TestConstants.TEST_ROOT_NODE.getNodeId(), false);

    assertEquals("There should be no outgoing batches since suspended locally", 0, batches.getBatches().size());

    assertNull(serverTestService.getOrder(order.getOrderId()));

    c = clientConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setIgnoreEnabled(true);
    clientConfigurationService.saveNodeChannel(c, true);
    clientPush();

    batches = clientOutgoingBatchService.getOutgoingBatches(TestConstants.TEST_ROOT_NODE.getNodeId(), false);

    assertEquals("There should be no outgoing batches since suspended locally", 0, batches.getBatches().size());

    assertNull(serverTestService.getOrder(order.getOrderId()));

    // Cleanup!
    c = clientConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setSuspendEnabled(false);
    c.setIgnoreEnabled(false);
    clientConfigurationService.saveNodeChannel(c, true);
    clientPush();
}

From source file:org.jumpmind.symmetric.test.SimpleIntegrationTest.java

@Test(timeout = 120000)
public void test13SuspendIgnorePullRemoteBatches() throws Exception {

    // test suspend / ignore with remote database specifying the suspends
    // and ignores

    logTestRunning();/*  www  .jav  a  2  s  .c o  m*/

    clientPull();

    Date date = DateUtils.parseDate("2009-09-30", new String[] { "yyyy-MM-dd" });
    Order order = new Order("42", 100, "C", date);
    serverTestService.insertOrder(order);

    clientPull();

    IOutgoingBatchService rootOutgoingBatchService = getServer().getOutgoingBatchService();
    OutgoingBatches batches = rootOutgoingBatchService
            .getOutgoingBatches(TestConstants.TEST_CLIENT_NODE.getNodeId(), false);

    assertNotNull(clientTestService.getOrder(order.getOrderId()));

    assertEquals("There should be no outgoing batches", 0, batches.getBatches().size());

    // Suspend the channel...

    IConfigurationService rootConfigurationService = getServer().getConfigurationService();
    NodeChannel c = rootConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setSuspendEnabled(true);
    rootConfigurationService.saveNodeChannel(c, true);

    date = DateUtils.parseDate("2009-09-30", new String[] { "yyyy-MM-dd" });
    order = new Order("43", 100, "C", date);
    serverTestService.insertOrder(order);

    clientPull();

    batches = rootOutgoingBatchService.getOutgoingBatches(TestConstants.TEST_CLIENT_NODE.getNodeId(), false);

    assertEquals("There should be 1 outgoing batch", 1, batches.getBatches().size());

    assertNull(clientTestService.getOrder(order.getOrderId()));

    c = rootConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setIgnoreEnabled(true);
    rootConfigurationService.saveNodeChannel(c, true);

    // ignore
    clientPull();

    batches = rootOutgoingBatchService.getOutgoingBatches(TestConstants.TEST_CLIENT_NODE.getNodeId(), false);

    assertNull(clientTestService.getOrder(order.getOrderId()));

    assertEquals("There should be no outgoing batches", 0, batches.getBatches().size());

    // Cleanup!
    c = rootConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setSuspendEnabled(false);
    c.setIgnoreEnabled(false);
    rootConfigurationService.saveNodeChannel(c, true);

    clientPull();

    batches = rootOutgoingBatchService.getOutgoingBatches(TestConstants.TEST_CLIENT_NODE.getNodeId(), false);

    assertEquals("There should be no outgoing batches", 0, batches.getBatches().size());

    assertNull(clientTestService.getOrder(order.getOrderId()));

}

From source file:org.jumpmind.symmetric.test.SimpleIntegrationTest.java

@Test(timeout = 120000)
public void test14SuspendIgnorePullRemoteLocalComboBatches() throws Exception {

    // test suspend / ignore with remote database specifying the suspends
    // and ignores

    logTestRunning();/* w  w w . j  a v a  2 s  .c  o  m*/

    Date date = DateUtils.parseDate("2009-09-30", new String[] { "yyyy-MM-dd" });
    Order order = new Order("442", 100, "C", date);
    serverTestService.insertOrder(order);
    clientPull();

    IOutgoingBatchService rootOutgoingBatchService = getServer().getOutgoingBatchService();

    OutgoingBatches batches = rootOutgoingBatchService
            .getOutgoingBatches(TestConstants.TEST_CLIENT_NODE.getNodeId(), false);

    assertNotNull(clientTestService.getOrder(order.getOrderId()));
    assertEquals("There should be no outgoing batches", 0, batches.getBatches().size());

    IConfigurationService rootConfigurationService = getServer().getConfigurationService();
    IConfigurationService clientConfigurationService = getClient().getConfigurationService();

    // suspend on remote

    NodeChannel c = rootConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setSuspendEnabled(true);
    rootConfigurationService.saveNodeChannel(c, true);

    // ignore on local

    c = clientConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setIgnoreEnabled(true);
    clientConfigurationService.saveNodeChannel(c, true);

    order = new Order("443", 100, "C", date);
    serverTestService.insertOrder(order);
    clientPull();

    batches = rootOutgoingBatchService.getOutgoingBatches(TestConstants.TEST_CLIENT_NODE.getNodeId(), false);
    assertEquals("There should be no outgoing batches", 0, batches.getBatches().size());
    assertNull(clientTestService.getOrder(order.getOrderId()));

    // ignore on remote

    c = rootConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setIgnoreEnabled(true);
    c.setSuspendEnabled(false);
    rootConfigurationService.saveNodeChannel(c, true);

    // suspend on local

    c = clientConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setIgnoreEnabled(false);
    c.setSuspendEnabled(true);
    clientConfigurationService.saveNodeChannel(c, true);

    order = new Order("444", 100, "C", date);
    clientPull();

    batches = rootOutgoingBatchService.getOutgoingBatches(TestConstants.TEST_CLIENT_NODE.getNodeId(), false);
    assertEquals("There should be no outgoing batches", 0, batches.getBatches().size());
    assertNull(clientTestService.getOrder(order.getOrderId()));

    // Cleanup!
    c = rootConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setSuspendEnabled(false);
    c.setIgnoreEnabled(false);
    rootConfigurationService.saveNodeChannel(c, true);

    c = clientConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setSuspendEnabled(false);
    c.setIgnoreEnabled(false);
    clientConfigurationService.saveNodeChannel(c, true);

    clientPull();

}

From source file:org.jumpmind.symmetric.test.SimpleIntegrationTest.java

@Test(timeout = 120000)
public void test16SuspendIgnorePullLocalBatches() throws Exception {

    // test suspend / ignore with local database specifying suspends and
    // ignores/* w  w  w.  ja  v a2  s . com*/

    logTestRunning();
    // Should not sync when status = null
    Date date = DateUtils.parseDate("2009-09-30", new String[] { "yyyy-MM-dd" });
    Order order = new Order("44", 100, "C", date);
    serverTestService.insertOrder(order);
    clientPull();

    IOutgoingBatchService rootOutgoingBatchService = getServer().getOutgoingBatchService();
    OutgoingBatches batches = rootOutgoingBatchService
            .getOutgoingBatches(TestConstants.TEST_CLIENT_NODE.getNodeId(), false);
    assertNotNull(clientTestService.getOrder(order.getOrderId()));
    assertEquals("There should be no outgoing batches", 0, batches.getBatches().size());

    // Suspend the channel...

    IConfigurationService clientConfigurationService = getClient().getConfigurationService();
    NodeChannel c = clientConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setSuspendEnabled(true);
    clientConfigurationService.saveNodeChannel(c, true);

    order = new Order("45", 100, "C", date);
    serverTestService.insertOrder(order);

    clientPull();

    batches = rootOutgoingBatchService.getOutgoingBatches(TestConstants.TEST_CLIENT_NODE.getNodeId(), false);
    assertNull(clientTestService.getOrder(order.getOrderId()));
    assertEquals("There should be 1 outgoing batches", 1, batches.getBatches().size());

    c = clientConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setIgnoreEnabled(true);
    clientConfigurationService.saveNodeChannel(c, true);

    // ignore

    clientPull();

    assertNoPendingBatchesOnServer();

    c = clientConfigurationService.getNodeChannel(TestConstants.TEST_CHANNEL_ID,
            TestConstants.TEST_CLIENT_EXTERNAL_ID, false);
    c.setSuspendEnabled(false);
    c.setIgnoreEnabled(false);
    clientConfigurationService.saveNodeChannel(c, true);

    clientPull();

}

From source file:org.jumpmind.symmetric.test.SimpleIntegrationTest.java

@Test(timeout = 120000)
public void test19SyncToRoot() throws Exception {
    logTestRunning();/*  w  w w . j a  va2s. c o m*/
    Date date = DateUtils.parseDate("2007-01-03", new String[] { "yyyy-MM-dd" });
    Order order = new Order("10", 100, null, date);
    order.getOrderDetails().add(new OrderDetail("10", 1, "STK", "110000065", 3, new BigDecimal("3.33")));
    clientTestService.insertOrder(order);
    assertNull(serverTestService.getOrder(order.getOrderId()));
    clientPush();
    assertNotNull(serverTestService.getOrder(order.getOrderId()));
}