Commit c925451c authored by hh1966's avatar hh1966
Browse files

Add ExportCollectionToRadarJob

parent 1d3e83f8
Pipeline #164446 failed with stages
......@@ -41,6 +41,7 @@
/config/spectra.yml
/config/editors.yml
/config/inference.yml
/config/radar.yml
/node_modules
......
......@@ -425,7 +425,6 @@ module Chemotion
requires :format, type: Symbol, values: [:json, :zip, :udm]
requires :nested, type: Boolean
end
post do
collection_ids = params[:collections].uniq
nested = params[:nested] == true
......@@ -448,6 +447,24 @@ module Chemotion
ExportCollectionsJob.perform_later(collection_ids, params[:format].to_s, nested, current_user.id)
status 204
end
desc "Create export radar job"
params do
requires :collection_id, type: Integer, desc: "Collection id"
end
before do
error!('401 Unauthorized', 401) unless CollectionPolicy.new(current_user, Collection.find(params[:collection_id])).create_archive?
end
post :radar do
#ExportCollectionToRadarJob.perform_later(params[:collection_id], current_user.id)
export = Export::ExportRadar.new(params[:collection_id])
export.fetch_access_token
export.create_dataset
export.upload_assets
status 204
end
end
namespace :imports do
......
......@@ -166,8 +166,8 @@ class CollectionActions {
});};
}
importCollectionsFromFile(params) {
return (dispatch) => { CollectionsFetcher.createImportJob(params)
exportCollectionToRadar(params) {
return (dispatch) => { CollectionsFetcher.createExportRadarJob(params)
.then((result) => {
dispatch(result);
}).catch((errorMessage) => {
......@@ -175,8 +175,8 @@ class CollectionActions {
});};
}
archiveCollectionToRadar(params) {
return (dispatch) => { CollectionsFetcher.createArchiveJob(params)
importCollectionsFromFile(params) {
return (dispatch) => { CollectionsFetcher.createImportJob(params)
.then((result) => {
dispatch(result);
}).catch((errorMessage) => {
......
......@@ -11,7 +11,7 @@ import ModalImportChemScanner from './ModalImportChemScanner';
import ModalExport from './ModalExport';
import ModalReactionExport from './ModalReactionExport';
import ModalExportCollection from './ModalExportCollection';
import ModalArchiveCollection from './ModalArchiveCollection';
import ModalExportRadarCollection from './ModalExportRadarCollection';
import ModalImportCollection from './ModalImportCollection';
import { elementShowOrNew } from '../routesUtils'
......@@ -49,7 +49,7 @@ const ExportImportButton = ({ isDisabled, updateModalProps, customClass }) => (
title='Edit metadata'>
Edit collection metadata
</MenuItem>
<MenuItem onSelect={() => archiveCollectionToRadarFunction(updateModalProps)} disabled={isDisabled}
<MenuItem onSelect={() => exportCollectionToRadarFunction(updateModalProps)} disabled={isDisabled}
title='Export to RADAR'>
Archive current collection to RADAR
</MenuItem>
......@@ -173,10 +173,10 @@ const editMetadataFunction = () => {
});
}
const archiveCollectionToRadarFunction = (updateModalProps) => {
const exportCollectionToRadarFunction = (updateModalProps) => {
const title = "Archive current Collection to RADAR";
const component = ModalArchiveCollection;
const action = CollectionActions.archiveCollectionToRadar;
const component = ModalExportRadarCollection;
const action = CollectionActions.exportCollectionToRadar;
const modalProps = {
show: true,
......
import React from 'react';
import {Button, ButtonToolbar} from 'react-bootstrap';
import UIStore from './../stores/UIStore';
import CollectionStore from './../stores/CollectionStore';
export default class ModalExportRadarCollection extends React.Component {
constructor(props) {
super(props);
this.state = {
processing: false
}
this.handleClick = this.handleClick.bind(this)
}
handleClick() {
const { onHide, action } = this.props;
const { currentCollection } = UIStore.getState();
this.setState({ processing: true });
const params = {
collection_id: currentCollection.id
};
action(params);
setTimeout(() => {
this.setState({ processing: false });
onHide();
}, 1000);
}
renderButtonBar() {
const { onHide } = this.props;
const { processing } = this.state;
const bStyle = processing === true ? 'danger' : 'warning';
const bClass = processing === true ? 'fa fa-spinner fa-pulse fa-fw' : 'fa fa-file-text-o';
const bTitle = processing === true ? 'Archiving' : 'Archive to RADAR';
return (
<ButtonToolbar>
<div className="pull-right">
<ButtonToolbar>
<Button bsStyle="primary" onClick={onHide}>Cancel</Button>
<Button
bsStyle={bStyle}
id="md-export-dropdown"
disabled={this.isDisabled()}
title="Archive to RADAR"
onClick={this.handleClick}
>
<span><i className={bClass} />&nbsp;{bTitle}</span>
</Button>
</ButtonToolbar>
</div>
</ButtonToolbar>
);
}
isDisabled() {
const { processing } = this.state;
return processing === true;
}
render() {
const onChange = (v) => this.setState(
previousState => {return { ...previousState, value: v }}
)
const { full } = this.props;
return (
<div className="export-collections-modal">
{this.renderButtonBar()}
</div>
)
}
}
......@@ -306,6 +306,22 @@ export default class CollectionsFetcher {
}).catch((errorMessage) => { throw new Error(errorMessage); });
}
static createExportRadarJob(params) {
return fetch('/api/v1/collections/exports/radar/', {
credentials: 'same-origin',
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(params)
}).then((response) => {
NotificationActions.notifyExImportStatus('export', response.status);
if (response.ok) { return true; }
throw new Error(response.status);
}).catch((errorMessage) => { throw new Error(errorMessage); });
}
static createImportJob(params) {
const data = new FormData();
data.append('file', params.file);
......
class ExportCollectionToRadarJob < ActiveJob::Base
include ActiveJob::Status
queue_as :export_collection_to_radar
after_perform do |job|
begin
# Email ELNer
# CollectionMailer.mail_archive_completed(
# @user_id,
# @label,
# @link,
# ).deliver_now
# Notify ELNer
# Message.create_msg_notification(
# channel_subject: Channel::ARCHIVE_RADAR,
# message_from: @user_id,
# data_args: {operation: 'Archive', label: @label },
# url: @link
# )
rescue StandardError => e
Delayed::Worker.logger.error e
end if @success
end
def perform(collection_id, user_id)
@success = true
@collection_id = collection_id
begin
@labels = Collection.where(id: @collection_id).pluck(:label)
@link = 'http://example.com'
export = Export::ExportRadar.new(collection_id)
export.fetch_access_token
export.create_dataset
export.upload_assets
rescue StandardError => e
Delayed::Worker.logger.error e
# Message.create_msg_notification(
# channel_subject: Channel::ARCHIVE_RADAR_FAIL,
# message_from: @user_id,
# data_args: { operation: 'Archive', col_labels: @labels}
# )
@success = false
end
end
end
......@@ -18,4 +18,8 @@ class CollectionPolicy
def update_metadata?
read_metadata?
end
def create_archive?
read_metadata?
end
end
begin
radar_config = Rails.application.config_for :radar
Rails.application.configure do
config.radar = ActiveSupport::OrderedOptions.new
config.radar.url = radar_config[:url]
config.radar.client_id = radar_config[:client_id]
config.radar.client_secret = radar_config[:client_secret]
config.radar.redirect_url = radar_config[:redirect_url]
config.radar.username = radar_config[:username]
config.radar.password = radar_config[:password]
config.radar.contract_id = radar_config[:contract_id]
config.radar.workspace_id = radar_config[:workspace_id]
config.radar.email = radar_config[:email]
config.radar.backlink = radar_config[:backlink]
end
rescue StandardError => e
puts e.message
Rails.application.configure do
config.radar = nil
end
end
development:
:url: https://test.radar-service.eu
:client_id:
:client_secret:
:redirect_url:
:username:
:password:
:contract_id:
:workspace_id:
:email:
:backlink:
production:
:url: https://www.radar-service.eu
:client_id:
:client_secret:
:redirect_url:
:username:
:password:
:contract_id:
:workspace_id:
:email:
:backlink:
module Export
class ExportRadar
def initialize(collection_id)
@collection_id = collection_id
@metadata = Metadata.where(collection_id: @collection_id).first.metadata
@archive_date = Time.now.utc
@publish_date = @metadata['issued']
@production_year = @metadata['created'] or @metadata['issued']
@responsible_email = Rails.configuration.radar.email
@publication_backlink = Rails.configuration.radar.backlink
@assets = []
end
def to_json
radar_metadata = {
'technicalMetadata' => {
"retentionPeriod" => 10,
"archiveDate" => @archive_date.to_i,
"publishDate" => @publish_date.to_i,
"responsibleEmail" => @responsible_email,
"publicationBacklink" => @publication_backlink,
"schema" => {
"key" => "RDDM",
"version" => "09"
}
},
'descriptiveMetadata' => {
'title' => @metadata['title'],
# 'productionYear' => production_year.year,
# 'publicationYear' => publish_date.year,
'language' => 'ENG',
'creators' => {
'creator' => []
},
'descriptions' => {
'description' => [
{
'value': @metadata['description'],
'descriptionType': 'ABSTRACT'
}
]
}
}
}
@metadata['creators'].each do |creator|
radar_creator = {
'creatorName' => creator['given_name'] + ' ' + creator['family_name'],
'givenName' => creator['given_name'],
'familyName' => creator['family_name'],
'nameIdentifier' => [{
'value': creator['orcid'],
'schemeURI': 'http://orcid.org',
'nameIdentifierScheme': 'ORCID',
}]
}
if creator['affiliations']
radar_creator['creatorAffiliation'] = creator['affiliations'][0]['affiliation']
end
radar_metadata['descriptiveMetadata']['creators']['creator'] << radar_creator
end
radar_metadata.to_json
end
def fetch_access_token
url = Rails.configuration.radar.url + '/radar/api/tokens'
body = {
'clientId' => Rails.configuration.radar.client_id,
'clientSecret' => Rails.configuration.radar.client_secret,
'redirectUrl' => Rails.configuration.radar.redirect_url,
'userName' => Rails.configuration.radar.username,
'userPassword' => Rails.configuration.radar.password
}
headers = {
'Content-Type' => 'application/json'
}
begin
response = HTTParty.post(url, :body => body.to_json, :headers => headers)
@access_token = JSON.parse(response.body)['access_token']
rescue StandardError => e
Rails.logger.error('Could not fetch access token from RADAR')
end
end
def create_dataset
url = Rails.configuration.radar.url + '/radar/api/workspaces/' + Rails.configuration.radar.workspace_id + '/datasets'
headers = {
'Authorization' => 'Bearer ' + @access_token,
'Content-Type' => 'application/json'
}
begin
response = HTTParty.post(url, :body => self.to_json, :headers => headers)
if response.code == 201
@dataset_id = JSON.parse(response.body)['id']
else
Rails.logger.error("Error with RADAR: #{response.body} (#{response.code})")
end
rescue StandardError => e
Rails.logger.error('Could not create dataset in RADAR')
end
end
def upload_assets
url = Rails.configuration.radar.url + '/radar-ingest/upload/' + @dataset_id + '/file'
headers = {
'Authorization' => 'Bearer ' + @access_token
}
puts @dataset_id
@assets.each do |asset|
body = {
'upload_file' => File.open(asset)
}
begin
response = HTTParty.post(url, :body => body, :headers => headers)
rescue StandardError => e
Rails.logger.error('Could not create dataset in RADAR')
end
end
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment