Java Code Examples for org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer

The following examples show how to use org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public JdbcDataflowTaskExecutionMetadataDao(DataSource dataSource,
		DataFieldMaxValueIncrementer incrementer) {

	this.incrementer = incrementer;

	this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);

	this.objectMapper = new ObjectMapper();
	SimpleModule module = new SimpleModule();
	module.addDeserializer(Resource.class,
			new ResourceDeserializer(new AppResourceCommon(new MavenProperties(), new DefaultResourceLoader())));
	this.objectMapper.registerModule(module);
	this.objectMapper.addMixIn(Resource.class, ResourceMixin.class);
	this.objectMapper.addMixIn(AppDefinition.class, AppDefinitionMixin.class);
	this.objectMapper.addMixIn(AppDeploymentRequest.class, AppDeploymentRequestMixin.class);
	this.objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

	this.dataSource = dataSource;
}
 
Example 2
/**
 * Tests that the incrementer returned from the factory returns the proper next int, long, and String values.
 */
@Test
public void testGetIncrementer_nextValues() {
    DataFieldMaxValueIncrementer incrementer = (DataFieldMaxValueIncrementer) context.getBean(TEST_INCREMENTER);
    assertNotNull(incrementer);

    // now that we have our incrementer, let's get the next value
    int nextIntValue = incrementer.nextIntValue();
    assertTrue("nextIntValue should be greater than 0", nextIntValue > 0);

    // do it again, should be 1 larger!
    int nextNextIntValue = incrementer.nextIntValue();
    assertEquals("Next value should be one higher", nextIntValue + 1, nextNextIntValue);

    // try getting the next value as a long
    long nextLongValue = incrementer.nextLongValue();
    assertEquals(nextNextIntValue + 1, nextLongValue);

    // try getting it as a String now
    String nextStringValue = incrementer.nextStringValue();
    assertEquals(nextLongValue + 1, Long.parseLong(nextStringValue));
}
 
Example 3
/**
 * Tests that the incrementer returned from the factory returns the proper next int, long, and String values.
 */
@Test
public void testGetIncrementer_nextValues() {
    DataSource dataSource = TestHarnessServiceLocator.getDataSource();
    DataFieldMaxValueIncrementer incrementer =
            MaxValueIncrementerFactory.getIncrementer(dataSource, ARBITRARY_SEQUENCE);
    assertNotNull(incrementer);

    // now that we have our incrementer, let's get the next value
    int nextIntValue = incrementer.nextIntValue();
    assertTrue("nextIntValue should be greater than 0", nextIntValue > 0);

    // do it again, should be 1 larger!
    int nextNextIntValue = incrementer.nextIntValue();
    assertEquals("Next value should be one higher", nextIntValue + 1, nextNextIntValue);

    // try getting the next value as a long
    long nextLongValue = incrementer.nextLongValue();
    assertEquals(nextNextIntValue + 1, nextLongValue);

    // try getting it as a String now
    String nextStringValue = incrementer.nextStringValue();
    assertEquals(nextLongValue + 1, Long.parseLong(nextStringValue));
}
 
Example 4
@Test
public void testCustomIncrementerDatasourceVersion() throws Exception {
    SimpleConfig config = new SimpleConfig();
    config.putProperty("rice.krad.data.platform.incrementer.mysql.5",
            "org.kuali.rice.krad.data.platform.testincrementers.CustomIncrementerMySQLVersion5");
    config.putProperty("rice.krad.data.platform.incrementer.oracle.11",
            "org.kuali.rice.krad.data.platform.testincrementers.CustomIncrementerOracleVersion11");
    ConfigContext.init(config);

    DataFieldMaxValueIncrementer mySQLMaxVal = MaxValueIncrementerFactory.getIncrementer(mysql,"test_mySQL");
    assertTrue("Found MySQL custom incrementer",mySQLMaxVal != null);
    assertTrue("Custom incrementer for MySQL should be mySQL5 for String val",
                    StringUtils.equals(mySQLMaxVal.nextStringValue(),"mySQL5"));

    DataFieldMaxValueIncrementer oracleMaxVal = MaxValueIncrementerFactory.getIncrementer(oracle,"test_oracle");
    assertTrue("Found Oracle custom incrementer", oracleMaxVal != null);
}
 
