/**
 * 
 * 新規
 * 2011/1/25
 * 孫奇全 作成
 * 
 */

package com.aimluck.eip.exttimecard;

import java.util.ArrayList;
import java.util.Date;

import org.apache.cayenne.access.DataContext;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.turbine.util.RunData;
import org.apache.velocity.context.Context;

import com.aimluck.commons.field.ALNumberField;
import com.aimluck.commons.field.ALStringField;
import com.aimluck.eip.cayenne.om.portlet.EipTExtTimecardSystem;
import com.aimluck.eip.cayenne.om.portlet.ImwTExtTimecardSystemDetail;
import com.aimluck.eip.common.ALAbstractFormData;
import com.aimluck.eip.common.ALDBErrorException;
import com.aimluck.eip.common.ALEipConstants;
import com.aimluck.eip.common.ALPageNotFoundException;
import com.aimluck.eip.common.ALPermissionException;
import com.aimluck.eip.exttimecard.util.ExtTimecardUtils;
import com.aimluck.eip.modules.actions.common.ALAction;
import com.aimluck.eip.orm.DatabaseOrmService;
import com.aimluck.eip.services.accessctl.ALAccessControlConstants;
import com.aimluck.eip.util.ALEipUtils;

public class ExtTimecardSystemDetailFormData extends ALAbstractFormData {

	private static final JetspeedLogger logger = JetspeedLogFactoryService
			.getLogger(ExtTimecardSystemDetailFormData.class.getName());

	/** 開始時刻 */
	private ALNumberField start_time_hour;

	private ALNumberField start_time_minute;

	/** 終了時刻 */
	private ALNumberField end_time_hour;

	private ALNumberField end_time_minute;

	private ALNumberField worktime_in;

	private ALNumberField resttime_in;

	private ALNumberField worktime_out;

	private ALNumberField resttime_out;

	private ALNumberField system_id;

	private ALStringField system_name;

	private ALNumberField end_hour;

	private ALNumberField end_minute;

	private ALNumberField start_hour;

	private ALNumberField start_minute;

	private ALStringField workstart_flag;

	private ALNumberField worktime;

	private ALNumberField worktime_status;

	private DataContext dataContext;

	/* Add 2011/1/25 孫奇全 Start---- */
	public String modeFlag = "";

	public ALNumberField system_detail_id;

	private ALStringField type;

	/** 現在のモード */
	private String mode;

	

	/* Add 2011/1/25 孫奇全 End---- */

	@Override
	protected boolean deleteFormData(RunData rundata, Context context,
			ArrayList msgList) throws ALPageNotFoundException,
			ALDBErrorException {
		try {
			ImwTExtTimecardSystemDetail record = ExtTimecardUtils
					.getImwTExtTimecardSystemDetail(rundata, context);
			if (record == null)
				return false;

			if (record.getSystemId().intValue() == 1) {
				// 勤務形態「通常」は削除不可
				msgList.add("選択範囲を削除。");
				return false;
			}

			dataContext.deleteObject(record);
			dataContext.commitChanges();

		} catch (Exception ex) {
			logger.error("Exception", ex);
			return false;
		}
		return true;

	}

	@Override
	protected boolean insertFormData(RunData rundata, Context context,
			ArrayList msgList) throws ALPageNotFoundException,
			ALDBErrorException {
	    try {
	      //Add 20110707 小濵 Start ---
          EipTExtTimecardSystem system = ExtTimecardUtils
              .getEipTExtTimecardSystem(dataContext, Long.valueOf(system_id.getValue()));
          //Add 20110707 小濵 Start ---
		  
	      ImwTExtTimecardSystemDetail record = (ImwTExtTimecardSystemDetail) dataContext
	          .createAndRegisterNewObject(ImwTExtTimecardSystemDetail.class);

//  	record.setSystemId((int) system_id.getValue());
			record.setEipTExtTimecardSystem(system);
			record.setStartHour((int) start_hour.getValue());
			record.setStartMinute((int) start_minute.getValue());
			record.setEndHour((int) end_hour.getValue());
			record.setEndMinute((int) end_minute.getValue());
			Date now = new Date();
			//Del 20110707 項目自体が存在しない
//			record.setCreateDate(now);
//			record.setUpdateDate(now);
			//Del 20110707 項目自体が存在しない
			
			String tmp;
			if ("1".equals(workstart_flag.getValue())) {
				tmp = "1";
			} else {
				tmp = "0";
			}
			record.setWorkstartFlag(tmp);
			int startMinute = (int) start_hour.getValue() * 60
					+ (int) start_minute.getValue();
			int endMinute = (int) end_hour.getValue() * 60
					+ (int) end_minute.getValue();
			if (endMinute > startMinute) {
				record.setWorktime(endMinute - startMinute);
			} else {
				record.setWorktime(endMinute + 24 * 60 - startMinute);
			}
			record.setWorktimeStatus(worktime_status.getValue() + "");
			dataContext.commitChanges();
		} catch (Exception ex) {
			logger.error("Exception", ex);
			return false;
		}
		return true;

	}

