📄 coordinationhandler.java
字号:
/*
* Copyright (c) 2006, University of Kent
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 1. Neither the name of the University of Kent nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED.
*
* 3. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE
* IN THE CIRCUMSTANCES. IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS
* SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS
* SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH
* GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH
* TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY
* IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE
* SERIOUS FAULTS, IN THIS SOFTWARE.
*
* 5. This license is governed, except to the extent that local laws
* necessarily apply, by the laws of England and Wales.
*/
/*
* CoordinationHandler.java
*
* Created on 21 June 2006, 20:39
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package uk.ac.kent.dpa.coord.database;
import uk.ac.kent.dpa.mysql.init.DatabaseException;
import uk.ac.kent.dpa.mysql.init.DbInitial;
import org.w3c.dom.*;
import java.sql.*;
import java.util.*;
import issrg.web.service.EncodeXML;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*
* @author ls97
*/
public class CoordinationHandler {
private static int READ = 0;
private static int WRITE = 1;
private String CADTName;
private String CoordinationAttributeName;
private String configFileName;
private DbInitial myDB;
private Element definingAttributes;
private String CoordinationDataType;
private String tableName;
private String initialValue;
static Log logger = LogFactory.getLog(CoordinationHandler.class.getName());
/** Creates a new instance of CoordinationHandler */
public CoordinationHandler(String config,String table) throws CoordDatabaseException {
this.CADTName=table;
this.configFileName=config;
}
public void dispose() {
try{this.myDB.getConnection().close();} catch (Exception e){};
try{this.myDB.getDB().setAutoCommit(true);} catch (Exception e){};
try{this.myDB.getDB().close();} catch (Exception e){};
logger.debug("MySQL connection pool is closed");
}
public void initialise(String name) throws CoordDatabaseException {
this.CoordinationAttributeName=name;
DbInitial db = null;
try {
db = new DbInitial(this.configFileName);
} catch (DatabaseException de) {
try{db.getConnection().close();} catch (Exception e){};
try{db.getDB().setAutoCommit(true);} catch (Exception e){};
try {db.getDB().close();} catch (Exception e) {};
}
this.definingAttributes = this.getDefiningAttributes(db);
this.CoordinationDataType = this.getCoordinationDataType();
this.tableName = this.getTableName(db);
this.initialValue = this.getInitialValue(db);
try{db.getConnection().close();} catch (Exception e){};
try{db.getDB().setAutoCommit(true);} catch (Exception e){};
try {db.getDB().close();} catch (Exception e) {};
int index1 = name.indexOf("//");
int index2 = name.lastIndexOf("/");
try {
if (index2-index1>2) {
String dbURL = name.substring(index1,index2);
this.myDB = new DbInitial(this.configFileName,dbURL);
} else this.myDB = new DbInitial(this.configFileName);
} catch (DatabaseException de) {
throw new CoordDatabaseException("DB cannot be intialised:"+de);
}
}
public void reset(String name) throws CoordDatabaseException {
if (name.equals(this.CoordinationAttributeName)) return;
else {
this.CoordinationAttributeName=name;
this.tableName = this.getTableName(this.myDB);
this.CoordinationDataType=this.getCoordinationDataType();
this.definingAttributes = this.getDefiningAttributes(this.myDB);
}
}
public String getCoordinationData(Element context) throws CoordDatabaseException {
AttributeAndValue[] pair = this.getDimension(context);
return this.get(pair);
}
public String addCoordinationData(Element context) throws CoordDatabaseException {
AttributeAndValue[] pair = this.getDimension(context);
return this.add(pair);
}
public boolean deleteCoordinationData(Element context) throws CoordDatabaseException {
AttributeAndValue[] pair = this.getDimension(context);
return this.delete(pair);
}
public boolean updateCoordinationData(Element context, Object value) throws CoordDatabaseException {
AttributeAndValue[] pair = this.getDimension(context);
return this.update(pair,value);
}
public boolean lockCoordinationData(int type) throws CoordDatabaseException {
return this.lock(type);
}
public boolean unlockCoordinationData() throws CoordDatabaseException{
return this.unlock();
}
public boolean lockAllCoordinationData(String param) throws CoordDatabaseException{
return this.lockAll(param);
}
private boolean lockAll(String param) throws CoordDatabaseException {
int indicator = -1;
String sql = new String();
sql = "LOCK TABLES "+param+";";
try {
indicator = this.myDB.getConnection().executeUpdate(sql);
} catch (SQLException se) {
throw new CoordDatabaseException("SQL error in lock tables:"+se);
}
if (indicator==-1) throw new CoordDatabaseException("SQL operation failure");
return true;
}
private boolean lock(int type) throws CoordDatabaseException {
int indicator = -1;
String table = this.tableName;
if (table==null) throw new CoordDatabaseException("table name is not found");
String sql = new String();
String lockType=(type==this.READ)?new String("READ"):new String("WRITE");
sql = "LOCK TABLES "+table+" "+lockType+";";
try {
indicator = this.myDB.getConnection().executeUpdate(sql);
} catch (SQLException se) {
throw new CoordDatabaseException("SQL error in lock tables:"+se);
}
if (indicator==-1) throw new CoordDatabaseException("SQL operation failure");
return true;
}
private boolean unlock() throws CoordDatabaseException {
int indicator = -1;
String table = this.tableName;
if (table==null) throw new CoordDatabaseException("table name is not found");
String sql = new String();
sql = "UNLOCK TABLES;";
try {
indicator = this.myDB.getConnection().executeUpdate(sql);
} catch (SQLException se) {
throw new CoordDatabaseException("SQL error in unlock tables:"+se);
}
if (indicator==-1) throw new CoordDatabaseException("SQL operation failure");
return true;
}
private String get(AttributeAndValue[] dim) throws CoordDatabaseException {
String table = this.tableName;
if (table==null) throw new CoordDatabaseException("table name is not found");
String sql = new String();
for (int i=0; i<dim.length; i++) {
sql += dim[i].toString();
if (i<dim.length-1) sql += " AND ";
else sql += ";";
}
if (sql.length()>0) sql = "SELECT Value FROM "+table+" WHERE "+sql;
else sql = "SELECT Value FROM "+table+";";
try {
ResultSet sqlResults = this.myDB.getConnection().executeQuery(sql);
if (sqlResults.next()) return sqlResults.getString("Value");
else return null;
} catch (SQLException se) {
throw new CoordDatabaseException("SQL error:"+se);
}
}
private String add(AttributeAndValue[] dim) throws CoordDatabaseException {
int indicator = -1;
String table = this.tableName;
if (table==null) throw new CoordDatabaseException("table name is not found");
String sql = new String();
String fields = new String("(");
String values = new String("VALUES (");
for (int i=0; i<dim.length; i++) {
String name = dim[i].getName();
name = name.replaceAll(":","_");
while (name.indexOf(".")>=0)name = name.replace(".","_");
name = name.replace("(","1");
name = name.replace(")","1");
while (name.indexOf("-")>=0) name = name.replace("-","_");
fields += name+",";
Object val = dim[i].getValue();
String res = null;
if (Integer.class.isAssignableFrom(val.getClass())) {
res = ((Integer)val).toString();
values += res;
} else if (Float.class.isAssignableFrom(val.getClass())) {
res = ((Float)val).toString();
values += res;
} else if (String.class.isAssignableFrom(val.getClass())) {
res = (String)val;
values += "'"+res+"'";
} else throw new CoordDatabaseException("illigal data type");
values += ",";
}
fields += "Value) ";
String temType = this.CoordinationDataType;
int index = this.CoordinationDataType.indexOf("#");
if (index>0) temType = this.CoordinationDataType.substring(index+1);
if (temType.equals("integer") || temType.equals("float")) values += this.initialValue+");";
else values += "'"+this.initialValue+"');";
sql = "INSERT INTO "+table+" "+fields+values;
try {
indicator = this.myDB.getConnection().executeUpdate(sql);
} catch (SQLException se) {
throw new CoordDatabaseException("SQL error:"+se);
}
if (indicator==-1) throw new CoordDatabaseException("SQL operation failure");
return this.initialValue;
}
private boolean update(AttributeAndValue[] dim, Object value) throws CoordDatabaseException {
int indicator = -1;
String table = this.tableName;
if (table==null) throw new CoordDatabaseException("table name is not found");
String sql = new String();
for (int i=0; i<dim.length; i++) {
sql += dim[i].toString();
if (i<dim.length-1) sql += " AND ";
else sql += ";";
}
String valueString = null;
if (Integer.class.isAssignableFrom(value.getClass())) valueString = ((Integer)value).toString();
else if (Float.class.isAssignableFrom(value.getClass())) valueString = ((Float)value).toString();
else if (String.class.isAssignableFrom(value.getClass())) valueString = "'"+(String)value+"'";
else throw new CoordDatabaseException("initial value has invalid data type");
if (sql.length()>0) sql = "UPDATE "+table+" SET Value="+valueString+" WHERE "+sql;
else sql = "UPDATE "+table+" SET Value="+valueString+";";
try {
indicator = this.myDB.getConnection().executeUpdate(sql);
} catch (SQLException se) {
throw new CoordDatabaseException("SQL error:"+se);
}
if (indicator==-1) throw new CoordDatabaseException("SQL operation failure");
else if (indicator==1) return true;
else return false;
}
private boolean delete(AttributeAndValue[] dim) throws CoordDatabaseException {
int indicator = -1;
String table = this.tableName;
if (table==null) throw new CoordDatabaseException("table name is not found");
String sql = new String();
for (int i=0; i<dim.length; i++) {
sql += dim[i].toString();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -