TemplateFormula.java 7.49 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 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
package com.csc.library.report;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Properties;

import com.csc.library.utilities.MyLog;

public class TemplateFormula {
	private HashMap condition;
	private HashMap data, orderByDesc;
	private MyReport myrep;
	private Properties formulaMethod;

	public TemplateFormula(HashMap condition, MyReport myrep) {
		this.condition = condition;
		this.myrep = myrep;
		this.setMethodProperties();
	}

	private void setMethodProperties() {
		formulaMethod = new Properties();
		formulaMethod.setProperty("PAGENUMBER", "getPageNumberFormula");
//		formulaMethod.setProperty("TOTALPAGENUMBER", "getTotalPageCountFormula");
		formulaMethod.setProperty("ONNEWPAGE", "getOnnewPageFormula");
		formulaMethod.setProperty("RECCOUNT", "getRecCountFormula");
		formulaMethod.setProperty("PSTIME", "getBeforPrintFormula");
		formulaMethod.setProperty("PETIME", "getAffterPrintFormula");
		if(myrep!=null){
		for (int i = 1; i <= myrep.getReportMetaData().getColumnOrderCount(); i++) {
			//formulaMethod.setProperty("INVISIBLE_VIRTUALFIELD.ORDERBY" + i, "getInvisibleFormular");
			formulaMethod.setProperty("INVISIBLE_ORDERBY" + i, "getInvisibleFormular");
			formulaMethod.setProperty("NEWPAGEORDER" + i, "getNewPageGroupFormular");
			formulaMethod.setProperty("ORDERBY" + i + "DESC", "getOrderByDescFormula");
		}
	  }
	}

	public String getPromptThaiLabel(String name, String value) {
		String dataIndex = "DEFAULT";
		if (name.indexOf("__") != -1) {
			dataIndex = name.substring(name.indexOf("__") + 2, name.length());
		}
		return myrep.getThaiLabel(this.conversString(value), dataIndex);
	}

	private String conversString(String value) {
		value = value.trim();
		value = value.replace(' ', '-');
		value = value.replace('(', '-');
		value = value.replace(')', '-');
		value = value.replace('#', '-');
		return value;
	}

	public String getPromptOrderLabel(String name, String value, String language) {
		String orderValue = (String) condition.get(value);
		value = (String) myrep.getReportMetaData().getFieldOrderLabel().get(orderValue);
		if (language.equalsIgnoreCase("THA"))
			return this.getPromptThaiLabel(name, value);
		else
			return value;
	}

	public String getFormulaValue(String name) {
		String methodName, value = null;
		methodName = this.formulaMethod.getProperty(name.toUpperCase());
		try {
			if (methodName != null) {
				Class c = this.getClass();
				Method m = c.getDeclaredMethod(methodName, new Class[] { String.class });
				value = (String) m.invoke(this, new Object[] { name });
			}
		} catch (Exception e) {
			MyLog.error(this, " Error in Set FormulaName " + name + " method " + methodName);
		}
		return value;
	}

	private boolean isBeforOnNewPage(int index) {
		String order = (String) condition.get("ORDERBY" + index);
		String newPage = (String) condition.get("ONNEWPAGE");
		int newPageIndex = 0;
		for (int i = 1; i <= myrep.getReportMetaData().getColumnOrderCount(); i++) {
			order = (String) condition.get("ORDERBY" + i);
			if (order != null && order.equals(newPage))
				newPageIndex = i;
		}
		if (index >= newPageIndex)
			return false;
		else
			return true;
	}

	private String getBeforPrintFormula(String s){
		return "timeString";
	}

	private String getAffterPrintFormula(String s){
		String screen=(String)this.condition.get("SCREEN");
		screen=screen.substring(screen.indexOf("-")+1,screen.length());
		return "\""+screen+"-\"+getDateShort()+\"-\"+getTimeShort()";
	}


