Importing Data into OpenInteract

Many times you can use OpenInteract to frame an existing data source with a web interface, and you can do this without any changes to the datasource at all.

But sometimes you may need to bring data from somewhere else into a datasource for use with OpenInteract. This short guide tells you how to do that.

Data Import File

Data import files are used by OpenInteract to have a datasource-independent way to install data. The format is fairly simple: an arrayref with the first element being a hashref describing what's to come, and then any number of arrayrefs containing actual data to be installed. Here's a simple example that we use in OpenInteract for themes:

$theme = [ 
  { spops_class => 'OpenInteract::Theme',
    field_order => [ qw/ theme_id title description parent credit / ] },
  [ 1, 'main', 'Your basic, parent of all themes. Main.', 0,
    'OpenInteract Developers ' ],
];

So the first element in the $theme arrayref is a hashref with two keys. The first key, spops_class, tells the installer what type of object should be created. The second key, field_order, describes how the succeeding data arrayrefs get mapped to fields in the object.

Other information you can use in the initial hashref:

Learn more about this whole process by reading the docs for OpenInteract::SQLInstall.

The easiest way to build an import file is to open an existing one and edit what you need in the initial hashref item. The data record arrayrefs are easily generated by some automated means -- you could even use Data::Dumper if you were so inclined.

The import file can then be run using oi_manage:

 $ export OIWEBSITE=/path/to/mysite
 $ oi_manage install_sql --package=mypackage

Or just to run the data import alone without structures:

 $ oi_manage install_sql --package=mypackage --sql_action=data

Setting Security on the Data

If you're not using per-object security on your data, then you can skip this section -- you're done!

If you need to set security, there's a script that will do just about everything you need. It comes with the 'base_security' package and is located in:

   $WEBSITE_DIR/pkg/base_security-x.xx/script/create_object_security.pl

For example, here's a run that gives WRITE permission to the group with ID 3 (usually the 'site admin' group):

  $ export OIWEBSITE=/path/to/mysite
  $ perl create_object_security.pl --class=MySite::News \
                                   --level=write
                                   --scope=group \
                                   --scope_id=3

To learn more about its operation:

  $ perl create_object_security.pl --help

If you need to setup security to distribute along with your package, follow one of the examples as distributed with OpenInteract, such as in the 'base_theme' or 'base_group' packages.