Class: ProjectsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/projects_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_breadcrumb, #after_sign_in_path_for, #breadcrumbs, #new_session_path, #require_admin_user

Instance Method Details

#approveObject



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'app/controllers/projects_controller.rb', line 227

def approve
  if current_user.eligible_sysadmin?
    add_breadcrumb(project.title, project_path)
    add_breadcrumb("Approval Settings", project_approve_path)
    add_breadcrumb("Edit")
    project
    @departments = project.departments.join(", ")
    @project_metadata = project.
    sponsor_uid = @project_metadata[:data_sponsor]
    @data_sponsor = User.find_by(uid: sponsor_uid)
    @provenance_events = project.provenance_events.where.not(event_type: ProvenanceEvent::STATUS_UPDATE_EVENT_TYPE)

    @title = @project_metadata["title"]
  else redirect_to root_path
  end
end

#confirmationObject



175
# File 'app/controllers/projects_controller.rb', line 175

def confirmation; end

#createObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/projects_controller.rb', line 17

def create
   = params.dup
  [:status] = Project::PENDING_STATUS
  [:created_by] = current_user.uid
  [:created_on] = Time.current.in_time_zone("America/New_York").iso8601
   = ProjectMetadata.new_from_params()

  build_new_project # calling private method to build a new project and set a class variable @project
  project.create!(initial_metadata: , user: current_user)
  if project..project_id != nil
    begin
      mailer = TigerdataMailer.with(project_id: project.id)
      message_delivery = mailer.project_creation
      message_delivery.deliver_later

      redirect_to project_confirmation_path(project)
    rescue StandardError => mailer_error
      raise(TigerData::MailerError, mailer_error)
    end
  else
    render :new
  end
rescue TigerData::MailerError => mailer_error
  logger_message = "Error encountered creating the project #{project.id} as user #{current_user.email}"
  Rails.logger.error(logger_message)
  honeybadger_context = {
    current_user_email: current_user.email,
    project_id: project.id,
    project_metadata: project.
  }
  Honeybadger.notify(mailer_error, context: honeybadger_context)

  error_message = "We are sorry, while the project was successfully created, an error was encountered which prevents the delivery of an e-mail message confirming this. Please know that this error has been logged, and shall be reviewed by members of RDSS."
  flash[:notice] = error_message

  render :new
rescue StandardError => error
  logger_message = if project.persisted?
                    "Error encountered creating the project #{project.id} as user #{current_user.email}"
                   else
                    "Error encountered creating the project #{[:title]} as user #{current_user.email}"
                   end
  Rails.logger.error(logger_message)
  honeybadger_context = {
    current_user_email: current_user.email,
    project_id: project.id,
    project_metadata: project.
  }
  Honeybadger.notify(error, context: honeybadger_context)

  error_message = "We are sorry, the project was not successfully created, and an error was encountered which prevents the delivery of an e-mail message confirming this. Please know that this error has been logged, and shall be reviewed by members of RDSS promptly."

  flash[:notice] = error_message
  render :new
end

#create_scriptObject



244
245
246
247
248
249
250
251
# File 'app/controllers/projects_controller.rb', line 244

def create_script
  project_id = params[:id]
  project = Project.find(project_id)
  service = MediafluxScriptFactory.new(project: project)
  respond_to do |format|
    format.json { render json: {script: service.aterm_script} }
  end
end

#detailsObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/controllers/projects_controller.rb', line 73

def details
  add_breadcrumb(project.title, project_path)
  add_breadcrumb("Details")
  project
  @departments = project.departments.join(", ")
  @project_metadata = project.

  @data_sponsor = User.find_by(uid: @project_metadata.data_sponsor)
  @data_manager = User.find_by(uid: @project_metadata.data_manager)

  read_only_uids = @project_metadata.ro_users
  data_read_only_users = read_only_uids.map { |uid| ReadOnlyUser.find_by(uid:) }.reject(&:blank?)

  read_write_uids = @project_metadata.rw_users
  data_read_write_users = read_write_uids.map { |uid| User.find_by(uid:) }.reject(&:blank?)

  unsorted_data_users = data_read_only_users + data_read_write_users
  sorted_data_users = unsorted_data_users.sort_by { |u| u.family_name || u.uid }
  @data_users = sorted_data_users.uniq { |u| u.uid }
  user_model_names = @data_users.map(&:display_name_safe)
  @data_user_names = user_model_names.join(", ")

  @provenance_events = project.provenance_events.where.not(event_type: ProvenanceEvent::STATUS_UPDATE_EVENT_TYPE)

  @project_eligible_to_edit = true if project.status == Project::APPROVED_STATUS && eligible_editor?

  @project_metadata = @project.
  @project_id = @project_metadata[:project_id] || {}
  @storage_capacity = @project_metadata[:storage_capacity]
  @size = @storage_capacity[:size]
  @unit = @storage_capacity[:unit]

  @requested_size = @size[:requested]
  @requested_unit = @unit[:requested]

  @approved_size = @size[:approved]
  @approved_unit = @unit[:approved]

  @storage_expectations = @project_metadata[:storage_performance_expectations]
  @requested_storage_expectations = @storage_expectations[:requested]
  @approved_storage_expectations = @storage_expectations[:approved]

  @project_purpose = @project_metadata[:project_purpose]


  @project_session = "details"


  respond_to do |format|
    format.html do
      @project = ProjectShowPresenter.new(project)
    end
    format.json do
      render json: project.to_json
    end
    format.xml do
      render xml: project.to_xml
    end
  end
