001package org.apache.archiva.redback.users.memory;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.archiva.redback.users.AbstractUserQuery;
023import org.apache.archiva.redback.users.User;
024
025import java.util.Comparator;
026
027public class SimpleUserQuery
028    extends AbstractUserQuery
029{
030
031    /**
032     * Returns true if this user should be considered a match of the current query
033     *
034     * @param user
035     * @return
036     */
037    public boolean matches( User user )
038    {
039        if ( getUsername() != null && user.getUsername() != null &&
040            user.getUsername().toLowerCase().indexOf( getUsername().toLowerCase() ) == -1 )
041        {
042            return false;
043        }
044        else if ( getFullName() != null && user.getFullName() != null &&
045            user.getFullName().toLowerCase().indexOf( getFullName().toLowerCase() ) == -1 )
046        {
047            return false;
048        }
049        else if ( getEmail() != null && user.getEmail() != null &&
050            user.getEmail().toLowerCase().indexOf( getEmail().toLowerCase() ) == -1 )
051        {
052            return false;
053        }
054        else
055        {
056            return true;
057        }
058
059    }
060
061    /**
062     * Returns a comparator used for sorting a collection of User objects based on the ordering set
063     * on this UserQuery's {@link #setOrderBy(String)} and {@link #setAscending(boolean)}. 
064     * @return
065     */
066    public Comparator<User> getComparator()
067    {
068        return new Comparator<User>()
069        {
070            public int compare( User user1, User user2 )
071            {
072                return ( isAscending() ? 1 : -1 ) * compareUsers( user1, user2 );
073            }
074        };
075    }
076
077    private int compareUsers( User user, User user1 )
078    {
079        if ( ORDER_BY_EMAIL.equals( getOrderBy() ) )
080        {
081            return user.getEmail() == null ? -1
082                : user1.getEmail() == null ? 1 : user.getEmail().compareTo( user1.getEmail() );
083        }
084        else if ( ORDER_BY_FULLNAME.equals( getOrderBy() ) )
085        {
086            return user.getFullName() == null ? -1
087                : user1.getFullName() == null ? 1 : user.getFullName().compareTo( user1.getFullName() );
088        }
089        else
090        {
091            return user.getUsername().compareTo( user1.getUsername() );
092        }
093    }
094}