/*
 * 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.
 */

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

import ilog.views.gantt.IlvActivity;
import ilog.views.gantt.IlvConstraintType;
import ilog.views.gantt.IlvDuration;
import ilog.views.gantt.IlvResource;
import ilog.views.gantt.IlvTimeUtil;
import ilog.views.gantt.model.IlvDefaultGanttModel;
import ilog.views.gantt.model.IlvSimpleActivity;
import ilog.views.gantt.model.IlvSimpleConstraint;
import ilog.views.gantt.model.IlvSimpleReservation;
import ilog.views.gantt.model.IlvSimpleResource;
import ilog.views.util.time.IlvCalendarFactory;
import ilog.views.util.time.IlvCalendarUtil;

/**
 * <code>SimpleProjectDataModel</code> is an in-memory data model that is
 * initialized with the schedule for a simple development project.
 */
public class SimpleProjectDataModel extends IlvDefaultGanttModel {
  // =========================================
  // Instance Variables
  // =========================================

  /**
   * The activities.
   */
  private IlvActivity aRoot;
  private IlvActivity aGatherReqts;
  private IlvActivity aTalkToCusts;
  private IlvActivity aCustomrList;
  private IlvActivity aContactCust;
  private IlvActivity aWriteUpReqs;
  private IlvActivity aMarktngSpec;
  private IlvActivity aDraftSpec_1;
  private IlvActivity aDraftSpec_2;
  private IlvActivity aPrfOfConcpt;
  private IlvActivity aRoughDesign;
  private IlvActivity a__CADLayout;
  private IlvActivity a__Detailing;
  private IlvActivity aPrototypFab;
  private IlvActivity aOrderMaterl;
  private IlvActivity a__Machining;
  private IlvActivity aBurninTestg;
  private IlvActivity aPrepareDemo;
  private IlvActivity aDesignDevel;
  private IlvActivity aDevelPhase1;
  private IlvActivity aDevelPhase2;
  private IlvActivity aDevelPhase3;

  /**
   * The resources.
   */
//  private IlvResource rRoot;
  private IlvResource rMkting;
  private IlvResource rBGates;
  private IlvResource rStvJbs;
  private IlvResource rMkDell;
  private IlvResource rLGerst;
  private IlvResource rRchDev;
  private IlvResource rLTrvld;
  private IlvResource rJGoslg;
  private IlvResource rScOaks;
  private IlvResource rGBooch;
  private IlvResource rLyWall;

  // =========================================
  // Instance Construction and Initialization
  // =========================================

  /**
   * Constructs a new <code>SimpleProjectDataModel</code> that contains the
   * schedule for a typical development project.
   */
  public SimpleProjectDataModel() {
    createActivities();
    createResources();
    createReservations();
    createConstraints();
  }