Example 5
@Test
public void testCustomIncrementerDatasourceNoVersion() throws Exception {
    SimpleConfig config = new SimpleConfig();
    config.putProperty("rice.krad.data.platform.incrementer.mysql",
            "org.kuali.rice.krad.data.platform.testincrementers.CustomIncrementerMySQLVersion5");
    config.putProperty("rice.krad.data.platform.incrementer.oracle",
            "org.kuali.rice.krad.data.platform.testincrementers.CustomIncrementerOracleVersion11");
    ConfigContext.init(config);

    DataFieldMaxValueIncrementer mySQLMaxVal = MaxValueIncrementerFactory.getIncrementer(mysql,"test_mySQL");
    assertTrue("Found MySQL custom incrementer",mySQLMaxVal != null);
    assertTrue("Custom incrementer for MySQL should be mySQL5 for String val",
            StringUtils.equals(mySQLMaxVal.nextStringValue(),"mySQL5"));

    DataFieldMaxValueIncrementer oracleMaxVal = MaxValueIncrementerFactory.getIncrementer(oracle,"test_oracle");
    assertTrue("Found Oracle custom incrementer", oracleMaxVal != null);
}
 
Example 6
Source Project: spring-cloud-task   Source File: TestDBUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a incrementer for the DataSource.
 * @param dataSource the datasource that the incrementer will use to record current
 * id.
 * @return a DataFieldMaxValueIncrementer object.
 */
public static DataFieldMaxValueIncrementer getIncrementer(DataSource dataSource) {
	DataFieldMaxValueIncrementerFactory incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(
			dataSource);
	String databaseType = null;
	try {
		databaseType = DatabaseType.fromMetaData(dataSource).name();
	}
	catch (MetaDataAccessException e) {
		throw new IllegalStateException(e);
	}
	return incrementerFactory.getIncrementer(databaseType, "TASK_SEQ");
}
 
Example 7
/**
 * Tests that the sequence name is case insensitive. We will do this by using the same sequence name as the
 * previous test, but changing the case to all lowercase.
 */
@Test
public void testGetIncrementer_CaseInsensitive() {
    DataFieldMaxValueIncrementer incrementer = (DataFieldMaxValueIncrementer) context.getBean(TEST_INCREMENTER);
    assertNotNull(incrementer);

    // now that we have our incrementer, let's get the next value
    int nextIntValue = incrementer.nextIntValue();
    assertTrue("nextIntValue should be greater than 0", nextIntValue > 0);
}
 
Example 8
/**
 * Tests that if you try to use the factory with an invalid sequence name, it will throw a DataAccessException.
 */
@Test(expected = DataAccessException.class)
public void testGetIncrementer_BadSequence() {
    DataFieldMaxValueIncrementer incrementer = (DataFieldMaxValueIncrementer) context.getBean(INVALID_TEST_INCREMENTER);

    // the incrementer *may* still be retrieved successfully (depending on the database this integration test is run against)
    assertNotNull(incrementer);

    // but at the very least it should throw an exception when executed
    incrementer.nextLongValue();
}
 
Example 9
/**
 * Tests that the sequence name is case insensitive. We will do this by using the same sequence name as the
 * previous test, but changing the case to all lowercase.
 */
@Test
public void testGetIncrementer_CaseInsensitive() {
    DataSource dataSource = TestHarnessServiceLocator.getDataSource();
    DataFieldMaxValueIncrementer incrementer =
            MaxValueIncrementerFactory.getIncrementer(dataSource, ARBITRARY_SEQUENCE.toLowerCase());
    assertNotNull(incrementer);

    // now that we have our incrementer, let's get the next value
    int nextIntValue = incrementer.nextIntValue();
    assertTrue("nextIntValue should be greater than 0", nextIntValue > 0);
}
 
Example 10
/**
 * Tests that if you try to use the factory with an invalid sequence name, it will throw a DataAccessException.
 */
@Test(expected = DataAccessException.class)
public void testGetIncrementer_BadSequence() {
    DataSource dataSource = TestHarnessServiceLocator.getDataSource();
    DataFieldMaxValueIncrementer incrementer =
            MaxValueIncrementerFactory.getIncrementer(dataSource, "OH_NO_YOU_DIDNT!");

    // the incrementer *may* still be retrieved successfully (depending on the database this integration test is run against)
    assertNotNull(incrementer);

    // but at the very least it should throw an exception when executed
    incrementer.nextLongValue();
}
 
