LabarchivesController.ts 8.73 KB
Newer Older
1
2
3
4
declare var module;
declare var sails, Model;
declare var _;

5
import {Observable} from 'rxjs';
6
7
8
9
10
11
12
13
import 'rxjs/add/operator/map';

declare var BrandingService, WorkspaceService, LabarchivesService;
/**
 * Package that contains all Controllers.
 */
import controller = require('../core/CoreController');
import {Config} from '../Config';
Moises Sacal's avatar
Moises Sacal committed
14
import {UserInfo} from "../UserInfo";
15
16
17
18
19
20
21
22
23
24
25

export module Controllers {

  /**
   * Omero related features....
   *
   */
  export class LabarchivesController extends controller.Controllers.Core.Controller {

    protected _exportedMethods: any = [
      'login',
Moises Sacal's avatar
Moises Sacal committed
26
27
      'link',
      'checkLink'
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    ];
    _config: any;

    protected config: Config;

    constructor() {
      super();
      this.config = new Config(sails.config.workspaces);
    }

    login(req, res) {
      const user = {
        username: req.param('username'),
        password: req.param('password')
      };
      if (user.username && user.password) {
Moises Sacal's avatar
Moises Sacal committed
44
        let info = {};
45
46
47
        const userId = req.user.id;
        this.config.brandingAndPortalUrl = BrandingService.getFullPath(req);
        const userInfo = LabarchivesService.login(this.config.key, user.username, user.password);
Moises Sacal's avatar
Moises Sacal committed
48
49
50
51
        Observable.fromPromise(userInfo).flatMap(response => {
          const userInfo = response['users'];
          if (userInfo) {
            info = new UserInfo(userInfo['id'], userInfo['orcid'], userInfo['fullname'], userInfo['notebooks']);
Moises Sacal's avatar
Moises Sacal committed
52
            sails.log.debug(info);
Moises Sacal's avatar
Moises Sacal committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
            return WorkspaceService.workspaceAppFromUserId(userId, this.config.appName);
          } else {
            console.log(response);
            //Doing this because if the password is incorrect labarchives returns a 404!
            const message = 'username and password invalid';
            throw new Error(message);
          }
        }).flatMap(response => {
          if (response && response['id']) {
            return WorkspaceService.updateWorkspaceInfo(response['id'], info);
          } else {
            return WorkspaceService.createWorkspaceInfo(userId, this.config.appName, info);
          }
        }).subscribe(response => {

          const data = {status: true, login: true};
          this.ajaxOk(req, res, null, {status: true, labUser: info});
        }, error => {
          const errorMessage = `Failed to login for user ${user.username}`;
          sails.log.error(error);
          this.ajaxFail(req, res, errorMessage, {status: false, message: errorMessage});
        });
75
76
77
78
79
80
81
      }
      else {
        const message = 'Input username and password';
        this.ajaxFail(req, res, message, {status: false, message: message});
      }
    }

Moises Sacal's avatar
Moises Sacal committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
    list(req, res) {
      sails.log.debug('list');
      const userId = req.user.id;
      this.config.brandingAndPortalUrl = BrandingService.getFullPath(req);
      let info = {};
      return WorkspaceService.workspaceAppFromUserId(userId, this.config.appName)
        .flatMap(response => {
          const user = response['users'] || null;
          if (user) {
            const userInfo = LabarchivesService.userInfo(this.config.key, user['id']);
            return Observable.fromPromise(userInfo);
          } else {
            return Observable.throw('');
          }
        })
        .subscribe(response => {
          this.ajaxOk(req, res, null, {status: true, labUser: response, message: 'list'});
        }, error => {
          sails.log.error('list: error');
          sails.log.error(error);
          this.ajaxFail(req, res, error.message, {status: false, message: error.message});
        });
    }

Moises Sacal's avatar
Moises Sacal committed
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    link(req, res) {
      const userId = req.user.id;
      const username = req.user.username;

      this.config.brandingAndPortalUrl = BrandingService.getFullPath(req);

      const rdmp = req.param('rdmp');
      const notebook = req.param('workspace');
      if (!notebook || !rdmp) {
        const message = 'rdmp, notebook are missing';
        this.ajaxFail(req, res, message, {status: false, message: message});
        return;
      }
      const nbId = notebook['id'];
      const nbName = notebook['name'];
      sails.log.debug('notebook: ' + nbId);
      sails.log.debug('name: ' + nbName);
      if (!nbId || !nbName) {
        const message = 'notebook or notebook name are missing';
        this.ajaxFail(req, res, message, {status: false, message: message});
        return;
      }
      let info = {};
      let workspace: any = null;
      let metadataContent = '';
      let rdmpTitle = '';
      let recordMetadata = null;

      WorkspaceService.getRecordMeta(this.config, rdmp)
        .flatMap(response => {
          sails.log.debug('recordMetadata');
          recordMetadata = response;
          rdmpTitle = recordMetadata.title;
          return WorkspaceService.workspaceAppFromUserId(userId, this.config.appName)
        })
        .flatMap(response => {
          info = response.info;
          const record = {
            rdmpOid: rdmp,
            rdmpTitle: rdmpTitle,
            title: nbName,
            location: `https://au-mynotebook.labarchives.com`,
            description: 'LabArchives Workspace',
            type: this.config.recordType
          };
          return WorkspaceService.createWorkspaceRecord(
            this.config, username, record, this.config.recordType, this.config.workflowStage
          );
        })
        .flatMap(response => {
          workspace = response;
          const insertNode = LabarchivesService.insertNode(this.config.key, info['id'], nbId, 'stash.workspace', false);
          return Observable.fromPromise(insertNode);

        })
        .flatMap(response => {
          sails.log.debug('insertNode');
          sails.log.debug(response);
164
165
166
167
          if (response && response['tree-tools']) {
            const tree = response['tree-tools'];
            const node = tree['node'];
            metadataContent = `
Moises Sacal's avatar
Moises Sacal committed
168
169
          <div id="${workspace.oid}">      
            <h1>UTS</h1>             
170
171
            <h3>Workspace <strong>${nbName}</strong> is linked to:</h3>                       
            <h2>Research Data Management Plan <a href="${this.config.brandingAndPortalUrl}/record/view/${rdmp}">${rdmpTitle}</a></h2>            
Moises Sacal's avatar
Moises Sacal committed
172
173
174
            <p>Stash Id: ${workspace.oid}</p>   
          </div>
          `;
175
176
177
178
179
180
            const partType = 'text entry';
            const insertNode = LabarchivesService.addEntry(
              this.config.key, info['id'], nbId, node['tree-id'], partType, metadataContent
            );
            return Observable.fromPromise(insertNode);
          } else return Observable.throwError(new Error('cannot insert node'));
Moises Sacal's avatar
Moises Sacal committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
        })
        .flatMap(response => {
          if (recordMetadata.workspaces) {
            const wss = recordMetadata.workspaces.find(id => workspace.oid === id);
            if (!wss) {
              recordMetadata.workspaces.push({id: workspace.oid});
            }
          }
          return WorkspaceService.updateRecordMeta(this.config, recordMetadata, rdmp);
        })
        .subscribe(response => {
          sails.log.debug(response);
          this.ajaxOk(req, res, null, {status: true, message: 'workspaceRecordCreated'});
        }, error => {
          sails.log.error('link: error');
          sails.log.error(error);
          this.ajaxFail(req, res, error.message, {status: false, message: error.message});
        });
    }

Moises Sacal's avatar
Moises Sacal committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
    checkLink(req, res) {
      sails.log.debug('checkLink');
      const userId = req.user.id;
      const username = req.user.username;
      const rdmp = req.param('rdmp');
      const nbId = req.param('nbId');
      const workspace = req.param('workspace');
      let info = {};
      let check = {
        link: ''
      };
      this.config.brandingAndPortalUrl = BrandingService.getFullPath(req);
      return WorkspaceService.workspaceAppFromUserId(userId, this.config.appName)
        .flatMap(response => {
          info = response.info;
          const nbTree = LabarchivesService.getNotebookTree(this.config.key, info['id'], nbId, 0);
          return Observable.fromPromise(nbTree);
        })
        .subscribe(response => {
          if (response['tree-tools'] && response['tree-tools']['level-nodes']) {
            const lvlNodes = response['tree-tools']['level-nodes'];
            const nodes = lvlNodes['level-node'];
            if (Array.isArray(nodes)) {
              nodes.map(node => {
                if (node['display-text'] === 'stash.workspace') {
                  check.link = 'linked';
                }
              });
            }
          }
          this.ajaxOk(req, res, null, {status: true, check: check, message: 'checkLink'});
        }, error => {
          sails.log.error('checkLink: error');
          sails.log.error(error);
          this.ajaxFail(req, res, error.message, {status: false, message: error.message});
        });
    }
238
239
240
  }
}
module.exports = new Controllers.LabarchivesController().exports();