	@Override
	protected boolean loadFormData(RunData rundata, Context context,
			ArrayList msgList) throws ALPageNotFoundException,
			ALDBErrorException {
		try {
			ImwTExtTimecardSystemDetail recordDetail = ExtTimecardUtils
					.getImwTExtTimecardSystemDetail(rundata, context);
			EipTExtTimecardSystem record = ExtTimecardUtils
					.getEipTExtTimecardSystem(rundata, context);
			if (record == null && recordDetail == null) {
				return false;
			}

			if (record != null) {
				start_time_hour = new ALNumberField(record.getStartHour());
				start_time_minute = new ALNumberField(record.getStartMinute());
				end_time_hour = new ALNumberField(record.getEndHour());
				end_time_minute = new ALNumberField(record.getEndMinute());
				worktime_in.setValue(record.getWorktimeIn());
				resttime_in.setValue(record.getResttimeIn());
				worktime_out.setValue(record.getWorktimeOut());
				resttime_out.setValue(record.getResttimeOut());
			}
			if (recordDetail != null) {
				system_id.setValue(String.valueOf(recordDetail.getSystemId()));
				start_hour
						.setValue(String.valueOf(recordDetail.getStartHour()));
				start_minute.setValue(String.valueOf(recordDetail
						.getStartMinute()));
				end_hour.setValue(String.valueOf(recordDetail.getEndHour()));
				end_minute
						.setValue(String.valueOf(recordDetail.getEndMinute()));
				worktime.setValue(String.valueOf(recordDetail.getWorktime()));
				worktime_status.setValue(String.valueOf(recordDetail
						.getWorktimeStatus()));
				workstart_flag.setValue(String.valueOf(recordDetail
						.getWorkstartFlag()));
			}

		} catch (Exception ex) {
			logger.error("Exception", ex);
			return false;
		}
		return true;
	}

	@Override
	protected void setValidator() throws ALPageNotFoundException,
			ALDBErrorException {
		start_hour.limitValue(0, 23);
		start_minute.limitValue(0, 59);
		end_hour.limitValue(0, 23);
		end_minute.limitValue(0, 59);
		worktime_status.limitValue(0, 3);
	}

	@Override
	protected boolean updateFormData(RunData rundata, Context context,
			ArrayList msgList) throws ALPageNotFoundException,
			ALDBErrorException {
		try {
			ImwTExtTimecardSystemDetail record = ExtTimecardUtils
					.getImwTExtTimecardSystemDetail(rundata, context);
			if (record == null) {
				return false;
			}
			record.setStartHour((int) start_hour.getValue());
			record.setStartMinute((int) start_minute.getValue());
			record.setEndHour((int) end_hour.getValue());
			record.setEndMinute((int) end_minute.getValue());
			record.setWorktime((int) worktime.getValue());
			Date now = new Date();
            //Del 20110707 項目自体が存在しない
//			record.setUpdateDate(now);
            //Del 20110707 項目自体が存在しない
			int startMinute = (int) start_hour.getValue() * 60
					+ (int) start_minute.getValue();
			int endMinute = (int) end_hour.getValue() * 60
					+ (int) end_minute.getValue();
			if (endMinute > startMinute) {
				record.setWorktime(endMinute - startMinute);
			} else {
				record.setWorktime(endMinute + 24 * 60 - startMinute);
			}
			record.setWorktimeStatus(worktime_status.getValue() + "");
			String tmp;
			/* Updata 2011/03/06 IMTT 武 Start */
			if ("1".equals(workstart_flag.getValue())) {
				tmp = "1";
			} else {
				tmp = "0";
			}
			/* Updata 2011/03/06 IMTT 武 End */
			record.setWorkstartFlag(tmp);
			dataContext.commitChanges();
		} catch (Exception ex) {
			logger.error("Exception", ex);
			return false;
		}
		return true;
	}

