/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */
package demo.editing;

import java.util.Calendar;
import java.util.Date;

import ilog.views.gantt.IlvActivity;
import ilog.views.gantt.IlvActivityFactory;
import ilog.views.gantt.IlvDuration;
import ilog.views.gantt.IlvGanttModel;
import ilog.views.gantt.IlvHierarchyChart;
import ilog.views.gantt.IlvHierarchyNode;
import ilog.views.gantt.IlvTimeInterval;
import ilog.views.gantt.faces.dhtml.component.IlvFacesGanttPropertyAccessor;
import ilog.views.util.time.IlvCalendarUtil;

/**
 * The bean that contains the logic of the JSF Gantt Chart Editing sample.
 */
public class EditingBean {
  /**
   * The property accessor used to get and set properties of Gantt model
   * elements.
   */
  private IlvFacesGanttPropertyAccessor propertyAccessor = new PropertyAccessor();

  /**
   * Creates a <code>EditingBean</code>.
   */
  public EditingBean() {
  }

  // Getters and Setters.
  /**
   * Returns the property accessor used to get and set properties of Gantt model
   * elements.
   * 
   * @return The property accessor.
   */
  public IlvFacesGanttPropertyAccessor getPropertyAccessor() {
    return propertyAccessor;
  }

  public void insertActivity(IlvHierarchyChart chart, String[] params) {
    IlvGanttModel model = chart.getGanttModel();
    if (model == null) {
      return;
    }
    IlvActivityFactory factory = chart.getActivityFactory();
    Calendar calendar = Calendar.getInstance();
    IlvCalendarUtil.dayFloor(calendar);
    Date today = calendar.getTime();
    IlvTimeInterval interval = new IlvTimeInterval(today, IlvDuration.ONE_DAY.multiply(4));
    IlvActivity newact = factory.createActivity(interval);
    IlvActivity root = model.getRootActivity();
    if (root == null) {
      model.setRootActivity(newact);
    } else {
      IlvHierarchyNode[] selected = chart.getSelectedRows();
      if (selected.length > 0 && selected[0] != root) {
        IlvActivity curact = (IlvActivity) selected[0];
        IlvActivity parent = model.getParentActivity(curact);
        int index = model.getParentActivityIndex(curact);
        interval = new IlvTimeInterval(curact.getStartTime(), IlvDuration.ONE_DAY.multiply(4));
        newact.setTimeInterval(interval);
        model.addActivity(newact, parent, index + 1);
      } else {
        int index = model.getChildActivityCount(root);
        interval = new IlvTimeInterval(root.getStartTime(), IlvDuration.ONE_DAY.multiply(4));
        newact.setTimeInterval(interval);
        model.addActivity(newact, root, index);
      }
    }
    chart.deSelectAllRows();
    chart.selectRow(newact, true);
  }

  public void deleteSelectedActivity(IlvHierarchyChart chart, String[] params) {
    IlvGanttModel model = chart.getGanttModel();
    if (model == null) {
      return;
    }
    IlvHierarchyNode[] selected = chart.getSelectedRows();
    for (int i = 0; i < selected.length; i++) {
      IlvActivity curact = (IlvActivity) selected[i];
      model.removeActivity(curact);
    }
    chart.deSelectAllRows();
  }
}