MyInqReport.java 26.8 KB
Newer Older
Naung1 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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
/*
 * Created on 31 .. 2548
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.csc.library.database;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;

import com.csc.library.session.DbRecord;
import com.csc.library.session.InitialRecord;
import com.csc.library.utilities.MyLog;
import com.csc.library.utilities.UProfile;

/**
 * @author Kim
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class MyInqReport extends MyInquiry {

	private DbRecord rec = null;

	private DataSet ds = null;

	private HashMap simColumn = new HashMap();

	private HashMap simTable = new HashMap();

	private HashMap simGroupby = new HashMap();

	private HashMap simHaving = new HashMap();

	private HashMap simRelate = new HashMap();
	
	private HashMap simWhere = new HashMap();
	
	// store all filter between table to table
	private Vector stringFilter = new Vector();
	
	// store parent table of filter
	private Vector parentTableFilter = new Vector();
	
	// store child table of filter
	private Vector childTableFilter = new Vector();

	// store relation type between table
	private Vector joinTypeFilter = new Vector();
	
	private boolean fetchAll = false;

	private String distinct = "";

	private String sql = "";

	private String column = "";

	private String from = "";

	private String where = "";

	private String groupby = "";

	private String having = "";

	private String orderby = "";

	private String join = "";

	public MyInqReport() {
		super();
	}

	public MyInqReport(UProfile user) {
		super(user);
	}

	public void setRecReport(DbRecord rec) {
		this.rec = rec;
	}

	public void appendSql(String cmd, String statement) {
		if (cmd.trim().equalsIgnoreCase("distinct")) {
			this.distinct = statement;
		} else if (cmd.trim().equalsIgnoreCase("where")) {
			this.where = statement;
		} else if (cmd.trim().equalsIgnoreCase("groupby")) {
			this.groupby = statement;
		} else if (cmd.trim().equalsIgnoreCase("having")) {
			this.having = statement;
		} else if (cmd.trim().equalsIgnoreCase("orderby")) {
			this.orderby = statement;
		}
		//		else if (cmd.trim().equalsIgnoreCase("leftjoin") && table != null) {
		//			this.join = "left join " + table + " on " + statement;
		//		} else if (cmd.trim().equalsIgnoreCase("rightjoin") && table != null)
		// {
		//			this.join = "right join " + table + " on " + statement;
		//		} else if (cmd.trim().equalsIgnoreCase("fulljoin") && table != null)
		// {
		//			this.join = "full join " + table + " on " + statement;
		//		}
	}

	public void refresh() {
		this.genSql();
		this.ds = new DataSet(this.charCode("point1", this.sql),
				this.databaseName, this.getTableName());
		this.ds.setFetchAll(this.fetchAll);
		try {
			this.ds.initConnection(this.getUProfile());
		} catch (SQLException e) {
	    	MyLog.error(this,  e);
		} catch (RemoteException e) {
	    	MyLog.error(this,  e);
		}

	}

	public boolean next() {
		boolean b = false;
		try {
			if (this.fetch()) {
				b = true;
			} else {
				b = false;
			}
		} catch (Exception ex) {
	    	MyLog.error(this,  ex);
		}
		return b;
	}

	protected boolean fetch() throws Exception {
		boolean flag = false;
		if (ds.next()) {
			flag = true;
			this.assignFetch(this.rec, this.ds);
		}
		return flag;
	}

	private void assignFetch(DbRecord record, DataSet data) {
		try {
			String tableName = record.getTableName();
			String actualName = record.getActualName();
			Vector fieldList = record.getArrayFieldList();
			for (int i = 0; i < this.aField.size(); i++) {
				String fieldName = this.aField.get(i).toString();
				FieldManager field = record.getField(fieldName);
//				String dataTemp = ds.getString(actualName + "#" + fieldName);
				String dataTemp = ds.getString(this.genAlias(actualName,fieldName,"#",30));
				if (field instanceof FieldDouble || field instanceof FieldAutoNumber
						|| field instanceof FieldBigDecimal || field instanceof FieldFloat
						|| field instanceof FieldInteger || field instanceof FieldLong) {
					if (dataTemp == null || dataTemp.equals("") || 
							dataTemp.equalsIgnoreCase("null"))
						dataTemp = "0";
				}
				field.set(dataTemp);
			}
			record.assignField(null);
			Hashtable htChild = record.getChildTable();
			if (!htChild.isEmpty()) {
				Enumeration enu = htChild.elements();
				while (enu.hasMoreElements()) {
					this.assignFetch((DbRecord) enu.nextElement(), this.ds);
				}
			}
		} catch (Exception e) {
	    	MyLog.error(this,  e);
		}
	}

	public String getString(String fieldName) {
		String value = "";
		fieldName = fieldName.toLowerCase();
		if (fieldName.equals("test")) {
			this.genSql();
			System.out.println(this.sql);
			return this.sql;
		}
		try {
			if (!this.simColumn.containsKey(fieldName)) {
				value = this.rec.getString(fieldName);
			} else {
				FieldManager fm = this.rec.getField(fieldName);
				String stringResult = "";
				try {
					stringResult = this.ds.getString(fieldName);
					if (stringResult == null || stringResult.equals("") 
							|| stringResult.equalsIgnoreCase("null")) {
						if (fm == null || fm instanceof FieldDouble 
								|| fm instanceof FieldAutoNumber
								|| fm instanceof FieldBigDecimal || fm instanceof FieldFloat
								|| fm instanceof FieldInteger || fm instanceof FieldLong) {
							stringResult = "0";
						} else stringResult = "";
					}
					fm.set(stringResult);
					value = fm.getString();
				} catch (NullPointerException ne) {
					MyLog.warn(this, this.getUProfile(), "=============== MyInqReport getString : Not " + "have Sim Field Name - " + fieldName);
					value = stringResult;
				}
			}
		} catch (RemoteException e) {
			MyLog.warn(this, "warning can not get data in field :"+fieldName);
			MyLog.warn(this, e);
		}
		return value;
	}

	public String getString(String tableName, String fieldName) {
		String value = "";
		fieldName = fieldName.toLowerCase();
		try {
			if (!this.simColumn.containsKey(fieldName)) {
				value = this.rec.getChild(tableName).getString(fieldName);
			} else {
				FieldManager fm = ((DbRecord) this.rec.getChild(tableName)).getField(fieldName);
				fm.set(this.ds.getString(fieldName));
				try {
					value = fm.getString();
				} catch (NullPointerException ne) {
					MyLog.warn(this, this.getUProfile(), "=============== MyInqReport getString : Not have " + "Sim Field Name - " + fieldName);
					value = this.ds.getString((String) this.simColumn.get(fieldName));
					if (value == null || value.equals("") 
							|| value.equalsIgnoreCase("null")) {
						if (fm instanceof FieldDouble || fm instanceof FieldAutoNumber
								|| fm instanceof FieldBigDecimal || fm instanceof FieldFloat
								|| fm instanceof FieldInteger || fm instanceof FieldLong) {
							value = "0";
						} else value = "";
					}
				}
			}
		} catch (RemoteException e) {
			MyLog.warn(this, "warning can not get data in field :"+this.rec+tableName+fieldName);
			MyLog.warn(this, e);
		}
		return value;
	}

	/**
	 * generate sql statement
	 *  
	 */
	public void genSql() {

		// clear data for all sql component
		this.sql = "";
		this.column = "";
		this.from = "";
		this.where = "";
		//this.groupby = "";
		//this.having = "";
		//this.orderby = "";
		this.join = "";

		String filter = this.getFilter();
		if (filter != null && !filter.equals("")) {
			String fixFilter = "";
			String tempFilter = new String(filter);
			String actualName = "";
			try {
				actualName = this.rec.getActualName();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			while (tempFilter.length() > 0) {
				int andPos = tempFilter.indexOf(" and ");
				int orPos = tempFilter.indexOf(" or ");
				String operator = "";
				if ((andPos < orPos || orPos == -1) && (andPos != -1)) {
					String subSql = tempFilter.substring(0, andPos+1).trim();
					if (subSql.indexOf(".") == -1) 
						subSql = actualName + "." + subSql;
					fixFilter = fixFilter + subSql;
					operator = " and ";
					tempFilter = tempFilter.substring(andPos + 4).trim();
				} else if ((orPos < andPos || andPos == -1) && (orPos != -1)) {
					String subSql = tempFilter.substring(0, orPos).trim();
					if (subSql.indexOf(".") == -1) 
						subSql = actualName + "." + subSql;
					fixFilter = fixFilter + subSql;
					operator = " or ";
					tempFilter = tempFilter.substring(orPos + 3).trim();
				} else if (orPos == -1 && andPos == -1) {
					String subSql = tempFilter;
					if (subSql.indexOf(".") == -1)
						subSql = actualName + "." + tempFilter;
					fixFilter = fixFilter + subSql;
					operator = " ";
					tempFilter = "";
				}
				fixFilter = fixFilter + operator;
			}
			this.where = fixFilter;
		}
		String orderby = this.getOrderBy();
		if (orderby != null && !orderby.equals(""))
			this.orderby = orderby;

		String tsql = new String("");
		HashMap hm = this.genFieldRelation(this.rec);
		String colsname = (String) hm.get("allfield");
		if (this.column != null && !this.column.equals("")) {
			this.column = this.column + "," + colsname;
		} else {
			this.column = colsname;
		}
		this.from = (String) hm.get("tablename");
		if (this.join != null && !this.join.equals("")) {
			this.from = this.from + " " + this.join;
		}
		String relateString = (String) hm.get("relation");
		String comfilter = this.getCompanyFilter();
		if (comfilter != null) {
			relateString += " and " + comfilter;
			if (relateString.indexOf(" and ") == 0) {
				relateString = relateString.substring(5);
			}
		}
		if (this.where != null && !this.where.equals("")) {
			if (relateString != null && !relateString.equals(""))
				this.where = this.where + " and " + relateString;
			relateString="";
		} else if (relateString != null && !relateString.equals("")) {
			this.where = relateString;
			relateString = "";
		}
		String simColumns = this.genSimColumns(this.where);
		if (simColumns != null && !simColumns.equals(""))
			if (this.column != null && !this.column.equals("")) {
				this.column = this.column + "," + simColumns;
			} else {
				this.column = simColumns;
			}
		if (this.distinct != null && !this.distinct.equals(""))
			tsql = "select distinct (" + this.distinct + "), " + this.column + " from " + this.from;
		else
			tsql = "select " + this.column + " from " + this.from;
		if (this.where != null && !this.where.equals("")) {
			tsql = tsql + " where " + this.where;
		}
		if (this.groupby != null && !this.groupby.equals("")) {
			tsql = tsql + " group by " + this.groupby;
		}
		if (this.having != null && !this.having.equals("")) {
			tsql = tsql + " having " + this.having;
		}
		if (this.orderby != null && !this.orderby.equals("")) {
			tsql = tsql + " order by " + this.orderby;
		}
		this.sql = tsql;
		MyLog.warn(this, this.getUProfile(), "========= Generate Sql : " + tsql);
	}

	/**
	 * generate field and relation of each record
	 * 
	 * @param db
	 * @return HashMap : obtain fieldList ,relation ,tableName in sql statement
	 */
	private HashMap genFieldRelation(DbRecord db) {
		HashMap hm = new HashMap();
		try {
			String tableName = db.getTableName();
			String actualName = db.getActualName();
			String fieldList = db.getFieldList();
			fieldList = this.reformField(actualName, fieldList);
			Hashtable ht = db.getChildTable();
			String relateString = new String("");
			if (ht.size() != 0) {
				Enumeration enu = ht.elements();
				String allChildName = "";
				String joincond = "";
				while (enu.hasMoreElements()) {
					DbRecord childdb = (DbRecord) enu.nextElement();
					String childname = childdb.getTableName();
					String actualChildName = childdb.getActualName();
					Relation relation = db.getRelation(actualChildName);
					String vFieldParent = "";
					String vFieldChild = "";
					String relChildname = relation.childTableName;
					if (relation == null) {
						relation = childdb.getRelation(actualName);
						vFieldParent = relation.getFieldtListChild();
						vFieldChild = relation.getFieldListParent();
					} else {
						if (!actualChildName.equals(relChildname)) {
							vFieldParent = relation.getFieldListParent();
							vFieldChild = relation.getFieldtListChild();
						} else {
							vFieldParent = relation.getFieldtListChild();
							vFieldChild = relation.getFieldListParent();
						}
					}
					int posFilter = stringFilter.size();
					String relateTemp = "";
					relateTemp = this.genRelation(actualName, vFieldParent,
							actualChildName, vFieldChild);
					String jointype = childdb.getParam("join_type");
					HashMap hmChild = this.genFieldRelation(childdb);
					this.stringFilter.add(posFilter,relateTemp);
					this.joinTypeFilter.add(posFilter,jointype);
					this.parentTableFilter.add(posFilter,actualName);
					String tnameChild = (String) hmChild.get("tablename");
					this.childTableFilter.add(posFilter,actualChildName);
					if (jointype == null || jointype.equals("")) {
						jointype = "left";
					}
					if (jointype.equalsIgnoreCase("left")) {
						joincond = joincond + " left join " + tnameChild
						//+ " as " + actualChildName
								+ " on " + relateTemp;
						relateTemp = "";
						tnameChild = "";
					} else if (jointype.equalsIgnoreCase("right")) {
						joincond = joincond + " right join " + tnameChild
						//+ " as " + actualChildName
								+ " on " + relateTemp;
						relateTemp = "";
						tnameChild = "";
					} else if (jointype.equalsIgnoreCase("full")) {
						joincond = joincond + " full join " + tnameChild
						//+ " as " + actualChildName
								+ " on " + relateTemp;
						relateTemp = "";
						tnameChild = "";
					} else if (jointype.equalsIgnoreCase("inner")) {
						joincond = joincond + " join " + tnameChild
						//+ " as " + actualChildName
								+ " on " + relateTemp;
						relateTemp = "";
						tnameChild = "";
					} else if (jointype.equalsIgnoreCase("simple")) {
						relateString = relateString + " and " + relateTemp;
					}
					fieldList = fieldList + ","
							+ (String) hmChild.get("allfield");
					relateTemp = (String) hmChild.get("relation");
					if (relateTemp != null && !relateTemp.equals("")) {
						relateString = relateString + " and " + relateTemp;
					}
					if (tnameChild != null && !tnameChild.equals(""))
						allChildName = allChildName + "," + tnameChild;
				}
				if (allChildName.indexOf(",") == 0) {
					allChildName = allChildName.substring(1);
				}
				tableName = tableName + " " + actualName + " " + joincond + "," + allChildName;
				if (tableName.lastIndexOf(",") + 1 == tableName.length())
					tableName = tableName.substring(0, tableName.lastIndexOf(","));
			}
			if (relateString.indexOf(" and ") == 0) {
				relateString = relateString.substring(5);
			}
			hm.put("allfield", fieldList);
			hm.put("relation", relateString);
			if (tableName.length() <= actualName.length()) {
				tableName = tableName + " " + actualName;
			}
			hm.put("tablename", tableName);
		} catch (RemoteException e) {
			MyLog.warn(this, this.getUProfile(), e);
		}
		return hm;
	}

	/**
	 * reform fieldlist from fieldname to tablename.fieldname
	 * 
	 * @param tname :
	 *            table name
	 * @param flist :
	 *            field list
	 * @return String : field list with table name
	 */
	private String reformField(String tname, String flist) {
		String nform = new String("");
		StringTokenizer stn = new StringTokenizer(flist, ",");
		while (stn.hasMoreTokens()) {
			String fieldName = stn.nextToken();
			nform = nform + "," + tname + "." + fieldName + " as " + this.genAlias(tname,fieldName,"#",30);
		}
		if (nform.indexOf(",") == 0)
			nform = nform.substring(1);
		return nform;
	}

	private String genAlias(String table,String field,String operator,int length) {
		String alias = table + operator + field;
		if (alias.length() > length) {
			alias = alias.substring(0,length);
		}
		return alias;
	}
	
	/**
	 * generate relation between parent record and child record
	 * 
	 * @param tNameParent :
	 *            parent's table name
	 * @param fieldParent :
	 *            field parent
	 * @param tNameChild :
	 *            child's table name
	 * @param fieldChild :
	 *            field child
	 * @return String : sql command relation between parent and child
	 */
	private String genRelation(String tNameParent, String fieldParent,
			String tNameChild, String fieldChild) {
		String relation = new String("");
		if (fieldParent != null && fieldChild != null) {
			StringTokenizer stnParent = new StringTokenizer(fieldParent, ",");
			StringTokenizer stnChild = new StringTokenizer(fieldChild, ",");
			while (stnParent.hasMoreTokens()) {
				String fnameParent = stnParent.nextToken();
				String fnameChild = stnChild.nextToken();
				relation = relation + " and " + tNameParent + "." + fnameParent
						+ "=" + tNameChild + "." + fnameChild;
			}
			if (!relation.equals(""))
				relation = relation.substring(5);
		}
		return relation;
	}

	/**
	 * set sim column and sql to Class to gen sql string
	 * 
	 * @param String
	 *            columnName
	 * @param String
	 *            tableName
	 * @param String
	 *            simName
	 * @param String
	 *            groupby
	 * @param String
	 *            having
	 * @param String
	 *            extCommand : extension sql command after from having (compute
	 *            etc.)
	 */
	public void setSimColumn(String columnName, String tableName,
			String simName, String groupby, String having, String tableRelate,String whereCond) {
		this.simColumn.put(simName.toLowerCase(), columnName.toLowerCase());
		this.simTable.put(simName.toLowerCase(), tableName.toLowerCase());
		this.simGroupby.put(simName.toLowerCase(), groupby);
		this.simHaving.put(simName.toLowerCase(), having);
		this.simRelate.put(simName.toLowerCase(), tableRelate);
		this.simWhere.put(simName.toLowerCase(), whereCond);
		//		if (this.column != null && !this.column.equals("")) {
		//			this.column = this.column + "," + columnName;
		//		} else {
		//			this.column = columnName;
		//		}
	}

	/**
	 * generate sim column in sql command for evaluate
	 * 
	 * @param relationString :
	 *            relation string from main sql
	 * @return String : Sql command of all sim column
	 */
	private String genSimColumns(String relationString) {
		String simColumns = "";
		Iterator iterSimCol = this.simColumn.keySet().iterator();
		while (iterSimCol.hasNext()) {
			String simCol = (String) iterSimCol.next();
			String simColumn = "";
			String simTableName = (String) this.simTable.get(simCol);
			String tableRelate = (String) this.simRelate.get(simCol);
			int posColumn = this.childTableFilter.indexOf(simTableName);
			String simWhereCond = (String) this.simWhere.get(simCol);
			String compFilter = "";
			if (simWhereCond != null && !simWhereCond.equals(""))
				if (relationString.length() > 0)
					compFilter = relationString + " and " + simWhereCond;
				else
					compFilter = simWhereCond;
			else 
				compFilter = relationString;
			
			// get relationString from vector to fill in sub query
			if (tableRelate != null && !tableRelate.equals("") && 
					!tableRelate.equalsIgnoreCase("all")) {
				StringTokenizer stn = new StringTokenizer(tableRelate,",");
				while (stn.hasMoreTokens()) {
					String table = stn.nextToken();
					if (compFilter.length() > 0) {
						String temp = this.getRelationBetTable(posColumn,simTableName,
								table,false);
						if (temp.length() > 0)
							compFilter = compFilter + " and " + temp;
					} else {
						compFilter = this.getRelationBetTable(posColumn,simTableName,
								table,false);
					}
				}
			} else {
				MyLog.warn(this, this.getUProfile(), "=========== MyInqReport genSimColumns(String) : DBXML not have relatetable on Field - " + simCol);
				// if tablerelate isn't fill in ,it mean not get relationString from 
				// vector have store relation in main query
				// if tablerelate fill 'all' ,it mean get all relationString from 
				// vector to fill in sub query
				if (tableRelate.equalsIgnoreCase("all")) {
					if (posColumn != -1) {
						if (compFilter.length() > 0) {
							compFilter = compFilter + " and "
									+ (String) this.stringFilter.get(posColumn);
						} else {
							compFilter = (String) this.stringFilter.get(posColumn);
						}
					} else {
						int posParent = this.parentTableFilter.indexOf(simTableName);
						String relateTemp = "";
						while (posParent != -1) {
							relateTemp = relateTemp + " and "
									+ (String) this.stringFilter.get(posParent);
							posParent = this.parentTableFilter.indexOf(
									simTableName, posParent + 1);
						}
						if (relateTemp.indexOf(" and ") == 0)
							relateTemp = relateTemp.substring(5);
						if (relationString.length() > 0) {
							if (relateTemp != null && !relateTemp.equals(""))
								compFilter = compFilter + " and " + relateTemp;
						} else
							compFilter = relateTemp;
					}
				}
			}
			if (compFilter.length() > 0) compFilter =  " where " + compFilter;
			simColumn = "(select " + this.simColumn.get(simCol) + " from "
					+ this.simTable.get(simCol) +  compFilter;
			String groupby = (String) this.simGroupby.get(simCol);
			if (groupby != null && !groupby.equals(""))
				simColumn = simColumn + " group by " + groupby;
			String having = (String) this.simHaving.get(simCol);
			if (having != null && !having.equals(""))
				simColumn = simColumn + " having " + having;
			simColumn = simColumn + ") as " + simCol;
			simColumns = simColumns + " ," + simColumn;
		}
		if (!simColumns.equals(""))
			simColumns = simColumns.substring(2);
		return simColumns;
	}
	
	private String getRelationBetTable(int startPos,String parent,String child,boolean change) {
		String relationString = "";
		if (startPos == -1) startPos=0;
		int posParent = this.parentTableFilter.indexOf(parent,startPos);
		int posChild = this.childTableFilter.indexOf(child,startPos);
		if (posParent != -1 && posChild != -1 && 
				this.parentTableFilter.get(posChild).equals(parent)) {
			relationString = (String) this.stringFilter.get(posChild);
		} else if (posParent == -1 && posChild == -1 && !change) {
			relationString = this.getRelationBetTable(startPos,child,parent,true);
		} else {
			String frelatep = "";
			String frelatec = "";
			try {
				String rootname = this.rec.getActualName();
				frelatep = "";
				frelatec = "";
				if (rootname.equalsIgnoreCase(parent)) {
					frelatep = this.rec.getRelation(child).parentField.toString();
					frelatep = frelatep.substring(1,frelatep.length()-1).trim();
					frelatec = this.rec.getRelation(child).childField.toString();
					frelatec = frelatec.substring(1,frelatec.length()-1).trim();
				} else {
					DbRecord db = (DbRecord) this.rec.getChild(child);
					frelatep = db.getRelation(parent).getVFieldListParent().toString();
					frelatep = frelatep.substring(1,frelatep.length()-1).trim();
					frelatec = db.getRelation(parent).getVFieldtListChild().toString();
					frelatec = frelatec.substring(1,frelatec.length()-1).trim();					
				}
			} catch (Exception e) {
				MyLog.warn(this, e);
			}
 			relationString = this.genRelation(parent,frelatep,child,frelatec);
		}
		return relationString;
	}
	
	private String getCompanyFilter() {
		String comfilter = null;
		try {
			String comid = this.rec.getUProfile().get("companyid");
			String fieldList = this.rec.getFieldList();
			if (fieldList.indexOf("companyid") > -1) {
				comfilter = new String();
				comfilter = this.rec.getActualName() + ".companyid='" + comid + "'";
			}
		} catch (RemoteException e) {
			MyLog.warn(this, e);
		}
		return comfilter;
	}

	public void setAllRequiredFound(boolean require) {
		ChildManager child;
		this.setRequiredFound(require);
		try {
			for (Enumeration enu = this.rec.getChildTable().elements(); enu
					.hasMoreElements();) {
				child = (ChildManager) enu.nextElement();
				child.setRequiredFound(require);
				this.setRequiredAllChild(child, require);
			}
		} catch (Exception e) {
			MyLog.warn(this, e);
		}
	}

	public Hashtable getChildTable() {
		try {
			return this.rec.getChildTable();
		} catch (Exception e) {
			MyLog.warn(this, e);
		}
		return null;
	}

	public String getTableName() {
		try {
			return this.rec.getTableName();
		} catch (Exception e) {
			MyLog.warn(this, e);
		}
		return null;
	}

	public ChildManager getChild(String childName) {
		ChildManager b = null;
		try {
			b = this.rec.getChild(childName);
		} catch (Exception e) {
			MyLog.warn(this, e);
		}
		return b;
	}

	public int recCount() {
		this.genSql();
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		int reccount = 0;
		String tsql = "select count(*)";
		if (this.distinct != null && !this.distinct.equals(""))
			tsql = "select count(distinct " + this.distinct + ") ";
		tsql += " from " + this.from;
		if (this.where != null && !this.where.equals("")) {
			tsql = tsql + " where " + this.where;
		}
		if (this.groupby != null && !this.groupby.equals("")) {
			tsql = tsql + " group by " + this.groupby;
		}
		if (this.having != null && !this.having.equals("")) {
			tsql = tsql + " having " + this.having;
		}
		//		if (this.orderby != null && !this.orderby.equals("")) {
		//			tsql = tsql + " order by " + this.orderby;
		//		}
		MyLog.info(this, "========= Generate Sql : " + tsql);
		try {
			if (this.databaseName.equals("")) { 
				con = StaticProperties.myConnection.getConnection(this.databaseName,userPro);
			} else {	
				con = StaticProperties.myConnection.getConnection(this.userPro.getDbName(),userPro);
			}	
			st = con.createStatement();
			rs = st.executeQuery(tsql);
			while (rs.next()) {
				reccount += rs.getInt(1);
			}
			rs.close();
			st.close();
			con.close();
		} catch (SQLException e) {
			MyLog.warn(this, e);
		}

		return reccount;
	}

	public void referLangOn() {
		try {
			this.rec.referLangOn();
			this.referLang = true;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void referLangOff() {
		try {
			this.rec.referLangOff();
			this.referLang = false;
		} catch (Exception ex) {
			MyLog.warn(this, ex);
		}
	}

	public String getType() {
		return "inqreport";
	}

	public void formatOn() {
		this.isFormat = true;
		this.rec.formatOn();
	}

	public void formatOff() {
		isFormat = false;
		this.rec.formatOff();
	}
	//	private String getSimColumn(String columnName,String tableName,
	//			String simName,String relateString,String groupby,String having,
	//			String extCommand) {
	//		String simColumn = "";
	//		simColumn = "(select " + columnName + " from " + tableName + " where " +
	//			relateString;
	//		if (groupby != null && !groupby.equals(""))
	//			simColumn = simColumn + " group by " + groupby;
	//		if (having != null && !having.equals(""))
	//			simColumn = simColumn + " having " + having;
	//		if (extCommand != null && !extCommand.equals(""))
	//			simColumn = simColumn + extCommand;
	//		simColumn = simColumn + ") as " + simName;
	//		return simColumn;
	//	}
	
	public DbRecord getCurrentDbRecord() {
		return this.rec;
	}
	
	public String getActualName() {
		try {
			return this.rec.getActualName();
		} catch (RemoteException e) {
			MyLog.warn(this, e);
			return null;
		}
	}
		
}