1 package org.apache.archiva.redback.users.jdo;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.archiva.redback.users.AbstractUserQuery;
23 import org.apache.archiva.redback.users.UserQuery;
24 import org.codehaus.plexus.util.StringUtils;
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 import java.util.List;
32 public class JdoUserQuery
33 extends AbstractUserQuery
37 * Create the ordering string for use in {@link javax.jdo.Query#setOrdering(String)}
39 * @return the created filter
41 public String getOrdering()
43 StringBuffer ordering = new StringBuffer();
45 if ( UserQuery.ORDER_BY_EMAIL.equals( getOrderBy() ) )
47 ordering.append( "email" );
49 else if ( UserQuery.ORDER_BY_FULLNAME.equals( getOrderBy() ) )
51 ordering.append( "fullName" );
55 ordering.append( "username" );
57 ordering.append( " " ).append( isAscending() ? "ascending" : "descending" );
58 return ordering.toString();
62 * Create and return the filter string for use in {@link javax.jdo.Query#setFilter(String)}
64 * @return the query filter
66 public String getFilter()
68 Set<String> terms = new HashSet<String>();
70 if ( getUsername() != null )
72 terms.add( "this.username.toLowerCase().indexOf(usernameKey.toLowerCase()) > -1" );
74 if ( getFullName() != null )
76 terms.add( "this.fullName.toLowerCase().indexOf(fullNameKey.toLowerCase()) > -1" );
78 if ( getEmail() != null )
80 terms.add( "this.email.toLowerCase().indexOf(emailKey.toLowerCase()) > -1" );
83 return StringUtils.join( terms.iterator(), " && " );
87 * Return an array of parameters for user in {@link javax.jdo.Query#executeWithArray(Object[])}
89 * @return the parameter array
91 public String[] getSearchKeys()
93 List<String> keys = new ArrayList<String>();
95 if ( getUsername() != null )
97 keys.add( getUsername() );
99 if ( getFullName() != null )
101 keys.add( getFullName() );
103 if ( getEmail() != null )
105 keys.add( getEmail() );
108 return (String[]) keys.toArray( new String[0] );
112 * Returns the parameters for use in {@link javax.jdo.Query#declareParameters(String)}
114 * @return the parameter list
116 public String getParameters()
119 List<String> params = new ArrayList<String>();
121 if ( getUsername() != null )
123 params.add( "String usernameKey" );
125 if ( getFullName() != null )
127 params.add( "String fullNameKey" );
129 if ( getEmail() != null )
131 params.add( "String emailKey" );
134 return StringUtils.join( params.iterator(), ", " );