RecordManager.java
资源名称:OA.rar [点击查看]
上传用户:mingda
上传日期:2017-06-20
资源大小:27691k
文件大小:19k
源码类别:
OA系统
开发平台:
Java
- package com.gforce.currency.database;
- /**
- * <p>Title: 吉力科技办公自动化系统</p>
- * <p>Description: 吉力科技办公自动化系统</p>
- * <p>Copyright: 版权所有 2003 (c) 西安吉力科技发展有限公司 Copyright (c) 2003 GForce Sceince & Technology</p>
- * <p>Company: 西安吉力科技发展有限公司 (GForce Sceince & Technology)</p>
- * @author 马登军
- * @version 1.0
- */
- import com.gforce.currency.database.*;
- import com.gforce.currency.*;
- import java.sql.*;
- import java.util.*;
- public abstract class RecordManager
- {
- /**
- * 通过ID字符串删除记录
- * @param strIDs ID字符串,多个ID之间使用“,”连接
- * @return 错误代码,如果小于1,说明删除错误
- */
- public int DeleteByIDs(String strIDs)
- {
- return DeleteBySQLWhere(getIDFieldName() + " in (" + strIDs + ")");
- }
- /**
- * 通过ID删除记录
- * @param intID ID值
- * @return 错误代码,如果小于1,说明删除错误
- */
- public int DeleteByID(int intID)
- {
- return DeleteBySQLWhere(getIDFieldName() + " = " + intID);
- }
- /**
- * 通过WhereSQL语句删除记录
- * @param strSQLWhere WhereSQL语句
- * @return 错误代码,如果小于1,说明删除错误
- */
- public int DeleteBySQLWhere(String strSQLWhere)
- {
- int intReturn = -100;
- int counter = 0;
- Vector vc = SQLManager.GetResultSet("select count(*) as counter from " + getTableName() + " where " + strSQLWhere);
- if (vc.size() == 1)
- {
- counter = Integer.parseInt( ( (Vector) vc.get(0)).get(0).toString());
- if (counter < 0)
- {
- return -3; //表示要删除的记录不存在
- }
- }
- else
- {
- return -3; //表示要删除的记录不存在
- }
- intReturn = SQLManager.ExcuteSQL("Delete from " + getTableName() + " where " + strSQLWhere);
- return intReturn;
- }
- /**
- * 插入从m_request获取的一条记录
- * @param m_request Request请求
- * @return 插入之后得到记录ID,如果小于1,则说明插入错误
- */
- public int InsertRecord(Request m_request)
- {
- int returnid = 0;
- int counter = 0;
- Vector vc = SQLManager.GetResultSet("select count(*) as counter from " +
- getTableName() +
- getInsertWhere(m_request) +
- "");
- if (vc.size() == 1)
- {
- counter = Integer.parseInt( ( (Vector) vc.get(0)).get(0).toString());
- }
- String strSQL = "Insert into " + getTableName() + " (" + getFieldsName() +
- ") values (" + getInsertValues(m_request) + ")";
- SQLManager.ExcuteSQL(strSQL);
- vc = SQLManager.GetResultSet("select top 1 " + getIDFieldName() +
- " from " +
- getTableName() +
- getInsertWhere(m_request) +
- " order by " + getIDFieldName() +
- " desc");
- if (vc.size() == 1)
- {
- returnid = Integer.parseInt( ( (Vector) vc.get(0)).get(0).toString());
- }
- else
- {
- returnid = -2; //表示新插入的记录不存在,新记录插入没有成功
- }
- return returnid;
- }
- /**
- * 更新从m_request获取的一条记录
- * @param m_request Request请求
- * @return 错误代码,小于1表示错误
- */
- public int UpdateRecord(Request m_request)
- {
- int counter = 0;
- String strUpdateDatas = getUpdateDatas(m_request);
- SystemOut.LogPrintLine(strUpdateDatas);
- int intIDFieldValue = getIDFieldValue(m_request);
- SystemOut.Log(intIDFieldValue + "");
- if (strUpdateDatas.trim().length() > 1 && intIDFieldValue > 0)
- {
- Vector vc = SQLManager.GetResultSet(
- "select count(*) as counter from " +
- getTableName() + " where " + getIDFieldName() + "=" + intIDFieldValue);
- if (vc.size() == 1)
- {
- counter = Integer.parseInt( ( (Vector) vc.get(0)).get(0).toString());
- if (counter < 0)
- {
- return -3; //表示要修改的记录不存在
- }
- }
- else
- {
- return -3; //表示要修改的记录不存在
- }
- String strSQL = "Update " + getTableName() + " set " + strUpdateDatas +
- " where " + getIDFieldName() + "=" + intIDFieldValue;
- return SQLManager.ExcuteSQL(strSQL);
- }
- else
- {
- return -106; //表示获取到的记录ID不正确或者Form字段值不正确
- }
- }
- /**
- * 从Request请求中获取更新SQL语句
- * @param m_request Request请求
- * @return 更新的SQL语句
- */
- private String getUpdateDatas(Request m_request)
- {
- String strReturnValue = "";
- strReturnValue += getUpdateNumeric(m_request);
- strReturnValue += getUpdateString(m_request);
- strReturnValue += getUpdateDateTime(m_request);
- strReturnValue += getUpdateText(m_request);
- if (strReturnValue.trim().length() > 2)
- {
- return strReturnValue.trim().substring(1);
- }
- else
- {
- return "";
- }
- }
- /**
- * 从Request请求中获取更新数值型字段值的SQL语句
- * @param m_request Request请求
- * @return 更新数值型字段值的SQL语句
- */
- private String getUpdateNumeric(Request m_request)
- {
- StringBuffer strbufTemp = new StringBuffer();
- String[] strArray = getNumericFieldsName();
- for (int i = 0; i < strArray.length; i++)
- {
- if (strArray[i].trim().length() > 0)
- {
- strbufTemp.append(", " + strArray[i] + "=" +
- m_request.GetFloat(UpdateParament(strArray[i])));
- }
- }
- return strbufTemp.toString();
- }
- /**
- * 从Request请求中获取更新字符串类型字段值的SQL语句
- * @param m_request Request请求
- * @return 更新字符串类型字段值的SQL语句
- */
- private String getUpdateString(Request m_request)
- {
- StringBuffer strbufTemp = new StringBuffer();
- String[] strArray = getStringFieldsName();
- for (int i = 0; i < strArray.length; i++)
- {
- if (strArray[i].trim().length() > 0)
- {
- strbufTemp.append(", " + strArray[i] + "='" +
- m_request.GetString(UpdateParament(strArray[i])).replaceAll("'", "''") +
- "'");
- }
- }
- return strbufTemp.toString();
- }
- /**
- * 从Request请求中获取更新日期时间类型字段值的SQL语句
- * @param m_request Request请求
- * @return 更新日期时间类型字段值的SQL语句
- */
- private String getUpdateDateTime(Request m_request)
- {
- StringBuffer strbufTemp = new StringBuffer();
- String[] strArray = getDatetimeFieldsName();
- for (int i = 0; i < strArray.length; i++)
- {
- if (strArray[i].trim().length() > 0)
- {
- if (m_request.GetString(UpdateParament(strArray[i])).length() > 6)
- {
- strbufTemp.append(", " + strArray[i] + "='" + m_request.GetString(UpdateParament(strArray[i])) +
- "'");
- }
- else
- {
- strbufTemp.append(", " + strArray[i] + "=null");
- }
- }
- }
- return strbufTemp.toString();
- }
- /**
- * 从Request请求中获取更新大字符串类型字段值的SQL语句
- * @param m_request Request请求
- * @return 更新大字符串类型字段值的SQL语句
- */
- private String getUpdateText(Request m_request)
- {
- StringBuffer strbufTemp = new StringBuffer();
- String[] strArray = getTextFieldsName();
- for (int i = 0; i < strArray.length; i++)
- {
- if (strArray[i].trim().length() > 0)
- {
- strbufTemp.append(", " + strArray[i] + "='" +
- m_request.GetString(UpdateParament(strArray[i])).replaceAll("'", "''") +
- "'");
- }
- }
- return strbufTemp.toString();
- }
- /**
- * 从Request请求中获取更新时的ID字段的值
- * @param m_request Request请求
- * @return ID字段值
- */
- private int getIDFieldValue(Request m_request)
- {
- return m_request.GetInt(UpdateParament(getIDFieldName()));
- }
- /**
- * 从Request请求中获取插入记录查询的SQL语句
- * @param m_request Request请求
- * @return 插入记录查询的SQL语句
- */
- private String getInsertWhere(Request m_request)
- {
- String strTemp = "";
- strTemp += getNumericInsertWhere(m_request);
- strTemp += getStringInsertWhere(m_request);
- strTemp += getDatetimeInsertWhere(m_request);
- if (strTemp.trim().length() > 3)
- {
- strTemp = " where " + strTemp.substring(4);
- }
- return strTemp;
- }
- /**
- * 从Request请求中获取更新记录查询的SQL语句
- * @param m_request Request请求
- * @return 更新记录查询的SQL语句
- */
- private String getUpdateWhere(Request m_request)
- {
- String strTemp = "";
- strTemp += getNumericUpdateWhere(m_request);
- strTemp += getStringUpdateWhere(m_request);
- strTemp += getDatetimeUpdateWhere(m_request);
- if (strTemp.trim().length() > 3)
- {
- strTemp = " where (not " + getIDFieldName() + "=" +
- getIDFieldValue(m_request) + ") " + strTemp;
- }
- else
- {
- strTemp = " where 1=2 ";
- }
- return strTemp;
- }
- /**
- * 从Request请求中获取插入记录中数值型字段的查询SQL语句
- * @param m_request Request请求
- * @return 插入记录中数值型字段的查询SQL语句
- */
- private String getNumericInsertWhere(Request m_request)
- {
- StringBuffer strbufTemp = new StringBuffer();
- String[] strArray = getNumericFieldsName();
- for (int i = 0; i < strArray.length; i++)
- {
- if (strArray[i].trim().length() > 0)
- {
- strbufTemp.append(" and " + strArray[i] + "=" +
- m_request.GetFloat(InsertParament(strArray[i])));
- }
- }
- return strbufTemp.toString();
- }
- /**
- * 从Request请求中获取插入记录中字符串类型字段的查询SQL语句
- * @param m_request Request请求
- * @return 插入记录中字符串类型字段的查询SQL语句
- */
- private String getStringInsertWhere(Request m_request)
- {
- StringBuffer strbufTemp = new StringBuffer();
- String[] strArray = getStringFieldsName();
- for (int i = 0; i < strArray.length; i++)
- {
- if (strArray[i].trim().length() > 0)
- {
- strbufTemp.append(" and " + strArray[i] + "='" +
- m_request.GetString(InsertParament(strArray[i])) +
- "'");
- }
- }
- return strbufTemp.toString();
- }
- /**
- * 从Request请求中获取插入记录中日期时间类型字段的查询SQL语句
- * @param m_request Request请求
- * @return 插入记录中日期时间类型字段的查询SQL语句
- */
- private String getDatetimeInsertWhere(Request m_request)
- {
- StringBuffer strbufTemp = new StringBuffer();
- String[] strArray = getDatetimeFieldsName();
- for (int i = 0; i < strArray.length; i++)
- {
- if (strArray[i].trim().length() > 0)
- {
- if (m_request.GetString(InsertParament(strArray[i])).length() > 3)
- {
- strbufTemp.append(" and " + strArray[i] + "='" +
- m_request.GetString(InsertParament(strArray[i])) +
- "'");
- }
- else
- {
- strbufTemp.append(" and " + strArray[i] + " is null");
- }
- }
- }
- return strbufTemp.toString();
- }
- /**
- * 从Request请求中获取更新记录中数值型字段的查询SQL语句
- * @param m_request Request请求
- * @return 更新记录中数值型字段的查询SQL语句
- */
- private String getNumericUpdateWhere(Request m_request)
- {
- StringBuffer strbufTemp = new StringBuffer();
- String[] strArray = getNumericFieldsName();
- for (int i = 0; i < strArray.length; i++)
- {
- if (strArray[i].trim().length() > 0)
- {
- strbufTemp.append(" and " + strArray[i] + "=" +
- m_request.GetFloat(UpdateParament(strArray[i])));
- }
- }
- return strbufTemp.toString();
- }
- /**
- * 从Request请求中获取更新记录中字符串类型字段的查询SQL语句
- * @param m_request Request请求
- * @return 更新记录中字符串类型字段的查询SQL语句
- */
- private String getStringUpdateWhere(Request m_request)
- {
- StringBuffer strbufTemp = new StringBuffer();
- String[] strArray = getStringFieldsName();
- for (int i = 0; i < strArray.length; i++)
- {
- if (strArray[i].trim().length() > 0)
- {
- strbufTemp.append(" and " + strArray[i] + "='" +
- m_request.GetString(UpdateParament(strArray[i])) +
- "'");
- }
- }
- return strbufTemp.toString();
- }
- /**
- * 从Request请求中获取更新记录中日期时间类型字段的查询SQL语句
- * @param m_request Request请求
- * @return 更新记录中日期时间类型字段的查询SQL语句
- */
- private String getDatetimeUpdateWhere(Request m_request)
- {
- StringBuffer strbufTemp = new StringBuffer();
- String[] strArray = getDatetimeFieldsName();
- for (int i = 0; i < strArray.length; i++)
- {
- if (strArray[i].trim().length() > 0)
- {
- strbufTemp.append(" and " + strArray[i] + "='" +
- m_request.GetString(UpdateParament(strArray[i])) +
- "'");
- }
- }
- return strbufTemp.toString();
- }
- /**
- * 获取字段名称
- * @return 字段名称
- */
- public String getFieldsName()
- {
- String strTemp = "";
- strTemp += getStringFromStringArray(getNumericFieldsName());
- strTemp += getStringFromStringArray(getStringFieldsName());
- strTemp += getStringFromStringArray(getDatetimeFieldsName());
- strTemp += getStringFromStringArray(getTextFieldsName());
- if (strTemp.length() > 1)
- {
- return strTemp.substring(1);
- }
- else
- {
- return "";
- }
- }
- /**
- * 将字符串数组中的字符串用“,”连接起来,如果字符串长度小于1,则忽略,并且不去字符串头部多余“,”
- * @param strArray 字符串数组
- * @return 连接后得到的字符串
- */
- private String getStringFromStringArray(String[] strArray)
- {
- StringBuffer strbufTemp = new StringBuffer();
- if (strArray.length > 0)
- {
- for (int i = 0; i < strArray.length; i++)
- {
- if (strArray[i].trim().length() > 0)
- {
- strbufTemp.append("," + strArray[i]);
- }
- }
- }
- return strbufTemp.toString();
- }
- /**
- * 从Request请求中获取插入记录时日期类型字段的SQL语句
- * @param m_request Request Request请求
- * @param strTextFieldsName String[] 日期字段名称数组
- * @return String 插入记录时日期类型字段的SQL语句
- */
- private String getInsertRequestText(Request m_request,
- String[] strTextFieldsName)
- {
- StringBuffer strbufTemp = new StringBuffer();
- if (strTextFieldsName.length > 0)
- {
- for (int i = 0; i < strTextFieldsName.length; i++)
- {
- if (strTextFieldsName[i].trim().length() > 0)
- {
- String strTempText = m_request.GetString(InsertParament(
- strTextFieldsName[i])).
- replaceAll("'", "''");
- strbufTemp.append(",'" + strTempText + "'");
- }
- }
- }
- return strbufTemp.toString();
- }
- /**
- * 从Request请求中获取插入记录时日期类型字段的SQL语句
- * @param m_request Request请求
- * @param strDateFieldsName 日期字段名称数组
- * @return 插入记录时日期类型字段的SQL语句
- */
- private String getInsertRequestDate(Request m_request,
- String[] strDateFieldsName)
- {
- StringBuffer strbufTemp = new StringBuffer();
- if (strDateFieldsName.length > 0)
- {
- for (int i = 0; i < strDateFieldsName.length; i++)
- {
- if (strDateFieldsName[i].trim().length() > 0)
- {
- String strTempDate = m_request.GetString(InsertParament(
- strDateFieldsName[i])).
- replaceAll("'", "''");
- if (strTempDate.length() < 6)
- {
- strbufTemp.append(",null");
- }
- else
- {
- strbufTemp.append(",'" + strTempDate + "'");
- }
- }
- }
- }
- return strbufTemp.toString();
- }
- /**
- * 从Request请求中获取插入记录时字符串类型字段的SQL语句
- * @param m_request Request请求
- * @param strStringFieldsName 字符串类型字段名称数组
- * @return 插入记录时字符串类型字段的SQL语句
- */
- private String getInsertRequestString(Request m_request,
- String[] strStringFieldsName)
- {
- StringBuffer strbufTemp = new StringBuffer();
- if (strStringFieldsName.length > 0)
- {
- for (int i = 0; i < strStringFieldsName.length; i++)
- {
- if (strStringFieldsName[i].trim().length() > 0)
- {
- strbufTemp.append(",'" + m_request.GetString(InsertParament(
- strStringFieldsName[i])).replaceAll("'", "''") + "'");
- }
- }
- }
- return strbufTemp.toString();
- }
- /**
- * 从Request请求中获取插入记录时数值型字段的SQL语句
- * @param m_request Request请求
- * @param strNumericFieldsName 数值类型字段名称数组
- * @return 插入记录时数值型字段的SQL语句
- */
- private String getInsertRequestNumeric(Request m_request,
- String[] strNumericFieldsName)
- {
- StringBuffer strbufTemp = new StringBuffer();
- if (strNumericFieldsName.length > 0)
- {
- for (int i = 0; i < strNumericFieldsName.length; i++)
- {
- if (strNumericFieldsName[i].trim().length() > 0)
- {
- strbufTemp.append("," +
- m_request.GetFloat(InsertParament(
- strNumericFieldsName[
- i])) +
- "");
- }
- }
- }
- return strbufTemp.toString();
- }
- /**
- * 从Request请求中获取插入记录时SQL语句
- * @param m_request Request请求
- * @return 插入记录时的SQL语句
- */
- public String getInsertValues(Request m_request)
- {
- String strTemp = "";
- strTemp += getInsertRequestNumeric(m_request, getNumericFieldsName());
- strTemp += getInsertRequestString(m_request, getStringFieldsName());
- strTemp += getInsertRequestDate(m_request, getDatetimeFieldsName());
- strTemp += getInsertRequestText(m_request, getTextFieldsName());
- if (strTemp.length() > 1)
- {
- return strTemp.substring(1);
- }
- else
- {
- return "";
- }
- }
- /**
- * 获取本类操作表名称
- * @return String 本类操作表名称
- */
- protected abstract String getTableName();
- /**
- * 获取主键或者可以确定唯一记录的字段名称
- * @return String 主键或者可以确定唯一记录的字段名称
- */
- protected abstract String getIDFieldName(); //
- /**
- * 获取数值型字段名称
- * @return String[] 数值型字段名称
- */
- protected abstract String[] getNumericFieldsName(); //获取数值型字段名称
- /**
- * 获取字符型字段名称
- * @return String[] 字符型字段名称
- */
- protected abstract String[] getStringFieldsName(); //获取字符型字段名称
- /**
- * 获取日期时间型字段名称
- * @return String[] 日期时间型字段名称
- */
- protected abstract String[] getDatetimeFieldsName(); //获取日期时间型字段名称
- /**
- * 获取大文本型字段名称
- * @return String[] 大文本型字段名称
- */
- protected abstract String[] getTextFieldsName(); //获取大文本型字段名称
- /**
- * 根据字段名称获取插入数据时表单元素名称
- * @param strFieldName String 字段名称
- * @return String 插入数据时表单元素名称
- */
- protected abstract String InsertParament(String strFieldName); //根据字段名称获取插入数据时表单元素名称
- /**
- * 根据字段名称获取修改数据时表单元素名称
- * @param strFieldName String 字段名称
- * @return String 修改数据时表单元素名称
- */
- protected abstract String UpdateParament(String strFieldName); //根据字段名称获取修改数据时表单元素名称
- }