end

#editObject



134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/controllers/projects_controller.rb', line 134

def edit
  add_breadcrumb(project.title, project_path)
  add_breadcrumb("Edit")
  project
  if project..status != Project::APPROVED_STATUS
    flash[:notice] = "Pending projects can not be edited."
    redirect_to project
  elsif project..status == Project::APPROVED_STATUS && !eligible_editor? #check if the current user is a sponsor or a manager
    flash[:notice] = "Only data sponsors and data managers can revise this project."
    redirect_to project
  end
end

#file_list_downloadObject



215
216
217
218
219
220
221
222
223
224
225
# File 'app/controllers/projects_controller.rb', line 215

def file_list_download
  job_id = params[:job_id]
  user_request = FileInventoryRequest.where(job_id:job_id).first
  if user_request.nil?
    # TODO: handle error
    redirect_to "/"
  else
    filename = user_request.output_file
    send_data File.read(filename), type: "text/plain", filename: "filelist.csv", disposition: "attachment"
  end
end

#indexObject



171
172
173
# File 'app/controllers/projects_controller.rb', line 171

def index
  @projects = Project.all
end

#list_contentsObject



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/controllers/projects_controller.rb', line 201

def list_contents
  project_job_service.list_contents_job(user: current_user)

  json_response = {
    message: "File list for \"#{project.title}\" is being generated in the background. A link to the downloadable file list will be available in the \"Recent Activity\" section of your dashboard when it is available. You may safely navigate away from this page or close this tab."
  }
  render json: json_response
rescue => ex
  message = "Error producing document list (project id: #{project&.id}): #{ex.message}"
  Rails.logger.error(message)
  Honeybadger.notify(message)
  render json: { message: "Document list could not be generated." }
end

#newObject



6
7
8
9
10
11
# File 'app/controllers/projects_controller.rb', line 6

def new
  add_breadcrumb("New Project Request")
  return build_new_project if current_user.eligible_sponsor?

  redirect_to root_path
end

#project_job_serviceObject



197
198
199
# File 'app/controllers/projects_controller.rb', line 197

def project_job_service
  @project_job_service ||= ProjectJobService.new(project:)
end

#project_paramsObject



13
14
15
# File 'app/controllers/projects_controller.rb', line 13

def project_params
  params.dup
end

#revision_confirmationObject



176
# File 'app/controllers/projects_controller.rb', line 176

def revision_confirmation; end

#showObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/controllers/projects_controller.rb', line 178

def show
  add_breadcrumb(project.title, project_path)
  add_breadcrumb("Contents")
  project

  @latest_completed_download = current_user.user_requests.where(project_id: @project.id, state: "completed").order(:completion_time).last
  @storage_usage = project.storage_usage(session_id: current_user.mediaflux_session)
  @storage_capacity = project.storage_capacity(session_id: current_user.mediaflux_session)

  @num_files = project.asset_count(session_id: current_user.mediaflux_session)

  @file_list = project.file_list(session_id: current_user.mediaflux_session, size: 100)
  @files = @file_list[:files]
  @files.sort_by!(&:path)
  @project = ProjectShowPresenter.new(project)

  @project_session = "content"
end

#updateObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/controllers/projects_controller.rb', line 147

def update
  @project = Project.find(params[:id])
  #Approve action
  if params.key?("mediaflux_id")
    @project..update_with_params(params, current_user)
    @project.approve!(mediaflux_id: params["mediaflux_id"],current_user:)
  end

  #Edit action
  if params.key?("title")
    @project..status = @project..status || Project::PENDING_STATUS
    @project..update_with_params(params, current_user)
  end

  # @todo ProjectMetadata should be refactored to implement ProjectMetadata.valid?(updated_metadata)
  if project.save and params.key?("mediaflux_id")
    redirect_to project_approval_received_path(@project)
  elsif project.save and params.key?("title")
    redirect_to project_revision_confirmation_path(@project)
  else
    render :edit
  end
end