This project has retired. For details please refer to its Attic page.
SimpleUserQuery xref
View Javadoc

1   package org.apache.archiva.redback.users.memory;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.archiva.redback.users.AbstractUserQuery;
23  import org.apache.archiva.redback.users.User;
24  
25  import java.util.Comparator;
26  
27  public class SimpleUserQuery
28      extends AbstractUserQuery
29  {
30  
31      /**
32       * Returns true if this user should be considered a match of the current query
33       *
34       * @param user
35       * @return
36       */
37      public boolean matches( User user )
38      {
39          if ( getUsername() != null && user.getUsername() != null &&
40              user.getUsername().toLowerCase().indexOf( getUsername().toLowerCase() ) == -1 )
41          {
42              return false;
43          }
44          else if ( getFullName() != null && user.getFullName() != null &&
45              user.getFullName().toLowerCase().indexOf( getFullName().toLowerCase() ) == -1 )
46          {
47              return false;
48          }
49          else if ( getEmail() != null && user.getEmail() != null &&
50              user.getEmail().toLowerCase().indexOf( getEmail().toLowerCase() ) == -1 )
51          {
52              return false;
53          }
54          else
55          {
56              return true;
57          }
58  
59      }
60  
61      /**
62       * Returns a comparator used for sorting a collection of User objects based on the ordering set
63       * on this UserQuery's {@link #setOrderBy(String)} and {@link #setAscending(boolean)}. 
64       * @return
65       */
66      public Comparator<User> getComparator()
67      {
68          return new Comparator<User>()
69          {
70              public int compare( User user1, User user2 )
71              {
72                  return ( isAscending() ? 1 : -1 ) * compareUsers( user1, user2 );
73              }
74          };
75      }
76  
77      private int compareUsers( User user, User user1 )
78      {
79          if ( ORDER_BY_EMAIL.equals( getOrderBy() ) )
80          {
81              return user.getEmail() == null ? -1
82                  : user1.getEmail() == null ? 1 : user.getEmail().compareTo( user1.getEmail() );
83          }
84          else if ( ORDER_BY_FULLNAME.equals( getOrderBy() ) )
85          {
86              return user.getFullName() == null ? -1
87                  : user1.getFullName() == null ? 1 : user.getFullName().compareTo( user1.getFullName() );
88          }
89          else
90          {
91              return user.getUsername().compareTo( user1.getUsername() );
92          }
93      }
94  }