Project

General

Profile

1
import { Component, Input, Output, EventEmitter} from '@angular/core';
2
import { SearchDataciteService } from '../../services/searchDatacite.service';
3
import {SearchDatasetsService} from '../../services/searchDatasets.service';
4
import {ClaimResult} from '../../utils/entities/claimEntities.class';
5
import { ErrorCodes} from '../../utils/properties/openaireProperties';
6
import{DOI} from '../../utils/string-utils.class';
7

    
8

    
9
@Component({
10
    selector: 'claim-dataset',
11
     template: `
12

    
13
  <!--div *ngIf="warningMessage.length > 0" class="alert alert-warning" role="alert">{{warningMessage}}</div>
14
  <div *ngIf="infoMessage.length > 0" class="alert alert-info" role="alert">{{infoMessage}}</div-->
15

    
16
    <div class="panel ">
17
      <div class="">
18

    
19
        <ul class="nav nav-tabs">
20
           <li class="active"><a data-toggle="tab" href="#datacite">Datacite ({{(dataciteResultsNum==null)?'0':dataciteResultsNum}})</a></li>
21
           <li><a data-toggle="tab" href="#openaire">Openaire  ({{(openaireResultsNum==null)?'0':openaireResultsNum}})</a></li>
22
        </ul>
23
        <div class="tab-content">
24
           <div id="datacite" class="tab-pane fade in active">
25
              <div class = "panel-body">
26
                 <div *ngIf="dataciteResultsNum != null && dataciteResultsNum > 0">
27
                    <p > {{dataciteResultsNum   }} Total Results </p>
28
                    <paging-no-load [currentPage]="datacitePage" [totalResults]="dataciteResultsNum" [navigateTo]="navigateTo" [term]="keyword" [size]="size"  (pageChange)="datacitePageChange($event)"> </paging-no-load>
29
                 </div>
30
                 <div *ngIf="dataciteResultsNum == null || dataciteResultsNum == 0" class="alert alert-info" role="alert">No results found</div>
31
                 <div >
32
                     <ul *ngIf="dataciteResults.length > 0 "  class="list-group">
33
                        <li *ngFor=" let item of dataciteResults "  [class]="(isSelected(item.doi))?'list-group-item panel-footer':'list-group-item'" >
34
                         <div >
35
                          <a *ngIf="item.doi" target="_blank" href="{{'http://dx.doi.org/'+item.doi}}" >{{item.title}}</a>
36
                          <span *ngIf="!item.doi" >{{item.title}}</span>
37
                          <span  *ngIf="!isSelected(item.doi)" (click)="add(item, item.doi,'dataset','datacite',item.title,'http://dx.doi.org/'+item.doi, null,'OPEN')" aria-hidden="true" class= " btn glyphicon glyphicon-plus"></span>
38
                        </div>
39
                        <span  *ngIf="item.publisher" class="sub-title-elements">Publisher: {{item.publisher}}</span>
40
                        <div *ngIf="item.creator" class="sub-title-elements">Authors: <span *ngFor="let author of item.creator let i = index">{{author}}{{(i < (item.creator.length-1))?"; ":""}}</span></div>
41

    
42
                       </li>
43
                   </ul>
44

    
45
                 </div>
46
              </div>
47
            </div>
48
            <div id="openaire" class="tab-pane fade">
49
              <div class = "panel-body">
50
                <div *ngIf="openaireResultsNum != null && openaireResultsNum > 0">
51
                  <paging-no-load [currentPage]="openairePage" [totalResults]="openaireResultsNum" [navigateTo]="navigateTo" [term]="keyword" [size]="size"  (pageChange)="openairePageChange($event)"> </paging-no-load>
52
                </div>
53
                <div >
54
                  <ul *ngIf="openaireResults.length > 0 "  class="list-group">
55
                     <li *ngFor=" let result of openaireResults " [class]="(isSelected(result.id))?'list-group-item panel-footer':'list-group-item'">
56
                      <div >
57
                       <a *ngIf="result['title'].url" target="_blank" [href]="result['title'].url" >{{result['title'].name}}</a>
58
                       <span *ngIf="!result['title'].url" >{{result['title'].name}}</span>
59
                       <span *ngIf="!isSelected(result.id)"
60
                       (click)="add(result, result.id, 'dataset', 'openaire', result['title'].name,  result['title'].url, result.year, result['title'].accessMode)" aria-hidden="true" class= " btn glyphicon glyphicon-plus"></span>
61
                     </div>
62
                      <span  *ngIf="result.publisher" class="sub-title-elements">Publisher: {{result.publisher}}</span><span *ngIf="(result.year)">({{result.year}})</span>
63
                      <div *ngIf="result.authors" class="sub-title-elements">Authors: <span *ngFor="let author of result.authors let i = index">{{author.name}}{{(i < (result.authors.length-1))?"; ":""}}</span></div>
64

    
65
                    </li>
66
                </ul>
67
                <div *ngIf="openaireResults.length == 0" class = "alert alert-info " > No results found  </div>
68
               </div>
69
             </div>
70
        </div>
71
      </div>
72
    </div>
73

    
74
    `
75
})
76
export class ClaimDatasetComponent {
77
  constructor (private _searchDataciteService: SearchDataciteService, private _searchDatasetsService:SearchDatasetsService){
78
    var myDate = new Date();
79
    this.todayDate = myDate.getFullYear()+ "-" +(myDate.getMonth() + 1) + "-" + myDate.getDate() ;
80
    this.nextDate =  (myDate.getFullYear()+100)+ "-" +(myDate.getMonth() + 1) + "-" + myDate.getDate() ;
81

    
82
  }
83
  ngOnInit() {
84
    if(this.keyword !=null && this.keyword.length > 0){
85
     this.search(this.keyword,this.size,1);
86
   }
87
}
88

    
89
@Input() public select:boolean = true ;
90
@Input() public keyword:string = '';
91
@Input() public selectedDatasets = [] ;
92
@Output() datasetsChange = new EventEmitter();
93
  public size:number = 10;
94
  public navigateTo: string = "Search";
95
  public source: string = "datacite";
96
  public type : string = "dataset";
97
  public errorCodes:ErrorCodes = new ErrorCodes();
98

    
99
  dataciteResults=[];
100
  dataciteResultsNum:number = null;
101
  // dataciteResultsNum : Observable<number> = null;
102
  dataciteStatus = this.errorCodes.NONE;
103
  datacitePage : number = 1;
104

    
105
  openaireResults=[];
106
  openaireResultsNum:number = 0 ;
107
  openaireStatus = this.errorCodes.NONE;
108
  openairePage : number = 1;
109

    
110
  public warningMessage = "";
111
  public infoMessage = "";
112

    
113
  public todayDate = '';
114
  public nextDate = '';
115
  public DOIs:string[] = [];
116

    
117
   public search(term: string, size : number, page : number){
118
     this.DOIs = DOI.getDOIsFromString(term);
119
     this.searchDatacite(term,10,1);
120
     this.searchOpenaire(term,10,1);
121
   }
122
    private searchDatacite (term: string, size : number, page : number) {
123
      this.getDataciteResults(term,size,page);
124
      this.warningMessage = "";
125
      this.infoMessage = "";
126

    
127
     }
128
     private searchOpenaire (term: string, size : number, page : number) {
129
       if(this.DOIs.length > 0 ){
130
         this.openaireStatus = this.errorCodes.LOADING;
131
         this._searchDatasetsService.searchDatasetsByDois(this.DOIs, null, page, size, []).subscribe(
132
           data => {
133
               if(data != null) {
134
                   this.openairePage=page;
135
                   this.openaireResultsNum = data[0];
136
                   this.openaireResults = data[1];
137
                   this.openaireStatus = this.errorCodes.DONE;
138
                   if(this.openaireResultsNum == 0){
139
                      this.openaireStatus = this.errorCodes.NONE;
140
                   }
141
               }
142
            },
143
           err => {
144
             this.openaireStatus = this.errorCodes.ERROR;
145
             console.log(err.status);
146
           }
147
         );
148
       }else{
149
         this._searchDatasetsService.searchDatasets('q="'+term+'"', null, page, size, []).subscribe(
150
                     data => {
151
                         if(data != null) {
152
                             this.openairePage=page;
153
                             this.openaireResultsNum = data[0];
154
                             this.openaireResults = data[1];
155
                             this.openaireStatus = this.errorCodes.DONE;
156
                             if(this.openaireResultsNum == 0){
157
                                this.openaireStatus = this.errorCodes.NONE;
158
                             }
159
                         }
160
                      },
161
                     err => {
162
                       this.openaireStatus = this.errorCodes.ERROR;
163
                       console.log(err.status);
164
                     }
165
                   );
166
                }
167
       this.warningMessage = "";
168
       this.infoMessage = "";
169

    
170
      }
171
   private getDataciteResults (term: string, size : number, page : number)  {
172
       this._searchDataciteService.searchDataciteResults(term, size, page).subscribe(
173
        data => {
174
          this.dataciteResults = data.docs;
175
          this.datacitePage=page;
176
          this.dataciteResultsNum = data.numFound;
177
          this.dataciteStatus = this.errorCodes.DONE;
178

    
179

    
180
        },
181
        err => {
182
          this.dataciteStatus = this.errorCodes.ERROR;
183
          console.log(err);
184
        }
185

    
186
      );
187
      }
188

    
189
   add(item, itemId,itemType,itemSource,itemTitle, itemUrl, date, accessmode){
190
     console.log(' adding dataset '+ itemSource+"  "+ itemTitle);
191
     var result: ClaimResult ;
192
      if(itemSource == 'datacite'){
193
        result = {id: itemId, type :itemType, source : itemSource, title: itemTitle,url: itemUrl, result: item, accessRights: 'OPEN', embargoEndDate: this.nextDate, date : date};
194
      }else if (itemSource == 'openaire'){
195
        //TODO put right access rights
196
        result = {id: itemId, type :itemType, source : itemSource, title: itemTitle,url: itemUrl, result: item, accessRights: accessmode, embargoEndDate: this.nextDate, date : date};
197
      }
198
      var found:boolean = this.isSelected( result.id);
199
      this.warningMessage = "";
200
       if (!found) {
201
        this.selectedDatasets.push(result);
202
        this.datasetsChange.emit({
203
           value: this.selectedDatasets
204
         });
205
      }else{
206
        this.warningMessage = "Dataset already in selected list";
207
      }
208

    
209
   }
210

    
211

    
212
   datacitePageChange($event) {
213
     this.datacitePage=$event.value;
214
     this.dataciteResults=[];
215
     this.searchDatacite(this.keyword,10,this.datacitePage);
216
     this.warningMessage = "";
217
     this.infoMessage = "";
218

    
219
   }
220
   openairePageChange($event) {
221
     this.openairePage=$event.value;
222
     this.openaireResults=[];
223
     this.searchOpenaire(this.keyword,10,this.openairePage);
224
     this.warningMessage = "";
225
     this.infoMessage = "";
226

    
227
   }
228
   isSelected(id:string){
229

    
230
     var found:boolean = false;
231
     this.warningMessage = "";
232
     for (var _i = 0; _i < this.selectedDatasets.length; _i++) {
233
         let item = this.selectedDatasets[_i];
234
         if(item.id == id){
235
               found=true;
236
               break;
237
          }
238
      }
239
      return found;
240
   }
241
}
(2-2/8)