Package com.trustyoram.kylieweb.html

Source Code of com.trustyoram.kylieweb.html.ArrayListHelper

package com.trustyoram.kylieweb.html;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import com.gracevallorani.web.spring.Configuration;
import com.netfever.common.datasource.api.IFile;
import com.netfever.common.datasource.api.IPath;
import com.netfever.common.exceptions.FaultDetails;
import com.netfever.common.exceptions.NetfeverException;
import com.trustyoram.web.storage.StorageServletException;
import com.trustyoram.web.storage.config.helpers.StorageHelper;
import com.trustyoram.web.storage.showreel.Categories;
import com.trustyoram.web.storage.showreel.HtmlShowreel;
import com.trustyoram.web.storage.showreel.Showreel;

public final class ArrayListHelper {
  private static final Logger log = Logger.getLogger(ArrayListHelper.class.getName());
  private static TreeMap<String, Showreel[]> categories = null;

  public static TreeMap<String, Showreel[]> getCategories(
      HttpServletRequest request) throws StorageServletException, IOException {
    IFile[] files;
    Showreel showreel;
    //InternalShowreel internalShowreel;
    HtmlShowreel htmlShowreel;
    //IFile f;
    ArrayList<Showreel> showreels;
    int i;
    IPath root;

    try {
      if (categories == null) {
        synchronized (ArrayListHelper.class) {
          if (categories == null) {
            root = Configuration.getSystemRoot().getRoot().getFolderItem("showreel");
            files = StorageHelper.getDefinitionFiles(root, "showreel.xml");

            showreels = new ArrayList<Showreel>();

            for (i = 0; i < files.length; i++) {
              try {
                showreel = Showreel.load(files[i]);

                if (showreel instanceof HtmlShowreel) {
                  htmlShowreel = (HtmlShowreel) showreel;
                  htmlShowreel.setFile(StorageHelper.getNonServletUrl(files[i].getParent(), htmlShowreel.getFile()));
                }

                showreels.add(showreel);
              } catch (Exception e) {
                log.log(Level.SEVERE,
                    "error loading "
                        + files[i].getAbsoluteName(), e);
              }
            }

            categories = Categories.getCategories(showreels
                .toArray(new Showreel[0]));
          }
        }
      }     
    } catch (NetfeverException e) {
      throw new StorageServletException(
          new FaultDetails(
              null,
              -1,
              e.getMessage(),
              null,
              null));
    }

    return categories;
  }

  public static void setCategories(TreeMap<String, Showreel[]> categories) {
    ArrayListHelper.categories = categories;
  }

  public static Showreel getCurrentView(
      TreeMap<String, Showreel[]> categories, String defaultview)
      throws ServletException {
    Showreel firstentry;
    Iterator<Entry<String, Showreel[]>> iterator;
    Entry<String, Showreel[]> entry;
    Showreel[] showreels;
    int i;

    if (categories == null)
      throw new ServletException("categories parameter cannot be null");

    iterator = categories.entrySet().iterator();

    firstentry = null;

    while (iterator.hasNext()) {
      entry = iterator.next();
      showreels = entry.getValue();

      if (showreels != null) {
        if ((firstentry == null) && (showreels.length > 0))
          firstentry = showreels[0];

        for (i = 0; i < showreels.length; i++) {
          if (showreels[i].getTitle().equals(defaultview))
            return showreels[i];
        }
      }
    }

    if (firstentry == null)
      throw new ServletException("no showreels found.");

    return firstentry;
  }
}
TOP

Related Classes of com.trustyoram.kylieweb.html.ArrayListHelper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.