📄 dictionarycreaterimpl.java.svn-base
字号:
package com.gisinfo.Dictionary;
import com.gisinfo.DBCon.GlobalConnection;
import com.gisinfo.sql.delegater.Statement;
import com.gisinfo.sql.delegater.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.Hashtable;
/**
* Author: Jimmy lin
* Date: 2003-11-11
* Time: 10:27:37
*/
/**
* DictionaryCreaterImpl is implementation of DictioanryCreater interface
*/
public class DictionaryCreaterImpl implements DictionaryCreater {
private DictionaryStructure dictionaryStructure = new DictionaryStructure();
/**
* Constructor
*/
public DictionaryCreaterImpl() {
this.dictionaryStructure .makeDefaultDictionaryStructure() ;
}
/**
* Constructor
* @param dictionaryStructure A dictionaryStructure describes a dictionary structure.
*/
public DictionaryCreaterImpl(DictionaryStructure dictionaryStructure) {
this.dictionaryStructure = dictionaryStructure;
}
/**
* Set dictionaryStructure
* @param dictionaryStructure
*/
public void setDictionaryStructure(DictionaryStructure dictionaryStructure) {
this.dictionaryStructure = dictionaryStructure;
}
private String keyToWhereClause(DictionaryKey key) {
if (key instanceof DictionaryFieldKey) {
DictionaryFieldKey tmpkey = (DictionaryFieldKey) key;
String whereClause = " where " + dictionaryStructure.getDbTableName() + " = '" + tmpkey.getTableName() + "' and " +
dictionaryStructure.getDbFieldName() + " = '" + tmpkey.getFieldName() + "'";
return whereClause;
} else if (key instanceof DictionaryKey) {
String whereClause = " where " + dictionaryStructure.getDbTableName() + " = '" + key.getTableName() + "'";
return whereClause;
} else {
try {
throw new Exception("Not support this dictionary field!");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
/**
* Create a dictionary
* @param dictionary Object which will contain result.
* @param key If key is instance of DictionaryFieldKey, Dictionary only contains data equals to
* DictionaryFieldKey. if key is instance of DictionaryKey,Dicationary contains data
* equals to DictionaryKey
*/
public void createDictionaryByKey(Dictionary dictionary, DictionaryKey key) {
if (key == null) {
createDictionary(dictionary);
} else {
// dictionary.setDataSet(
// createDictionary0(
// "select * from " + dictionaryStructure.getDictionaryName() + " " + keyToWhereClause(key))
// );
dictionary.setDataSet(
vector2Hashtable(
createDictionary0("select * from " + dictionaryStructure.getDictionaryName() + " " + keyToWhereClause(key))
)
);
}
}
/**
* Create a dictionary
* @param dictionary contains all dictionayField.
*/
public void createDictionary(Dictionary dictionary) {
// dictionary.setDataSet(
// createDictionary0(
// "select * from " + dictionaryStructure.getDictionaryName()
// )
// );
dictionary.setDataSet(
vector2Hashtable(
createDictionary0("select * from " + dictionaryStructure.getDictionaryName())
)
);
}
private Hashtable vector2Hashtable(Vector dataSet){
Hashtable ht = new Hashtable();
DictionaryKey tmpDicKey = new DictionaryKey();
for (int i = 0; i < dataSet.size(); i ++ ){
DictionaryField df = (DictionaryField)dataSet.get(i);
tmpDicKey.setTableName(df.getTableName() );
if (ht.get(tmpDicKey) == null ){
DictionaryKey dk = new DictionaryKey();
dk.setTableName(df.getTableName() );
Vector fields = new Vector();
fields.add(df);
ht.put(dk,fields) ;
}else{
Vector fields = (Vector)ht.get(tmpDicKey);
fields.add(df);
}
}
return ht;
}
/**
* create dictionary, ignore field which field name equal <code>NULL</code>
* @param sql
* @return
*/
private Vector createDictionary0(String sql) {
Statement stat = GlobalConnection.createStatement(GlobalConnection.getCon(DictionaryProperties.getDatabaseName()));
Vector dataSet = new Vector();
ResultSet rs = null;
try {
System.out.println(sql);
rs = stat.executeQuery(sql);
while (rs.next()) {
DictionaryField df = new DictionaryField();
//set table name
String table = null;
if (dictionaryStructure.getDbTableName() != null) {
table = rs.getString(dictionaryStructure.getDbTableName() );
if (null != table)
df.setTableName(table);
}
//set alias_table
if (dictionaryStructure.getDbAliasTable() != null) {
String aliasTable = rs.getString(dictionaryStructure.getDbAliasTable());
if (null != aliasTable)
df.setAliasTable(aliasTable);
}
//set field name
String field = null;
if (dictionaryStructure.getDbFieldName() != null) {
field = rs.getString(dictionaryStructure.getDbFieldName() );
if (null != field)
df.setFieldName(field);
}
//set alias_field
if (dictionaryStructure.getDbAliasField() != null) {
String aliasField = rs.getString(dictionaryStructure.getDbAliasField());
if (null != aliasField)
df.setAliasField(aliasField);
}
//set field_type
if (dictionaryStructure.getDbFieldType() != null) {
String fieldField = rs.getString(dictionaryStructure.getDbFieldType());
if (null != fieldField)
df.setFieldType(fieldField);
}
//set field_description
if (dictionaryStructure.getDbDescription() != null) {
String description = rs.getString(dictionaryStructure.getDbDescription());
if (null != description)
df.setDescription(description);
}
//set fields display order
if (dictionaryStructure.getDbDisplayOrder() != null){
int displayOrder = rs.getInt(dictionaryStructure.getDbDisplayOrder());
if (rs.wasNull()){
//we consider it is the last column when no set displayOrder
displayOrder = Integer.MAX_VALUE ;
}
df.setDisplayOrder(displayOrder);
}
//add visible field
if (dictionaryStructure.getDbVisible() != null){
String visibleCode = rs.getString(dictionaryStructure.getDbVisible());
if (rs.wasNull()){
visibleCode = "visible";
}
df.setVisibleCode(visibleCode);
}
//end add
df.setKey(new DictionaryFieldKey(table, field)); //setkey as DictionaryFieldKey
dataSet.addElement(df);
}
} catch (SQLException e) {
System.out.println("Create data dictionary error!");
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
return null;
} finally {
try {
rs.close();
stat.close();
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
}
return dataSet;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -