View Javadoc

1   /*
2    * Copyright 2004-2005 The Apache Software Foundation or its licensors,
3    *                     as applicable.
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.portals.graffito.jcr.query.impl;
19  
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  
23  import org.apache.portals.graffito.jcr.mapper.Mapper;
24  import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
25  import org.apache.portals.graffito.jcr.query.Filter;
26  import org.apache.portals.graffito.jcr.query.Query;
27  
28  /***
29   * Default Query implementation 
30   * 
31   * @author <a href="mailto:christophe.lombart@sword-technologies.com">Christophe Lombart</a>
32   *
33   */
34  public class QueryImpl implements Query
35  {
36  		
37  	private Filter filter;
38  	
39      ClassDescriptor classDescriptor;
40      
41      private ArrayList orderByExpressions = new ArrayList();
42  
43  	/***
44  	 * Constructor 
45  	 * 
46  	 * @param filter
47  	 * @param mapper
48  	 */
49  	public QueryImpl(Filter filter, Mapper mapper) 
50  	{				
51  		this.filter = filter;
52  		classDescriptor = mapper.getClassDescriptorByClass(filter.getFilterClass());
53  	}
54  
55  	/***
56  	 * @see org.apache.portals.graffito.jcr.query.Query#setFilter(org.apache.portals.graffito.jcr.query.Filter)
57  	 */
58  	public void setFilter(Filter filter)
59  	{
60  		this.filter = filter;
61  	}
62  
63  	/***
64  	 * @see org.apache.portals.graffito.jcr.query.Query#getFilter()
65  	 */
66  	public Filter getFilter()
67  	{
68          return this.filter;
69  	}
70  
71  	public void addOrderByDescending(String fieldNameAttribute)
72  	{
73  		orderByExpressions.add("@" + this.getJcrFieldName(fieldNameAttribute) + " descending");
74  	}
75  
76  	/***
77  	 * 
78  	 * @see org.apache.portals.graffito.jcr.query.Query#addOrderByAscending(java.lang.String)
79  	 */
80  	public void addOrderByAscending(String fieldNameAttribute)
81  	{
82  		orderByExpressions.add("@" + this.getJcrFieldName(fieldNameAttribute) + " ascending");
83  	}
84  	
85  	public String getOrderByExpression()
86  	{
87  		
88  		if (orderByExpressions.size() == 0)
89  		{
90  		   return "";	
91  		}
92  		
93  		String orderByExpression = "order by ";
94  		Iterator iterator   = orderByExpressions.iterator();
95  		int count=1;
96  		while (iterator.hasNext())
97  		{
98  			   if (count > 1)
99  			   {
100 				   orderByExpression += " , ";
101 			   }
102 			   orderByExpression+= (String) iterator.next();
103 			   count++;
104 		}
105 		
106 		return orderByExpression;
107 	}
108 	
109 	
110 	private String getJcrFieldName(String fieldAttribute)
111 	{
112 		
113 		return classDescriptor.getJcrName(fieldAttribute);
114 	    	
115 	}
116 
117 }