ImportAgent.h

Go to the documentation of this file.
00001 /*
00002  * The information in this file is
00003  * Copyright(c) 2007 Ball Aerospace & Technologies Corporation
00004  * and is subject to the terms and conditions of the
00005  * GNU Lesser General Public License Version 2.1
00006  * The license text is available from   
00007  * http://www.gnu.org/licenses/lgpl.html
00008  */
00009 
00010 #ifndef IMPORTAGENT_H
00011 #define IMPORTAGENT_H
00012 
00013 #include "EnumWrapper.h"
00014 #include "ExecutableAgent.h"
00015 
00016 #include <map>
00017 #include <string>
00018 #include <vector>
00019 
00020 class DataElement;
00021 class ImportDescriptor;
00022 
00023 /**
00024  *  A convenience class that makes working with Importer plug-ins easier.
00025  *
00026  *  This class manages the lifecycle of an Importer plug-in.  This class can be
00027  *  used to import one or more files or just individual data sets within one or
00028  *  more files.
00029  *
00030  *  After executing the importer, the imported data sets can be retrieved by
00031  *  calling getImportedElements().  You should not create an instance of this
00032  *  class using ObjectFactory, but you should use ImporterResource instead.
00033  *
00034  *  @warning    If you do not call an overload of instantiate() before calling
00035  *              any other methods, a std::logic_error will be thrown.  You
00036  *              cannot call instantiate() twice on the same instance or a
00037  *              std::logic_error will be thrown.
00038  *
00039  *  @see        ImporterResource, Importer
00040  */
00041 class ImportAgent : public ExecutableAgent
00042 {
00043 public:
00044    using ExecutableAgent::instantiate;
00045 
00046    /**
00047     *  Specifies the conditions in which the user can edit the import data
00048     *  parameters via the import options dialog.
00049     *
00050     *  @see     setEditType()
00051     */
00052    enum EditTypeEnum
00053    {
00054       NEVER_EDIT,       /**< The user can never edit the import parameters
00055                              before the import is performed. */
00056       AS_NEEDED_EDIT,   /**< The user can only edit the import parameters if
00057                              the importer cannot import the data with the
00058                              current parameters. */
00059       ALWAYS_EDIT       /**< The user is always given the chance to edit the
00060                              import parameters before the import is
00061                              performed. */
00062    };
00063 
00064    /**
00065     *  @EnumWrapper  EditTypeEnum
00066     */
00067    typedef EnumWrapper<EditTypeEnum> EditType;
00068 
00069    /**
00070     *  Creates an invalid object for delayed initialization of an ImportAgent.
00071     *
00072     *  Creates an invalid object where no importer is initially created.  The
00073     *  importer can then be initialized later by calling setPlugIn().
00074     *
00075     *  @param   pProgress
00076     *           The progress object to pass into the importer.  If \c NULL is
00077     *           passed in, a progress object is obtained by calling
00078     *           PlugInManagerServices::getProgress().
00079     *  @param   batch
00080     *           Set this value to \c true to execute the importer in batch mode
00081     *           or to \c false to execute the importer in interactive mode.
00082     *
00083     *  @throw   std::logic_error
00084     *           Thrown if the instantiate() method is called more than once on
00085     *           this instance.
00086     */
00087    virtual void instantiate(Progress* pProgress, bool batch) = 0;
00088 
00089    /**
00090     *  Creates an importer to import data.
00091     *
00092     *  Creates the importer but does not specify the files or data sets to
00093     *  import.  The setFilename(), setFilenames(), or setDatasets() method must
00094     *  therefore be called before calling execute().
00095     *
00096     *  @param   importerName
00097     *           The name of the importer to create and execute.
00098     *  @param   pProgress
00099     *           The progress object to pass into the importer.  If \c NULL is
00100     *           passed in, a progress object is obtained by calling
00101     *           PlugInManagerServices::getProgress().
00102     *  @param   batch
00103     *           Set this value to \c true to execute the importer in batch mode
00104     *           or to \c false to execute the importer in interactive mode.
00105     *
00106     *  @throw   std::logic_error
00107     *           Thrown if the instantiate() method is called more than once on
00108     *           this instance.
00109     */
00110    virtual void instantiate(const std::string& importerName, Progress* pProgress, bool batch) = 0;
00111 
00112    /**
00113     *  Creates an importer to import data from a single file.
00114     *
00115     *  When importing data from a file, all data sets returned by
00116     *  Importer::getImportDescriptors() are imported.
00117     *
00118     *  @param   importerName
00119     *           The name of the importer to create and execute.
00120     *  @param   filename
00121     *           The file from which to load the data.
00122     *  @param   pProgress
00123     *           The progress object to pass into the importer.  If \c NULL is
00124     *           passed in, a progress object is obtained by calling
00125     *           PlugInManagerServices::getProgress().
00126     *  @param   batch
00127     *           Set this value to \c true to execute the importer in batch mode
00128     *           or to \c false to execute the importer in interactive mode.
00129     *
00130     *  @throw   std::logic_error
00131     *           Thrown if the instantiate() method is called more than once on
00132     *           this instance.
00133     */
00134    virtual void instantiate(const std::string& importerName, const std::string& filename, Progress* pProgress,
00135       bool batch) = 0;
00136 
00137    /**
00138     *  Creates an importer to import data from multiple files.
00139     *
00140     *  When importing data from multiple files, all data sets returned by
00141     *  Importer::getImportDescriptors() are imported.
00142     *
00143     *  @param   importerName
00144     *           The name of the importer to create and execute.
00145     *  @param   filenames
00146     *           The files from which to load the data.
00147     *  @param   pProgress
00148     *           The progress object to pass into the importer.  If \c NULL is
00149     *           passed in, a progress object is obtained by calling
00150     *           PlugInManagerServices::getProgress().
00151     *  @param   batch
00152     *           Set this value to \c true to execute the importer in batch mode
00153     *           or to \c false to execute the importer in interactive mode.
00154     *
00155     *  @throw   std::logic_error
00156     *           Thrown if the instantiate() method is called more than once on
00157     *           this instance.
00158     */
00159    virtual void instantiate(const std::string& importerName, const std::vector<std::string>& filenames,
00160       Progress* pProgress, bool batch) = 0;
00161 
00162    /**
00163     *  Creates an importer to import individual data sets from one or more
00164     *  files.
00165     *
00166     *  @param   importerName
00167     *           The name of the importer to create and execute.
00168     *  @param   datasets
00169     *           The files and data sets to import.  The descriptor values should
00170     *           have been obtained by calling Importer::getImportDescriptors().
00171     *           The provided import descriptors will be owned by the agent.
00172     *  @param   pProgress
00173     *           The progress object to pass into the importer.  If \c NULL is
00174     *           passed in, a progress object is obtained by calling
00175     *           PlugInManagerServices::getProgress().
00176     *  @param   batch
00177     *           Set this value to \c true to execute the importer in batch mode
00178     *           or to \c false to execute the importer in interactive mode.
00179     *
00180     *  @throw   std::logic_error
00181     *           Thrown if the instantiate() method is called more than once on
00182     *           this instance.
00183     */
00184    virtual void instantiate(const std::string& importerName,
00185       const std::map<std::string, std::vector<ImportDescriptor*> >& datasets, Progress* pProgress, bool batch) = 0;
00186 
00187    /**
00188     *  Uses an existing importer to import individual data sets from one or more
00189     *  files.
00190     *
00191     *  @param   pPlugIn
00192     *           The importer to execute.  The agent assumes ownership of the
00193     *           importer and will destroy it as necessary upon agent
00194     *           destruction.
00195     *  @param   descriptors
00196     *           The files and data sets to import.  The descriptor values should
00197     *           have been obtained by calling Importer::getImportDescriptors().
00198     *           The provided import descriptors will be owned by the agent.
00199     *  @param   pProgress
00200     *           The progress object to pass into the importer.  If \c NULL is
00201     *           passed in, a progress object is obtained by calling
00202     *           PlugInManagerServices::getProgress().
00203     *  @param   batch
00204     *           Set this value to \c true to execute the importer in batch mode
00205     *           or to \c false to execute the importer in interactive mode.
00206     *
00207     *  @throw   std::logic_error
00208     *           Thrown if the instantiate() method is called more than once on
00209     *           this instance.
00210     */
00211    virtual void instantiate(PlugIn* pPlugIn, const std::map<std::string, std::vector<ImportDescriptor*> >& descriptors,
00212       Progress* pProgress, bool batch) = 0;
00213 
00214    /**
00215     *  Sets the agent to import all data from a single file.
00216     *
00217     *  This method sets one file from which to import data.  %Any existing data
00218     *  descriptors are removed and deleted.  After calling this method, the next
00219     *  call to execute() will call Importer::getImportDescriptors() to determine
00220     *  the data sets to import.
00221     *
00222     *  @param   filename
00223     *           The name of the file to import.
00224     *
00225     *  @throw   std::logic_error
00226     *           Thrown if the instantiate() method has not yet been called on
00227     *           this instance.
00228     *
00229     *  @see     setFilenames(), setDatasets()
00230     */
00231    virtual void setFilename(const std::string& filename) = 0;
00232 
00233    /**
00234     *  Sets the agent to import data from multiple files.
00235     *
00236     *  This method sets multiple files from which to import data.  %Any existing
00237     *  data descriptors are removed and deleted.  After calling this method, the
00238     *  next call to execute() will call Importer::getImportDescriptors() to
00239     *  determine the data sets to import.
00240     *
00241     *  @param   filenames
00242     *           The names of the files to import.
00243     *
00244     *  @throw   std::logic_error
00245     *           Thrown if the instantiate() method has not yet been called on
00246     *           this instance.
00247     *
00248     *  @see     setFilename(), setDatasets()
00249     */
00250    virtual void setFilenames(const std::vector<std::string>& filenames) = 0;
00251 
00252    /**
00253     *  Filters importers displayed in the import dialog based on subtype.
00254     *
00255     *  If the agent executes in interactive mode and no filenames have been set,
00256     *  the import dialog is displayed for the user to choose the importer and
00257     *  one or more files to import.  This method filters the available importers
00258     *  in the dialog based on the given subtype.  Only importers that have a
00259     *  subtype equal to the given subtype will be displayed in the dialog.
00260     *
00261     *  By default, if this method is not called, all importers are displayed in
00262     *  the import dialog.
00263     *
00264     *  This method does nothing if ApplicationServices::isInteractive() returns
00265     *  \c false.
00266     *
00267     *  @param   subtype
00268     *           The subtype for importers that should be displayed in the
00269     *           import dialog.  Passing in an empty string will display all
00270     *           importers in the import dialog.
00271     *
00272     *  @throw   std::logic_error
00273     *           Thrown if the instantiate() method has not yet been called on
00274     *           this instance.
00275     *
00276     *  @see     getImporterSubtype()
00277     */
00278    virtual void setImporterSubtype(const std::string& subtype) = 0;
00279 
00280    /**
00281     *  Returns the subtype of the importers displayed in the import dialog.
00282     *
00283     *  @throw   std::logic_error
00284     *           Thrown if the instantiate() method has not yet been called on
00285     *           this instance.
00286     *
00287     *  @return  Returns the subtype of the importers displayed in the import
00288     *           dialog.  Returns an empty string if all importers are displayed
00289     *           or if ApplicationServices::isInteractive() returns \c false.
00290     *
00291     *  @see     setImporterSubtype()
00292     */
00293    virtual std::string getImporterSubtype() const = 0;
00294 
00295    /**
00296     *  Allows the user to edit the import data parameters before importing the
00297     *  data.
00298     *
00299     *  This method sets when the user should be able to edit the data to be
00300     *  imported.  If editing is enabled, the import options dialog is displayed
00301     *  for the user to make any desired changes.
00302     *
00303     *  The default state of the agent is to never allow the user to edit the
00304     *  data before import.
00305     *
00306     *  This method does nothing if ApplicationServices::isInteractive() returns
00307     *  \c false.
00308     *
00309     *  @param   editType
00310     *           The conditions in which the user can edit the import data
00311     *           parameters.
00312     *
00313     *  @throw   std::logic_error
00314     *           Thrown if the instantiate() method has not yet been called on
00315     *           this instance.
00316     *
00317     *  @see     getEditType()
00318     */
00319    virtual void setEditType(EditType editType) = 0;
00320 
00321    /**
00322     *  Returns the conditions in which the user can edit the import data
00323     *  parameters before importing the data.
00324     *
00325     *  @throw   std::logic_error
00326     *           Thrown if the instantiate() method has not yet been called on
00327     *           this instance.
00328     *
00329     *  @return  Returns the conditions in which the user can edit the import
00330     *           data parameters.  Returns EditType::NEVER_EDIT if
00331     *           ApplicationServices::isInteractive() returns \c false.
00332     *
00333     *  @see     setEditType()
00334     */
00335    virtual EditType getEditType() const = 0;
00336 
00337    /**
00338     *  Adds the imported files to the most recently used (MRU) file list.
00339     *
00340     *  This method allows for imported files to be added to the MRU file list
00341     *  so that users can quickly reimport the data at a later time.  Typically
00342     *  files should only be added to the MRU list if the user initiated the
00343     *  import.  Otherwise, it may confuse the user to see a file in the list
00344     *  that was not directly imported.
00345     *
00346     *  The filename that is added to the MRU file list is the filename that is
00347     *  passed into the instantiate(), setFilename(), setFilenames() or
00348     *  setDatasets() method.  If a filename is not set in the agent and the
00349     *  import dialog is displayed, the files that the user selects in the dialog
00350     *  are added to the MRU file list.  Otherwise, no files are added to the MRU
00351     *  list.
00352     *
00353     *  By default, if this method is not called, imported files are not added
00354     *  to the MRU file list.
00355     *
00356     *  @param   updateList
00357     *           Set this parameter to \c true to add imported files to the MRU
00358     *           file list.
00359     *
00360     *  @throw   std::logic_error
00361     *           Thrown if the instantiate() method has not yet been called on
00362     *           this instance.
00363     */
00364    virtual void updateMruFileList(bool updateList) = 0;
00365 
00366    /**
00367     *  Queries whether an imported file is added to the most recently used
00368     *  (MRU) file list.
00369     *
00370     *  @throw   std::logic_error
00371     *           Thrown if the instantiate() method has not yet been called on
00372     *           this instance.
00373     *
00374     *  @return  Returns \c true if an imported file is added to the MRU file
00375     *           list, otherwise returns \c false.
00376     *
00377     *  @see     updateMruFileList()
00378     */
00379    virtual bool isMruFileListUpdated() const = 0;
00380 
00381    /**
00382     *  Sets the files and data sets to import.
00383     *
00384     *  This method sets the files and their data sets that will be imported.
00385     *  The agent will take ownership over the provided import descriptors.
00386     *  After calling this method, the next call to execute() will import these
00387     *  data sets and will not call Importer::getImportDescriptors() to obtain
00388     *  the data sets to import.
00389     *
00390     *  @param   datasets
00391     *           The files and the import descriptors to import.
00392     *
00393     *  @throw   std::logic_error
00394     *           Thrown if the instantiate() method has not yet been called on
00395     *           this instance.
00396     *
00397     *  @see     setFilename(), setFilenames()
00398     */
00399    virtual void setDatasets(const std::map<std::string, std::vector<ImportDescriptor*> >& datasets) = 0;
00400 
00401    /**
00402     *  Returns the data sets for all files that will be used on import.
00403     *
00404     *  This method returns all data sets in all files specified in instantiate()
00405     *  or setDatasets().  If the import descriptors have not been set,
00406     *  Importer::getImportDescriptors() is called using the filenames set in
00407     *  instantiate(), setFilename(), or setFilenames().
00408     *
00409     *  Regardless of how they are obtained, all import descriptors stored and
00410     *  returned by this method are owned by the agent.
00411     *
00412     *  @throw   std::logic_error
00413     *           Thrown if the instantiate() method has not yet been called on
00414     *           this instance.
00415     *
00416     *  @return  The import descriptors that will be used on import.
00417     *
00418     *  @see     getImportDescriptors(const std::string&)
00419     */
00420    virtual std::vector<ImportDescriptor*> getImportDescriptors() = 0;
00421 
00422    /**
00423     *  Returns the data sets for a given file that will be used on import.
00424     *
00425     *  This method returns all data sets in the given file that have been
00426     *  specified in instantiate() or setDatasets().  If the import descriptors
00427     *  have not been set, Importer::getImportDescriptors() is called using the
00428     *  filenames set in instantiate(), setFilename(), or setFilenames().
00429     *
00430     *  Regardless of how they are obtained, all import descriptors stored and
00431     *  returned by this method are owned by the agent.
00432     *
00433     *  @param   filename
00434     *           The file for which to get its data sets to import.
00435     *
00436     *  @throw   std::logic_error
00437     *           Thrown if the instantiate() method has not yet been called on
00438     *           this instance.
00439     *
00440     *  @return  The import descriptors for the given file that will be used on
00441     *           import.  An empty vector is returned if the file has not
00442     *           previously been by calling instantiate(), setFilename(),
00443     *           setFilenames(), or setDatasets().
00444     *
00445     *  @see     getImportDescriptors(), getDatasets()
00446     */
00447    virtual std::vector<ImportDescriptor*> getImportDescriptors(const std::string& filename) = 0;
00448 
00449    /**
00450     *  Returns the files and data sets that will be used on import.
00451     *
00452     *  This method returns the files and data sets specified in instantiate() or
00453     *  setDatasets().  If the import descriptors have not been set,
00454     *  Importer::getImportDescriptors() is called using the filenames set in
00455     *  instantiate(), setFilename(), or setFilenames().
00456     *
00457     *  Regardless of how they are obtained, all import descriptors stored and
00458     *  returned by this method are owned by the agent.
00459     *
00460     *  @throw   std::logic_error
00461     *           Thrown if the instantiate() method has not yet been called on
00462     *           this instance.
00463     *
00464     *  @return  The filenames and import descriptors that will be used on
00465     *           import.
00466     *
00467     *  @see     getImportDescriptors(), getImportDescriptors(const std::string&)
00468     */
00469    virtual std::map<std::string, std::vector<ImportDescriptor*> > getDatasets() = 0;
00470 
00471    /**
00472     *  Returns the default file extensions from the importer.
00473     *
00474     *  @throw   std::logic_error
00475     *           Thrown if the instantiate() method has not yet been called on
00476     *           this instance.
00477     *
00478     *  @return  The default extensions string returned by calling
00479     *           Importer::getDefaultExtensions().  An empty string is returned
00480     *           if no importer has been set.
00481     */
00482    virtual std::string getDefaultExtensions() const = 0;
00483 
00484    /**
00485     *  Executes the importer to load the data from the files.
00486     *
00487     *  This method creates the input and output arg lists if necessary,
00488     *  populates the input arg list, and then executes the importer.
00489     *
00490     *  If the import descriptors of the files to import have not been set,
00491     *  getImportDescriptors() is called.  A model element is created for each
00492     *  data set using the data descriptor contained in each import descriptor,
00493     *  and the ExecutableAgent::execute() base class method is called to execute
00494     *  the importer.  All successfully imported data elements can be retrieved
00495     *  by calling getImportedElements().
00496     *
00497     *  @par Arg Values:
00498     *  The following input arg values are automatically populated unless the
00499     *  actual value in the arg has already been set and PlugInArg::isActualSet()
00500     *  returns \c true:
00501     *  - All args documented in ExecutableAgent::execute().
00502     *  - Importer::ImportElementArg() is populated with the DataElement that was
00503     *    created for each data set based on each DataDescriptor contained in the
00504     *    ImportDescriptor returned from getImportDescriptors().
00505     *
00506     *  @throw   std::logic_error
00507     *           Thrown if the instantiate() method has not yet been called on
00508     *           this instance.
00509     *
00510     *  @return  Returns \c true if at least one data set is imported
00511     *           successfully.  Returns \c false for any of the following
00512     *           conditions:
00513     *           - The options dialog is displayed and the user cancels it.
00514     *           - None of the data descriptors selected for import validate.
00515     *           - %Any data element could not be created.
00516     *           - The importer fails to import all data sets selected for
00517     *             import.
00518     *
00519     *  @see     setEditType()
00520     */
00521    virtual bool execute() = 0;
00522 
00523    /**
00524     *  Returns all successfully imported data elements.
00525     *
00526     *  The execute() method must be called prior to calling this method for
00527     *  the returned vector to be populated.
00528     *
00529     *  @throw   std::logic_error
00530     *           Thrown if the instantiate() method has not yet been called on
00531     *           this instance.
00532     *
00533     *  @return  A vector containing pointers to the data elements that were
00534     *           created as a result of calling execute().  An empty vector is
00535     *           returned if execute() has not previously been called or if the
00536     *           importer was not able to import any data.
00537     */
00538    virtual std::vector<DataElement*> getImportedElements() const = 0;
00539 
00540 protected:
00541    /**
00542     *  Destroys the agent.
00543     *
00544     *  This object should not be destroyed directly.  It is most commonly
00545     *  destroyed by ImporterResource.
00546     */
00547    virtual ~ImportAgent()
00548    {}
00549 };
00550 
00551 #endif

Software Development Kit - Opticks 4.9.0 Build 16218