Bric::Dist::Resource - Interface to distribution files and directories. $LastChangedRevision$ $LastChangedDate: 2006-08-09 06:57:25 +0200 (Wed, 09 Aug 2006) $ use Bric::Dist::Resource; # Constructors. # Create a new resource. my $res = Bric::Dist::Resource->new; # Look up an existing resource. $res = Bric::Dist::Resource->lookup({ id => 1 }); # Get a list of resources. my @res = Bric::Dist::Resource->list({ path => '/tech/feature%' }); # Class methods. # Get a list of resource IDs. my @res_ids = Bric::Dist::Resource->list_ids({ path => '/tech/feature%' }); # Get an introspection hashref. my $int = Bric::Dist::Resource->my_meths; # Instance methods. my $id = $res->get_id; my $path = $res->get_path; $res = $res->set_path($path); my $uri = $res->get_uri; $res = $res->set_uri($uri); my $tmp_path = $res->get_tmp_path; $res = $res->set_tmp_path($tmp_path); my $size = $res->get_size; $res = $res->set_size($size); my $mod_time = $res->get_mod_time; $res = $res->set_mod_time($mod_time) my $media_type = $res->get_media_type; $res = $res->set_media_type($media_type) print "It's a directory!\n" if $res->is_dir; # Reload size and mod_time, from the resource on the file system. $res = $res->stat_me; # Story relationships. my @story_ids = $res->get_story_ids; $res = $res->add_story_ids(@story_ids); $res = $res->del_story_ids(@story_ids); # Media relationships. my @media_ids = $res->get_media_ids; $res = $res->add_media_ids(@media_ids); $res = $res->del_media_ids(@media_ids); # File relationships. if ($res->is_dir) { my @file_ids = $res->get_file_ids; $res = $res->add_file_ids(@file_ids); $res = $res->del_file_ids(@file_ids); } # Save it! $res = $res->save; This class manages distribution resources. A resource is a file or directory. Directory resources can be associated with file resources (in order to keep a list of the contents of a directory), but not vice versa. Resources may also track associations with assets, such that they can have story and/or media associations. Other properties of resources are filesystem path, file size, MEDIA type, and last modified date.

Constructors
Instantiates a Bric::Dist::Resource object. An anonymous hash of initial values may be passed. The supported initial value keys are: path uri media_type size mod_time Call $res->save() to save the new object. Throws: NONE. Side Effects: NONE. Notes: NONE. Looks up and instantiates a new Bric::Dist::Resource object based on the Bric::Dist::Resource object ID or based on the path and uri. If a Bric::Dist::Resource object is not found in the database, "lookup()" returns "undef". Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to execute SQL statement. Unable to select row. Too many Bric::Dist::Resource objects found. Side Effects: If $id or $path is found, populates the new Bric::Dist::Resource object with data from the database before returning it. Notes: NONE. Returns a list or anonymous array of Bric::Dist::Resource objects based on the search parameters passed via an anonymous hash. The supported lookup keys are: Resource ID. May use "ANY" for a list of possible values. The path to the resource on the file system. Usually used in combination with uri. May use "ANY" for a list of possible values. The URI for a resource. Usually used in combination with path. May use "ANY" for a list of possible values. Search for resources with URIs not like the specified URI. May use "ANY" for a list of possible values. The resources' MEDIA type. May use "ANY" for a list of possible values. The resources' last modified time. If passed as an anonymous array of two values, those values will be used to retreive resources whose mod_times are between the two times. Otherwise, may use "ANY" for a list of possible values. The size, in bytes, of the file. If passed as an anonymous array of two values, those values will be used to retreive resources whose sizes are between the two sizes. Otherwise, may use "ANY" for a list of possible values. If true, return only those resources that are directories. Resources associated with a given story ID. May use "ANY" for a list of possible values. Resources associated with a given media ID. May use "ANY" for a list of possible values. Resources associated with a given media ID. May use "ANY" for a list of possible values. File resources that are associated with a directory Resource's ID. May use "ANY" for a list of possible values. Resources associated with a given job ID. May use "ANY" for a list of possible values. Resources not associated with a given job ID. Best used in combination with "story_id" or "media_id". May use "ANY" for a list of possible values. Resources associated with an output channel ID. Only returns the correct results if the "job" table has not been cleared, because it joins to "job__resource", "job__server_type", and "server_type__output_channel". Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to select column into arrayref. Unable to execute SQL statement. Unable to bind to columns to statement handle. Unable to fetch row from statement handle. Side Effects: Populates each Bric::Dist::Resource object with data from the database before returning them all. Notes: NONE. Returns a list or anonymous array of Bric::Dist::Resource objects based on the search parameters passed via an anonymous hash. The supported lookup keys are the same as for list(). Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to select column into arrayref. Unable to execute SQL statement. Unable to bind to columns to statement handle. Unable to fetch row from statement handle. Side Effects: Populates each Bric::Dist::Resource object with data from the database before returning them all. Notes: NONE.

