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

1   package org.apache.archiva.redback.users.jdo;
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.UserQuery;
24  import org.codehaus.plexus.util.StringUtils;
25  
26  import java.util.ArrayList;
27  import java.util.HashSet;
28  import java.util.List;
29  import java.util.Set;
30  
31  
32  public class JdoUserQuery
33      extends AbstractUserQuery
34  {
35  
36      /**
37       * Create the ordering string for use in {@link javax.jdo.Query#setOrdering(String)}
38       *
39       * @return the created filter
40       */
41      public String getOrdering()
42      {
43          StringBuilder ordering = new StringBuilder();
44  
45          if ( UserQuery.ORDER_BY_EMAIL.equals( getOrderBy() ) )
46          {
47              ordering.append( "email" );
48          }
49          else if ( UserQuery.ORDER_BY_FULLNAME.equals( getOrderBy() ) )
50          {
51              ordering.append( "fullName" );
52          }
53          else
54          {
55              ordering.append( "username" );
56          }
57          ordering.append( " " ).append( isAscending() ? "ascending" : "descending" );
58          return ordering.toString();
59      }
60  
61      /**
62       * Create and return the filter string for use in {@link javax.jdo.Query#setFilter(String)}
63       *
64       * @return the query filter
65       */
66      public String getFilter()
67      {
68          Set<String> terms = new HashSet<String>();
69  
70          if ( getUsername() != null )
71          {
72              terms.add( "this.username.toLowerCase().indexOf(usernameKey.toLowerCase()) > -1" );
73          }
74          if ( getFullName() != null )
75          {
76              terms.add( "this.fullName.toLowerCase().indexOf(fullNameKey.toLowerCase()) > -1" );
77          }
78          if ( getEmail() != null )
79          {
80              terms.add( "this.email.toLowerCase().indexOf(emailKey.toLowerCase()) > -1" );
81          }
82  
83          return StringUtils.join( terms.iterator(), " && " );
84      }
85  
86      /**
87       * Return an array of parameters for user in {@link javax.jdo.Query#executeWithArray(Object[])}
88       *
89       * @return the parameter array
90       */
91      public String[] getSearchKeys()
92      {
93          List<String> keys = new ArrayList<String>();
94  
95          if ( getUsername() != null )
96          {
97              keys.add( getUsername() );
98          }
99          if ( getFullName() != null )
100         {
101             keys.add( getFullName() );
102         }
103         if ( getEmail() != null )
104         {
105             keys.add( getEmail() );
106         }
107 
108         return keys.toArray( new String[keys.size()] );
109     }
110 
111     /**
112      * Returns the parameters for use in {@link javax.jdo.Query#declareParameters(String)}
113      *
114      * @return the parameter list
115      */
116     public String getParameters()
117     {
118 
119         List<String> params = new ArrayList<String>();
120 
121         if ( getUsername() != null )
122         {
123             params.add( "String usernameKey" );
124         }
125         if ( getFullName() != null )
126         {
127             params.add( "String fullNameKey" );
128         }
129         if ( getEmail() != null )
130         {
131             params.add( "String emailKey" );
132         }
133 
134         return StringUtils.join( params.iterator(), ", " );
135     }
136 }