+ def validate_state_change
+ ok = true
+ if self.state_changed?
+ ok = case self.state_was
+ when nil
+ # state isn't set yet
+ true
+ when Queued
+ # Permit going from queued to any state
+ true
+ when Running
+ # From running, may only transition to a finished state
+ [Complete, Failed, Cancelled].include? self.state
+ when Complete, Failed, Cancelled
+ # Once in a finished state, don't permit any more state changes
+ false
+ else
+ # Any other state transition is also invalid
+ false
+ end
+ if not ok
+ errors.add :state, "invalid change from #{self.state_was} to #{self.state}"
+ end
+ end
+ ok
+ end
+
+ def ensure_no_collection_uuids_in_script_params
+ # recursive_hash_search searches recursively through hashes and
+ # arrays in 'thing' for string fields matching regular expression
+ # 'pattern'. Returns true if pattern is found, false otherwise.
+ def recursive_hash_search thing, pattern
+ if thing.is_a? Hash
+ thing.each do |k, v|
+ return true if recursive_hash_search v, pattern
+ end
+ elsif thing.is_a? Array
+ thing.each do |k|
+ return true if recursive_hash_search k, pattern
+ end
+ elsif thing.is_a? String
+ return true if thing.match pattern
+ end
+ false
+ end
+
+ # Fail validation if any script_parameters field includes a string containing a
+ # collection uuid pattern.
+ if self.script_parameters_changed?
+ if recursive_hash_search(self.script_parameters, Collection.uuid_regex)
+ self.errors.add :script_parameters, "must use portable_data_hash instead of collection uuid"
+ return false
+ end
+ end
+ true
+ end