head	1.3;
access;
symbols
	TIGRIS_0_9_0:1.2.0.2;
locks; strict;
comment	@# @;


1.3
date	2000.12.29.17.11.54;	author leonardr;	state dead;
branches;
next	1.2;

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

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


desc
@@


1.3
log
@Many, many pagination changes.
@
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.paginate;

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.database.UserManager;
import org.joist.util.*;

import org.tigris.helm.domain.User;
import org.tigris.helm.domain.Domain;

/**
 * Paginates a list for partial display (i.e. showing only a window of the 
 * list elements at a time). Also handles browsing the list. A lot of this
 * code is taken from Joist.
 *
 * @@author <a href="mailto:leonardr@@collab.net">Leonard Richardson</a>
 * @@author Daniel M. Pellegrini <A HREF="mailto:pilgrim@@dataweb-systems.com">pilgrim@@dataweb-systems.com</A>
 * @@version $Id: UserPaginator.java,v 1.2 2000/12/20 20:40:56 leonardr Exp $
 */
public class UserPaginator extends Paginator
{
    /**
     * Open the log file.
     */
    private static Log log = new Log("usrPgr", "UserPaginator");

    /**
     * The properties file key used to retrieve the number of users to display 
     * per page.
     */
    private static final String USERS_PER_PAGE = "nbrUsersPerPage";

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

    /**
     * The context key used to retrieve the loginID search string.
     */
    public static final String BROWSE_LOGIN_ID = "browseLoginID";

    /**
     * The context key used to retrieve the loginID search type.
     */
    public static final String BROWSE_LOGIN_ID_BY = "browseLoginIDBy";

    /**
     * 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 email search string.
     */
    public static final String BROWSE_EMAIL = "browseEmail";

    /**
     * The context key used to retrieve the email search type.
     */
    public static final String BROWSE_EMAIL_BY = "browseEmailBy";

    /**
     * The number of users to appear per page.
     */
    public static final int NUMBER_USERS_PER_PAGE =
         Integer.parseInt(JoistProperties.getInstance().getProperty(USERS_PER_PAGE));



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

        //Get a DataSet matching what the context says to browse by.
        String browseId = context.getForm(BROWSE_LOGIN_ID);
        String browseName = context.getForm(BROWSE_NAME);
        String browseEmail = context.getForm(BROWSE_EMAIL);
        String browseIdMatch = context.getForm(BROWSE_LOGIN_ID_BY);
        String browseNameMatch = context.getForm(BROWSE_NAME_BY);
        String browseEmailMatch = context.getForm(BROWSE_EMAIL_BY);
        
        DataSet userSet = browseUser(connection, dMgr, context, domain,
                                     browseId, browseIdMatch,
                                     browseName, browseNameMatch,
                                     browseEmail, browseEmailMatch);

        ArrayList users = new ArrayList();
        for (int i = 0; i < userSet.size(); i++)
        {
            try 
            {
                users.add(new User(connection, dMgr, userSet.getRecord(i)));
            } catch (org.tigris.helm.DoesNotExist ignore) {}
        }
                                   
        int nbrPages = Paginator.getNbrPages (NUMBER_USERS_PER_PAGE, userSet.size());
        if (nbrPages > 1)
        {
            getAlphabetRanges(context, users, nbrPages);
            return Paginator.paginate(users, userSet.size(), NUMBER_USERS_PER_PAGE, context);
        } else
        {
           return users;
        }
        
    }

    /**
     *  Browse the user by name, userID, email
     *
     * @@param	connection  A JDBC database connection
     * @@param	context  A WebContext
     * @@param	browseId  The user ID to search for
     * @@param	browseIdMatch  A variable of type String
     * @@param	browseName  The user name to search for
     * @@param	browseNameMatch  A variable of type String
     * @@param	browseEmail  The user email to search for
     * @@param	browseEmailMatch  A variable of type String
     * @@exception	SQLException
     * @@exception	DataSetException
     */
    protected static DataSet
        browseUser (Connection connection, DataManager dMgr,
                    WebContext context, Domain domain,
                    String browseId, String browseIdMatch,
                    String browseName, String browseNameMatch,
                    String browseEmail, String browseEmailMatch)
        throws SQLException, DataSetException
    {
        if (debug) log.info("browseUser enter");
        
        // build the search query statement
        StringBuffer statement = new StringBuffer();
        statement.append("select * from ").append(UserManager.TABLE_NAME);
        String connector = " where ";               

        if (browseId != null && browseId.length() != 0)
        {
            browseId = makeSQLRegex(browseIdMatch, browseId);
            statement.append(connector);
            statement.append("loginID LIKE ");
            statement.append(QuotedString.from(browseId));
            connector = " and ";
        }
        
        if ((browseName != null) && (browseName.length() != 0))
        {
            browseName = makeSQLRegex(browseNameMatch, browseName);
            statement.append(connector);
            statement.append("name LIKE ");
            statement.append(QuotedString.from(browseName));
            connector = " and ";
        }
        
        if (browseEmail != null && browseEmail.length() != 0)
        {
            browseEmail = makeSQLRegex(browseEmailMatch, browseEmail);
            statement.append(connector);
            statement.append("email LIKE ");
            statement.append(QuotedString.from(browseEmail));
            connector = " and ";
        }
        
        statement.append(connector).append(" type='base'");
        connector = " and ";
        statement.append(connector).append(" domainID=")
            .append(domain.getID());
        statement.append(" order by loginID");
        String selectStatement = statement.toString();

        if (debug) log.info("statement: " + selectStatement);
        
        QueryDataSet qds = new QueryDataSet (connection, selectStatement);
        dMgr.registerToClose(qds);
        qds.fetchRecords();
        
        if (!qds.containsRecord(0))
	    context.put("Message", "No matching users found");

        if (debug) log.info("browseUser exit");
        return qds;
    }

    /**
     *  Get the alphabetical range of user loginIDs for each page.
     *  Put into context a vector of page numbers with first letter
     *  Of first user name on page, and first letter of last user name.
     */
    private static void
        getAlphabetRanges (WebContext context, List users, int nbrPages)
        throws DataSetException
    {
            ArrayList pagesWithAlphabetRanges = new ArrayList();
            for (int i = 1; i <= nbrPages; i++)
            {
               ArrayList pageWithAlphabetRanges = new ArrayList();
               List userSubset = Paginator.getPage((List)users, 
                                                    i, NUMBER_USERS_PER_PAGE);
               User firstUser = (User)userSubset.get(0);
               User lastUser = (User)userSubset.get(userSubset.size()-1);
               String firstUserLetter = firstUser.getLoginID().
                                        substring(0,1).toUpperCase();
               String lastUserLetter = lastUser.getLoginID().                                                                substring(0,1).toUpperCase();
               pageWithAlphabetRanges.add(Integer.toString(i));
               pageWithAlphabetRanges.add(firstUserLetter);
               pageWithAlphabetRanges.add(lastUserLetter);
               pagesWithAlphabetRanges.add(pageWithAlphabetRanges);
            }
            context.put("counter", new Counter());
            context.put("pagesWithAlphabetRanges", pagesWithAlphabetRanges);
    }
}
@


1.2
log
@Fixing some bugs before I destroy this class.
@
text
@d73 1
a73 1
 * @@version $Id: UserPaginator.java,v 1.1 2000/12/14 03:10:00 leonardr Exp $
@


1.1
log
@Moved paginator stuff to its own directory.
@
text
@d73 1
a73 1
 * @@version $Id: UserPaginator.java,v 1.9 2000/12/09 01:40:44 elicia Exp $
d226 1
@

