net.sf.ginp.tags.GetFolders.java Source code

Java tutorial

Introduction

Here is the source code for net.sf.ginp.tags.GetFolders.java

Source

/*
 *  ginp - Java Web Application for Viewing Photo Collections
 *  Copyright (C) 2004  Douglas John Culnane <doug@culnane.net>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA  02110-1301  USA
 */
package net.sf.ginp.tags;

import net.sf.ginp.GinpModel;
import net.sf.ginp.config.ModelUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.tagext.IterationTag;

/**
 *  Get Folders.
 *
 * @author     Doug Culnane
 * @version    $Revision$
 */
public class GetFolders extends TagSupport implements IterationTag {
    /**
     *
     */
    private static final long serialVersionUID = 6072550251995966439L;
    private int count;
    private int end;

    /**
     *  instance of a single user session.
     */
    private GinpModel model;

    /**
     *  apache Commons Logger specific to this class.
     */
    private Log log = LogFactory.getLog(GetFolders.class);

    /**
     *  called after the body section has been assembled.
     *
     * @return                      Description of the Return Value
     * @exception  JspTagException  Description of the Exception
     */
    public final int doAfterBody() throws JspTagException {
        count++;

        if (count < end) {
            pageContext.setAttribute("link", "ginpservlet?cmd=selectpath&path="
                    + model.getCollection().getFolder(count) + "&colid=" + model.getCurrCollectionId());

            pageContext.setAttribute("name", (model.getCollection().getFolder(count))
                    .substring((model.getCollection().getFolder(count)).lastIndexOf("/") + 1));

            return EVAL_BODY_AGAIN;
        } else {
            return SKIP_BODY;
        }
    }

    /**
     *  called when the end tag is processed.
     *
     * @return                   Description of the Return Value
     * @exception  JspException  Description of the Exception
     */
    public final int doEndTag() throws JspException {
        count = 0;

        return EVAL_PAGE;
    }

    /**
     *  called when the start tag is processed.
     *
     * @return                   Description of the Return Value
     * @exception  JspException  Description of the Exception
     */
    public final int doStartTag() throws JspException {
        count = 0;

        try {
            model = ModelUtil.getModel((HttpServletRequest) pageContext.getRequest());
        } catch (Exception e) {
            log.error(e);
            throw new JspException(e);
        }

        end = model.getCollection().getFoldersLength();

        if (end > 0) {
            pageContext.setAttribute("link", "ginpservlet?cmd=selectpath&path="
                    + model.getCollection().getFolder(count) + "&colid=" + model.getCurrCollectionId());

            pageContext.setAttribute("name", (model.getCollection().getFolder(count))
                    .substring((model.getCollection().getFolder(count)).lastIndexOf("/") + 1));

            return EVAL_BODY_AGAIN;
        } else {
            return SKIP_BODY;
        }
    }

    /**
     * required by Interface.
     */
    public final void release() {
    }
}