	private String getOrderIndex(String field) {
		String order = "";
		for (int i = 1; i <= myrep.getReportMetaData().getColumnOrderCount(); i++) {
			order = (String) condition.get("ORDERBY" + i);
			if (order != null && order.equals(field))
				return "ORDERBY" + i;
		}
		return "";
	}

/*	private String getPageNumberFormula(String name) {
		int startPage = Integer.parseInt((String) condition.get("STARTPAGE"));
		String formula = "PageNumber+" + (startPage - 1);
		return formula;
	}
*/

	private String getPageNumberFormula(String name) {
		String page="PAGE";
		String language=(String)this.condition.get("LANGUAGE");
		if(language!=null && language.equals("THA"))
			page=this.getPromptThaiLabel("PAGE","PAGE");
		return "\""+page+"  \" +Totext(PageNumber,0) +\" / \"";
	}


/*	private String getTotalPageCountFormula(String name) {
		int startPage = Integer.parseInt((String) condition.get("STARTPAGE"));
		String formula = "TotalPageCount+" + (startPage - 1);
		return formula;
	}
*/
	private String getOnnewPageFormula(String name) {
		String tableName, fieldName = "";
		String formula = "";
		String newPage = (String) condition.get("ONNEWPAGE");
		if (newPage != null & !newPage.equals("")) {
			formula =
				"if Previous ({"
					+ this.condition.get("SCREEN")
					+ "."
					+ this.getOrderIndex(newPage)
					+ "})={"
					+ this.condition.get("SCREEN")
					+ "."
					+ this.getOrderIndex(newPage)
					+ "} or OnFirstRecord then false else true";
			return formula;
		} else
			return "false";
	}

	private String getOrderByDescFormula(String name) {
		String formula = "{" + (String) myrep.getReportMetaData().getFieldOrderDesc().get(name) + "}";
		if(formula==null){
			return "";
		}
		return formula;
	}

	private String getInvisibleFormular(String name) {
		String dbName=(String)this.condition.get("SCREEN");
		String formula = "IF {" + dbName+"."+name.substring(name.indexOf("_") + 1, name.length()) + "}=\"\" THEN TRUE ELSE FALSE";
		return formula;
	}

	private String getNewPageGroupFormular(String name) {
		int index = Integer.parseInt(name.substring(12, name.length()));
		StringBuffer value = new StringBuffer();
		value = new StringBuffer();
		if (condition.get("ONNEWPAGE") != null && !condition.get("ONNEWPAGE").equals("")) {
			if (index == 1) {
				value.append("if{@OnNewPage}=false  then(");
				value.append("if Previous ({");
				value.append(this.condition.get("SCREEN") + ".ORDERBY1");
				value.append("})<>{");
				value.append(this.condition.get("SCREEN") + ".ORDERBY1");
				value.append("} and OnFirstRecord=false  then true");
				value.append(" else false");
				value.append(")else true");
			} else {
				value.append("if{@OnNewPage}=true then(if ");
				for (int i = index; i > 1; i--) {
					value.append("Previous ({");
					value.append(this.condition.get("SCREEN") + ".ORDERBY" + (i - 1));
					value.append("})<>{");
					value.append(this.condition.get("SCREEN") + ".ORDERBY" + (i - 1));
					value.append("} ");
					if (i > 2)
						value.append(" or ");
				}
				value.append(" then false else true )else");
				if (this.isBeforOnNewPage(index)) {
					value.append("(if ");
					value.append("Previous ({");
					value.append(this.condition.get("SCREEN") + ".ORDERBY" + (index));
					value.append("})<>{");
					value.append(this.condition.get("SCREEN") + ".ORDERBY" + (index));
					value.append("} ");
					value.append(" and ");
					for (int i = index; i > 1; i--) {
						value.append("Previous ({");
						value.append(this.condition.get("SCREEN") + ".ORDERBY" + (i - 1));
						value.append("})={");
						value.append(this.condition.get("SCREEN") + ".ORDERBY" + (i - 1));
						value.append("} ");
						if (i > 2)
							value.append(" or ");
					}
					value.append(" then true else false) ");
				} else
					value.append(" false");
			}
		} else
			return "false";
		return value.toString();
	}

	private String getRecCountFormula(String Name) {
		return "countReccord;";
	}
}