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

import java.util.Enumeration;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import ilog.views.diagrammer.IlvDiagrammer;
import ilog.views.maps.servlet.IlvFacesMapsServlet;
import ilog.views.sdm.IlvSDMModel;
import ilog.views.sdm.servlet.IlvSDMServletSupport;
import ilog.views.servlet.IlvHitmapDefinition;
import ilog.views.servlet.ServerActionEvent;
import ilog.views.servlet.ServerActionListener;
import ilog.views.util.hitmap.IlvHitmapConstants;

/**
 * Server side bean that regularly changes the node state. Note: for the
 * demonstration, updating is based on a TimerTask, which should not be the case
 * in a pure J2EE application.
 */
public class UpdatingServlet extends IlvFacesMapsServlet implements ServerActionListener {
  private static final class DiagrammerUpdateTask extends TimerTask {
    private final IlvDiagrammer diagrammer;

    private DiagrammerUpdateTask(IlvDiagrammer diagrammer) {
      this.diagrammer = diagrammer;
    }

    Override
    public void run() {
      IlvSDMModel model = diagrammer.getEngine().getModel();
      Enumeration<?> objs = diagrammer.getEngine().getAllObjects();
      while (objs.hasMoreElements()) {
        Object obj = objs.nextElement();
        Object otype = model.getObjectProperty(obj, "type"); //$NON-NLS-1$
        if (otype != null) {// change the type of all objects
          int rnd = (int) (Math.random() * 4 + 1);
          model.setObjectProperty(obj, "type", "" + rnd); //$NON-NLS-1$ //$NON-NLS-2$
        }
      }
    }
  }

  private static final String TIMER_PROPERTY = "DemoTimer"; //$NON-NLS-1$
  private static final String TASK_PROPERTY = "DemoTask";//$NON-NLS-1$

  /**
   * Creates a new <code>UpdatingServlet</code>.
   */
  public UpdatingServlet() {// do nothing.
  }

  Override
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
    IlvHitmapDefinition hitmapDefinition = getServletSupport().getHitmapSupport().getHitmapDefinition();
    hitmapDefinition.registerHitmapInfoGenerator(IlvHitmapConstants.TOOLTIP_TYPE,
        new TooltipInfoGenerator(hitmapDefinition));
    addServerActionListener(this);
  }

  /**
   * {@inheritDoc}
   */
  Override
  protected IlvSDMServletSupport createServletSupport(ServletContext context) {
    return new UpdatingServletSupport(context);
  }

  Override
  public void actionPerformed(ServerActionEvent e) {
    final IlvDiagrammer diagrammer = getDiagrammer(e.getRequest());
    String name = e.getActionName();
    synchronized (diagrammer) {
      TimerTask task;
      if ("start".equals(name)) { //$NON-NLS-1$
        task = (TimerTask) diagrammer.getClientProperty(TASK_PROPERTY);
        if (task == null) {
          task = new DiagrammerUpdateTask(diagrammer);
          Timer timer = (Timer) diagrammer.getClientProperty(TIMER_PROPERTY);
          if (timer == null) {
            timer = new Timer();
            diagrammer.putClientProperty(TIMER_PROPERTY, timer);
          }
          diagrammer.putClientProperty(TASK_PROPERTY, task);
          timer.schedule(task, 0, 5000);
        }
      } else if ("stop".equals(name)) {//$NON-NLS-1$
        task = (TimerTask) diagrammer.getClientProperty(TASK_PROPERTY);
        if (task != null) {
          task.cancel();
          // diagrammer.putClientProperty(TASK_PROPERTY, null);
        }
        task.cancel();
      }
    }
  }
}