View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  package org.caleigo.core;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.caleigo.core.exception.*;
24  
25  
26  /*** This is a simple container class that wraps data that speciffies parametres
27   * for performing data querys. Note that all DataServices may not implement
28   * all the query parameters which could result in an exception being thrown.
29   *
30   * @author  Dennis Zikovic
31   * @version 1.00
32   * 
33   *//* 
34   *
35   * WHEN        WHO               WHY & WHAT
36   * ------------------------------------------------------------------------------
37   * 2001-07-21  Dennis Zikovic    Creation
38   * 2001-12-17  Dennis Zikovic    Redesigned to use the EntityCollator class.
39   * 2004-04-01  Niklas Norberg    Added External qualifiers
40   */
41  public class DataQuery implements java.io.Serializable
42  {
43      // Constants ---------------------------------------------------------------
44      
45      // Data members ------------------------------------------------------------
46      private IEntityDescriptor mEntityDescriptor;
47      private Qualifier mQualifier;
48      private EntityCollator mEntityCollator;
49      private List externalQualifiers;
50  //    private int mLimit;
51          
52      // Constructos -------------------------------------------------------------
53      
54      /*** Default constructor.
55       */
56      public DataQuery()
57      {
58      }
59      
60      /*** Creates a DataQuery instance. The created query will if executed with
61       * no further modification return all entities of the specified kind.
62       */
63      public DataQuery(IEntityDescriptor entityDescriptor)
64      {
65          mEntityDescriptor = entityDescriptor;
66      }
67      
68      
69      /*** Creates a DataQuery instance. The qualifier must be able to select 
70       * entities of the type defined by the provided entity descriptor without
71       * the nead of any complementary data, null is also valid.
72       * @see org.caleigo.core.Qualifier#canDirectlyQualify(IEntityDescriptor entityDescriptor)
73       * @throws InvalidQualifierException 
74       */
75      public DataQuery(IEntityDescriptor entityDescriptor, Qualifier qualifier)
76      {
77          //validate the qualifier
78          if(qualifier != null && !qualifier.canDirectlyQualify(entityDescriptor))
79              throw new InvalidQualifierException("The qualifier must be able to " +
80                      "select entities of the type defined by the provided entity" +
81                      " descriptor without the nead of any complementary data");
82          
83          mEntityDescriptor = entityDescriptor;
84          mQualifier = qualifier;
85      }
86      
87      /*** Add an external qualifier to the data query, used to construct relation
88       * queries.
89       * The qualifier must be able to select  entities of the type defined by
90       * the first entity descriptor in the entity relation path without the nead
91       * of any complementary data.
92       * @param qualifier that qualifies the first entity descriptor
93       * in the relationPath.
94       * @param relationPath the path to the entity descriptor that the qualifier
95       * qualifies.
96       * @see org.caleigo.core.Qualifier#canDirectlyQualify(IEntityDescriptor entityDescriptor)
97       */
98      public void addExternalQualifier(Qualifier qualifier, IEntityRelationPath relationPath)
99      {
100         if(qualifier.canDirectlyQualify(relationPath.getFirstEntityDescriptor()))
101         {
102             if (externalQualifiers == null)
103                 externalQualifiers = new ArrayList();
104             externalQualifiers.add(new ExternalQualifier(qualifier, relationPath));
105         }
106         else
107             throw new InvalidQualifierException("The qualifier in an " +
108                     "instance of an ExternalQualifier must be able to select " +
109                     "entities of the type defined by the first entity " +
110                     "descriptor in the entity relation path without the nead" +
111                     " of any complementary data.");
112     }
113     
114     
115     
116     // Action methods ----------------------------------------------------------
117     
118     /*** This conviniance method will run the query using the default datasource
119      * defined for the DataSourceDescriptor that the requested EntityDescriptor
120      * belongs to.
121      */
122     public ISelection execute()
123     {
124         if(mEntityDescriptor==null)
125             throw new InvalidDescriptorException("No entity descriptor is set for the query!");
126         if(mEntityDescriptor.getDataSourceDescriptor().getDefaultDataSource()==null)
127             throw new DataServiceNotFoundException("No default data source has been set for: "+mEntityDescriptor.getCodeName());
128         return mEntityDescriptor.getDataSourceDescriptor().getDefaultDataSource().getDataService().loadSelection(this);
129     }
130     
131     // Access methods ----------------------------------------------------------
132     
133     /*** Returns the IEntityDescriptorobject that the DataQuery targets.
134      */
135     public IEntityDescriptor getEntityDescriptor()
136     {
137         return mEntityDescriptor;
138     }
139     
140     /*** Sets the IEntityDescriptorobject that the DataQuery targets.
141      */
142     public void setEntityDescriptor(IEntityDescriptor descriptor)
143     {
144         mEntityDescriptor = descriptor;
145         if(mEntityCollator!=null)
146             mEntityCollator = new EntityCollator(mEntityDescriptor, mEntityCollator);
147     }
148     
149     /*** Returns the Qualifier object that the DataQuery uses to qualify 
150      * individual entities of the targeted entity descriptor type.
151      */
152     public Qualifier getQualifier()
153     {
154         return mQualifier;
155     }
156     
157     /*** @return an array with all external qualifiers in this data query, if no
158      * external qualifiers have been added an empty array is returned.
159      */
160     public ExternalQualifier[] getExternalQualifierArray()
161     {
162         if (externalQualifiers == null)
163             return new ExternalQualifier[]{};
164         else
165             return (ExternalQualifier[])externalQualifiers.toArray(new ExternalQualifier[]{});
166     }
167 
168         
169     /*** Sets the Qualifier object that the DataQuery uses to qualify 
170      * individual entities of the tageted entity descriptor type.
171      */
172     public void setQualifier(Qualifier qualifier)
173     {
174         mQualifier = qualifier;
175     }
176         
177     /*** Returns the EntityCollator object that the DataQuery uses to order 
178      * the qualified entities.
179      */
180     public EntityCollator getEntityCollator()
181     {
182         return mEntityCollator;
183     }
184     
185     /*** Sets the EntityCollator object that the DataQuery uses to order 
186      * the qualified entities.
187      */
188     public void setEntityCollator(EntityCollator collator)
189     {
190         mEntityCollator = collator;
191     }
192     
193 //    /*** Returns the limit of how many entities that should be accepted 
194 //     * by the query. Zero means that no limit is defined.
195 //     */ 
196 //    public int getLimit()
197 //    {
198 //        return mLimit;
199 //    }
200 //    
201 //    /*** Sets the limit of how many entities that should be accepted 
202 //     * by the query. Zero means that no limit is defined.
203 //     */ 
204 //    public void setLimit(int limit)
205 //    {
206 //        mLimit = limit;
207 //    }
208     
209     // Help methods ------------------------------------------------------------
210     
211     /*** Help method that allows the entity order to be set using 
212      * a single IFieldDescriptor.
213      */
214     public void setCollationField(IFieldDescriptor fieldDescriptor)
215     {
216         mEntityCollator = new EntityCollator(mEntityDescriptor);
217         mEntityCollator.addCollationField(fieldDescriptor);
218     }
219     
220     /*** Help method that allows the entity order to be set using 
221      * a single IFieldDescriptor.
222      */
223     public void setCollationField(IFieldDescriptor fieldDescriptor, boolean ascending)
224     {
225         mEntityCollator = new EntityCollator(mEntityDescriptor);
226         mEntityCollator.addCollationField(fieldDescriptor, ascending);
227     }
228                 
229     /*** Help method that allows the entity order to be defined using multiple
230      * IFieldDescriptors by calling this method repeatedly.
231      */
232     public void addCollationField(IFieldDescriptor fieldDescriptor)
233     {
234         if(mEntityCollator==null)
235             mEntityCollator = new EntityCollator(mEntityDescriptor);
236         mEntityCollator.addCollationField(fieldDescriptor);
237     }
238     
239     /*** Help method that allows the entity order to be defined using multiple
240      * IFieldDescriptors by calling this method repeatedly.
241      */
242     public void addCollationField(IFieldDescriptor fieldDescriptor, boolean ascending)
243     {
244         if(mEntityCollator==null)
245             mEntityCollator = new EntityCollator(mEntityDescriptor);
246         mEntityCollator.addCollationField(fieldDescriptor, ascending);
247     }
248                 
249     /*** Help method the resets the query's order data causing the entities to
250      * be returned in their default order.
251      */
252     public void clearCollationFields()
253     {
254         mEntityCollator = null;
255     }
256     
257     
258     // Nested classes ----------------------------------------------------------
259     
260     /***
261      * 
262      * @author Niklas Norberg
263      *
264      * This inner class is used to relation qualifications(selections) on the
265      * data query object's entity descriptor
266      */
267     public class ExternalQualifier{
268     
269         private Qualifier mQualifier;
270         private IEntityRelationPath mEntityRelationPath;
271         
272         private ExternalQualifier(Qualifier qualifier, IEntityRelationPath entityRelationPath)
273         {
274                 mQualifier = qualifier;
275                 mEntityRelationPath = entityRelationPath;
276         }
277         
278         public Qualifier getQualifer()
279         {
280             return mQualifier;
281         }
282         
283         public IEntityRelationPath getEntityRelationPath()
284         {
285             return mEntityRelationPath;
286         }
287     }
288     
289 }