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.Date;
008    
009    /**
010     * The query for retrieving a prevalent object from the system identified
011     * by its {@link com.sptci.prevayler.PrevalentObject#objectId} field.
012     *
013     * @see com.sptci.prevayler.PrevalentManager#fetch(Class, Object)
014     * <p>&copy; Copyright 2008 <a href='http://sptci.com/' target='_top'>Sans Pareil
015     *   Technologies, Inc.</a></p>
016     * @author Rakesh Vidyadharan 2008-05-27
017     * @version $Id: Fetch.java 22 2008-11-24 19:04:25Z sptrakesh $
018     */
019    public class Fetch<P extends PrevalentObject, S extends PrevalentSystem>
020        extends AbstractQuery<P,S>
021    {
022      /** The type of object that is to be retrieved. */
023      private final Class cls;
024    
025      /**
026       * The {@link com.sptci.prevayler.PrevalentObject#objectId} to use to
027       * fetch the prevalent object instance.
028       */
029      private final Object objectId;
030    
031      /**
032       * Create a new instance of the query for fetching a prevalent object
033       * the specified object id.
034       *
035       * @param cls The {@link #cls} to use.
036       * @param objectId The {@link #objectId} to use.
037       */
038      public Fetch( final Class cls, final Object objectId )
039      {
040        this.cls = cls;
041        this.objectId = objectId;
042      }
043    
044      /**
045       * Execute the query on the prevalent system and return the prevalent
046       *  object uniquely identified by the specified {@link #objectId}.
047       *
048       * @param system The prevalent system that is to be acted upon.
049       * @param timestamp The timestamp for the query.
050       * @return The required prevalent object or <code>null</code> if no
051       *   object with the specified {@link #objectId} exists in the prevalent
052       *   system.
053       * @throws PrevalentException If errors are encountered while fetching
054       *   the required prevalent object.
055       */
056      @SuppressWarnings( {"unchecked"} )
057      protected P query( final S system, final Date timestamp )
058          throws PrevalentException
059      {
060        return (P) system.fetch( cls, objectId );
061      }
062    }