abstractroaddefectdaojdbc.java

来自「Java的框架」· Java 代码 · 共 577 行 · 第 1/2 页

JAVA
577
字号
	/* (non-Javadoc)
	 * @see mcapss.prrm.roaddefect.dao.RoadDefectDAO#getRoadDefects(mcapss.prrm.roaddefect.model.RoadDefect)
	 */
	public List getRoadDefects(RoadDefect roadDefect) throws DataAccessException {
		Object[] obj = RoadDefectQueryUtil.getWhereClauseAndParameterTypes(roadDefect);
		Object[] params = RoadDefectQueryUtil.getParameters(roadDefect);
		RoadDefectQuery roadDefectQuery = new RoadDefectQuery(getDataSource(),(String)obj[0],(int[])obj[1]);
		return roadDefectQuery.execute(params);
	}

	//========================================================================================

	/* (non-Javadoc)
	 * @see mcapss.prrm.roaddefect.dao.RoadDefectDAO#getRoadDefectsByContactID(java.lang.Integer)
	 */
	public List getRoadDefectsByContactID(Integer contactID)throws DataAccessException {
		RoadDefect roadDefect = new RoadDefect();
		roadDefect.setContact(new Contact());
		roadDefect.getContact().setId(contactID);
		Object[] obj = RoadDefectQueryUtil.getWhereClauseAndParameterTypes(roadDefect);
		Object[] params = RoadDefectQueryUtil.getParameters(roadDefect);
		RoadDefectQuery roadDefectQuery = new RoadDefectQuery(getDataSource(),(String)obj[0],(int[])obj[1]);
		return roadDefectQuery.execute(params);
	}

	/* (non-Javadoc)
	 * @see mcapss.prrm.roaddefect.dao.RoadDefectDAO#saveRoadDefect(mcapss.prrm.roaddefect.model.RoadDefect)
	 */
	public void saveRoadDefect(RoadDefect roadDefect) throws DataAccessException {
		if (roadDefect.isNew()){
			roadDefect.getContact().setId(
					contactInsert.insert(roadDefect.getContact()));			
			roadDefectInsert.insert(roadDefect);
		}else{
			contactUpdate.update(roadDefect.getContact());			
			roadDefectUpdate.update(roadDefect);
		}
	}


	//========================================================================================

	/* (non-Javadoc)
	 * @see mcaps.apps.prrm.roaddefect.dao.RoadDefectDAO#removeRoadDefect(mcaps.apps.prrm.roaddefect.model.RoadDefect)
	 */
	public void removeRoadDefect(RoadDefect roadDefect) throws DataAccessException {
		// TODO Auto-generated method stub
		roadDefectDelete.delete(new Object[]{roadDefect.getId()});
	}
	
	//========================================================================================
	
	//
	/* (non-Javadoc)
	 * @see mcapss.prrm.roaddefect.dao.RoadDefectDAO#purgeRoadDefect(java.util.Date, java.util.Date)
	 * Eg: DELETE roaddefect, contact FROM roaddefect, contact WHERE
	 *     lastmodifiedtime >= ? AND lastmodifiedtime <= ? 
	 *     AND roaddefect.contactid = contact.id	 * 
	 */
	public int purgeRoadDefect(Date startDate, Date endDate){
		
		Object paramValues[] = null;
		
		System.out.println("purgeRoadDefect");
		if ((startDate != null) && (endDate != null)){
			System.out.println("purgeRoadDefect StartEnd");
			paramValues = new Object[]{new Timestamp(startDate.getTime()),new Timestamp(endDate.getTime())};			
			return roadDefectPurgeStartEnd.delete(paramValues);
		}else if ((startDate != null) && (endDate == null)){
			System.out.println("purgeRoadDefect Start");
			paramValues = new Object[]{startDate};
			return roadDefectPurgeStart.delete(paramValues);
		}else if ((startDate == null) && (endDate != null)){
			System.out.println("purgeRoadDefect End");
			paramValues = new Object[]{endDate};
			return roadDefectPurgeEnd.delete(paramValues);
		}
		return 0;
	}
	
	//========================================================================================

	/* (non-Javadoc)
	 * @see mcapss.prrm.roaddefect.dao.RoadDefectDAO#copyRoadDefectToFile(java.util.Date, java.util.Date)
	 */
	public int copyRoadDefectToFile(Date startDate, Date endDate) {
		
		String roadDefectTable = TableNameConstants.ROADDEFECT_TABLENAME;
		String contactTable = TableNameConstants.CONTACT_TABLENAME;
		StringBuffer sb = new StringBuffer();
		
		//Get the file name prefix
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		dateFormat.setLenient(false);

		sb.append(dateFormat.format(new Date()))
			.append("_").append(roadDefectTable).append(".dat");
		String roadDefectFile = sb.toString();
		
		sb.delete(sb.indexOf("_")+1,sb.length());
		sb.append(contactTable).append(".dat");
		String contactFile = sb.toString();

			// Export RoadDefect
		String whereClause = copyRoadDefectWhereClause (startDate, endDate);
		roadDefectExport = new RoadDefectExport (getDataSource (), roadDefectTable,
				whereClause);
		roadDefectExport.doExecute (roadDefectFile);

		// Export Contact
		whereClause = copyContactWhereClause (startDate, endDate);
		contactExport = new ContactExport (getDataSource (), contactFile,
				contactTable, whereClause);
		contactExport.doExecute (contactFile);
		return purgeRoadDefect(startDate,endDate);
	}
	
	/**
	 * Generates the WhereClause of the copy road defect SQL Query.
	 * @param startDate query parameter for startDate 
	 * @param endDate query parameter for endDate 
	 * @return the SQL where clause string
	 */
	private String copyRoadDefectWhereClause(Date startDate, Date endDate) throws DataAccessException{

		String lastModifiedTime = RoadDefectField.FIELDS[RoadDefectField.LASTMODIFIEDTIME];

		//Create the SQL query whereClause
		StringBuffer sb = new StringBuffer();
		if (startDate != null){
			sb.append(lastModifiedTime).append(" ")
				.append(">=").append(" '")
				.append(new Timestamp(startDate.getTime()).toString()).append("' ");
		}
		if ((startDate != null) && (endDate != null)){
			sb.append("AND").append(" ");
		}
		if (endDate != null){
			sb.append(lastModifiedTime).append(" ")
				.append("<=").append(" '")
				.append(new Timestamp(endDate.getTime()).toString()).append("' ");
		}
		return sb.toString();
	}
	
	/**
	 * Generates the WhereClause of the copy contact SQL Query.
	 * @param startDate query parameter for startDate 
	 * @param endDate query parameter for endDate 
	 * @return the SQL where clause string
	 * Eg: 	Select contact.* INTO OUTFILE 'c:\\.dat' from roaddefect inner join contact 
	 * 	    on roaddefect.contactid = contact.id where roaddefect.LastModifiedTime <= '2005-08-04 12:00:00' 
	 */
	private String copyContactWhereClause(Date startDate, Date endDate) throws DataAccessException{
		String roadDefectTable = TableNameConstants.ROADDEFECT_TABLENAME;

		//Create the SQL query whereClause
		StringBuffer sb = new StringBuffer();
		//Create full qualified LastModifiedTime field name in terms of 
		//<table>.<field> due to existence of the same field name in two joined tables
		sb.append(roadDefectTable).append(".")
			.append(RoadDefectField.FIELDS[RoadDefectField.LASTMODIFIEDTIME]);
		String lastModifiedTime = sb.toString();
		
		sb = new StringBuffer();		
		if (startDate != null){
			sb.append(lastModifiedTime).append(" ")
				.append(">=").append(" '")
				.append(new Timestamp(startDate.getTime()).toString()).append("' ");
		}
		if ((startDate != null) && (endDate != null)){
			sb.append("AND").append(" ");
		}
		if (endDate != null){
			sb.append(lastModifiedTime).append(" ")
				.append("<=").append(" '")
				.append(new Timestamp(endDate.getTime()).toString()).append("' ");
		}
		return sb.toString();
	}
	
	
	//========================================================================================

	/* (non-Javadoc)
	 * @see mcapss.prrm.roaddefect.dao.RoadDefectDAO#getRoadDefects(java.util.Date, java.util.Date)
	 */
	public List getClosedRoadDefects(Date startLastModifiedTime, Date endLastModifiedTime){
		
		String whereClause = null;
		int paramTypes[] = null;
		Object paramValues[] = null;
		if ((startLastModifiedTime != null) && (endLastModifiedTime != null)){
			whereClause = "lastmodifiedtime >= ? AND lastmodifiedtime <= ?";
			paramTypes = new int[]{
											RoadDefectField.FIELDTYPES[RoadDefectField.LASTMODIFIEDTIME],
											RoadDefectField.FIELDTYPES[RoadDefectField.LASTMODIFIEDTIME]
											};
			paramValues = new Object[]{startLastModifiedTime,endLastModifiedTime};
			System.out.println("getRoadDefects startLastModifiedTime : " + startLastModifiedTime.toString());
			System.out.println("getRoadDefects endLastModifiedTime : " + endLastModifiedTime.toString());
		}else if ((startLastModifiedTime != null) && (endLastModifiedTime == null)){
			whereClause = "lastmodifiedtime >= ?";
			paramTypes = new int[]{
					RoadDefectField.FIELDTYPES[RoadDefectField.LASTMODIFIEDTIME]
					};
			paramValues = new Object[]{startLastModifiedTime};
			System.out.println("getRoadDefects startLastModifiedTime : " + startLastModifiedTime.toString());
		}else if ((startLastModifiedTime == null) && (endLastModifiedTime != null)){
			whereClause = "lastmodifiedtime <= ?";
			paramTypes = new int[]{
					RoadDefectField.FIELDTYPES[RoadDefectField.LASTMODIFIEDTIME],
					};
			paramValues = new Object[]{endLastModifiedTime};
			System.out.println("getRoadDefects endLastModifiedTime : " + endLastModifiedTime.toString());
		}
		RoadDefectQuery roadDefectQuery = new RoadDefectQuery(
				getDataSource(),
				whereClause,
				paramTypes);
		roadDefectQuery.setContactByIdQuery(contactByIdQuery);
		roadDefectQuery.setRoadQuery(roadQuery);
		return roadDefectQuery.execute(paramValues);
	}	

	//========================================================================================

	/* (non-Javadoc)
	 * @see mcapss.prrm.roaddefect.dao.RoadDefectDAO#changeRoadDefectStatus(java.lang.Integer, mcapss.prrm.roaddefect.model.RoadDefectStatus)
	 */
	public void changeRoadDefectStatus(Integer roadDefectID, String status)
			throws DataAccessException {
		Object[] params = new Object[]{roadDefectID};
		RoadDefect roadDefect = (RoadDefect)roadDefectByIdQuery.execute(params).get(0);
		if (roadDefect != null){
			roadDefect.setStatus(status);
			Date date = new Date();
			roadDefect.setLastModifiedTime(date);
			if (status.equals(RoadDefectStatus.CLOSED)){
				roadDefect.setClosedDate(date);
			}
			saveRoadDefect(roadDefect);
		}
	}
	
	//========================================================================================

	/**
	 * Retrieve and set the identity for the given entity,
	 * assuming that the last executed insert affected that entity
	 * and generated an auto-increment value for it.
	 * @param entity the entity object to retrieved the id for
	 * @see #getIdentityQuery
	 */
	protected void retrieveIdentity(BaseTimeObject entity) {
		entity.setId(new Integer(getJdbcTemplate().queryForInt(getIdentityQuery())));
	}

	//========================================================================================

	/**
	 * Return the identity query for the particular database:
	 * a query that can be used to retrieve the id of a row
	 * that has just been inserted.
	 * @return the identity query
	 */
	protected abstract String getIdentityQuery();

	/**
	 * Sets the roadMapDataSource.
	 * @param roadMapDataSource The roadMapDataSource to set.
	 */
	public void setRoadMapDataSource (DataSource roadMapDataSource) {
		this.roadMapDataSource = roadMapDataSource;
	}

	/**
	 * Returns the roadMapDataSource.
	 * @return DataSource
	 */
	public DataSource getRoadMapDataSource () {
		return roadMapDataSource;
	}

	//========================================================================================

}

⌨️ 快捷键说明

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