Destructors
Dummy method to prevent wasting time trying to AUTOLOAD DESTROY. Throws: NONE. Side Effects: NONE. Notes: NONE.

Public Class Methods
Returns a list or anonymous array of Bric::Dist::Resource object IDs based on the search criteria passed via an anonymous hash. The supported lookup keys are the same as those for list(). Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to select column into arrayref. Unable to execute SQL statement. Unable to bind to columns to statement handle. Unable to fetch row from statement handle. Side Effects: NONE. Notes: NONE. Returns an anonymous hash of introspection data for this object. If called with a true argument, it will return an ordered list or anonymous array of introspection data. If a second true argument is passed instead of a first, then a list or anonymous array of introspection data will be returned for properties that uniquely identify an object (excluding "id", which is assumed). Each hash key is the name of a property or attribute of the object. The value for a hash key is another anonymous hash containing the following keys: The name of the property or attribute. Is the same as the hash key when an anonymous hash is returned. The display name of the property or attribute. A reference to the method that will retrieve the value of the property or attribute. An anonymous array of arguments to pass to a call to get_meth in order to retrieve the value of the property or attribute. A reference to the method that will set the value of the property or attribute. An anonymous array of arguments to pass to a call to set_meth in order to set the value of the property or attribute. The type of value the property or attribute contains. There are only three types: If the value is a 'short' value, this hash key contains the length of the field. The property is searchable via the list() and list_ids() methods. The property or attribute is required. An anonymous hash of properties used to display the property or attribute. Possible keys include: The display field type. Possible values are The Length, in letters, to display a text or password field. The maximum length of the property or value - usually defined by the SQL DDL. The number of rows to format in a textarea field. The number of columns to format in a textarea field. An anonymous hash of key/value pairs representing the values and display names to use in a select list. Throws: NONE. Side Effects: NONE. Notes: NONE.

Public Instance Methods
Returns the ID of the Bric::Dist::Resource object. Throws: Bad AUTOLOAD method format. Cannot AUTOLOAD private methods. Access denied: READ access for field 'id' required. No AUTOLOAD method. Side Effects: NONE. Notes: If the Bric::Dist::Resource object has been instantiated via the new() constructor and has not yet been "save"d, the object will not yet have an ID, so this method call will return undef. Returns the MEDIA type of the resource. Returns undef in the resource is a directory. Throws: Bric::_get() - Problems retrieving fields. Side Effects: NONE. Notes: NONE. Sets the MEDIA type of the resource. Call Bric::Util::MediaType->list() to get a list of available MEDIA types. Setting the MEDIA type to "none" will cause is_dir() to return true, while any other MEDIA type setting will cause is_dir() to return false. Throws: Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: NONE. Returns the file system path to the resource. Throws: Bad AUTOLOAD method format. Cannot AUTOLOAD private methods. Access denied: READ access for field 'path' required. No AUTOLOAD method. Side Effects: NONE. Notes: For cross-platform portability, we may later want to implement the get_path() and set_path() methods using File::Spec. Sets the file system path to the resource. This property cannot be changed in an existing Bric::Dist::Resource object. The resource must exist on the file system to set the path. In addition, once the resource has been found on the file system the media_type, size, and mod_time properties will also be filled out, as best as possible. These settings can be overridden by calling their own set_ methods. Throws: Cannot change path in existing Bric::Dist::Resource object. Path does not exist. Unable to format date. Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: For cross-platform portability, we may later want to implement the get_path() and set_path() methods using File::Spec. Returns the resource's URI. Throws: Bad AUTOLOAD method format. Cannot AUTOLOAD private methods. Access denied: READ access for field 'uri' required. No AUTOLOAD method. Side Effects: NONE. Notes: NONE. Sets the resource's URI. Throws: Bad AUTOLOAD method format. Cannot AUTOLOAD private methods. Access denied: WRITE access for field 'uri' required. No AUTOLOAD method. Side Effects: NONE. Notes: NONE. Returns the file system path to the a temporary copy of the resource. Used by Bric::Util::Job and does not persist to the database. Throws: Bad AUTOLOAD method format. Cannot AUTOLOAD private methods. Access denied: READ access for field 'tmp_path' required. No AUTOLOAD method. Side Effects: NONE. Notes: For cross-platform portability, we may later want to implement the get_path() and set_path() methods using File::Spec. Sets the file system path to a temporary copy of the resource. Used by Bric::Util::Job and does not persist to the database. Throws: Bad AUTOLOAD method format. Cannot AUTOLOAD private methods. Access denied: WRITE access for field 'tmp_path' required. No AUTOLOAD method. Side Effects: NONE. Notes: For cross-platform portability, we may later want to implement the get_path() and set_path() methods using File::Spec. Finds the resource on the file system and reloads the size, mod_time, and media_type properties. If the MEDIA type cannot be determined from the file the existing media_type property setting is retained. Throws: Bric::_get() - Problems retrieving fields. Bric::Dist::Resource::stat_me() requires the path property to be set. Path does not exist. Unable to format date. Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: For cross-platform portability, we may later want to implement the get_path() and set_path() methods using File::Spec. Returns the size of the resource in bytes. If the resource is a directory, this method will return 0. Throws: Bad AUTOLOAD method format. Cannot AUTOLOAD private methods. Access denied: READ access for field 'size' required. No AUTOLOAD method. Side Effects: NONE. Notes: NONE. Sets the size of the resource in bytes. If the resource is a directory, set the size to 0. Chances are that if you called set_path() first, this property will already be enumerated. Throws: Bad AUTOLOAD method format. Cannot AUTOLOAD private methods. Access denied: WRITE access for field 'size' required. No AUTOLOAD method. Side Effects: NONE. Notes: NONE. Returns the last modified time of the resource. Pass in a strftime format string to get the last modified time returned in that format. Throws: Unable to unpack date. Unable to format date. Bric::_get() - Problems retrieving fields. Side Effects: NONE. Notes: NONE. Sets the mod_time of the resource in bytes. Chances are that if you called set_path() first, this property will already be enumerated. Throws: Unable to unpack date. Unable to format date. Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: NONE. Returns true if the resources is a directory. Throws: Bric::_get() - Problems retrieving fields. Side Effects: NONE. Notes: NONE. Returns a list or anonymous array of story IDs representing the stories with which this resources is associated. Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to select column into arrayref. Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: NONE. Associates this resource with the story IDs passed in. Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to select column into arrayref. Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: NONE. Dissociates this resource with the story IDs passed in. If no story IDs are passed, then all story IDs will be dissociated from this resource. Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to select column into arrayref. Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: NONE. Returns a list or anonymous array of media IDs representing the stories with which this resources is associated. Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to select column into arrayref. Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: NONE. Associates this resource with the media IDs passed in. Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to select column into arrayref. Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: NONE. Dissociates this resource with the media IDs passed in. If no media IDs are passed, then all media IDs will be dissociated from this resource. Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to select column into arrayref. Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: NONE. Returns a list or anonymous array of Bric::Dist::Resource object IDs representing the files that are the contents of this resource, assuming that this resource is a directory. If this resource is a file, this method will return an empty list. Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to select column into arrayref. Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: NONE. Associates this resource with the File IDs passed in. Note that is_dir() must return true in order to associate files with this resource. Throws: Cannot associate file resources with another file resource. Unable to connect to database. Unable to prepare SQL statement. Unable to select column into arrayref. Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: NONE. Dissociates this resource with the file IDs passed in. If no file IDs are passed, then all file IDs will be dissociated from this resource. If this is_dir() returns false, then this resource cannot be assocated with files and will return undef. Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to select column into arrayref. Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: NONE. Saves any changes to the Bric::Dist::Resource object. Returns $self on success and undef on failure. Throws: Bric::_get() - Problems retrieving fields. Unable to connect to database. Unable to prepare SQL statement. Unable to execute SQL statement. Unable to select row. Incorrect number of args to _set. Bric::_set() - Problems setting fields. Side Effects: NONE. Notes: NONE. Returns the contents of the file represented by the Bric::Dist::Resource object. Throws: Cannot open file. Side Effects: NONE. Notes: NONE.