Example 11
/**
 * Either constructs a new incrementer or retrieves a cached instance for the given DataSource and target
 * incrementer name.
 *
 * @param dataSource the {@link DataSource} for which to retrieve the incrementer.
 * @param incrementerName the case-insensitive name of the incrementer to use, this will generally be the name of
 *        the database object which is used to implement the incrementer.
 * @return an incrementer that can be used to generate the next incremented value for the given incrementer against
 *         the specified {@link DataSource}.
 *
 * @throws IllegalArgumentException if dataSource or incrementerName are null or blank.
 */
public static DataFieldMaxValueIncrementer getIncrementer(DataSource dataSource, String incrementerName) {
    if (dataSource == null) {
        throw new IllegalArgumentException("DataSource must not be null");
    }
    if (StringUtils.isBlank(incrementerName)) {
        throw new IllegalArgumentException("Incrementer name must not be null or blank");
    }

    // yes, we want to check if it's there first, then put if absent, for max speed! This is like ConcurrentMap's
    // version of double-checked locking.
    ConcurrentMap<String, DataFieldMaxValueIncrementer> incrementerCache = cache.get(dataSource);

    if (incrementerCache == null) {
        cache.put(dataSource,
                new ConcurrentHashMap<String, DataFieldMaxValueIncrementer>(8, 0.9f, 1));
        if (incrementerCache == null) {
            incrementerCache = cache.get(dataSource);
        }
    }

    // now check if we have a cached incrementer
    DataFieldMaxValueIncrementer incrementer = incrementerCache.get(incrementerName.toUpperCase());
    if (incrementer == null) {
        incrementer = incrementerCache.putIfAbsent(incrementerName.toUpperCase(), createIncrementer(dataSource,
                incrementerName));
        if (incrementer == null) {
            incrementer = incrementerCache.get(incrementerName.toUpperCase());
        }
    }
    return incrementer;

}
 
Example 12
/**
 * {@inheritDoc}
 */
@Override
public Object getGeneratedValue(Accessor accessor, AbstractSession writeSession, String seqName) {
    DataSource dataSource = ((JNDIConnector) writeSession.getLogin().getConnector()).getDataSource();
    DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(dataSource,
            sequenceName);
    return Long.valueOf(incrementer.nextLongValue());
}
 
Example 13
@Test
public void testGetIncrementer_Oracle() throws Exception {
    DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(oracle, "MY_SEQUENCE");
    assertTrue(incrementer instanceof OracleSequenceMaxValueIncrementer);
    OracleSequenceMaxValueIncrementer oracleIncrementer = (OracleSequenceMaxValueIncrementer)incrementer;
    assertEquals("MY_SEQUENCE", oracleIncrementer.getIncrementerName());

    // ensure that it's caching the incrementer
    assertSame(incrementer, MaxValueIncrementerFactory.getIncrementer(oracle, "MY_SEQUENCE"));
    // ensure that different sequence gives a different incrementer
    assertNotSame(incrementer, MaxValueIncrementerFactory.getIncrementer(oracle, "MY_SEQUENCE_2"));

}
 
Example 14
@Test
public void testGetIncrementer_MySQL() throws Exception {
    DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(mysql, "MY_SEQUENCE");
    assertTrue(incrementer instanceof MaxValueIncrementerFactory.EnhancedMySQLMaxValueIncrementer);
    MaxValueIncrementerFactory.EnhancedMySQLMaxValueIncrementer mysqlIncrementer =
            (MaxValueIncrementerFactory.EnhancedMySQLMaxValueIncrementer)incrementer;
    assertEquals("MY_SEQUENCE", mysqlIncrementer.getIncrementerName());
    assertEquals("ID", mysqlIncrementer.getColumnName());

    // ensure that it's caching the incrementer
    assertSame(incrementer, MaxValueIncrementerFactory.getIncrementer(mysql, "MY_SEQUENCE"));
    // ensure that different sequence gives a different incrementer
    assertNotSame(incrementer, MaxValueIncrementerFactory.getIncrementer(mysql, "MY_SEQUENCE_2"));

}
 
