From 4a900c88000f6942585d64bd122810eda2029f30 Mon Sep 17 00:00:00 2001 From: dsyer Date: Mon, 21 Jan 2008 15:53:04 +0000 Subject: [PATCH] Comment on use of non-nulls in parameters table --- .../execution/repository/dao/JdbcJobDao.java | 252 ++++++++---------- 1 file changed, 112 insertions(+), 140 deletions(-) diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcJobDao.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcJobDao.java index f93432baa..acbc33196 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcJobDao.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcJobDao.java @@ -60,11 +60,11 @@ public class JdbcJobDao implements JobDao, InitializingBean { // Job SQL statements private static final String CREATE_JOB = "INSERT into %PREFIX%JOB_INSTANCE(ID, JOB_NAME, JOB_KEY)" + " values (?, ?, ?)"; - - private static final String CREATE_JOB_PARAMETERS = "INSERT into %PREFIX%JOB_INSTANCE_PROPERTIES(JOB_ID, KEY, TYPE_CD, " + - "STRING_VAL, DATE_VAL, LONG_VAL) values (?, ?, ?, ?, ?, ?)"; - - /** + + private static final String CREATE_JOB_PARAMETERS = "INSERT into %PREFIX%JOB_INSTANCE_PROPERTIES(JOB_ID, KEY, TYPE_CD, " + + "STRING_VAL, DATE_VAL, LONG_VAL) values (?, ?, ?, ?, ?, ?)"; + + /** * Default value for the table prefix property. */ public static final String DEFAULT_TABLE_PREFIX = "BATCH_"; @@ -106,8 +106,7 @@ public class JdbcJobDao implements JobDao, InitializingBean { Assert.notNull(jdbcTemplate, "JdbcTemplate cannot be null"); Assert.notNull(jobIncrementer, "JobIncrementor cannot be null"); - Assert.notNull(jobExecutionIncrementer, - "JobExecutionIncrementer cannot be null"); + Assert.notNull(jobExecutionIncrementer, "JobExecutionIncrementer cannot be null"); } /** @@ -117,33 +116,33 @@ public class JdbcJobDao implements JobDao, InitializingBean { * into an INSERT statement. * * @see JobDao#createJob(JobIdentifier) - * @throws IllegalArgumentException - * if any {@link JobIdentifier} fields are null. + * @throws IllegalArgumentException if any {@link JobIdentifier} fields are + * null. */ public JobInstance createJob(JobIdentifier jobIdentifier) { validateJobIdentifier(jobIdentifier); Long jobId = new Long(jobIncrementer.nextLongValue()); - Object[] parameters = new Object[] { jobId, jobIdentifier.getName(), createJobKey(jobIdentifier.getJobInstanceProperties()) }; - jdbcTemplate.update(getCreateJobQuery(), parameters, new int[] { - Types.INTEGER, Types.VARCHAR, Types.VARCHAR}); + Object[] parameters = new Object[] { jobId, jobIdentifier.getName(), + createJobKey(jobIdentifier.getJobInstanceProperties()) }; + jdbcTemplate.update(getCreateJobQuery(), parameters, new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR }); insertJobParameters(jobId, jobIdentifier.getJobInstanceProperties()); - + JobInstance job = new JobInstance(jobIdentifier, jobId); return job; } - - private String createJobKey(JobInstanceProperties jobInstanceProperties){ - + + private String createJobKey(JobInstanceProperties jobInstanceProperties) { + Map props = jobInstanceProperties.getParameters(); StringBuilder stringBuilder = new StringBuilder("params:"); - for(Iterator it = props.entrySet().iterator();it.hasNext();){ - Entry entry = (Entry)it.next(); + for (Iterator it = props.entrySet().iterator(); it.hasNext();) { + Entry entry = (Entry) it.next(); stringBuilder.append(entry.toString() + ";"); } - + return stringBuilder.toString(); } @@ -152,9 +151,8 @@ public class JdbcJobDao implements JobDao, InitializingBean { Assert.notNull(job, "Job cannot be null."); Assert.notNull(job.getId(), "Job Id cannot be null."); - return jdbcTemplate.query( - getQuery(JobExecutionRowMapper.FIND_JOB_EXECUTIONS), - new Object[] { job.getId() }, new JobExecutionRowMapper(job)); + return jdbcTemplate.query(getQuery(JobExecutionRowMapper.FIND_JOB_EXECUTIONS), new Object[] { job.getId() }, + new JobExecutionRowMapper(job)); } /** @@ -162,8 +160,8 @@ public class JdbcJobDao implements JobDao, InitializingBean { * given identifier, adding them to a list via the RowMapper callback. * * @see JobDao#findJobs(JobIdentifier) - * @throws IllegalArgumentException - * if any {@link JobIdentifier} fields are null. + * @throws IllegalArgumentException if any {@link JobIdentifier} fields are + * null. */ public List findJobs(final JobIdentifier jobIdentifier) { @@ -175,8 +173,7 @@ public class JdbcJobDao implements JobDao, InitializingBean { RowMapper rowMapper = new RowMapper() { public Object mapRow(ResultSet rs, int rowNum) throws SQLException { - JobInstance job = new JobInstance(jobIdentifier, new Long(rs - .getLong(1))); + JobInstance job = new JobInstance(jobIdentifier, new Long(rs.getLong(1))); job.setStatus(BatchStatus.getStatus(rs.getString(2))); return job; @@ -197,15 +194,14 @@ public class JdbcJobDao implements JobDao, InitializingBean { private String getFindJobsQuery() { return getQuery(FIND_JOBS); } - - private String getCreateJobParamsQuery(){ + + private String getCreateJobParamsQuery() { return getQuery(CREATE_JOB_PARAMETERS); } /** * @see JobDao#getJobExecutionCount(JobInstance) - * @throws IllegalArgumentException - * if jobId is null. + * @throws IllegalArgumentException if jobId is null. */ public int getJobExecutionCount(Long jobId) { @@ -213,8 +209,7 @@ public class JdbcJobDao implements JobDao, InitializingBean { Object[] parameters = new Object[] { jobId }; - return jdbcTemplate - .queryForInt(getJobExecutionCountQuery(), parameters); + return jdbcTemplate.queryForInt(getJobExecutionCountQuery(), parameters); } private String getJobExecutionCountQuery() { @@ -236,59 +231,65 @@ public class JdbcJobDao implements JobDao, InitializingBean { private String getUpdateJobQuery() { return getQuery(UPDATE_JOB); } - + /* - * Convenience method that inserts all parameters from the provided JobParameters. + * Convenience method that inserts all parameters from the provided + * JobParameters. * */ - private void insertJobParameters(Long jobId, JobInstanceProperties jobParameters){ - + private void insertJobParameters(Long jobId, JobInstanceProperties jobParameters) { + Map parameters = jobParameters.getStringParameters(); - - if(!parameters.isEmpty()){ - for(Iterator it = parameters.entrySet().iterator(); it.hasNext();){ - Entry entry = (Entry)it.next(); - insertParameter(jobId, ParameterType.STRING, entry.getKey().toString(), entry.getValue()); + + if (!parameters.isEmpty()) { + for (Iterator it = parameters.entrySet().iterator(); it.hasNext();) { + Entry entry = (Entry) it.next(); + insertParameter(jobId, ParameterType.STRING, entry.getKey().toString(), entry.getValue()); } } - + parameters = jobParameters.getLongParameters(); - - if(!parameters.isEmpty()){ - for(Iterator it = parameters.entrySet().iterator(); it.hasNext();){ - Entry entry = (Entry)it.next(); - insertParameter(jobId, ParameterType.LONG, entry.getKey().toString(), entry.getValue()); + + if (!parameters.isEmpty()) { + for (Iterator it = parameters.entrySet().iterator(); it.hasNext();) { + Entry entry = (Entry) it.next(); + insertParameter(jobId, ParameterType.LONG, entry.getKey().toString(), entry.getValue()); } } - + parameters = jobParameters.getDateParameters(); - - if(!parameters.isEmpty()){ - for(Iterator it = parameters.entrySet().iterator(); it.hasNext();){ - Entry entry = (Entry)it.next(); - insertParameter(jobId, ParameterType.DATE, entry.getKey().toString(), entry.getValue()); + + if (!parameters.isEmpty()) { + for (Iterator it = parameters.entrySet().iterator(); it.hasNext();) { + Entry entry = (Entry) it.next(); + insertParameter(jobId, ParameterType.DATE, entry.getKey().toString(), entry.getValue()); } } } - + /* - * Convenience method that inserts an individual records into the JobParameters table. + * Convenience method that inserts an individual records into the + * JobParameters table. Uses non-null values in the "empty" columns to avoid + * any possible ambiguity between null and a real value (on some platforms + * it is sometimes a problem). The type of the value is fixed by the type + * code anyway, so the value is not ambiguous. */ - private void insertParameter(Long jobId, ParameterType type, String key, Object value){ - + private void insertParameter(Long jobId, ParameterType type, String key, Object value) { + Object[] args = new Object[0]; - int[] argTypes = new int[]{Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP, Types.INTEGER}; - - if(type == ParameterType.STRING){ - args = new Object[]{jobId, key, type, value, new Timestamp(0L), new Long(0)}; + int[] argTypes = new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP, + Types.INTEGER }; + + if (type == ParameterType.STRING) { + args = new Object[] { jobId, key, type, value, new Timestamp(0L), new Long(0) }; } - else if(type == ParameterType.LONG){ - args = new Object[]{jobId, key, type, "", new Timestamp(0L), value}; + else if (type == ParameterType.LONG) { + args = new Object[] { jobId, key, type, "", new Timestamp(0L), value }; } - else if(type == ParameterType.DATE){ - args = new Object[]{jobId, key, type, "", value, new Long(0)}; + else if (type == ParameterType.DATE) { + args = new Object[] { jobId, key, type, "", value, new Long(0) }; } - + jdbcTemplate.update(getCreateJobParamsQuery(), args, argTypes); } @@ -299,24 +300,20 @@ public class JdbcJobDao implements JobDao, InitializingBean { * via a SQL INSERT statement. * * @see JobDao#save(JobExecution) - * @throws IllegalArgumentException - * if jobExecution is null, as well as any of it's fields to be - * persisted. + * @throws IllegalArgumentException if jobExecution is null, as well as any + * of it's fields to be persisted. */ public void save(JobExecution jobExecution) { validateJobExecution(jobExecution); jobExecution.setId(new Long(jobExecutionIncrementer.nextLongValue())); - Object[] parameters = new Object[] { jobExecution.getId(), - jobExecution.getJobId(), jobExecution.getStartTime(), - jobExecution.getEndTime(), jobExecution.getStatus().toString(), - jobExecution.getExitStatus().isContinuable() ? "Y" : "N", - jobExecution.getExitStatus().getExitCode(), + Object[] parameters = new Object[] { jobExecution.getId(), jobExecution.getJobId(), + jobExecution.getStartTime(), jobExecution.getEndTime(), jobExecution.getStatus().toString(), + jobExecution.getExitStatus().isContinuable() ? "Y" : "N", jobExecution.getExitStatus().getExitCode(), jobExecution.getExitStatus().getExitDescription() }; - jdbcTemplate.update(getSaveJobExecutionQuery(), parameters, new int[] { - Types.INTEGER, Types.INTEGER, Types.TIMESTAMP, Types.TIMESTAMP, - Types.VARCHAR, Types.CHAR, Types.VARCHAR, Types.VARCHAR }); + jdbcTemplate.update(getSaveJobExecutionQuery(), parameters, new int[] { Types.INTEGER, Types.INTEGER, + Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.CHAR, Types.VARCHAR, Types.VARCHAR }); } public void setJdbcTemplate(JdbcOperations jdbcTemplate) { @@ -327,11 +324,9 @@ public class JdbcJobDao implements JobDao, InitializingBean { * Setter for {@link DataFieldMaxValueIncrementer} to be used when * generating primary keys for {@link JobExecution} instances. * - * @param jobExecutionIncrementer - * the {@link DataFieldMaxValueIncrementer} + * @param jobExecutionIncrementer the {@link DataFieldMaxValueIncrementer} */ - public void setJobExecutionIncrementer( - DataFieldMaxValueIncrementer jobExecutionIncrementer) { + public void setJobExecutionIncrementer(DataFieldMaxValueIncrementer jobExecutionIncrementer) { this.jobExecutionIncrementer = jobExecutionIncrementer; } @@ -339,8 +334,7 @@ public class JdbcJobDao implements JobDao, InitializingBean { * Setter for {@link DataFieldMaxValueIncrementer} to be used when * generating primary keys for {@link JobInstance} instances. * - * @param jobIncrementer - * the {@link DataFieldMaxValueIncrementer} + * @param jobIncrementer the {@link DataFieldMaxValueIncrementer} */ public void setJobIncrementer(DataFieldMaxValueIncrementer jobIncrementer) { this.jobIncrementer = jobIncrementer; @@ -351,8 +345,7 @@ public class JdbcJobDao implements JobDao, InitializingBean { * the table names before queries are executed. Defaults to * {@value #DEFAULT_TABLE_PREFIX}. * - * @param tablePrefix - * the tablePrefix to set + * @param tablePrefix the tablePrefix to set */ public void setTablePrefix(String tablePrefix) { this.tablePrefix = tablePrefix; @@ -370,48 +363,35 @@ public class JdbcJobDao implements JobDao, InitializingBean { validateJobExecution(jobExecution); - String exitDescription = jobExecution.getExitStatus() - .getExitDescription(); - if (exitDescription != null - && exitDescription.length() > EXIT_MESSAGE_LENGTH) { + String exitDescription = jobExecution.getExitStatus().getExitDescription(); + if (exitDescription != null && exitDescription.length() > EXIT_MESSAGE_LENGTH) { exitDescription = exitDescription.substring(0, EXIT_MESSAGE_LENGTH); - logger - .debug("Truncating long message before update of JobExecution: " - + jobExecution); + logger.debug("Truncating long message before update of JobExecution: " + jobExecution); } - Object[] parameters = new Object[] { jobExecution.getStartTime(), - jobExecution.getEndTime(), jobExecution.getStatus().toString(), - jobExecution.getExitStatus().isContinuable() ? "Y" : "N", - jobExecution.getExitStatus().getExitCode(), exitDescription, - jobExecution.getId() }; + Object[] parameters = new Object[] { jobExecution.getStartTime(), jobExecution.getEndTime(), + jobExecution.getStatus().toString(), jobExecution.getExitStatus().isContinuable() ? "Y" : "N", + jobExecution.getExitStatus().getExitCode(), exitDescription, jobExecution.getId() }; if (jobExecution.getId() == null) { - throw new IllegalArgumentException( - "JobExecution ID cannot be null. JobExecution must be saved " - + "before it can be updated."); + throw new IllegalArgumentException("JobExecution ID cannot be null. JobExecution must be saved " + + "before it can be updated."); } // Check if given JobExecution's Id already exists, if none is found it // is invalid and // an exception should be thrown. - if (jdbcTemplate.queryForInt(getCheckJobExecutionExistsQuery(), - new Object[] { jobExecution.getId() }) != 1) { - throw new NoSuchBatchDomainObjectException( - "Invalid JobExecution, ID " + jobExecution.getId() - + " not found."); + if (jdbcTemplate.queryForInt(getCheckJobExecutionExistsQuery(), new Object[] { jobExecution.getId() }) != 1) { + throw new NoSuchBatchDomainObjectException("Invalid JobExecution, ID " + jobExecution.getId() + + " not found."); } - jdbcTemplate - .update(getUpdateJobExecutionQuery(), parameters, - new int[] { Types.TIMESTAMP, Types.TIMESTAMP, - Types.VARCHAR, Types.CHAR, Types.VARCHAR, - Types.VARCHAR, Types.INTEGER }); + jdbcTemplate.update(getUpdateJobExecutionQuery(), parameters, new int[] { Types.TIMESTAMP, Types.TIMESTAMP, + Types.VARCHAR, Types.CHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER }); } /** * @see JobDao#update(JobInstance) - * @throws IllegalArgumentException - * if Job, Job.status, or job.id is null + * @throws IllegalArgumentException if Job, Job.status, or job.id is null */ public void update(JobInstance job) { @@ -419,10 +399,8 @@ public class JdbcJobDao implements JobDao, InitializingBean { Assert.notNull(job.getStatus(), "Job Status cannot be Null"); Assert.notNull(job.getId(), "Job ID cannot be null"); - Object[] parameters = new Object[] { job.getStatus().toString(), - job.getId() }; - jdbcTemplate.update(getUpdateJobQuery(), parameters, new int[] { - Types.VARCHAR, Types.INTEGER}); + Object[] parameters = new Object[] { job.getStatus().toString(), job.getId() }; + jdbcTemplate.update(getUpdateJobQuery(), parameters, new int[] { Types.VARCHAR, Types.INTEGER }); } /* @@ -434,12 +412,9 @@ public class JdbcJobDao implements JobDao, InitializingBean { private void validateJobExecution(JobExecution jobExecution) { Assert.notNull(jobExecution); - Assert.notNull(jobExecution.getJobId(), - "JobExecution Job-Id cannot be null."); - Assert.notNull(jobExecution.getStartTime(), - "JobExecution start time cannot be null."); - Assert.notNull(jobExecution.getStatus(), - "JobExecution status cannot be null."); + Assert.notNull(jobExecution.getJobId(), "JobExecution Job-Id cannot be null."); + Assert.notNull(jobExecution.getStartTime(), "JobExecution start time cannot be null."); + Assert.notNull(jobExecution.getStatus(), "JobExecution status cannot be null."); } /** @@ -450,8 +425,7 @@ public class JdbcJobDao implements JobDao, InitializingBean { private void validateJobIdentifier(JobIdentifier jobIdentifier) { Assert.notNull(jobIdentifier, "JobIdentifier cannot be null."); - Assert.notNull(jobIdentifier.getName(), - "JobIdentifier name cannot be null."); + Assert.notNull(jobIdentifier.getName(), "JobIdentifier name cannot be null."); Assert.notNull(jobIdentifier.getJobInstanceProperties(), "JobIdentifier runtime parameters must not be null."); } @@ -482,44 +456,42 @@ public class JdbcJobDao implements JobDao, InitializingBean { jobExecution.setStartTime(rs.getTimestamp(2)); jobExecution.setEndTime(rs.getTimestamp(3)); jobExecution.setStatus(BatchStatus.getStatus(rs.getString(4))); - jobExecution.setExitStatus(new ExitStatus("Y".equals(rs - .getString(5)), rs.getString(6), rs.getString(7))); + jobExecution.setExitStatus(new ExitStatus("Y".equals(rs.getString(5)), rs.getString(6), rs.getString(7))); return jobExecution; } } - + private static class ParameterType { - + private final String type; - + private ParameterType(String type) { this.type = type; } - public String toString(){ + public String toString() { return type; } - + public static final ParameterType STRING = new ParameterType("STRING"); public static final ParameterType DATE = new ParameterType("DATE"); public static final ParameterType LONG = new ParameterType("LONG"); - - private static final ParameterType[] VALUES = {STRING, DATE, LONG}; - public static ParameterType getType(String typeAsString){ - - for(int i = 0; i < VALUES.length; i++){ - if(VALUES[i].toString().equals(typeAsString)){ - return (ParameterType)VALUES[i]; + private static final ParameterType[] VALUES = { STRING, DATE, LONG }; + + public static ParameterType getType(String typeAsString) { + + for (int i = 0; i < VALUES.length; i++) { + if (VALUES[i].toString().equals(typeAsString)) { + return (ParameterType) VALUES[i]; } } - + return null; } } - }