taskupdate.java

来自「Java的框架」· Java 代码 · 共 199 行

JAVA
199
字号
package mcaps.apps.prrm.task.dao.jdbc.query;


import javax.sql.DataSource;

import mcaps.apps.prrm.task.dao.util.TaskField;
import mcaps.apps.prrm.task.model.Task;
import mcaps.apps.prrm.util.TableNameConstants;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.SqlUpdate;

/**
 * This class extends SqlUpdate to perform JDBC Update 
 * operation to the task table. This class is 
 * used to set the precompiled UPDATE SQL statement and
 * to execute the UPDATE SQL statement.
 * 
 * @author jov
 * @date Oct 20, 2005
 * @version 1.0.1.0
 */
public class TaskUpdate extends SqlUpdate {

	private int[] paramFieldIdx;

	/**
	 * Default constructor that accepts a datasource object to allow
	 * the connection to the database for update operation.
	 * @param ds the datasource object
	 */
	public TaskUpdate(DataSource ds) {
		setDataSource(ds);
		setDefaultParameterTypes();
		setSql(getDefaultSQL());
		compile();
	}

	/**
	 * Constructor that accepts a datasource object and other 
	 * parammeters like whereClause, paramTypes and paramFieldIdx
	 * to enable the connection to the database for update operation.
	 * @param ds the datasource object
	 * @param whereClause where clause to refine the query
	 * @param paramTypes the array of parameter types
	 * @param paramFieldIdx the index of the task field as set in
	 * the RoadInspectionField object
	 */
	public TaskUpdate(DataSource ds, String whereClause, 
														int[] paramTypes,
														int[] paramFieldIdx) {
		setDataSource(ds);
		setParameter(paramTypes,paramFieldIdx);
		setSql(getSQL(whereClause));
		compile();
	}

	/**
	 * Execute the JDBC update operation using the values as
	 * in the task object
	 * @param task the object the holds the field values to be inserted
	 * @return the number of rows affected by the update  
	 */
	public int update(Task task) {
//	TODO Exception code If not initialized throw excp
		Object[] paramValues = new Object[] { 
				task.getCategory(),
				task.getPriority(),
				task.getStatus(),
				task.getRemarks(),
				task.getAssignedUserId(), 
				task.getRoadDefectId(),
				task.getDueDate(), 
				task.getLastModifiedTime(), 
				task.getId() };
		return update(paramValues);
	}
	
	/**
	 * Generates the task default SQL statement.
	 * @return the default sql statement.
	 */
	private String getDefaultSQL() {
		//Note: No loop was written due to selective field update
		StringBuffer sb = new StringBuffer();
		sb.append("UPDATE ")
			.append(TableNameConstants.TASK_TABLENAME)
			.append(" SET ")
			.append(TaskField.FIELDS[TaskField.CATEGORY])
			.append("=?,")
			.append(TaskField.FIELDS[TaskField.PRIORITY])
			.append("=?,")
			.append(TaskField.FIELDS[TaskField.STATUS])
			.append("=?,")
			.append(TaskField.FIELDS[TaskField.REMARKS])
			.append("=?,")
			.append(TaskField.FIELDS[TaskField.ASSIGNEDUSERID])
			.append("=?,")
			.append(TaskField.FIELDS[TaskField.ROADDEFECTID])
			.append("=?,")
			.append(TaskField.FIELDS[TaskField.DUEDATE])
			.append("=?,")
			.append(TaskField.FIELDS[TaskField.LASTMODIFIEDTIME])
			.append("=?")
			.append(" ")
			.append("WHERE")
			.append(" ")
			.append(TaskField.FIELDS[TaskField.ID])
			.append("=?");
		return sb.toString();
	}
	
	/**
	 * Sets the default parameter types.
	 */
	private void setDefaultParameterTypes(){	
		//Note: No loop was written due to selective field update
		declareParameter(new SqlParameter(
				TaskField.FIELDTYPES[TaskField.CATEGORY]));
		declareParameter(new SqlParameter(
				TaskField.FIELDTYPES[TaskField.PRIORITY]));
		declareParameter(new SqlParameter(
				TaskField.FIELDTYPES[TaskField.STATUS]));
		declareParameter(new SqlParameter(
				TaskField.FIELDTYPES[TaskField.REMARKS]));
		declareParameter(new SqlParameter(
				TaskField.FIELDTYPES[TaskField.ASSIGNEDUSERID]));
		declareParameter(new SqlParameter(
				TaskField.FIELDTYPES[TaskField.ROADDEFECTID]));
		declareParameter(new SqlParameter(
				TaskField.FIELDTYPES[TaskField.DUEDATE]));
		declareParameter(new SqlParameter(
				TaskField.FIELDTYPES[TaskField.LASTMODIFIEDTIME]));
		declareParameter(new SqlParameter(
				TaskField.FIELDTYPES[TaskField.ID]));
	}
		
	/**
	 * Generates the task custom SQL statement using the array
	 * of param indexes for selective update.
	 * @return the default sql statement.
	 */
	private String getSQL() {
		//Note: No loop was written due to selective field update
		StringBuffer sb = new StringBuffer();
		sb.append("UPDATE").append(" ")
			.append(TableNameConstants.TASK_TABLENAME).append(" ")
			.append("SET").append(" ");
			
		for (int i = 0; i < paramFieldIdx.length; i++) {
			sb.append(TaskField.FIELDS[paramFieldIdx[i]])
			.append("=?");
			if (i < paramFieldIdx.length-1){
				sb.append(",");
			}
		}
		return sb.toString();
	}
	
	/**
	 * Generates the task custom SQL statement using the
	 * where clause statement for selective update.
	 * @param whereClause
	 * @return the default sql statement.
	 */
	private String getSQL(String whereClause) {
		StringBuffer sb = new StringBuffer();
		sb.append(getSQL()).append(" ")
			.append("WHERE").append(" ")
			.append(whereClause);
		return sb.toString();
	}
	
	/**
	 * Sets the custom parameter types for selective update.
	 * @param paramTypes
	 * @param paramFieldIdx
	 */
	private void setParameter(int[] paramTypes, int[] paramFieldIdx) {
//TODO Exception code
//		if (paramsTypes.length != paramValues.length){
//			throw new Exception("The number of parameter types and parameter value has to be the same");
//		}
		if (paramTypes != null) {
			for (int i = 0; i < paramTypes.length; i++) {
				declareParameter(new SqlParameter(paramTypes[i]));
			}
		}
				
		this.paramFieldIdx = new int[paramFieldIdx.length];
		System.arraycopy(paramFieldIdx, 0, 
				this.paramFieldIdx, 0, 
				paramFieldIdx.length);
	}
	


}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?