/**
 *
 * @(#)author  [lsm]
 * @(#)version 1.0
 * @(#)date   2014. 3. 5
 * @(#)since   JDK 1.4.2
 *
 * Copyright (c) www.hwenc.co.kr, Inc.
 * All rights reserved.
 * This software is the proprietary information of Hanwha E&C, Inc.
 */
 package kr.co.udapsoft.ebid.buyer.sycs.common.biz;


import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.sql.RowSet;
import kr.co.hsnc.common.sql.RowSetImpl;
import kr.co.hsnc.common.sql.persistent.Persistent;
import kr.co.hsnc.common.sql.persistent.PersistentImpl;
import kr.co.hsnc.common.sql.search.Search;
import kr.co.hsnc.common.sql.search.SearchImpl;
import kr.co.hsnc.common.util.ValueObject;

import com.udapsoft.modular.web.ibsheet7.IBSheet7Factory;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

public class CommGongCodeSectListSaveBiz extends Biz {

    
	public CommGongCodeSectListSaveBiz(HandlerStorage storage) {
		super(storage);
	}

	public void doBiz() throws Exception {

		boolean success = saveData(params);
		
		super.storage.setSuccess(success);
		
        storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
	}
	
	/**
	 * Persistent 
	 * @param params
	 * @return
	 * * 
     * @param connection
     * @param pgmName
     * @param screenName
     * @param systemCode
     * @throws Exception
	 */
	private boolean saveData(ValueObject params) {
		
		Persistent persistent_i = null;
		Persistent persistent_u = null;
		Persistent persistent_d = null;
		String sqlstr_i = "";
		String sqlstr_u = "";
		String sqlstr_d = "";
		
		try {

			conn.begin();
			
			persistent_i = new PersistentImpl(super.conn.getConnection());
			persistent_u = new PersistentImpl(super.conn.getConnection());
			persistent_d = new PersistentImpl(super.conn.getConnection());


			  sqlstr_i += "  INSERT INTO OUT_ITEM       \n"
					  	+ "   (          				\n"		
				        + "   	ITEM_CD, 				\n"	
				        + "		KIND_BIG,				\n"	
				        + "		KIND_MID,				\n"	
				        + "		ITEM_NAME,				\n"	
				        + "		DEPTH, 				    \n"	
				        + "		PARENT_LEVEL,			\n"	
				        + "		CRE_BY,				    \n"	
				        + "		CRE_DATE, 				\n"	
				        + "		UPD_BY,				    \n"	
				        + "		UPD_DATE  		        \n"		 
				        + " ) VALUES                  	\n"
				        + "  ( 							\n"
				        + "    ?,                     	\n"
				        + "    ?,                       \n"
				        + "    ?,                       \n"
				        + "    ?,                       \n"
				        + "    ?,                       \n"
				        + "    ?,                       \n"
				        + "    ?,                       \n"
				        + "    sysdate,             	\n"
				        + "    ?,                       \n"
				        + "    sysdate)             	\n";
				
			  sqlstr_u = " UPDATE OUT_ITEM     			\n"
		              + "     SET ITEM_NAME = ?         \n"
		              + "        ,UPD_DATE  = SYSDATE  	\n"
		              + "        ,UPD_BY    = ?    		\n"
		              + "   WHERE ITEM_CD   = ?     	\n" ;
			  
			  
			
			  
			String[] status  	= super.gets("sheet1.STATUS");
			String[] item_cd    = super.gets("sheet1.ITEM_CD");
			String[] kind_big   = super.gets("sheet1.KIND_BIG");
			String[] kind_mid   = super.gets("sheet1.KIND_MID");
			String[] item_name  = super.gets("sheet1.ITEM_NAME");
			String[] depth      = super.gets("sheet1.DEPTH");
			String[] level      = super.gets("sheet1.PARENT_LEVEL");
			
			persistent_i.setStatement(sqlstr_i);
			persistent_u.setStatement(sqlstr_u);
			
			if( status != null ) {
				for( int i = 0 ; i < status.length ; i++ ) {
					if( "I".equals(status[i]) ) {						
						persistent_i.addParameter(item_cd[i].trim());
						persistent_i.addParameter(kind_big[i].trim());
						persistent_i.addParameter(kind_mid[i].trim());
						persistent_i.addParameter(item_name[i].trim());
						persistent_i.addParameter(depth[i].trim());
						persistent_i.addParameter(level[i].trim());
						persistent_i.addParameter(storage.getUser().get("ACCT_ID"));	
						persistent_i.addParameter(storage.getUser().get("ACCT_ID"));	
								
						persistent_i.execute();
						persistent_i.clearParameters();
					}
					
					else if( "U".equals(status[i]) ) {
						persistent_u.addParameter(item_name[i].trim());
						persistent_u.addParameter(storage.getUser().get("ACCT_ID"));	
						persistent_u.addParameter(item_cd[i].trim());
						
						persistent_u.execute();
						persistent_u.clearParameters();
					}					
				}
			}
			return true;
        }
		catch(Exception e) {

            System.out.println(this.getClass().getName() + " --> saveData() : " + e.getMessage());
            System.out.println("SQL : " + persistent_i.getStatement());
            System.out.println(e);
            return false;
        }
        finally {
        	super.conn.end();
        }
	}	
}
