001    package com.sptci.prevayler.query;
002    
003    import com.sptci.prevayler.PrevalentException;
004    import com.sptci.prevayler.PrevalentObject;
005    import com.sptci.prevayler.PrevalentSystem;
006    
007    import java.util.Collection;
008    import java.util.Date;
009    import java.util.Map;
010    
011    /**
012     * A query used to fetch prevalent instances by the specified indexed fields.
013     *
014     * <p>&copy; Copyright 2008 <a href='http://sptci.com/' target='_top'>Sans
015     * Pareil Technologies, Inc.</a></p>
016     *
017     * @author Rakesh Vidyadharan 2008-07-19
018     * @version $Id: FetchByIndices.java 22 2008-11-24 19:04:25Z sptrakesh $
019     */
020    public class FetchByIndices<P extends Collection<PrevalentObject>, S extends PrevalentSystem>
021      extends AbstractQuery<P,S>
022    {
023      /**
024       * An enumeration used to indicate whether the query results should
025       * represent a <code>union</code> or <code>intersection</code>.
026       */
027      public enum AggregationType { UNION, INTERSECTION }
028    
029      /** The type of the prevalent object that is to be queried. */
030      private final Class type;
031    
032      /** The map of parameters to use to filter the prevalent instances. */
033      private final Map<String,?> parameters;
034    
035      /** The aggregation type to use for the results of the query. */
036      private final AggregationType resultType;
037    
038      /**
039       * Create a new instance of the query with the specified values.
040       *
041       * @param type The {@link #type} to use for the query.
042       * @param parameters The map of fields in the prevalent class to use
043       *   to fetch matching objects.
044       * @param resultType The aggregation type to use for the results.
045       */
046      public FetchByIndices( final Class type, final Map<String,?> parameters,
047          final AggregationType resultType )
048      {
049        this.type = type;
050        this.parameters = parameters;
051        this.resultType = resultType;
052      }
053    
054      /**
055       * Execute the query on the prevalent system and return the collection of
056       *
057       * @see PrevalentSystem#fetchUnion(Class, java.util.Map)
058       * @see PrevalentSystem#fetchIntersection(Class, java.util.Map)
059       * @param system The prevalent system that is to be acted upon.
060       * @param timestamp The timestamp for the query.
061       * @return The collection of prevalent objects matching the specified
062       *   indexed field.
063       * @throws com.sptci.prevayler.PrevalentException If errors are encountered while querying
064       *   the system.
065       */
066      @Override
067      @SuppressWarnings( {"unchecked"} )
068      protected P query( final S system, final Date timestamp )
069          throws PrevalentException
070      {
071        switch ( resultType )
072        {
073          case UNION:
074            return (P) system.fetchUnion( type, parameters );
075          case INTERSECTION:
076            return (P) system.fetchIntersection( type, parameters );
077        }
078    
079        return null;
080      }
081    }