  /**
   * Creates the schedule's activities.
   */
  protected void createActivities() {
    IlvDuration _1Day = IlvDuration.ONE_DAY;
    IlvDuration _2Days = IlvDuration.ONE_DAY.multiply(2);
    IlvDuration _3Days = IlvDuration.ONE_DAY.multiply(3);
    IlvDuration _5Days = IlvDuration.ONE_DAY.multiply(5);
    IlvDuration _1Week = IlvDuration.ONE_WEEK;
    IlvDuration _2Weeks = IlvDuration.ONE_WEEK.multiply(2);
    IlvDuration _3Weeks = IlvDuration.ONE_WEEK.multiply(3);
    IlvDuration _4Weeks = IlvDuration.ONE_WEEK.multiply(4);
    IlvDuration _5Weeks = IlvDuration.ONE_WEEK.multiply(5);
    IlvDuration _6Weeks = IlvDuration.ONE_WEEK.multiply(6);
    IlvDuration _7Weeks = IlvDuration.ONE_WEEK.multiply(7);
    IlvDuration _8Weeks = IlvDuration.ONE_WEEK.multiply(8);
//    IlvDuration _9Weeks = IlvDuration.ONE_WEEK.multiply(9);
//    IlvDuration _10Weeks = IlvDuration.ONE_WEEK.multiply(10);
    IlvDuration _12Weeks = IlvDuration.ONE_WEEK.multiply(12);
    IlvDuration _14Weeks = IlvDuration.ONE_WEEK.multiply(14);
    IlvDuration _20Weeks = IlvDuration.ONE_WEEK.multiply(20);

    Calendar calendar = IlvCalendarFactory.createInstance();
    IlvCalendarUtil.dayFloor(calendar);
    Date now = calendar.getTime();
    Date in1Day = IlvTimeUtil.add(now, _1Day);
//    Date in3Days = IlvTimeUtil.add(now, _3Days);
//    Date in1Week = IlvTimeUtil.add(now, _1Week);
    Date in2Weeks = IlvTimeUtil.add(now, _2Weeks);
    Date in3Weeks = IlvTimeUtil.add(now, _3Weeks);
    Date in4Weeks = IlvTimeUtil.add(now, _4Weeks);
    Date in6Weeks = IlvTimeUtil.add(now, _6Weeks);
    Date in7Weeks = IlvTimeUtil.add(now, _7Weeks);
    Date in8Weeks = IlvTimeUtil.add(now, _8Weeks);
//    Date in9Weeks = IlvTimeUtil.add(now, IlvDuration.ONE_WEEK.multiply(9));
//    Date in10Weeks = IlvTimeUtil.add(now, IlvDuration.ONE_WEEK.multiply(10));
    Date in15Weeks = IlvTimeUtil.add(now, IlvDuration.ONE_WEEK.multiply(15));
    Date in20Weeks = IlvTimeUtil.add(now, IlvDuration.ONE_WEEK.multiply(20));
//    Date in25Weeks = IlvTimeUtil.add(now, IlvDuration.ONE_WEEK.multiply(25));
//    Date in30Weeks = IlvTimeUtil.add(now, IlvDuration.ONE_WEEK.multiply(30));
//    Date in35Weeks = IlvTimeUtil.add(now, IlvDuration.ONE_WEEK.multiply(10));

    aRoot = new IlvSimpleActivity("A-Root", "Project Summary", now, _20Weeks);
    setRootActivity(aRoot);
    aGatherReqts = new IlvSimpleActivity("A-1", "Gather Requirements", now, _2Weeks);
    addActivity(aGatherReqts, aRoot);
    aTalkToCusts = new IlvSimpleActivity("A-1.1", "Talk to customers", now, _3Days);
    addActivity(aTalkToCusts, aGatherReqts);
    aCustomrList = new IlvSimpleActivity("A-1.1.1", "Compile customer list", now, _1Day);
    addActivity(aCustomrList, aTalkToCusts);
    aContactCust = new IlvSimpleActivity("A-1.1.2", "Contact customers", in1Day, _2Days);
    addActivity(aContactCust, aTalkToCusts);
    aWriteUpReqs = new IlvSimpleActivity("A-1.2", "Write up requirements", in1Day, in2Weeks);
    addActivity(aWriteUpReqs, aGatherReqts);
    aMarktngSpec = new IlvSimpleActivity("A-2", "Marketing Specification", in2Weeks, _1Week);
    addActivity(aMarktngSpec, aRoot);
    aDraftSpec_1 = new IlvSimpleActivity("A-2.1", "First Draft Specification", in2Weeks, _3Days);
    addActivity(aDraftSpec_1, aMarktngSpec);
    aDraftSpec_2 = new IlvSimpleActivity("A-2.2", "Second Draft Specification", in2Weeks, _5Days);
    addActivity(aDraftSpec_2, aMarktngSpec);
    aPrfOfConcpt = new IlvSimpleActivity("A-3", "Proof of Concept", in3Weeks, _5Weeks);
    addActivity(aPrfOfConcpt, aRoot);
    aRoughDesign = new IlvSimpleActivity("A-3.1", "Rough Design", in3Weeks, _1Week);
    addActivity(aRoughDesign, aPrfOfConcpt);
    a__CADLayout = new IlvSimpleActivity("A-3.1.1", "CAD Layout", in3Weeks, _1Day);
    addActivity(a__CADLayout, aRoughDesign);
    a__Detailing = new IlvSimpleActivity("A-3.1.2", "Detailing", in3Weeks, _3Days);
    addActivity(a__Detailing, aRoughDesign);
    aPrototypFab = new IlvSimpleActivity("A-3.2", "Fabricate Prototype", in4Weeks, _2Weeks);
    addActivity(aPrototypFab, aPrfOfConcpt);
    aOrderMaterl = new IlvSimpleActivity("A-3.2.1", "Order Materials", in4Weeks, _1Day);
    addActivity(aOrderMaterl, aPrototypFab);
    a__Machining = new IlvSimpleActivity("A-3.2.2", "Machining", in4Weeks, _1Week);
    addActivity(a__Machining, aPrototypFab);
    aBurninTestg = new IlvSimpleActivity("A-3.3", "Burnin Testing", in6Weeks, _1Week);
    addActivity(aBurninTestg, aPrfOfConcpt);
    aPrepareDemo = new IlvSimpleActivity("A-3.4", "Prepare Demo", in7Weeks, _1Week);
    addActivity(aPrepareDemo, aPrfOfConcpt);
    aDesignDevel = new IlvSimpleActivity("A-4", "Design and Development", in6Weeks, _14Weeks);
    addActivity(aDesignDevel, aRoot);
    aDevelPhase1 = new IlvSimpleActivity("A-4.1", "Phase I Development", in8Weeks, _7Weeks);
    addActivity(aDevelPhase1, aDesignDevel);
    aDevelPhase2 = new IlvSimpleActivity("A-4.2", "Phase II Development", in15Weeks, _5Weeks);
    addActivity(aDevelPhase2, aDesignDevel);
    aDevelPhase3 = new IlvSimpleActivity("A-4.3", "Phase III Development", in20Weeks, _12Weeks);
    addActivity(aDevelPhase3, aDesignDevel);
  }

