Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: class Workflow < ActiveRecord::Base Chris@0: belongs_to :role Chris@0: belongs_to :old_status, :class_name => 'IssueStatus', :foreign_key => 'old_status_id' Chris@0: belongs_to :new_status, :class_name => 'IssueStatus', :foreign_key => 'new_status_id' Chris@0: Chris@0: validates_presence_of :role, :old_status, :new_status Chris@0: Chris@0: # Returns workflow transitions count by tracker and role Chris@0: def self.count_by_tracker_and_role Chris@0: counts = connection.select_all("SELECT role_id, tracker_id, count(id) AS c FROM #{Workflow.table_name} GROUP BY role_id, tracker_id") Chris@0: roles = Role.find(:all, :order => 'builtin, position') Chris@0: trackers = Tracker.find(:all, :order => 'position') Chris@0: Chris@0: result = [] Chris@0: trackers.each do |tracker| Chris@0: t = [] Chris@0: roles.each do |role| Chris@0: row = counts.detect {|c| c['role_id'].to_s == role.id.to_s && c['tracker_id'].to_s == tracker.id.to_s} Chris@0: t << [role, (row.nil? ? 0 : row['c'].to_i)] Chris@0: end Chris@0: result << [tracker, t] Chris@0: end Chris@0: Chris@0: result Chris@0: end Chris@0: Chris@0: # Find potential statuses the user could be allowed to switch issues to Chris@0: def self.available_statuses(project, user=User.current) Chris@0: Workflow.find(:all, Chris@0: :include => :new_status, Chris@0: :conditions => {:role_id => user.roles_for_project(project).collect(&:id)}). Chris@0: collect(&:new_status). Chris@0: compact. Chris@0: uniq. Chris@0: sort Chris@0: end Chris@0: Chris@0: # Copies workflows from source to targets Chris@0: def self.copy(source_tracker, source_role, target_trackers, target_roles) Chris@0: unless source_tracker.is_a?(Tracker) || source_role.is_a?(Role) Chris@0: raise ArgumentError.new("source_tracker or source_role must be specified") Chris@0: end Chris@0: Chris@0: target_trackers = [target_trackers].flatten.compact Chris@0: target_roles = [target_roles].flatten.compact Chris@0: Chris@0: target_trackers = Tracker.all if target_trackers.empty? Chris@0: target_roles = Role.all if target_roles.empty? Chris@0: Chris@0: target_trackers.each do |target_tracker| Chris@0: target_roles.each do |target_role| Chris@0: copy_one(source_tracker || target_tracker, Chris@0: source_role || target_role, Chris@0: target_tracker, Chris@0: target_role) Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: # Copies a single set of workflows from source to target Chris@0: def self.copy_one(source_tracker, source_role, target_tracker, target_role) Chris@0: unless source_tracker.is_a?(Tracker) && !source_tracker.new_record? && Chris@0: source_role.is_a?(Role) && !source_role.new_record? && Chris@0: target_tracker.is_a?(Tracker) && !target_tracker.new_record? && Chris@0: target_role.is_a?(Role) && !target_role.new_record? Chris@0: Chris@0: raise ArgumentError.new("arguments can not be nil or unsaved objects") Chris@0: end Chris@0: Chris@0: if source_tracker == target_tracker && source_role == target_role Chris@0: false Chris@0: else Chris@0: transaction do Chris@0: delete_all :tracker_id => target_tracker.id, :role_id => target_role.id Chris@0: connection.insert "INSERT INTO #{Workflow.table_name} (tracker_id, role_id, old_status_id, new_status_id)" + Chris@0: " SELECT #{target_tracker.id}, #{target_role.id}, old_status_id, new_status_id" + Chris@0: " FROM #{Workflow.table_name}" + Chris@0: " WHERE tracker_id = #{source_tracker.id} AND role_id = #{source_role.id}" Chris@0: end Chris@0: true Chris@0: end Chris@0: end Chris@0: end