Private Class Methods
NONE.

Private Instance Methods
NONE.

Private Functions
Function used by lookup() and list() to return a list of Bric::Dist::Resource objects or, if called with an optional third argument, returns a listof Bric::Dist::Resource object IDs (used by list_ids()). Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to select column into arrayref. Unable to execute SQL statement. Unable to bind to columns to statement handle. Unable to fetch row from statement handle. Side Effects: NONE. Notes: Returns the Asset IDs associated with this resource. If $type is 'story', it returns Story IDs. If $type is 'media', it returns Media IDs. Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to select column into arrayref. Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: Associates asset IDs with this resource. If $type is 'story', it associates Story IDs. If $type is 'media', it associates Media IDs. Returns true. Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to select column into arrayref. Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: Dissociates asset IDs from this resource. If $type is 'story', it dissociates Story IDs. If $type is 'media', it dissociates Media IDs. Returns true. Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to select column into arrayref. Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: Returns the Asset IDs associated with this resource. If $type is 'story', it returns Story IDs. If $type is 'media', it returns Media IDs. The anonymous hash returned has the following keys, each of which contains another anonymous hash where the relevant IDs are the hash keys. cur - All new IDs and IDs currently in the database. new - IDs to be added to the database. del - IDs to be deleted from the database. Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to select column into arrayref. Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: Finds $path on the file system and loads the properties size, mod_time, and media_type (if $path is a directory). Throws: Path does not exist. Unable to format date. Incorrect number of args to Bric::_set(). Bric::set() - Problems setting fields. Side Effects: NONE. Notes: Saves the associated Story, Media, or File IDs by deleting those that need deleting and inserting those that need inserting. Throws: Unable to connect to database. Unable to prepare SQL statement. Unable to execute SQL statement. Side Effects: NONE. Notes: NONE. David Wheeler <david@justatheory.com> Bric, Bric::Util::Job