Example 15
@Test
public void testGetIncrementer_CaseInsensitive() throws Exception {
    DataFieldMaxValueIncrementer incrementer1 = MaxValueIncrementerFactory.getIncrementer(mysql, "MY_SEQUENCE");
    DataFieldMaxValueIncrementer incrementer2 = MaxValueIncrementerFactory.getIncrementer(mysql, "MY_sequence");
    DataFieldMaxValueIncrementer incrementer3 = MaxValueIncrementerFactory.getIncrementer(mysql, "my_sequence");
    assertSame(incrementer1, incrementer2);
    assertSame(incrementer2, incrementer3);
}
 
Example 16
@Test(expected = InstantiationError.class)
public void testCustomIncrementerDatasourceInvalidClass() throws Exception {
    SimpleConfig config = new SimpleConfig();
    config.putProperty("rice.krad.data.platform.incrementer.mysql",
            "org.kuali.rice.krad.data.platform.testincrementers.NonExistent");
    ConfigContext.init(config);

    DataFieldMaxValueIncrementer mySQLMaxVal = MaxValueIncrementerFactory.getIncrementer(mysql,"test_mySQL");
    assertTrue("Cannot create incrementer", mySQLMaxVal == null);
}
 
Example 17
private void initializeResponsibilityId(Object dataObject) {
    if (dataObject instanceof ReviewResponsibilityBo) {
        ReviewResponsibilityBo responsibilityBo = (ReviewResponsibilityBo) dataObject;

        if (StringUtils.isBlank(responsibilityBo.getId())) {
            DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(
                    KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_RSP_ID_S);
            responsibilityBo.setId(incrementer.nextStringValue());
        }
    }
}
 
Example 18
public void initializeDocumentForNewRole() {
    if (StringUtils.isBlank(this.roleId)) {
        DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_ROLE_ID_S);
        this.roleId = incrementer.nextStringValue();
    }
    if (StringUtils.isBlank(this.roleTypeId)) {
        this.roleTypeId = "1";
    }
}
 
Example 19
public void initializeDocumentForNewGroup() {
    if (StringUtils.isBlank(this.groupId)) {
        DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_GROUP_ID_S);
        this.groupId = incrementer.nextStringValue();
    }
    if (StringUtils.isBlank(this.groupTypeId)) {
        this.groupTypeId = "1";
    }
}
 
Example 20
private void initializePermissionId(Object dataObject) {
    if (dataObject instanceof GenericPermissionBo) {
        GenericPermissionBo permissionBo = (GenericPermissionBo) dataObject;

        if (StringUtils.isBlank(permissionBo.getId())) {
            DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(
                    KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_PERM_ID_S);
            permissionBo.setId(incrementer.nextStringValue());
        }
    }
}
 
Example 21
private DataFieldMaxValueIncrementer getRuleSequenceIncrementer() {
    if (ruleSequenceIncrementer == null) {
        ruleSequenceIncrementer = MaxValueIncrementerFactory.getIncrementer(dataSource, RULE_SEQ_NAME);
    }

    return ruleSequenceIncrementer;
}
 
Example 22
private DataFieldMaxValueIncrementer getReferenceObjectBindingSequenceIncrementer() {
    if (referenceObjectBindingSequenceIncrementer == null) {
        referenceObjectBindingSequenceIncrementer = MaxValueIncrementerFactory.getIncrementer(dataSource, REF_OBJ_SEQ_NAME);
    }

    return referenceObjectBindingSequenceIncrementer;
}
 
Example 23
Source Project: spring-cloud-task   Source File: JdbcTaskExecutionDao.java    License: Apache License 2.0 4 votes vote down vote up
public void setTaskIncrementer(DataFieldMaxValueIncrementer taskIncrementer) {
	this.taskIncrementer = taskIncrementer;
}
 
Example 24
/**
 * {@inheritDoc}
 */
@Override
public DataFieldMaxValueIncrementer getObject() throws Exception {
    return MaxValueIncrementerFactory.getIncrementer(dataSource, incrementerName);
}
 
Example 25
/**
 * {@inheritDoc}
 */
@Override
public Class<DataFieldMaxValueIncrementer> getObjectType() {
    return DataFieldMaxValueIncrementer.class;
}
 
