添加工具类
This commit is contained in:
parent
0480aef548
commit
d08101e068
177
src_core/com/tenwa/jbo/manager/BizObject.java
Normal file
177
src_core/com/tenwa/jbo/manager/BizObject.java
Normal file
@ -0,0 +1,177 @@
|
||||
package com.tenwa.jbo.manager;
|
||||
|
||||
import com.tenwa.comm.exception.BusinessException;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class BizObject implements Cloneable {
|
||||
|
||||
private String className;
|
||||
private List<DataObject> datas;
|
||||
|
||||
private BizObject(String className ) {
|
||||
this.className = className;
|
||||
}
|
||||
|
||||
public static BizObject getBizObject(String className ) {
|
||||
return new BizObject( className );
|
||||
}
|
||||
|
||||
public void setAttributeValue( String columnName, Object value ) throws SQLException {
|
||||
if ( columnName == null || "".equals( columnName ) ) {
|
||||
throw new SQLException( "column name can not be null" );
|
||||
}
|
||||
|
||||
if ( datas == null ) {
|
||||
datas = new ArrayList<DataObject>();
|
||||
}
|
||||
|
||||
if ( value == null || "".equals( value ) ) {
|
||||
datas.add( new DataObject( columnName, "string", null ) );
|
||||
} else {
|
||||
String type = value.getClass().getSimpleName().toLowerCase();
|
||||
if ( !DataObject.types.contains( type ) ) {
|
||||
throw new SQLException( "unrecognized value type" );
|
||||
}
|
||||
datas.add( new DataObject( columnName, type, value ) );
|
||||
}
|
||||
}
|
||||
|
||||
public List<BizObject> query(String params ) throws Exception {
|
||||
return query( null, params );
|
||||
}
|
||||
|
||||
public List<BizObject> query(List<Map<String, String>> params ) throws Exception {
|
||||
return query( params, null );
|
||||
}
|
||||
|
||||
public List<BizObject> query(List<Map<String, String>> paramList, String params ) throws Exception {
|
||||
List<BizObject> bizObjects = DataUtil.getArrayList();
|
||||
String sql = "select * from " + this.getTableName();
|
||||
String sqlWhere = "";
|
||||
String[] sqlParam = null;
|
||||
if ( StringUtils.isNotEmpty( params ) ) {
|
||||
sqlWhere += " where 1 = 1";
|
||||
String[] ps = params.split( "," );
|
||||
sqlParam = new String[ ps.length ];
|
||||
int i = 0;
|
||||
for ( String p : ps ) {
|
||||
sqlWhere += " and " + p.split( "=" )[ 0 ] + " = ? ";
|
||||
sqlParam[ i ++ ] = p.split( "=" )[ 1 ];
|
||||
}
|
||||
}
|
||||
List<Map<String, String>> datas;
|
||||
if ( paramList != null ) {
|
||||
List<List<String>> pl = DataUtil.getArrayList();
|
||||
sqlWhere += " where 1 = 1";
|
||||
List<String> keys = DataUtil.getArrayList();
|
||||
for ( int i = 0; i < paramList.size(); i ++ ) {
|
||||
List<String> p;
|
||||
Map<String, String> mp = paramList.get( i );
|
||||
int j = 0;
|
||||
for ( Map.Entry<String, String> m : mp.entrySet() ) {
|
||||
String key = m.getKey();
|
||||
if ( !keys.contains( key ) ) {
|
||||
String s = "";
|
||||
for ( int x = 0; x < paramList.size(); x ++ ) {
|
||||
if ( s.length() > 0 ) {
|
||||
s += ",";
|
||||
}
|
||||
s += "?";
|
||||
}
|
||||
sqlWhere += " and " + m.getKey() + " in ( " + s + " )";
|
||||
keys.add( key );
|
||||
}
|
||||
if ( pl.size() > j ) {
|
||||
p = pl.get( j );
|
||||
} else {
|
||||
p = DataUtil.getArrayList();
|
||||
pl.add( p );
|
||||
}
|
||||
p.add( m.getValue() );
|
||||
j ++;
|
||||
}
|
||||
}
|
||||
List<String> r = DataUtil.getArrayList();
|
||||
for ( List<String> p : pl ) {
|
||||
r.addAll( p );
|
||||
}
|
||||
datas = DataUtil.query( sql + sqlWhere, null, r.toArray() );
|
||||
} else {
|
||||
datas = DataUtil.query( sql + sqlWhere, null, sqlParam );
|
||||
}
|
||||
for ( Map<String, String> data : datas ) {
|
||||
BizObject bo = (BizObject) this.clone();
|
||||
for ( Map.Entry<String, String> entry : data.entrySet() ) {
|
||||
bo.setAttributeValue( entry.getKey(), entry.getValue() );
|
||||
}
|
||||
bizObjects.add( bo );
|
||||
}
|
||||
|
||||
return bizObjects;
|
||||
}
|
||||
|
||||
public String getTableName() {
|
||||
if ( this.className.indexOf( "." ) != -1 ) {
|
||||
return this.className.substring( this.className.lastIndexOf( "." ) + 1 );
|
||||
}
|
||||
return this.className;
|
||||
}
|
||||
|
||||
public String getClassName() {
|
||||
return className;
|
||||
}
|
||||
|
||||
public void setClassName(String className) {
|
||||
this.className = className;
|
||||
}
|
||||
|
||||
public List<DataObject> getDatas() {
|
||||
return this.datas;
|
||||
}
|
||||
|
||||
public String getAttribute( String key ) throws Exception {
|
||||
for ( DataObject data : this.datas ) {
|
||||
if ( data.getColumnName().equalsIgnoreCase( key ) ) {
|
||||
return DataUtil.getString( data.getColumnValue() );
|
||||
}
|
||||
}
|
||||
throw new BusinessException( "column name not found" );
|
||||
}
|
||||
|
||||
public void setAttributes( List<DataObject> datas ) {
|
||||
this.datas = datas;
|
||||
}
|
||||
|
||||
public void removeColumn( String columnName ) {
|
||||
int removeIndex = -1;
|
||||
for ( int i = 0; i < getDatas().size(); i ++ ) {
|
||||
DataObject dataObject = getDatas().get( i );
|
||||
if ( columnName.equals( dataObject.getColumnName() ) ) {
|
||||
removeIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
getDatas().remove( removeIndex );
|
||||
}
|
||||
|
||||
@Override
|
||||
public BizObject clone() throws CloneNotSupportedException {
|
||||
BizObject newBo = getBizObject( this.getClassName() );
|
||||
List<DataObject> dataObjects = this.getDatas();
|
||||
if ( dataObjects == null ) {
|
||||
return newBo;
|
||||
}
|
||||
for ( DataObject dataObject : dataObjects ) {
|
||||
try {
|
||||
newBo.setAttributeValue( dataObject.getColumnName(), dataObject.getColumnValue() );
|
||||
} catch ( SQLException e ) {
|
||||
}
|
||||
}
|
||||
return newBo;
|
||||
}
|
||||
}
|
||||
109
src_core/com/tenwa/jbo/manager/ConnUtil.java
Normal file
109
src_core/com/tenwa/jbo/manager/ConnUtil.java
Normal file
@ -0,0 +1,109 @@
|
||||
package com.tenwa.jbo.manager;
|
||||
|
||||
import com.amarsoft.are.jbo.JBOException;
|
||||
import com.amarsoft.are.jbo.JBOFactory;
|
||||
import com.amarsoft.are.jbo.JBOTransaction;
|
||||
import com.amarsoft.awe.util.Transaction;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
public class ConnUtil {
|
||||
|
||||
private Connection connection;
|
||||
private JBOTransaction jboTransaction;
|
||||
private Transaction transaction;
|
||||
private boolean flag = false;
|
||||
|
||||
public ConnUtil() throws Exception {
|
||||
this.flag = true;
|
||||
this.jboTransaction = JBOFactory.createJBOTransaction();
|
||||
this.transaction = Transaction.createTransaction( this.jboTransaction );
|
||||
this.connection = this.transaction.getConnection( this.transaction );
|
||||
}
|
||||
|
||||
public ConnUtil(JBOTransaction jboTransaction ) throws Exception {
|
||||
if ( jboTransaction == null ) {
|
||||
this.flag = true;
|
||||
this.jboTransaction = JBOFactory.createJBOTransaction();
|
||||
} else {
|
||||
this.jboTransaction = jboTransaction;
|
||||
}
|
||||
this.transaction = Transaction.createTransaction( this.jboTransaction );
|
||||
this.connection = this.transaction.getConnection( this.transaction );
|
||||
}
|
||||
|
||||
public ConnUtil(Transaction transaction ) throws Exception {
|
||||
if ( transaction == null ) {
|
||||
this.flag = true;
|
||||
this.transaction = Transaction.createTransaction( JBOFactory.createJBOTransaction() );
|
||||
} else {
|
||||
this.transaction = transaction;
|
||||
}
|
||||
this.jboTransaction = this.transaction.getTransaction();
|
||||
this.connection = this.transaction.getConnection( transaction );
|
||||
}
|
||||
|
||||
public JBOTransaction getJboTransaction() {
|
||||
return jboTransaction;
|
||||
}
|
||||
|
||||
public void setJboTransaction(JBOTransaction jboTransaction) {
|
||||
this.jboTransaction = jboTransaction;
|
||||
}
|
||||
|
||||
public Transaction getTransaction() {
|
||||
return transaction;
|
||||
}
|
||||
|
||||
public void setTransaction(Transaction transaction) {
|
||||
this.transaction = transaction;
|
||||
}
|
||||
|
||||
public Connection getConnection() {
|
||||
return this.connection;
|
||||
}
|
||||
|
||||
public void setConnection(Connection connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
public void commit() throws JBOException {
|
||||
if ( this.transaction != null && this.flag ) {
|
||||
this.transaction.commit();
|
||||
this.transaction = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void rollback() throws JBOException {
|
||||
if ( this.transaction != null && this.flag ) {
|
||||
this.transaction.rollback();
|
||||
this.transaction = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void close() throws JBOException {
|
||||
if ( this.transaction != null && this.flag ) {
|
||||
this.transaction.disConnect();
|
||||
this.transaction = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在类被销毁时,判断内部链接是否存在,如果存在,则关闭连接
|
||||
* @throws Throwable
|
||||
*/
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
if ( connection != null && !connection.isClosed() && this.flag ) {
|
||||
connection.close();
|
||||
}
|
||||
|
||||
if ( transaction != null && this.flag ) {
|
||||
transaction.rollback();
|
||||
}
|
||||
|
||||
if ( jboTransaction != null && this.flag ) {
|
||||
jboTransaction.rollback();
|
||||
}
|
||||
}
|
||||
}
|
||||
51
src_core/com/tenwa/jbo/manager/DataObject.java
Normal file
51
src_core/com/tenwa/jbo/manager/DataObject.java
Normal file
@ -0,0 +1,51 @@
|
||||
package com.tenwa.jbo.manager;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Êý¾Ý´æ´¢¶ÔÏó
|
||||
*/
|
||||
public class DataObject {
|
||||
|
||||
private String columnName;
|
||||
/**
|
||||
* ĿǰֻÓÐstring, int, long, double, boolean, date, bigdecimal, serializable
|
||||
*/
|
||||
private String columnType;
|
||||
private Object columnValue;
|
||||
|
||||
public static List<String> types = Arrays.asList( new String[]{ "string", "int", "integer", "long", "double", "boolean", "date", "bigdecimal" } );
|
||||
|
||||
public DataObject(String columnName, String columnType, Object columnValue) {
|
||||
this.columnName = columnName;
|
||||
this.columnType = columnType;
|
||||
this.columnValue = columnValue;
|
||||
}
|
||||
|
||||
public String getColumnName() {
|
||||
return columnName;
|
||||
}
|
||||
|
||||
public void setColumnName(String columnName) {
|
||||
this.columnName = columnName;
|
||||
}
|
||||
|
||||
public String getColumnType() {
|
||||
return columnType;
|
||||
}
|
||||
|
||||
public void setColumnType(String columnType) {
|
||||
this.columnType = columnType;
|
||||
}
|
||||
|
||||
public Object getColumnValue() {
|
||||
return columnValue;
|
||||
}
|
||||
|
||||
public void setColumnValue(Object columnValue) {
|
||||
this.columnValue = columnValue;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
4746
src_core/com/tenwa/jbo/manager/DataUtil.java
Normal file
4746
src_core/com/tenwa/jbo/manager/DataUtil.java
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user