  /**
   * Creates the schedule's resources.
   */
  protected void createResources() {
    IlvResource rRoot = new IlvSimpleResource("ACME Corp.", "All ACME Employees");
    setRootResource(rRoot);
    rMkting = new IlvSimpleResource("MKTG", "Marketing");
    addResource(rMkting, rRoot);
    rBGates = new IlvSimpleResource("BG", "Bill Gates");
    addResource(rBGates, rMkting);
    rStvJbs = new IlvSimpleResource("SJ", "Steve Jobs");
    addResource(rStvJbs, rMkting);
    rMkDell = new IlvSimpleResource("MD", "Michael Dell");
    addResource(rMkDell, rMkting);
    rLGerst = new IlvSimpleResource("LG", "Lou Gerstner");
    addResource(rLGerst, rMkting);
    rRchDev = new IlvSimpleResource("R&D", "Research and Development");
    addResource(rRchDev, rRoot);
    rLTrvld = new IlvSimpleResource("LT", "Linus Torvalds");
    addResource(rLTrvld, rRchDev);
    rJGoslg = new IlvSimpleResource("JG", "James Gosling");
    addResource(rJGoslg, rRchDev);
    rScOaks = new IlvSimpleResource("SO", "Scott Oaks");
    addResource(rScOaks, rRchDev);
    rGBooch = new IlvSimpleResource("GB", "Grady Booch");
    addResource(rGBooch, rRchDev);
    rLyWall = new IlvSimpleResource("LW", "Larry Wall");
    addResource(rLyWall, rRchDev);
  }

