Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4
import {DeletedByInferenceResult} from '../../../utils/entities/deletedByInferenceResult';
5
import 'rxjs/add/observable/of';
6
import 'rxjs/add/operator/do';
7
import 'rxjs/add/operator/share';
8
import 'rxjs/add/operator/map';
9
import{EnvProperties} from '../../../utils/properties/env-properties';
10

    
11

    
12
import { ParsingFunctions } from '../parsingFunctions.class';
13

    
14
@Injectable()
15
export class DeletedByInferenceService {
16
  private sizeOfDescription: number = 270;
17

    
18
  constructor(private http: Http ) {
19
    this.parsingFunctions = new ParsingFunctions();
20
  }
21

    
22
  public parsingFunctions: ParsingFunctions;
23

    
24
  getDeletedByInferencePublications (id: string, properties:EnvProperties):any {
25
    let url = properties.searchAPIURLLAst + 'deletedByInferencePublications/' +id+"?format=json";
26
    let key = url;
27

    
28
    return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
29
                .map(res => <any> res.json())
30
                .map(res => res['result']['metadata']['oaf:entity'])
31
                .map(res => this.parseDeletedByInferencePublications(res));
32
  }
33

    
34
  parseDeletedByInferencePublications (result: any): DeletedByInferenceResult {
35
    /*title, authors, abstract, List of projects, PIDs,
36
      collectedfrom (link pointing to the download url), access rights*/
37

    
38
    //let publications: DeletedByInferenceResult[] = [];
39
    //if(results) {
40
      let publication : DeletedByInferenceResult;
41

    
42
      //let length = Array.isArray(results) ? results.length : 1;
43
      //for(let i=0; i<length; i++) {
44
        //var result = Array.isArray(results) ? results[i] : results;
45

    
46
        publication = new DeletedByInferenceResult();
47

    
48
        if(result) {
49
          if(result['oaf:result']) {
50
            let data = result['oaf:result'];
51

    
52
            var date:string = (data.dateofacceptance)+""; // transform to string in case it is an integer
53
            publication.date  = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
54
            publication.dateofacceptance = data.dateofacceptance;
55
            publication.embargoEndDate = data.embargoenddate;
56

    
57
            if(!Array.isArray(data.description)) {
58
                publication.description = data.description;
59
            } else {
60
                publication.description = data.description[0];
61
            }
62
            if(publication.description && publication.description.length > this.sizeOfDescription) {
63
                publication.description = publication.description.substring(0, this.sizeOfDescription) + "...";
64
            }
65

    
66
            if(data['bestaccessright'] && data['bestaccessright'].hasOwnProperty("classid")) {
67
              publication.accessMode = data['bestaccessright'].classid;
68
            }
69
          }
70

    
71
          if(result['oaf:result'] && result['oaf:result']['title']) {
72
            let title = result['oaf:result']['title'];
73

    
74
            if(Array.isArray(title)) {
75
              publication.title = title[0].content;
76
            } else {
77
              publication.title = title.content;
78
            }
79
          }
80

    
81
          if(result['oaf:result'] && result['oaf:result']['language']) {
82
            publication.languages = this.parsingFunctions.parseLanguages(result['oaf:result']['language']);
83
          }
84
          if(result['oaf:result'] && result['oaf:result']['country']) {
85
            publication.countries = this.parsingFunctions.parseCountries(result['oaf:result']['country']);
86
          }
87

    
88
          if(result['oaf:result'] && result['oaf:result']['rels'] && result['oaf:result']['rels']['rel']) {
89
            let relation;
90
            let length = Array.isArray(result['oaf:result']['rels']['rel']) ? result['oaf:result']['rels']['rel'].length : 1;
91

    
92
            for(let i=0; i<length; i++) {
93
              relation = Array.isArray(result['oaf:result']['rels']['rel']) ? result['oaf:result']['rels']['rel'][i] : result['oaf:result']['rels']['rel'];
94
              if(relation.hasOwnProperty("to")) {
95
                if(relation['to'].class == "isProducedBy") {
96
                  publication.fundedByProjects = this.parsingFunctions.parseFundingByProjects(publication.fundedByProjects, relation, publication.projectsProvenanceVocabulary);
97
                }
98
              }
99
            }
100
          }
101

    
102
          if(result['oaf:result'] && result['oaf:result']['children']) {
103
            let children = result['oaf:result']['children'];
104

    
105
            if(children.hasOwnProperty("instance")) {
106
              publication.types = new Array<string>();
107
              let types = new Set<string>();
108

    
109
              publication.hostedBy_collectedFrom = new Array<{"downloadName": string, "downloadUrl": string[], "collectedName": string, "collectedId": string, "accessMode": string[], "bestAccessMode": string, "type": string, "year":string}>();
110

    
111
              let counter = 0;
112
              let instance;
113

    
114
              let length = Array.isArray(children['instance']) ? children['instance'].length : 1;
115

    
116
              for(let i=0; i<length; i++) {
117
                instance = Array.isArray(children['instance']) ? children['instance'][i] : children['instance'];
118

    
119
                this.parsingFunctions.parseTypes(publication.types, types, instance);
120

    
121
                if(instance.hasOwnProperty("webresource")) {
122
                  let url;
123
                  if(!Array.isArray(instance['webresource'])) {
124
                    url = instance['webresource'].url;
125
                  } else{
126
                    url = instance['webresource'][0].url;
127
                  }
128

    
129
                  if(instance.hasOwnProperty("hostedby")) {
130
                    counter = this.parsingFunctions.parseHostedBy_collectedFrom(publication.hostedBy_collectedFrom, instance, result['oaf:result'], url, counter, publication.accessMode);
131
                  }
132
                }
133
              }
134
            }
135
          }
136

    
137
          if(result['oaf:result'] && result['oaf:result']['pid']) {
138
            publication.identifiers = this.parsingFunctions.parseIdentifiers(result['oaf:result']['pid']);
139
          }
140

    
141
          if(result['oaf:result'] && result['oaf:result']['creator']) {
142
            if(publication.authors == undefined) {
143
              publication.authors = new Array<string>();
144
            }
145

    
146
            let authors = result['oaf:result']['creator'];
147
            let length = Array.isArray(authors) ? authors.length : 1;
148

    
149
            for(let i=0; i<length; i++) {
150
              let author = Array.isArray(authors) ? authors[i] : authors;
151
              publication.authors[author.rank] = author.content;
152
            }
153
            publication.authors = publication.authors.filter(function (item) {
154
              return (item != undefined);
155
            });
156
          }
157

    
158
        //}
159
        //publications.push(publication);
160
      //}
161
    }
162

    
163
    return publication;
164
  }
165
}
(3-3/3)