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