  /**
   * Creates the schedule's reservations.
   */
  protected void createReservations() {
    addReservation(new IlvSimpleReservation(rMkting, aTalkToCusts));
    addReservation(new IlvSimpleReservation(rMkDell, aCustomrList));
    addReservation(new IlvSimpleReservation(rLGerst, aCustomrList));
    addReservation(new IlvSimpleReservation(rStvJbs, aCustomrList));
    addReservation(new IlvSimpleReservation(rBGates, aContactCust));
    addReservation(new IlvSimpleReservation(rStvJbs, aContactCust));
    addReservation(new IlvSimpleReservation(rMkDell, aWriteUpReqs));
    addReservation(new IlvSimpleReservation(rMkDell, aDraftSpec_1));
    addReservation(new IlvSimpleReservation(rLGerst, aDraftSpec_1));
    addReservation(new IlvSimpleReservation(rMkDell, aDraftSpec_2));
    addReservation(new IlvSimpleReservation(rLGerst, aDraftSpec_2));
    addReservation(new IlvSimpleReservation(rBGates, aPrfOfConcpt));
    addReservation(new IlvSimpleReservation(rRchDev, aPrfOfConcpt));
    addReservation(new IlvSimpleReservation(rJGoslg, a__CADLayout));
    addReservation(new IlvSimpleReservation(rLyWall, a__CADLayout));
    addReservation(new IlvSimpleReservation(rJGoslg, a__Detailing));
    addReservation(new IlvSimpleReservation(rGBooch, a__Detailing));
    addReservation(new IlvSimpleReservation(rScOaks, a__Detailing));
    addReservation(new IlvSimpleReservation(rBGates, aPrototypFab));
    addReservation(new IlvSimpleReservation(rLTrvld, aPrototypFab));
    addReservation(new IlvSimpleReservation(rScOaks, aBurninTestg));
    addReservation(new IlvSimpleReservation(rStvJbs, aBurninTestg));
    addReservation(new IlvSimpleReservation(rGBooch, aPrepareDemo));
    addReservation(new IlvSimpleReservation(rLyWall, aPrepareDemo));
    addReservation(new IlvSimpleReservation(rLGerst, aPrepareDemo));
    addReservation(new IlvSimpleReservation(rJGoslg, aDevelPhase1));
    addReservation(new IlvSimpleReservation(rLTrvld, aDevelPhase1));
    addReservation(new IlvSimpleReservation(rJGoslg, aDevelPhase2));
    addReservation(new IlvSimpleReservation(rLTrvld, aDevelPhase2));
    addReservation(new IlvSimpleReservation(rScOaks, aDevelPhase2));
    addReservation(new IlvSimpleReservation(rJGoslg, aDevelPhase3));
    addReservation(new IlvSimpleReservation(rLTrvld, aDevelPhase3));
    addReservation(new IlvSimpleReservation(rScOaks, aDevelPhase3));
    addReservation(new IlvSimpleReservation(rLyWall, aDevelPhase3));
  }

  /**
   * Creates the schedule's constraints.
   */
  protected void createConstraints() {
    addConstraint(new IlvSimpleConstraint(aCustomrList, aContactCust, IlvConstraintType.END_START));
    addConstraint(new IlvSimpleConstraint(aTalkToCusts, aWriteUpReqs, IlvConstraintType.END_START));
    addConstraint(new IlvSimpleConstraint(aGatherReqts, aMarktngSpec, IlvConstraintType.END_START));
    addConstraint(new IlvSimpleConstraint(aDraftSpec_1, aDraftSpec_2, IlvConstraintType.END_START));
    addConstraint(new IlvSimpleConstraint(a__CADLayout, a__Detailing, IlvConstraintType.END_START));
    addConstraint(new IlvSimpleConstraint(aRoughDesign, aOrderMaterl, IlvConstraintType.START_START));
    addConstraint(new IlvSimpleConstraint(aDevelPhase1, aDevelPhase2, IlvConstraintType.END_START));
    addConstraint(new IlvSimpleConstraint(aDevelPhase2, aDevelPhase3, IlvConstraintType.END_START));
  }

}