/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */
package com.icesoft.faces.facelets.example.editabletable;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import com.icesoft.faces.application.showcase.model.entity.Employee;
import com.icesoft.faces.application.showcase.model.service.EmployeeService;
import com.icesoft.faces.facelets.component.table.editable.EditableTableException;
import com.icesoft.faces.application.showcase.view.bean.examples.component.dataTable.DataTableBase;
import com.icesoft.faces.facelets.component.table.editable.EditableTableBean;
import com.icesoft.faces.facelets.component.table.editable.IEditableTableEventListener;

public class EditableTableExampleBean extends DataTableBase {

	private EditableTableBean editableTableBean;
	private List<SelectItem> departments;
	private boolean showIdColumn;
	private boolean readOnly;

	private static final Logger LOG = Logger.getLogger(EditableTableExampleBean.class.getName());

	public boolean isReadOnly() {
		return readOnly;
	}

	public void setReadOnly(boolean readOnly) {
		this.readOnly = readOnly;
	}

	public boolean isShowIdColumn() {
		return showIdColumn;
	}

	public void setShowIdColumn(boolean showIdColumn) {
		this.showIdColumn = showIdColumn;
	}

	public EditableTableExampleBean() {
	}

	public EditableTableBean getEditableTableBean() {
		return editableTableBean;
	}

	public List<SelectItem> getDepartments() {
		if (departments == null) {
			departments = new ArrayList<SelectItem>();
			Set<String> departmentSet = new HashSet<String>();
			for (Employee emp : (List<Employee>) this.getEmployees()) {
				if (emp.getDepartmentName() != null
						&& emp.getDepartmentName().length() > 0)
					departmentSet.add(emp.getDepartmentName());
			}
			for (String dep : departmentSet) {
				departments.add(new SelectItem(dep));
			}
		}
		return departments;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		super.setEmployeeService(employeeService);
		editableTableBean = new EditableTableBean(getEmployees(),
				new EditableTableEventListener());
	}

	public void validateLastName(FacesContext fc, UIComponent comp, Object obj) {
		String str = (String) obj;
		if (str != null && !Character.isUpperCase(str.charAt(0))) {
			throw new FacesException(
					"The Last Name must start with the capital letter");
		}
	}

	class EditableTableEventListener implements IEditableTableEventListener,
			Serializable {

		public Object createNewRow() {
			LOG.fine("createNewRow()");
			Employee emp = new Employee();
			emp.setBirthDate(new Date());
			emp.setId(460 + editableTableBean.getData().getRowCount());
			emp.setDepartmentName("Central");
			return emp;
		}

		public void rowDeleted(Object row) {
			LOG.fine("row deleted ---> " + row);
		}

		public void rowSelected(Object row) {
			LOG.fine("row selected ---> " + row);
		}

		public void rowUpdated(Object oldBean, Object newBean) {
			LOG.fine("row updated: " + oldBean + " ---> " + newBean);
		}

		public void rowEditCanceled(Object oldBean, Object newBean)
				throws EditableTableException {
			LOG.fine("row edit canceled");
		}

	}
}
