File size: 27,515 Bytes
fe66731
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
72
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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
module.exports = function ( graph ){
  /** some constants **/
  var PREDEFINED = 0,
    FILE_UPLOAD = 1,
    JSON_URL = 2,
    IRI_URL = 3;
  
  var PROGRESS_BAR_ERROR = 0,
    PROGRESS_BAR_BUSY = 1,
    PROGRESS_BAR_PERCENT = 2,
    progressBarMode = 1;
  
  var loadingWasSuccessFul = false;
  var missingImportsWarning = false;
  var showLoadingDetails = false;
  var visibilityStatus = true;
  
  var DEFAULT_JSON_NAME = "foaf"; // This file is loaded by default
  var conversion_sessionId;
  
  /** variable defs **/
  var loadingModule = {},
    menuContainer = d3.select("#loading-info"),
    loadingInfoContainer = d3.select("#loadingInfo-container"),
    detailsButton = d3.select("#show-loadingInfo-button"),
    closeButton = d3.select("#loadingIndicator_closeButton"),
    ontologyMenu,
    ontologyIdentifierFromURL;
  
  /** functon defs **/
  loadingModule.checkForScreenSize = function (){
    // checks for window size and adjusts the loading indicator
    var w = graph.options().width(),
      h = graph.options().height();
    
    if ( w < 270 ) {
      d3.select("#loading-info").classed("hidden", true);
    } else {
      // check if it should be visible
      if ( visibilityStatus === true ) {
        d3.select("#loading-info").classed("hidden", false);
      } else {
        d3.select("#loading-info").classed("hidden", true);
      }
    }
    if ( h < 150 ) {
      d3.select("#loadingInfo_msgBox").classed("hidden", true);
    } else {
      d3.select("#loadingInfo_msgBox").classed("hidden", false);
    }
    if ( h < 80 ) {
      d3.select("#progressBarContext").classed("hidden", true);
      d3.select("#layoutLoadingProgressBarContainer").style("height", "20px");
    } else {
      d3.select("#progressBarContext").classed("hidden", false);
      d3.select("#layoutLoadingProgressBarContainer").style("height", "50px");
    }
  };
  
  loadingModule.getMessageVisibilityStatus = function (){
    return visibilityStatus;
  };
  
  loadingModule.getProgressBarMode = function (){
    return progressBarMode;
  };
  
  loadingModule.successfullyLoadedOntology = function (){
    return loadingWasSuccessFul;
  };
  
  loadingModule.missingImportsWarning = function (){
    return missingImportsWarning;
  };
  
  loadingModule.setOntologyMenu = function ( m ){
    ontologyMenu = m;
  };
  
  loadingModule.showErrorDetailsMessage = function (){
    loadingModule.showLoadingIndicator();
    loadingModule.expandDetails();
    d3.select("#loadingIndicator_closeButton").classed("hidden", true);
    loadingModule.scrollDownDetails();
  };
  
  loadingModule.showWarningDetailsMessage = function (){
    d3.select("#currentLoadingStep").style("color", "#ff0");
    loadingModule.showLoadingIndicator();
    loadingModule.expandDetails();
    d3.select("#loadingIndicator_closeButton").classed("hidden", false);
    loadingModule.scrollDownDetails();
  };
  
  loadingModule.scrollDownDetails = function (){
    var scrollingElement = d3.select("#loadingInfo-container").node();
    scrollingElement.scrollTop = scrollingElement.scrollHeight;
  };
  
  loadingModule.hideLoadingIndicator = function (){
    d3.select("#loading-info").classed("hidden", true);
    visibilityStatus = false;
  };
  
  loadingModule.showLoadingIndicator = function (){
    d3.select("#loading-info").classed("hidden", false);
    visibilityStatus = true;
    
  };
  
  /** -- SETUP -- **/
  loadingModule.setup = function (){
    // create connections for close and details button;
    loadingInfoContainer.classed("hidden", !showLoadingDetails);
    detailsButton.on("click", function (){
      showLoadingDetails = !showLoadingDetails;
      loadingInfoContainer.classed("hidden", !showLoadingDetails);
      detailsButton.classed("accordion-trigger-active", showLoadingDetails);
    });
    
    closeButton.on("click", function (){
      menuContainer.classed("hidden", true);
    });
    loadingModule.setBusyMode();
  };
  
  loadingModule.updateSize = function (){
    showLoadingDetails = !(loadingInfoContainer.classed("hidden"));
    loadingInfoContainer.classed("hidden", !showLoadingDetails);
    detailsButton.classed("accordion-trigger-active", showLoadingDetails);
  };
  
  loadingModule.getDetailsState = function (){
    return showLoadingDetails;
  };
  
  loadingModule.expandDetails = function (){
    showLoadingDetails = true;
    loadingInfoContainer.classed("hidden", !showLoadingDetails);
    detailsButton.classed("accordion-trigger-active", showLoadingDetails);
  };
  
  loadingModule.collapseDetails = function (){
    showLoadingDetails = false;
    loadingInfoContainer.classed("hidden", !showLoadingDetails);
    detailsButton.classed("accordion-trigger-active", showLoadingDetails);
  };
  
  loadingModule.setBusyMode = function (){
    d3.select("#currentLoadingStep").style("color", "#fff");
    d3.select("#progressBarValue").node().innherHTML = "";
    d3.select("#progressBarValue").style("width", "20%");
    d3.select("#progressBarValue").classed("busyProgressBar", true);
    progressBarMode = PROGRESS_BAR_BUSY;
  };
  
  loadingModule.setSuccessful = function (){
    d3.select("#currentLoadingStep").style("color", "#0f0");
  };
  
  loadingModule.setErrorMode = function (){
    d3.select("#currentLoadingStep").style("color", "#f00");
    d3.select("#progressBarValue").style("width", "0%");
    d3.select("#progressBarValue").classed("busyProgressBar", false);
    d3.select("#progressBarValue").node().innherHTML = "";
    progressBarMode = PROGRESS_BAR_ERROR;
  };
  
  loadingModule.setPercentMode = function (){
    d3.select("#currentLoadingStep").style("color", "#fff");
    d3.select("#progressBarValue").classed("busyProgressBar", false);
    d3.select("#progressBarValue").node().innherHTML = "0%";
    d3.select("#progressBarValue").style("width", "0%");
    progressBarMode = PROGRESS_BAR_PERCENT;
  };
  
  loadingModule.setPercentValue = function ( val ){
    d3.select("#progressBarValue").node().innherHTML = val;
  };
  
  loadingModule.emptyGraphContentError = function (){
    graph.clearGraphData();
    ontologyMenu.append_message_toLastBulletPoint("<span style='color:red;'>failed</span>");
    ontologyMenu.append_message_toLastBulletPoint("<br><span style=\"color:red;\">Error: Received empty graph</span>");
    loadingWasSuccessFul = false;
    graph.handleOnLoadingError();
    loadingModule.setErrorMode();
  };
  
  loadingModule.isThreadCanceled = function (){
    
  };
  
  loadingModule.initializeLoader = function ( storeCache ){
    if ( storeCache === true && graph.getCachedJsonObj() !== null ) {
      // save cached ontology;
      var cachedContent = JSON.stringify(graph.getCachedJsonObj());
      var cachedName = ontologyIdentifierFromURL;
      ontologyMenu.setCachedOntology(cachedName, cachedContent);
    }
    conversion_sessionId = -10000;
    ontologyMenu.setConversionID(conversion_sessionId);
    ontologyMenu.stopLoadingTimer();
    graph.clearGraphData();
    loadingModule.setBusyMode();
    loadingModule.showLoadingIndicator();
    loadingModule.collapseDetails();
    missingImportsWarning = false;
    d3.select("#loadingIndicator_closeButton").classed("hidden", true);
    ontologyMenu.clearDetailInformation();
  };
  
  /** ------------------ URL Interpreter -------------- **/
  loadingModule.parseUrlAndLoadOntology = function ( storeCache ){
    var autoStore = true;
    if ( storeCache === false ) {
      autoStore = false;
    }
    
    graph.clearAllGraphData();
    loadingModule.initializeLoader(autoStore);
    var urlString = String(location);
    var parameterArray = identifyParameter(urlString);
    ontologyIdentifierFromURL = DEFAULT_JSON_NAME;
    loadGraphOptions(parameterArray); // identifies and loads configuration values
    var loadingMethod = identifyOntologyLoadingMethod(ontologyIdentifierFromURL);
    d3.select("#progressBarValue").node().innerHTML = " ";
    switch ( loadingMethod ) {
      case 0:
        loadingModule.from_presetOntology(ontologyIdentifierFromURL);
        break;
      case 1:
        loadingModule.from_FileUpload(ontologyIdentifierFromURL);
        break;
      case 2:
        loadingModule.from_JSON_URL(ontologyIdentifierFromURL);
        break;
      case 3:
        loadingModule.from_IRI_URL(ontologyIdentifierFromURL);
        break;
      default:
        console.log("Could not identify loading method , or not IMPLEMENTED YET");
    }
  };
  
  /** ------------------- LOADING --------------------- **/
  // the loading module splits into 3 branches
  // 1] PresetOntology Loading
  // 2] File Upload
  // 3] Load From URL / IRI
  
  loadingModule.from_JSON_URL = function ( fileName ){
    var filename = decodeURIComponent(fileName.slice("url=".length));
    ontologyIdentifierFromURL = filename;
    
    var ontologyContent = "";
    if ( ontologyMenu.cachedOntology(filename) ) {
      ontologyMenu.append_bulletPoint("Loading already cached ontology: " + filename);
      ontologyContent = ontologyMenu.cachedOntology(filename);
      loadingWasSuccessFul = true; // cached Ontology should be true;
      parseOntologyContent(ontologyContent);
      
    } else {
      // involve the o2v conveter;
      ontologyMenu.append_message("Retrieving ontology from JSON URL " + filename);
      requestServerTimeStampForJSON_URL(ontologyMenu.callbackLoad_JSON_FromURL, ["read?json=" + filename, filename]);
    }
  };
  
  function requestServerTimeStampForJSON_URL( callback, parameter ){
    d3.xhr("serverTimeStamp", "application/text", function ( error, request ){
      if ( error ) {
        // could not get server timestamp -> no connection to owl2vowl
        ontologyMenu.append_bulletPoint("Could not establish connection to OWL2VOWL service");
        fallbackForJSON_URL(callback, parameter);
      } else {
        conversion_sessionId = request.responseText;
        ontologyMenu.setConversionID(conversion_sessionId);
        parameter.push(conversion_sessionId);
        callback(parameter);
      }
    });
    
  }
  
  loadingModule.requestServerTimeStampForDirectInput = function ( callback, text ){
    d3.xhr("serverTimeStamp", "application/text", function ( error, request ){
      if ( error ) {
        // could not get server timestamp -> no connection to owl2vowl
        ontologyMenu.append_bulletPoint("Could not establish connection to OWL2VOWL service");
        loadingModule.setErrorMode();
        ontologyMenu.append_message_toLastBulletPoint("<br><span style='color:red'>Could not connect to OWL2VOWL service </span>");
        loadingModule.showErrorDetailsMessage();
        d3.select("#progressBarValue").style("width", "0%");
        d3.select("#progressBarValue").classed("busyProgressBar", false);
        d3.select("#progressBarValue").text("0%");
        
      } else {
        conversion_sessionId = request.responseText;
        ontologyMenu.setConversionID(conversion_sessionId);
        callback(text, ["conversionID" + conversion_sessionId, conversion_sessionId]);
      }
    });
  };
  
  loadingModule.from_IRI_URL = function ( fileName ){
    // owl2vowl converters the given ontology url and returns json file;
    var filename = decodeURIComponent(fileName.slice("iri=".length));
    ontologyIdentifierFromURL = filename;
    
    var ontologyContent = "";
    if ( ontologyMenu.cachedOntology(filename) ) {
      ontologyMenu.append_bulletPoint("Loading already cached ontology: " + filename);
      ontologyContent = ontologyMenu.cachedOntology(filename);
      loadingWasSuccessFul = true; // cached Ontology should be true;
      parseOntologyContent(ontologyContent);
    } else {
      // involve the o2v conveter;
      var encoded = encodeURIComponent(filename);
      ontologyMenu.append_bulletPoint("Retrieving ontology from IRI: " + filename);
      requestServerTimeStampForIRI_Converte(ontologyMenu.callbackLoad_Ontology_FromIRI, ["convert?iri=" + encoded, filename]);
    }
  };
  
  loadingModule.fromFileDrop = function ( fileName, file ){
    d3.select("#progressBarValue").node().innerHTML = " ";
    loadingModule.initializeLoader(false);
    
    ontologyMenu.append_bulletPoint("Retrieving ontology from dropped file: " + fileName);
    var ontologyContent = "";
    
    // two options here
    //1] Direct Json Upload
    if ( fileName.match(/\.json$/) ) {
      ontologyMenu.setConversionID(-10000);
      var reader = new FileReader();
      reader.readAsText(file);
      reader.onload = function (){
        ontologyContent = reader.result;
        ontologyIdentifierFromURL = fileName;
        parseOntologyContent(ontologyContent);
      };
    } else {
      //2] File Upload to OWL2VOWL Converter
      // 1) check if we can get a timeStamp;
      var parameterArray = [file, fileName];
      requestServerTimeStamp(ontologyMenu.callbackLoadFromOntology, parameterArray);
    }
  };
  
  
  loadingModule.from_FileUpload = function ( fileName ){
    loadingModule.setBusyMode();
    var filename = decodeURIComponent(fileName.slice("file=".length));
    ontologyIdentifierFromURL = filename;
    var ontologyContent = "";
    if ( ontologyMenu.cachedOntology(filename) ) {
      ontologyMenu.append_bulletPoint("Loading already cached ontology: " + filename);
      ontologyContent = ontologyMenu.cachedOntology(filename);
      loadingWasSuccessFul = true; // cached Ontology should be true;
      parseOntologyContent(ontologyContent);
      
    } else {
      // d3.select("#currentLoadingStep").node().innerHTML="Loading ontology from file "+ filename;
      ontologyMenu.append_bulletPoint("Retrieving ontology from file: " + filename);
      // get the file
      var selectedFile = d3.select("#file-converter-input").property("files")[0];
      // No selection -> this was triggered by the iri. Unequal names -> reuploading another file
      if ( !selectedFile || (filename && (filename !== selectedFile.name)) ) {
        ontologyMenu.append_message_toLastBulletPoint("<br><span style=\"color:red;\">No cached version of \"" + filename + "\" was found.</span><br>Please reupload the file.");
        loadingModule.setErrorMode();
        d3.select("#progressBarValue").classed("busyProgressBar", false);
        graph.handleOnLoadingError();
        return;
      } else {
        filename = selectedFile.name;
      }


// two options here
//1] Direct Json Upload
      if ( filename.match(/\.json$/) ) {
        ontologyMenu.setConversionID(-10000);
        var reader = new FileReader();
        reader.readAsText(selectedFile);
        reader.onload = function (){
          ontologyContent = reader.result;
          ontologyIdentifierFromURL = filename;
          parseOntologyContent(ontologyContent);
        };
      } else {
//2] File Upload to OWL2VOWL Converter
        // 1) check if we can get a timeStamp;
        var parameterArray = [selectedFile, filename];
        requestServerTimeStamp(ontologyMenu.callbackLoadFromOntology, parameterArray);
      }
    }
  };
  
  function fallbackForJSON_URL( callback, parameter ){
    ontologyMenu.append_message_toLastBulletPoint("<br>Trying to convert with other communication protocol.");
    callback(parameter);
    
  }
  
  function fallbackConversion( parameter ){
    ontologyMenu.append_message_toLastBulletPoint("<br>Trying to convert with other communication protocol.");
    var file = parameter[0];
    var name = parameter[1];
    var formData = new FormData();
    formData.append("ontology", file);
    
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "convert", true);
    var ontologyContent = "";
    xhr.onload = function (){
      if ( xhr.status === 200 ) {
        ontologyContent = xhr.responseText;
        ontologyMenu.setCachedOntology(name, ontologyContent);
        ontologyIdentifierFromURL = name;
        missingImportsWarning = true; // using this variable for warnings
        ontologyMenu.append_message_toLastBulletPoint("<br>Success, <span style='color:yellow'>but you are using a deprecated OWL2VOWL service!<span>");
        parseOntologyContent(ontologyContent);
      }
    };
    
    // check what this thing is doing;
    xhr.onreadystatechange = function (){
      if ( xhr.readyState === 4 && xhr.status === 0 ) {
        ontologyMenu.append_message_toLastBulletPoint("<br>Old protocol also failed to establish connection to OWL2VOWL service!");
        loadingModule.setErrorMode();
        ontologyMenu.append_bulletPoint("Failed to load ontology");
        ontologyMenu.append_message_toLastBulletPoint("<br><span style='color:red'>Could not connect to OWL2VOWL service </span>");
        loadingModule.showErrorDetailsMessage();
      }
    };
    xhr.send(formData);
  }
  
  function requestServerTimeStampForIRI_Converte( callback, parameterArray ){
    d3.xhr("serverTimeStamp", "application/text", function ( error, request ){
      loadingModule.setBusyMode();
      if ( error ) {
        // could not get server timestamp -> no connection to owl2vowl
        ontologyMenu.append_bulletPoint("Could not establish connection to OWL2VOWL service");
        loadingModule.setErrorMode();
        ontologyMenu.append_bulletPoint("Failed to load ontology");
        ontologyMenu.append_message_toLastBulletPoint("<br><span style='color:red'>Could not connect to OWL2VOWL service </span>");
        loadingModule.showErrorDetailsMessage();
      } else {
        conversion_sessionId = request.responseText;
        ontologyMenu.setConversionID(conversion_sessionId);
        // update paramater for new communication paradigm
        parameterArray[0] = parameterArray[0] + "&sessionId=" + conversion_sessionId;
        parameterArray.push(conversion_sessionId);
        callback(parameterArray);
      }
    });
  }
  
  function requestServerTimeStamp( callback, parameterArray ){
    d3.xhr("serverTimeStamp", "application/text", function ( error, request ){
      if ( error ) {
        // could not get server timestamp -> no connection to owl2vowl
        ontologyMenu.append_bulletPoint("Could not establish connection to OWL2VOWL service");
        fallbackConversion(parameterArray); // tries o2v version0.3.4 communication
      } else {
        conversion_sessionId = request.responseText;
        ontologyMenu.setConversionID(conversion_sessionId);
        console.log("Request Session ID:" + conversion_sessionId);
        callback(parameterArray[0], parameterArray[1], conversion_sessionId);
      }
    });
  }
  
  loadingModule.directInput = function ( text ){
    ontologyMenu.clearDetailInformation();
    parseOntologyContent(text);
  };
  
  loadingModule.loadFromOWL2VOWL = function ( ontoContent, filename ){
    loadingWasSuccessFul = false;
    
    var old = d3.select("#bulletPoint_container").node().innerHTML;
    if ( old.indexOf("(with warnings)") !== -1 ) {
      missingImportsWarning = true;
    }
    
    if ( ontologyMenu.cachedOntology(ontoContent) ) {
      ontologyMenu.append_bulletPoint("Loading already cached ontology: " + filename);
      parseOntologyContent(ontoContent);
    } else { // set parse the ontology content;
      parseOntologyContent(ontoContent);
    }
  };
  
  loadingModule.from_presetOntology = function ( selectedOntology ){
    ontologyMenu.append_bulletPoint("Retrieving ontology: " + selectedOntology);
    loadPresetOntology(selectedOntology);
  };
  
  function loadPresetOntology( ontology ){
    // check if already cached in ontology menu?
    var f2r;
    var loadingNewOntologyForEditor=false;
    if ( ontology.indexOf("new_ontology") !== -1 ) {
      loadingModule.hideLoadingIndicator();
      graph.showEditorHintIfNeeded();
      f2r = "./data/new_ontology.json";
      loadingNewOntologyForEditor=true;
    }
    
    loadingWasSuccessFul = false;
    var ontologyContent = "";
    if ( ontologyMenu.cachedOntology(ontology) ) {
      ontologyMenu.append_bulletPoint("Loading already cached ontology: " + ontology);
      ontologyContent = ontologyMenu.cachedOntology(ontology);
      loadingWasSuccessFul = true; // cached Ontology should be true;
      loadingModule.showLoadingIndicator();
      parseOntologyContent(ontologyContent);
      
    } else {
      // read the file name
      
      var fileToRead = "./data/" + ontology + ".json";
      if ( f2r ) {
        fileToRead = f2r;
      } // overwrite the newOntology Index
      // read file
      d3.xhr(fileToRead, "application/json", function ( error, request ){
        var loadingSuccessful = !error;
        if ( loadingSuccessful ) {
          ontologyContent = request.responseText;
          parseOntologyContent(ontologyContent);
        } else {

          if (loadingNewOntologyForEditor){
            ontologyContent = '{\n' +
              '  "_comment": "Empty ontology for WebVOWL Editor",\n' +
              '  "header": {\n' +
              '    "languages": [\n' +
              '      "en"\n' +
              '    ],\n' +
              '    "baseIris": [\n' +
              '      "http://www.w3.org/2000/01/rdf-schema"\n' +
              '    ],\n' +
              '    "iri": "http://visualdataweb.org/newOntology/",\n' +
              '    "title": {\n' +
              '      "en": "New ontology"\n' +
              '    },\n' +
              '    "description": {\n' +
              '      "en": "New ontology description"\n' +
              '    }\n' +
              '  },\n' +
              '  "namespace": [],\n' +
              '  "metrics": {\n' +
              '    "classCount": 0,\n' +
              '    "datatypeCount": 0,\n' +
              '    "objectPropertyCount": 0,\n' +
              '    "datatypePropertyCount": 0,\n' +
              '    "propertyCount": 0,\n' +
              '    "nodeCount": 0,\n' +
              '    "individualCount": 0\n' +
              '  }\n' +
              '}\n';
            parseOntologyContent(ontologyContent);
          }else{
          // some error occurred
          ontologyMenu.append_bulletPoint("Failed to load: " + ontology);
          if (error.status===0){ // assumption this is CORS error when running locally (error status == 0)
            ontologyMenu.append_message_toLastBulletPoint(" <span style='color: red'>ERROR STATUS:</span> " + error.status);
            if (window.location.toString().startsWith("file:/")){
              ontologyMenu.append_message_toLastBulletPoint("<br><p>WebVOWL runs in a local instance.</p>");
              ontologyMenu.append_message_toLastBulletPoint("<p>CORS prevents to automatically load files on host system.</p>");
              ontologyMenu.append_message_toLastBulletPoint("<p>You can load preprocessed ontologies (i.e. VOWL-JSON files) using the upload feature in the ontology menu or by dragging the files and dropping them on the canvas.</p>");
              ontologyMenu.append_message_toLastBulletPoint("<p><i>Hint: </i>Note that the conversion of ontologies into the VOWL-JSON format is not part of WebVOWL but requires an additional converter such as OWL2VOWL.</p>");
              ontologyMenu.append_message_toLastBulletPoint("<p>Ontologies can be created using the editor mode (i.e. activate editing mode in <b>Modes</b> menu and create a new ontology using the <b>Ontology</b> menu.</p>");
            }
          }else {
            ontologyMenu.append_message_toLastBulletPoint(" <span style='color: red'>ERROR STATUS:</span> " + error.status);
          }



          graph.handleOnLoadingError();
          loadingModule.setErrorMode();
          }
        }
      });
    }
  }
  
  
  /** -- PARSE JSON CONTENT -- **/
  function parseOntologyContent( content ){
    
    ontologyMenu.append_bulletPoint("Reading ontology graph ... ");
    var _loader = ontologyMenu.getLoadingFunction();
    _loader(content, ontologyIdentifierFromURL, "noAlternativeNameYet");
  }
  
  loadingModule.notValidJsonFile = function (){
    graph.clearGraphData();
    ontologyMenu.append_message_toLastBulletPoint(" <span style='color:red;'>failed</span>");
    ontologyMenu.append_message_toLastBulletPoint("<br><span style='color:red;'>Error: Received empty graph</span>");
    loadingWasSuccessFul = false;
    graph.handleOnLoadingError();
    
  };
  
  loadingModule.validJsonFile = function (){
    ontologyMenu.append_message_toLastBulletPoint("done");
    loadingWasSuccessFul = true;
  };
  
  
  /** --- HELPER FUNCTIONS **/
  
  function identifyParameter( url ){
    var numParameters = (url.match(/#/g) || []).length;
    // create parameters array
    var paramArray = [];
    if ( numParameters > 0 ) {
      var tokens = url.split("#");
      // skip the first token since it is the address of the server
      for ( var i = 1; i < tokens.length; i++ ) {
        if ( tokens[i].length === 0 ) {
          // this token belongs actually to the last paramArray
          paramArray[paramArray.length - 1] = paramArray[paramArray.length - 1] + "#";
        } else {
          paramArray.push(tokens[i]);
        }
      }
    }
    return paramArray;
  }
  
  
  function loadGraphOptions( parameterArray ){
    var optString = "opts=";
    
    function loadDefaultConfig(){
      graph.options().setOptionsFromURL(graph.options().defaultConfig(), false);
    }
    
    function loadCustomConfig( opts ){
      var changeEditingFlag = false;
      var defObj = graph.options().defaultConfig();
      for ( var i = 0; i < opts.length; i++ ) {
        var keyVal = opts[i].split('=');
        if ( keyVal[0] === "editorMode" ) {
          changeEditingFlag = true;
        }
        defObj[keyVal[0]] = keyVal[1];
      }
      graph.options().setOptionsFromURL(defObj, changeEditingFlag);
    }
    
    function identifyOptions( paramArray ){
      if ( paramArray[0].indexOf(optString) >= 0 ) {
        // parse the parameters;
        var parameterLength = paramArray[0].length;
        var givenOptionsStr = paramArray[0].substr(5, parameterLength - 6);
        var optionsArray = givenOptionsStr.split(';');
        loadCustomConfig(optionsArray);
      } else {
        ontologyIdentifierFromURL = paramArray[0];
        loadDefaultConfig();
      }
    }
    
    function identifyOptionsAndOntology( paramArray ){
      
      if ( paramArray[0].indexOf(optString) >= 0 ) {
        // parse the parameters;
        var parameterLength = paramArray[0].length;
        var givenOptionsStr = paramArray[0].substr(5, parameterLength - 6);
        var optionsArray = givenOptionsStr.split(';');
        loadCustomConfig(optionsArray);
      } else {
        loadDefaultConfig();
      }
      ontologyIdentifierFromURL = paramArray[1];
    }
    
    switch ( parameterArray.length ) {
      case 0:
        loadDefaultConfig();
        break;
      case 1:
        identifyOptions(parameterArray);
        break;
      case 2:
        identifyOptionsAndOntology(parameterArray);
        break;
      default :
        console.log("To many input parameters , loading default config");
        loadDefaultConfig();
        ontologyIdentifierFromURL = "ERROR_TO_MANY_INPUT_PARAMETERS";
    }
  }
  
  
  function identifyOntologyLoadingMethod( url ){
    var iriKey = "iri=";
    var urlKey = "url=";
    var fileKey = "file=";
    
    var method = -1;
    if ( url.substr(0, fileKey.length) === fileKey ) {
      method = FILE_UPLOAD;
    } else if ( url.substr(0, urlKey.length) === urlKey ) {
      method = JSON_URL;
    } else if ( url.substr(0, iriKey.length) === iriKey ) {
      method = IRI_URL;
    } else {
      method = PREDEFINED;
    }
    return method;
  }
  
  return loadingModule;
}
;