	@Override
	protected boolean validate(ArrayList msgList)
			throws ALPageNotFoundException, ALDBErrorException {
		return (msgList.size() == 0);
	}

	public void initField() {
		system_id = new ALNumberField();
		system_id.setNotNull(true);

		system_detail_id = new ALNumberField();
		system_detail_id.setNotNull(true);

		system_name = new ALStringField();
		system_name.setFieldName("勤務形態");

		start_hour = new ALNumberField();

		start_minute = new ALNumberField();

		end_hour = new ALNumberField();

		end_minute = new ALNumberField();

		workstart_flag = new ALStringField();

		worktime = new ALNumberField();
		worktime.setFieldName("勤務時間");

		worktime_status = new ALNumberField();
		worktime_status.setFieldName("種別");

		start_time_hour = new ALNumberField();
		start_time_minute = new ALNumberField();
		end_time_hour = new ALNumberField();
		end_time_minute = new ALNumberField();

		worktime_in = new ALNumberField();
		worktime_in.setFieldName("勤務時間内の勤務時間");
		worktime_in.setNotNull(true);
		worktime_in.limitMinValue(0);

		resttime_in = new ALNumberField();
		resttime_in.setFieldName("勤務時間内の休憩時間");
		resttime_in.setNotNull(true);
		resttime_in.limitValue(0, 360);

		worktime_out = new ALNumberField();
		worktime_out.setFieldName("勤務時間外の勤務時間");
		worktime_out.setNotNull(true);
		worktime_out.limitMinValue(0);

		resttime_out = new ALNumberField();
		resttime_out.setFieldName("勤務時間外の休憩時間");
		resttime_out.setNotNull(true);
		resttime_out.limitValue(0, 360);
	}

	@Override
	public void init(ALAction action, RunData rundata, Context context)
			throws ALPageNotFoundException, ALDBErrorException {
		super.init(action, rundata, context);

		dataContext = DatabaseOrmService.getInstance().getDataContext();
	}

	/**
	 * フォームを表示します。
	 * 
	 * @param action
	 * @param rundata
	 * @param context
	 * @return TRUE 成功 FALSE 失敗
	 */
	public boolean doDetailViewForm(ALAction action, RunData rundata,
			Context context) {
		try {
			init(action, rundata, context);
			boolean isedit = (ALEipUtils.getTemp(rundata, context,
					ALEipConstants.ENTITY_ID) != null);

			int aclType = ALAccessControlConstants.VALUE_ACL_INSERT;
			if (isedit) {
				aclType = ALAccessControlConstants.VALUE_ACL_UPDATE;
			}
			doCheckAclPermission(rundata, context, aclType);

			action.setMode(isedit ? ALEipConstants.MODE_EDIT_FORM
					: ALEipConstants.MODE_NEW_FORM);
			mode = action.getMode();

			ArrayList msgList = new ArrayList();
			boolean res = (isedit) ? loadFormData(rundata, context, msgList)
					: setFormData(rundata, context, msgList);
			action.setResultData(this);
			action.addErrorMessages(msgList);
			action.putData(rundata, context);
			return res;
		} catch (ALPermissionException e) {
			ALEipUtils.redirectPermissionError(rundata);
			return false;
		} catch (ALPageNotFoundException e) {
			ALEipUtils.redirectPageNotFound(rundata);
			return false;
		} catch (ALDBErrorException e) {
			ALEipUtils.redirectDBError(rundata);
			return false;
		}

	}

	/**
	 * 
	 */
	protected boolean setFormData(RunData rundata, Context context,
			ArrayList msgList) throws ALPageNotFoundException,
			ALDBErrorException {
		boolean res = super.setFormData(rundata, context, msgList);

		if (res) {
			try {
				String id = ALEipUtils.getTemp(rundata, context, "systemId");
				system_id.setValue(id);

			} catch (Exception ex) {
				logger.error("[ExtTimecardSystemDetailFormData]", ex);
			}
		}
		return res;
	}

	public ALNumberField getStartHour() {
		return this.start_hour;
	}

	/**
	 * 
	 * @return
	 */
	public ALNumberField getStartMinute() {
		return this.start_minute;
	}

