Class: ObjectCopy

Inherits:
Object
  • Object
show all
Defined in:
lib/translate/copy/object_copy.rb

Instance Method Summary collapse

Constructor Details

#initialize(object, workspace, card = nil) ⇒ ObjectCopy

Returns a new instance of ObjectCopy.



2
3
4
5
6
# File 'lib/translate/copy/object_copy.rb', line 2

def initialize(object, workspace, card = nil)
  @object = object
  @card = card
  @new_workspace = workspace
end

Instance Method Details

#card_values(card) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/translate/copy/object_copy.rb', line 86

def card_values(card)
  values = {}
  project = nil
  values[:rank] = card.rank 
  values[:schedule_state] = card.state 

  unless card.iteration.nil?
    iteration = fetch_object(:iteration, card.iteration.oid)
    if iteration
	values[:iteration] = iteration
	project = iteration.project
    end
  end

  unless card.release.nil?
    release = fetch_object(:release, card.release.oid)
    if release
	values[:release] = release
	project = release.project
    end
  end
  values[:project] = project if project
  values
end

#copy(additional_values = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/translate/copy/object_copy.rb', line 8

def copy(additional_values = {})
  unless TranslationStore[@object.oid]
    logger.info "Copying #{@object.type} -- #{@object.name} -- #{@object.oid}"
    values = shallow_copy @object
    values.merge! additional_values

    values[:workspace] = new_workspace
    values[:project] = find_project_in_new_workspace unless values[:project]

    @new_object = create_object values
    remember @object, @new_object
  else
    @new_object = fetch_object(type_as_symbol, @object.oid)
  end
  copy_children
  @new_object
end

#copy_childrenObject



115
116
# File 'lib/translate/copy/object_copy.rb', line 115

def copy_children
end

#create_object(values) ⇒ Object



36
37
38
# File 'lib/translate/copy/object_copy.rb', line 36

def create_object(values)
  rally_rest.create(@object.type_as_symbol, values)
end

#excluded_attributes(object) ⇒ Object



111
112
113
# File 'lib/translate/copy/object_copy.rb', line 111

def excluded_attributes(object)
  object.typedef.reference_attributes(true).keys
end

#fetch_object(type, oid) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/translate/copy/object_copy.rb', line 132

def fetch_object(type, oid)
  # post-copy, there should always be a new_oid. The new_oid is the oid of the object in the 
  # new workspace
  new_oid = TranslationStore[oid]

  return TranslationStore.instance.objects[new_oid] if TranslationStore.instance.objects.key? new_oid
  logger.debug "Fetch object: Cache hit failed for new_oid = #{new_oid}"

  object = @object.rally_rest.find(type, :workspace => new_workspace) { equal :object_i_d, new_oid }.first
  TranslationStore.instance.objects[new_oid] = object
end

#fetch_project(project_name) ⇒ Object



54
55
56
57
58
59
# File 'lib/translate/copy/object_copy.rb', line 54

def fetch_project(project_name)
  cached_projects = TranslationStore.instance.projects
  return cached_projects[project_name] if cached_projects.key? project_name
  project = rally_rest.find(:project, :workspace => new_workspace) { equal :name, project_name }.first
  cached_projects[project_name] = project
end

#find_project_in_new_workspaceObject



44
45
46
47
48
49
50
51
52
# File 'lib/translate/copy/object_copy.rb', line 44

def find_project_in_new_workspace
  # I'm assuming (after talking to nate) that if the project is nil, there there is more then one project in the 
  # workspace. At least for single project workspaces that were migrated for project scoping, this is true.
  if @object.project.nil?
    return nil
  else
    fetch_project @object.project.name
  end
end

#loggerObject



144
145
146
# File 'lib/translate/copy/object_copy.rb', line 144

def logger
  rally_rest.logger
end

#new_workspaceObject



152
153
154
# File 'lib/translate/copy/object_copy.rb', line 152

def new_workspace
  @new_workspace
end

#rally_restObject



148
149
150
# File 'lib/translate/copy/object_copy.rb', line 148

def rally_rest
  @object.rally_rest
end

#remember(old, new) ⇒ Object



31
32
33
34
# File 'lib/translate/copy/object_copy.rb', line 31

def remember old, new
  logger.debug "rembering #{old.type} : #{old.oid} => #{new.type} => #{new.oid}"
  TranslationStore[old.oid] = new.oid
end

#shallow_copy(object) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/translate/copy/object_copy.rb', line 61

def shallow_copy(object)
  keys_to_exclude = excluded_attributes(object)
  values = object.elements.reject { |key, value| keys_to_exclude.include? key }
  if !user_exists? object.owner
    logger.warn "Removing unknown user '#{object.owner}' from a '#{@object.type}' with name '#{@object.name}'. The user no longer exists."
    values.delete(:owner) 
  end

  # Here we need to fix any deleted custom dropdown values that have been deleted
  values.each do |attribute, value|
    logger.debug "Check values #{attribute} = #{value}"      
    if object.typedef.custom_dropdown_attributes.key? attribute
	attrdef = object.typedef.custom_dropdown_attributes[attribute]
	logger.debug "Check custom dropdown values #{attribute} = #{value}"
	logger.debug "Check custom dropdown allowed_values = #{attrdef.allowed_values.inspect}"
	unless attrdef.allowed_values.include? value
 values[attribute] = nil 
 logger.warn "Deleting Custom Dropdown value '#{attribute}' = '#{value}' from a '#{@object.type}' with name '#{object.name}' because it no longer exists."
	end
    end
  end

  values
end

#tangleObject



26
27
28
29
# File 'lib/translate/copy/object_copy.rb', line 26

def tangle
  logger.info "Tangle #{@object.type} -- #{@object.name}"
  @new_object = fetch_object(:artifact, @object.oid)
end

#type_as_symbolObject



40
41
42
# File 'lib/translate/copy/object_copy.rb', line 40

def type_as_symbol
  @object.type_as_symbol
end

#user_exists?(username) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/translate/copy/object_copy.rb', line 118

def user_exists?(username)
  cached_users = TranslationStore.instance.users

  return true if username.nil?
  return cached_users[username] if cached_users.key? username

  result = @object.rally_rest.find(:user) { equal :login_name, username }
  if result.total_result_count > 0
    cached_users[username] = true
  else
    cached_users[username] = false
  end
end