head	1.5;
access;
symbols
	TIGRIS_0_8_4:1.3.0.2;
locks; strict;
comment	@# @;


1.5
date	2000.12.14.03.10.00;	author leonardr;	state dead;
branches;
next	1.4;

1.4
date	2000.12.06.18.37.02;	author leonardr;	state Exp;
branches;
next	1.3;

1.3
date	2000.12.02.04.51.49;	author elicia;	state Exp;
branches;
next	1.2;

1.2
date	2000.11.27.22.07.34;	author leonardr;	state Exp;
branches;
next	1.1;

1.1
date	2000.11.27.18.17.46;	author leonardr;	state Exp;
branches;
next	;


desc
@@


1.5
log
@Moved paginator stuff to its own directory.
@
text
@/* ================================================================
 * Copyright (c) 2000 CollabNet.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by CollabNet (http://www.collab.net/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of CollabNet.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */


package org.tigris.helm.tool;

import java.util.*;
import java.sql.Connection;
import java.sql.SQLException;

import org.webmacro.util.Log;
import org.webmacro.servlet.WebContext;

import com.workingdogs.village.*;

import org.joist.database.DataManager;
import org.joist.util.QuotedString;
import org.joist.util.JoistProperties;

import org.tigris.helm.DoesNotExist;
import org.tigris.helm.project.Project;
import org.tigris.helm.domain.Domain;
import org.tigris.helm.database.ProjectManager;
import org.tigris.helm.security.AccessControlList;
import org.tigris.helm.tool.AccessControlledDatabaseFilter;

/**
 * Paginates a list for partial display (i.e. showing only a window of the 
 * list elements at a time). Also handles browsing the list.
 *
 * @@author <a href="mailto:leonardr@@collab.net">Leonard Richardson</a>
 * @@version $Id: ProjectPaginator.java,v 1.4 2000/12/06 18:37:02 leonardr Exp $
 */
public class ProjectPaginator extends Paginator
{
    /**
     * Open the log file.
     */
    private static Log log = new Log("usrpgr", "ProjectPaginator");

    /**
     * Whether or not to log debugging information.
     */
    private static final boolean debug = true;

    /**
     * The properties file key used to retrieve the number of projects
     * to display per page.  
     */
    private static final String PROJECTS_PER_PAGE = "nbrProjectsPerPage";

    /**
     * The context key used to retrieve the name search string.
     */
    public static final String BROWSE_NAME = "browseName";

    /**
     * The context key used to retrieve the name search type.
     */
    public static final String BROWSE_NAME_BY = "browseNameBy";

    /**
     * The context key used to retrieve the summary search string.
     */
    public static final String BROWSE_SUMMARY = "browseSummary";

    /**
     * The context key used to retrieve the summary search type.
     */
    public static final String BROWSE_SUMMARY_BY = "browseSummaryBy";

    /**
     * The context key used to retrieve the description search string.
     */
    public static final String BROWSE_DESCRIPTION = "browseDescription";

    /**
     * The context key used to retrieve the description search type.
     */
    public static final String BROWSE_DESCRIPTION_BY = "browseDescriptionBy";

    
    /**
     * Handles paging of the list.
     */
    public static List paginate (Connection connection, DataManager dMgr,
                                 WebContext context, AccessControlList acl,
                                 Domain domain)
        throws SQLException, DataSetException
    {

        int nbrItemsPerPage = 
            Integer.parseInt(JoistProperties.getInstance().getProperty(PROJECTS_PER_PAGE));

        //Get a DataSet matching what the context says to browse by.
        String browseName = context.getForm(BROWSE_NAME);
        String browseSummary = context.getForm(BROWSE_SUMMARY);
        String browseDescription = context.getForm(BROWSE_DESCRIPTION);
        String browseNameMatch = context.getForm(BROWSE_NAME_BY);
        String browseSummaryMatch = context.getForm(BROWSE_SUMMARY_BY);
        String browseDescriptionMatch = context.getForm(BROWSE_DESCRIPTION_BY);
        
        DataSet projectSet = browseProject(connection, dMgr, context, domain,
                                           browseName, browseNameMatch,
                                           browseSummary, browseSummaryMatch,
                                           browseDescription, 
                                           browseDescriptionMatch);        
        if (projectSet == null)
        {
            return new ArrayList();
        } else {
           List projects = null;
           try {
               projects = AccessControlledDatabaseFilter.getViewableProjects
                     (connection, dMgr, acl, projectSet);
           } catch (DoesNotExist ignore) {}
           return Paginator.paginate(projects, projectSet.size(), 
                                     nbrItemsPerPage, context);
        } 
        
    }