Example 26
/**
 * Checks the config file for any references to
 * {@code rice.krad.data.platform.incrementer.(DATASOURCE, ex mysql, oracle).(VERSION optional)}.
 *
 * <p>If matching one found attempts to instantiate it to return back to factory for use.</p>
 *
 * @param platformInfo the {@link DatabasePlatformInfo}.
 * @param dataSource the {@link DataSource} for which to retrieve the incrementer.
 * @param incrementerName the name of the incrementer.
 * @param columnName the name of the column to increment.
 * @return a config set customized incrementer that matches and can be used to generate the next incremented value
 *         for the given incrementer against the specified {@link DataSource}
 * @throws InstantiationError if cannot instantiate passed in class.
 */
private static DataFieldMaxValueIncrementer getCustomizedIncrementer(DatabasePlatformInfo platformInfo, DataSource dataSource, String incrementerName, String columnName){
    if(platformInfo == null){
        throw new  IllegalArgumentException("DataSource platform must not be null");
    }
    if(ConfigContext.getCurrentContextConfig() == null){
        return null;
    }
    Map<String,String> incrementerPropToIncrementer = ConfigContext.getCurrentContextConfig().
                            getPropertiesWithPrefix(PLATFORM_INCREMENTER_PREFIX, true);
    String platformNameVersion = platformInfo.getName().toLowerCase() + "." + platformInfo.getMajorVersion();
    String incrementerClassName = "";

     if(incrementerPropToIncrementer.containsKey(platformNameVersion)){
        incrementerClassName = incrementerPropToIncrementer.get(platformNameVersion);
     } else if(incrementerPropToIncrementer.containsKey(platformInfo.getName().toLowerCase())){
         incrementerClassName = incrementerPropToIncrementer.get(platformInfo.getName().toLowerCase());
     }

    if(StringUtils.isNotBlank(incrementerClassName)){
        try {
            Class incrementerClass = Class.forName(incrementerClassName);
            if(AbstractSequenceMaxValueIncrementer.class.isAssignableFrom(incrementerClass)){
                AbstractSequenceMaxValueIncrementer abstractSequenceMaxValueIncrementer = (AbstractSequenceMaxValueIncrementer)incrementerClass.newInstance();
                abstractSequenceMaxValueIncrementer.setDataSource(dataSource);
                abstractSequenceMaxValueIncrementer.setIncrementerName(incrementerName);
                return abstractSequenceMaxValueIncrementer;

            } else if(AbstractColumnMaxValueIncrementer.class.isAssignableFrom(incrementerClass)){
                AbstractColumnMaxValueIncrementer abstractColumnMaxValueIncrementer = (AbstractColumnMaxValueIncrementer)incrementerClass.newInstance();
                abstractColumnMaxValueIncrementer.setDataSource(dataSource);
                abstractColumnMaxValueIncrementer.setIncrementerName(incrementerName);
                abstractColumnMaxValueIncrementer.setColumnName(columnName);
                return abstractColumnMaxValueIncrementer;
            } else {
                throw new InstantiationError("Cannot create incrementer class "+incrementerClassName +" it has to extend "
                        + "AbstractSequenceMaxValueIncrementer or AbstractColumnMaxValueIncrementer");
            }
        } catch (Exception e){
            throw new InstantiationError("Could not instantiate custom incrementer "+incrementerClassName);
        }
    }
    return null;
}
 
Example 27
@Test
public void testGetIncrementer_CacheByDataSource() throws Exception {
    DataFieldMaxValueIncrementer incrementer1 = MaxValueIncrementerFactory.getIncrementer(mysql, "MY_SEQUENCE");
    DataFieldMaxValueIncrementer incrementer2 = MaxValueIncrementerFactory.getIncrementer(oracle, "MY_SEQUENCE");
    assertNotSame(incrementer1, incrementer2);
}
 
Example 28
@Override
public String getNextDocumentId(){
    DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(
                                    getDataSource(), "KREW_DOC_HDR_S");
    return incrementer.nextStringValue();
}
 
Example 29
Source Project: rice   Source File: RuleTemplateDAOJpa.java    License: Educational Community License v2.0 4 votes vote down vote up
public String getNextRuleTemplateId() {
    DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(
            getDataSource(), SEQUENCE_NAME);
    return incrementer.nextStringValue();
}
 
Example 30
public void addMember(KimDocumentRoleMember member) {
    DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_ROLE_MBR_ID_S);
    member.setRoleMemberId(incrementer.nextStringValue());
    setupMemberRspActions(member);
    getModifiedMembers().add(member);
}