GenerateShiftPattern.java 10.9 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 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
package com.csc.library.process;

import com.csc.library.utilities.MyLog;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;

import com.csc.library.database.MyHashMap;
import com.csc.library.session.DbInquiry;
import com.csc.library.session.DbRecord;
import com.csc.library.session.InitialInquiry;
import com.csc.library.session.InitialRecord;
import com.csc.library.utilities.CscCalendar;
import com.csc.library.utilities.MyTreeMap;
import com.csc.library.utilities.UProfile;

/*
 * class Ѻҧҧ
 *  Ѻӹǹѹͧҧ gen ¡˹Ҩӡ˹ѹ
 *  	 01		5 ѹ
 * 			 02		3 ѹ
 * 			 03		2 ѹ
 * ѹ - ش ҡ鹨зӡǹҧ ˹
 */
public class GenerateShiftPattern extends MyProcess {

	private int maxLoop = 0;
	MyHashMap hmShiftPattern = new MyHashMap(); //ٻẺͧѹ
	MyHashMap hmShiftCount = new MyHashMap(); //ѹ
	MyHashMap hmHoliday = new MyHashMap(); //ѹش
	
	//nuiss edit 
	 private DbRecord rec= null;  
	 private MyTreeMap myTMmtime2 =  new MyTreeMap();
  
	public Vector getProcessList() {
		Vector v = new Vector();
		v.addElement("All");
		return v;
	}
	
	/*
	 * ֧ѹش  HashMap 
	 * ͹㹡ѹش
	 */
	private void loadHolidayData() {
		String timeCode = (String) this.data.get("time0id");
		String holidayCode = "";
		try {			
			DbInquiry inq = new InitialInquiry(this.upf).getDbInquiry();		
			inq.setSchemaName(this.upf.getSchemaName());						
			inq.initMyTable("mtime0", "time0id='"+timeCode+"'", "");
			inq.setColumn("time0id, companyid, holidaycode");
			inq.refresh();
			if (inq.next()) {
				holidayCode = inq.getString("holidaycode");
				
				DbInquiry inqChild = new InitialInquiry(this.upf).getDbInquiry();		
				inqChild.setSchemaName(this.upf.getSchemaName());						
				inqChild.initMyTable("mholiday1", "", "");
				inqChild.setColumn("*");
				inqChild.refresh();
				while (inqChild.next()) {
					CscCalendar csc = new CscCalendar(inqChild.getString("hdate"));
					this.hmHoliday.put(csc.getYYYYMMDD(), csc.getYYYYMMDD());
				}
			}
		} catch (Exception e) {
			MyLog.error(this, e);
		}
	}
	
	/*
	 * 纤Ҩӹǹѹŧ HashMap
	 */
	private void initialShiftPattern() {
		int loop=0;
		String shift1="", shift2="", shift3="", count="", shift="";
		
		for (int i=1;i<=10;i++) {
			shift = "";
			count = (String) this.data.get("count"+i); 
			if (count.equals("")) {
				continue;
			}				
			
			shift1 = (String) this.data.get("shift"+i+"1");
			shift2 = (String) this.data.get("shift"+i+"2");
			shift3 = (String) this.data.get("shift"+i+"3");				
			
			if (shift1.equals("")) {
				shift += "NONE,";
			} else shift += shift1+",";
			
			if (shift2.equals("")) {
				shift += "NONE,";
			} else shift += shift2+",";
			
			if (shift3.equals("")) {
				shift += "NONE";
			} else shift += shift3+",";
									
			this.hmShiftPattern.put(String.valueOf(i), shift); //1=01,02,03 : 2=01,STP,00
			for (int j=1;j<=(Integer.parseInt(count));j++) {
				//ѹ 1=Ẻ 1 : ѹ 2=Ẻ 1 : ѹ 3=Ẻ 2
				int id=j+loop;
				this.hmShiftCount.put(String.valueOf(id), String.valueOf(i)); 
			}
			loop += Integer.parseInt(count);
		}
		this.maxLoop = loop;		
		MyLog.debug(this, "maxLoop : "+this.maxLoop);
		MyLog.debug(this, "hmShiftPattern : "+this.hmShiftPattern);
		MyLog.debug(this, "hmShiftCount : "+this.hmShiftCount);
		MyLog.debug(this, "hmHoliday : "+this.hmHoliday);
	}
	
	/*
	 * ӡҧ ´ٵӹǹѹ ʡ 
	 * ¨зӡҧ start_date  end_date
	 */
	private MyTreeMap generateShift() {
		String check = (String) this.data.get("check");
		String startDate = (String) this.data.get("start_date");
		String endDate = (String) this.data.get("end_date");	
		
		this.initialShiftPattern(); //纤Ҩӹǹѹŧ HashMap
		MyTreeMap tr = new MyTreeMap();
		CscCalendar cs1 = new CscCalendar(startDate);
		CscCalendar cs2 = new CscCalendar(endDate);
		int i=1;		
		String tmpShift="", tmpCount="";
		while (cs1.beforeEquals(cs2)) {			
			if (check.equals("1")) { //ѹشġ
				if (!this.hmHoliday.containsKey(cs1.getYYYYMMDD())) { //ѹشѵġ
					tmpCount = this.hmShiftCount.getString(String.valueOf(i));
					tmpShift = this.hmShiftPattern.getString(tmpCount);
					tr.put(cs1.getYYYYMMDD(), tmpShift);	
					i++;
					if (i>this.maxLoop) {
						i=1;				
					}	
				}								
			} else { //ѹشġ
				tmpCount = this.hmShiftCount.getString(String.valueOf(i));
				tmpShift = this.hmShiftPattern.getString(tmpCount);
				tr.put(cs1.getYYYYMMDD(), tmpShift);
				i++;
				if (i>this.maxLoop) {
					i=1;
				}
			}	
			cs1.incDate(1);	
		}		
		return tr;
	}

	/**
	 * Nuiss Edit 17/09/2551
	 */
	public void process() throws Exception {		
		String pkMtime2 = "";
		//MyTreeMap myTMmtime2 = new MyTreeMap();
		
		if (this.data.get("cmdValue").toString().equalsIgnoreCase("genshift")) {
			String timeCode = (String) this.data.get("time0id");		
			//DbRecord rec = new InitialRecord(this.upf).getDbRecord("Mtime2");	
			rec = new InitialRecord(this.upf).getDbRecord("Mtime2");	
			rec.setColumn("*");
			this.loadHolidayData(); //Ŵѹشѵġ
			
			MyTreeMap tr = this.generateShift(); //ҧе˹
			MyLog.debug(this, "========== DISPLAY ==========");
			
			Iterator it = tr.keySet().iterator(); //ǹзҧ
			while (it.hasNext()) {
				String key = (String) it.next();
				String value = (String) tr.get(key);
				MyLog.debug(this, key + " : " + (String) tr.get(key));
				
				CscCalendar csc = new CscCalendar(key);
				
				int i=1;
				StringTokenizer str = new StringTokenizer(value, ",");
				while (str.hasMoreTokens()) { //ǹӹǹ 1 ѹ ҡش 3 е 1 ѹ
					String sh = str.nextToken();
					
					if (sh.equals("NONE")) {
						sh="";
					}					
					
					/**Commit2 DB mtime2*/	
		 			String curPk = ""+(csc.getMonth()+1)+timeCode+(csc.getYear());
					
					if(pkMtime2.equals("")){
						pkMtime2 = curPk ;
						//set data 2 PK for new record
						rec.set("monthid", csc.getMonth()+1);
						rec.set("yearid", csc.getYear());
						rec.set("time0id", timeCode);
						
						//retrieve recent data from mtime2
						myTMmtime2 = retrieveMtime2((csc.getMonth()+1),timeCode,csc.getYear());
					}else if(pkMtime2.equals(curPk)){
						
					}else{
						//set recent data too
						if (myTMmtime2.size()>0){
							this.setRecentMtime2();
						}//end if 
						//save when change PK
						rec.save(); 
						//clear when change PK
						rec.clearField();
						//set data 2 PK for new record
						rec.set("monthid", csc.getMonth()+1);
						rec.set("yearid", csc.getYear());
						rec.set("time0id", timeCode);
						//set new PK to pkMtime2
						pkMtime2 = curPk ;
						myTMmtime2.clear();
						myTMmtime2 = retrieveMtime2((csc.getMonth()+1),timeCode,csc.getYear());
					}//end if 
					
					//set 
					rec.set("time"+csc.getDD()+i, sh); //ʡзҧѹ
		 			i++;
				} //end while  
		 	
				// ŷ  myTMmtime2 key = tr key ͡
				if (myTMmtime2.size()>0){
					myTMmtime2.remove(key);
				}//end if 
				
			}//end while
			
			//set recent data too
			if (myTMmtime2.size()>0){
				this.setRecentMtime2();
			}//end if 
	 		
			//last save
			rec.save(); 
		}//end if 		
	}//end method process
	
	public MyTreeMap retrieveMtime2(int monthid,String time0id,int yearid){
		MyTreeMap myTM = new MyTreeMap (); 
		/**Retrieve */
		try{
			DbInquiry tblMtime2 = new InitialInquiry(this.getUProfile()).getDbInquiry("MTIME2");
			tblMtime2.setFilter(" companyid='" + this.getUProfile().get("companyid").toString()+"'"
									+" and monthid=" + monthid 
									+" and time0id ='" +time0id+"'" 
									+" and yearid=" +yearid); 
								 
			tblMtime2.refresh();
			
			String timeidInDay="";
			CscCalendar aa = new CscCalendar(yearid+"-"+monthid+"-01");
			int dayCount = aa.getEndDateOfMonth();
			
			while (tblMtime2.next()) {
				for (int _day=1;_day<=dayCount;_day++){
					 for(int _time=1;_time<=3;_time++){
						if(_day<10){
							 if(_time==1){
								 if (tblMtime2.getCurrentDbRecord().getString("time0"+_day+_time).trim().equals("") ){
									 timeidInDay = "NONE"; 
								 }else{
									 timeidInDay =  tblMtime2.getCurrentDbRecord().getString("time0"+_day+_time); 
								 }
							 }else{
								 if (tblMtime2.getCurrentDbRecord().getString("time0"+_day+_time).trim().equals("") ){
									 timeidInDay +=","+"NONE"; 
								 }else{
									 timeidInDay +="," +tblMtime2.getCurrentDbRecord().getString("time0"+_day+_time); 
								 }
							}
						}else{
							 if(_time==1){
								 if (tblMtime2.getCurrentDbRecord().getString("time"+_day+_time).trim().equals("") ){
									 timeidInDay = "NONE"; 
								 }else{
									 timeidInDay =  tblMtime2.getCurrentDbRecord().getString("time"+_day+_time); 
								 }
							 }else{
								 if (tblMtime2.getCurrentDbRecord().getString("time"+_day+_time).trim().equals("") ){
									 timeidInDay +=","+"NONE"; 
								 }else{
									 timeidInDay +="," +tblMtime2.getCurrentDbRecord().getString("time"+_day+_time); 
								 }
							}
						}//end if 
					 }//end for _time
					 
					//set data 2 myTM
				 	CscCalendar dateKey = new CscCalendar(yearid,monthid,_day);
					myTM.put(dateKey.getYYYYMMDD(), timeidInDay);
				}//end for _day
			}//end while
		}catch(Exception ex){
			System.out.println("error in retrieveMtime2 \n"+ex.getStackTrace());
		}//end try
		return myTM;
	}//end method retrieveMtime2
		
	private void setRecentMtime2() {
		//㹡 remove key öǷѹѧ next  
		//ͧ clone value   pointer ѹ  address 
		MyTreeMap myTMmtime2tmp = (MyTreeMap) this.myTMmtime2.clone();
		Iterator itMtime2tmp =myTMmtime2tmp.keySet().iterator();
	 try{
	 	Iterator itMtime2 = this.myTMmtime2.keySet().iterator(); //ǹзҧ
		while (itMtime2tmp.hasNext()) {
			String key = (String) itMtime2tmp.next();
			String value = (String) myTMmtime2.get(key);
			int i=1;
			StringTokenizer str = new StringTokenizer(value, ",");
			CscCalendar csc = new CscCalendar(key);
			while (str.hasMoreTokens()) { //ǹӹǹ 1 ѹ ҡش 3 е 1 ѹ
				String sh = str.nextToken();
				
				if (sh.equals("NONE")) {
					sh="";
				}		
				//set data 2 this.rec
				this.rec.set("time"+csc.getDD()+i, sh); //ʡзҧѹ
				i++;
			}//end while
			
			//remove key
			this.myTMmtime2.remove(key);
		}//end while
	 }catch(Exception ex){
		 System.out.print(ex.getStackTrace());
	 }
	}//end method setRecentMtime2
		
}//end class