    /**
     *  Browse the project list by name, summary, description
     *
     * @@param	connection  A JDBC database connection
     * @@param	context  A WebContext
     * @@param	browseName  The project name to search for
     * @@param	browseNameMatch  A variable of type String
     * @@param	browseSummary  The project summary to search for
     * @@param	browseSummaryMatch  A variable of type String
     * @@param	browseDescription  The project description to search for
     * @@param	browseDescriptionMatch  A variable of type String
     * @@exception	SQLException
     * @@exception	DataSetException
     */
    protected static DataSet
        browseProject (Connection connection, DataManager dMgr,
                    WebContext context, Domain domain,
                    String browseName, String browseNameMatch,
                    String browseSummary, String browseSummaryMatch,
                    String browseDescription, String browseDescriptionMatch)
        throws SQLException, DataSetException
    {
        // build the search query statement
        StringBuffer statement = new StringBuffer();
        statement.append("select * from ").append(ProjectManager.TABLE_NAME);
        String connector = " where ";               

        if ((browseName != null) && (browseName.length() != 0))
        {
            browseName = makeSQLRegex(browseNameMatch, browseName);
            statement.append(connector);
            statement.append(ProjectManager.PROJECT_NAME).append(" LIKE ");
            statement.append(QuotedString.from(browseName));
            connector = " and ";
        }

        if (browseSummary != null && browseSummary.length() != 0)
        {
            browseSummary = makeSQLRegex(browseSummaryMatch, browseSummary);
            statement.append(connector);
            statement.append(ProjectManager.PROJECT_SHORT_DESCRIPTION)
                .append(" LIKE ");
            statement.append(QuotedString.from(browseSummary));
            connector = " and ";
        }
                
        if (browseDescription != null && browseDescription.length() != 0)
        {
            browseDescription = makeSQLRegex
                (browseDescriptionMatch, browseDescription);
            statement.append(connector);
            statement.append(ProjectManager.PROJECT_LONG_DESCRIPTION)
                .append(" LIKE ");
            statement.append(QuotedString.from(browseDescription));
        }
        
        statement.append(connector).append(" ").append
            (ProjectManager.PROJECT_TYPE).append("='base'");
        connector = " and ";
        statement.append(connector).append(" ")
            .append(ProjectManager.PROJECT_DOMAIN_ID).append("=")
            .append(domain.getID());
        statement.append(" order by ").append(ProjectManager.PROJECT_NAME);
        String selectStatement = statement.toString();

        if (debug) log.info("ProjectPaginator: statement: " + selectStatement);
        
        QueryDataSet qds = new QueryDataSet (connection, selectStatement);
        dMgr.registerToClose(qds);
        qds.fetchRecords();
        
        if (!qds.containsRecord(0))
	    return null;
        else
            return qds;
    }
}







@


1.4
log
@Return an empty list instead of null.
@
text
@d75 1
a75 1
 * @@version $Id: ProjectPaginator.java,v 1.3 2000/12/02 04:51:49 elicia Exp $
@


1.3
log
@now returning null if there are no projects found
@
text
@d75 1
a75 1
 * @@version $Id: ProjectPaginator.java,v 1.2 2000/11/27 22:07:34 leonardr Exp $
d153 1
a153 1
            return null;
@


1.2
log
@Finished project pagination for ProjectList
@
text
@d75 1
a75 1
 * @@version $Id: ProjectPaginator.java,v 1.1 2000/11/27 18:17:46 leonardr Exp $
d151 12
a162 7
        List projects = null;
        try {
            projects = AccessControlledDatabaseFilter.getViewableProjects
                (connection, dMgr, acl, projectSet);
        } catch (DoesNotExist ignore) {}
        return Paginator.paginate(projects, projectSet.size(), 
                                  nbrItemsPerPage, context);
d238 3
a240 3
	    context.put("Message", "No matching projects found");

        return qds;
@


1.1
log
@More Paginator changes.
@
text
@d75 1
a75 1
 * @@version $Id: UserPaginator.java,v 1.1 2000/11/27 16:23:05 leonardr Exp $
d82 1
a82 1
    private static Log log = new Log("usrpgr", "UserPaginator");
d118 1
a118 1
    public static final String BROWSE_DESCRIPTION = "browseEmail";
d123 1
a123 1
    public static final String BROWSE_DESCRIPTION_BY = "browseEmailBy";
d201 2
a202 1
            statement.append(ProjectManager.PROJECT_SHORT_DESCRIPTION).append(" LIKE ");
d209 2
a210 1
            browseDescription = makeSQLRegex(browseDescriptionMatch, browseDescription);
d212 2
a213 1
            statement.append(ProjectManager.PROJECT_LONG_DESCRIPTION).append(" LIKE ");
d217 2
a218 2
        statement.append(connector).append(" ").append(ProjectManager.PROJECT_TYPE)
            .append("='base'");
@

