/*
 * 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.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletContext;

import ilog.views.util.servlet.tiling.IlvFileTileManager;
import ilog.views.util.servlet.tiling.IlvTileManager;

/**
 * Server side bean used to store the Diagrammer project url.
 */
public class ProjectBean {
  private IlvFileTileManager tileManager;
  private String project;

  /**
   * Returns the current project.
   * 
   * @return the current project.
   */
  public String getProject() {
    return project;
  }

  /**
   * Sets the diagrammer project url.
   * 
   * @param project
   *          the diagrammer project url.
   */
  public void setProject(String project) {
    this.project = project;
    tileManager = null;
  }

  /**
   * Returns true if the project model is editable.
   * 
   * @return true if the project model is editable.
   */
  public boolean isEditable() {
    return project != null && project.endsWith(".idpr"); //$NON-NLS-1$
  }

  /**
   * Returns true if the bean has no project loaded .
   * 
   * @return true if the bean has no project loaded .
   */
  public boolean isNoProjectLoaded() {
    return project == null || "null".equals(project); //$NON-NLS-1$ used to set
                                                      // a null project in the
                                                      // jsp
  }

  /**
   * JSF Action listener that clears server cache.
   * 
   * @param event
   *          ignored parameter.
   */
  public void clearServerCache(ActionEvent event) {
    if (event == null) {
      // unused
    }
    File root = getCacheRootDirectory();
    if (root != null && root.isDirectory()) {
      // Clearing Server Tile Cache in memory
      if (tileManager != null && tileManager.getCache() != null) {
        tileManager.getCache().clear();
      }
      // Clearing Server Tile Cache from other (previous) calls
      File cache2[] = root.listFiles();
      for (int i = 0; i < cache2.length; i++) {
        try {
          cache2[i].delete();
        } catch (SecurityException e) {
          e.printStackTrace();
        }
      }
      cache2 = null;
    }
  }

  private File getCacheRootDirectory() {
    FacesContext fc = FacesContext.getCurrentInstance();
    if (fc != null) {
      Object o = fc.getExternalContext().getContext();
      if (o instanceof ServletContext && project != null) {
        ServletContext context = (ServletContext) o;
        File tmpdir = (File) context.getAttribute("javax.servlet.context.tempdir"); //$NON-NLS-1$
        if (tmpdir == null) {
          tmpdir = new File(System.getProperty("java.io.tmpdir")); //$NON-NLS-1$
        }

        // encoding directory name
        // JV-3550
        String encodedName = null;
        try {
          encodedName = URLEncoder.encode(context.getServletContextName(), "UTF-8");//$NON-NLS-1$
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }
        return new File(tmpdir, encodedName + project);
      }
    }
    return null;
  }

  /**
   * Returns the tile manager that stores data on the server cache.
   * 
   * @return the tile manager that stores data on the server cache.
   */
  public IlvTileManager getTileManager() {
    if (tileManager == null) {
      File root = getCacheRootDirectory();
      if (root != null) {
        tileManager = new IlvFileTileManager(root, 1024 * 1024 * 5, 1024 * 1024);
      }
    }
    return tileManager;
  }

}