	/**
	 * 
	 * @return
	 */
	public ALNumberField getEndHour() {
		return this.end_hour;
	}

	/**
	 * 
	 * @return
	 */
	public ALNumberField getEndTimeMinute() {
		return this.end_minute;
	}

	/**
	 * 
	 * @return
	 */
	public ALNumberField getSystemDetailId() {
		return this.system_detail_id;
	}

	/**
	 * 
	 * @return
	 */
	public ALNumberField getWorktimeStatus() {
		return this.worktime_status;
	}

	public ALNumberField getSystem_id() {
		return system_id;
	}

	public void setSystem_id(ALNumberField system_id) {
		this.system_id = system_id;
	}

	public ALStringField getSystem_name() {
		return system_name;
	}

	public void setSystem_name(ALStringField system_name) {
		this.system_name = system_name;
	}

	public ALNumberField getEnd_hour() {
		return end_hour;
	}

	public void setEnd_hour(ALNumberField end_hour) {
		this.end_hour = end_hour;
	}

	public ALNumberField getEndMinute() {
		return end_minute;
	}

	public void setEndMinute(ALNumberField end_minute) {
		this.end_minute = end_minute;
	}

	public ALNumberField getStart_hour() {
		return start_hour;
	}

	public void setStart_hour(ALNumberField start_hour) {
		this.start_hour = start_hour;
	}

	public ALNumberField getStart_minute() {
		return start_minute;
	}

	public void setStart_minute(ALNumberField start_minute) {
		this.start_minute = start_minute;
	}

	public ALStringField getWorkstartFlag() {
		return workstart_flag;
	}

	public void setWorkstartFlag(ALStringField workstart_flag) {
		this.workstart_flag = workstart_flag;
	}

	public ALNumberField getWorktime() {
		return worktime;
	}

	public void setWorktime(ALNumberField worktime) {
		this.worktime = worktime;
	}

	public void setWorktimeStatus(ALNumberField worktimeStatus) {
		this.worktime_status = worktimeStatus;
	}

	public DataContext getDataContext() {
		return dataContext;
	}

	public void setDataContext(DataContext dataContext) {
		this.dataContext = dataContext;
	}

	public String getModeFlag() {
		return modeFlag;
	}

	public void setModeFlag(String modeFlag) {
		this.modeFlag = modeFlag;
	}

	public ALNumberField getSystem_detail_id() {
		return system_detail_id;
	}

	public void setSystem_detail_id(ALNumberField system_detail_id) {
		this.system_detail_id = system_detail_id;
	}

	public ALStringField getType() {
		return type;
	}

	public void setType(ALStringField type) {
		this.type = type;
	}

	public String getMode() {
		return mode;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}

	public static JetspeedLogger getLogger() {
		return logger;
	}

	public ALNumberField getStart_time_hour() {
		return start_time_hour;
	}

	public void setStart_time_hour(ALNumberField start_time_hour) {
		this.start_time_hour = start_time_hour;
	}

	public ALNumberField getStart_time_minute() {
		return start_time_minute;
	}

	public void setStart_time_minute(ALNumberField start_time_minute) {
		this.start_time_minute = start_time_minute;
	}

	public ALNumberField getEnd_time_hour() {
		return end_time_hour;
	}

	public void setEnd_time_hour(ALNumberField end_time_hour) {
		this.end_time_hour = end_time_hour;
	}

	public ALNumberField getEnd_time_minute() {
		return end_time_minute;
	}

	public void setEnd_time_minute(ALNumberField end_time_minute) {
		this.end_time_minute = end_time_minute;
	}

	public ALNumberField getWorktime_in() {
		return worktime_in;
	}

	public void setWorktime_in(ALNumberField worktime_in) {
		this.worktime_in = worktime_in;
	}

	public ALNumberField getResttime_in() {
		return resttime_in;
	}

	public void setResttime_in(ALNumberField resttime_in) {
		this.resttime_in = resttime_in;
	}

	public ALNumberField getWorktime_out() {
		return worktime_out;
	}

	public void setWorktime_out(ALNumberField worktime_out) {
		this.worktime_out = worktime_out;
	}

	public ALNumberField getResttime_out() {
		return resttime_out;
	}

	public void setResttime_out(ALNumberField resttime_out) {
		this.resttime_out = resttime_out;
	}

	

}
