DbData.java 3.44 KB
Newer Older
TongZuu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
package com.csc.library.utilities;

import java.rmi.RemoteException;
import java.util.HashMap;

import com.csc.library.session.DbInquiry;
import com.csc.library.session.InitialInquiry;

/**
 * @author
 * @SoCooL@
 * 
 */
public class DbData {
	private UProfile uprofile = null;

	private HashMap cacheData = new HashMap();
	
	private HashMap param = new HashMap();

	private boolean cache = false;


	public DbData(UProfile up) {
		this.setUProfile(up);
	}

	private String fetchDataToHash(String table, String field, String filter,
			String order, HashMap data) {
		try {
// comment by pas ͧҡ  initalInquiry Ẻö param 件֧ record 	Ҩ set param 仵ͧ set ͹ initMyTable		
//			DbInquiry inq = new InitialInquiry(this.getUprofile()).getDbInquiry(table, false);
			DbInquiry inq = new InitialInquiry(this.getUProfile()).getDbInquiry();
			if(this.param!=null&&this.param.size()>0)
				inq.setParam(param);
			inq.initMyTable(table,"","");
			if ((filter != null) && (!filter.equals(""))) {
				inq.setFilter(filter);
			}
			if ((order != null) && (!order.equals(""))) {
				inq.setOrderBy(order);
			}
			inq.setGroupfield("import");
			inq.refresh();
			inq.next();
			if (data != null)
				data.put(table, inq.getDataSet("").get("0"));
			return inq.getString(field);
		} catch (RemoteException e) {
			MyLog.error(this, this.getUProfile(),  e);
		}
		return "";
	}

	private String getFetchData(String table, String field) {
		return this.fetchDataToHash(table, field, "", "", null);
	}

	public String getString(String table, String field) {
		return this.getString(table, field, "", "");
	}

	public String getString(String table, String field, String filter) {
		return this.getString(table, field, filter, "");
	}

	public String getString(String table, String field, String filter,
			String order) {
		String fetchData = "";
		if (this.isCache()) {
			if (this.getCacheData().containsKey(table)) {// have
																						// data
																						// in
																						// cache
				return this.getFieldCache(table, field);
			} else {
				this.fetchDataToHash(table, field, filter, order, this
						.getCacheData());
				return this.getFieldCache(table, field);
			}
		} else {
			return this.getFetchData(table, field);
		}
	}

	public boolean isCache() {
		return cache;
	}

	public void setCache(boolean cache) {
		this.cache = cache;
	}

	protected String getFieldCache(String table, String field) {
		if (this.getCacheData().containsKey(table)) {
			HashMap result = (HashMap) this.getCacheData().get(table);
			if (result != null)
				return (String) result.get(field);
			return "";
		} else {
			MyLog.warn(this, getUProfile(),"Not Found Key:" + table);
			return null;
		}
	}

	protected HashMap getCacheData() {
		return cacheData;
	}

	protected void setCacheData(HashMap cacheData) {
		this.cacheData = cacheData;
	}

	/**
	 * Returns the uprofile.
	 * 
	 * @return UProfile
	 */
	public UProfile getUProfile() {
		return uprofile;
	}

	/**
	 * Sets the uprofile.
	 * 
	 * @param uprofile
	 *                   The uprofile to set
	 */
	public void setUProfile(UProfile uprofile) {
		this.uprofile = uprofile;
	}

	public void clearCacheData() {
		this.getCacheData().clear();
	}

	public HashMap getParam() {
		return param;
	}

	public void setParam(HashMap param) {
		this.param = param;
	}
	
	
}