Module: WorkflowREST

Defined in:
lib/rbbt/workflow/rest.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.workflowsObject

Returns the value of attribute workflows.



20
21
22
# File 'lib/rbbt/workflow/rest.rb', line 20

def workflows
  @workflows
end

Class Method Details

.add_task(workflow, task_name, type = nil) ⇒ Object



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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
# File 'lib/rbbt/workflow/rest.rb', line 512

def self.add_task(workflow, task_name, type = nil)
  type ||= :synchronous
  task_name = task_name.to_s
  workflow_name = workflow.to_s

  # context info
  delete File.join("/context", workflow_name, ":task") do
    task, jobname = params.values_at :task
    del_context_job(workflow, task)
    true
  end

  post File.join("/context", workflow_name, ":task", ":jobname") do
    task, jobname = params.values_at :task, :jobname
    add_context_job(workflow, task, jobname)
    true
  end

  # task info
  get File.join("/", workflow_name, task_name, 'info') do

    visualization_parameters = get_visualization_parameters(params)
    task_info = workflow.task_info(task_name)

    case
    when visualization_parameters[:_format] == "json"
      content_type "application/json"
      task_info.to_json
    when visualization_parameters[:_format] == "raw"
      content_type "text/plain"
      task_info.collect{|k,v| [k,v] * "\t"} * "\n"
    when visualization_parameters[:_format] == "html"
      workflow_render("task_info", workflow, task_name, workflow.task_info(task_name).merge(visualization_parameters).merge(:task_info => task_info))
    end
  end

  # task jobs
  get File.join("/", workflow_name, task_name, 'jobs') do
    visualization_parameters = get_visualization_parameters(params)
    task_info = workflow.task_info(task_name)

    authorize!

    job_list = workflow.jobs(task_name, user + "/").collect{|jobname| File.basename(jobname)}

    workflow_render("job_list", workflow, task_name, visualization_parameters.merge(:workflow => workflow, :task_name => task_name, :job_list => job_list))
  end


  # task form or exec
  get File.join("/", workflow_name, task_name) do

    visualization_parameters = get_visualization_parameters(params)
    inputs = get_inputs(workflow, task_name, params)

    case
      # direct exection when job has all paramteres
    when (type == :exec and has_all_inputs(workflow, task_name, inputs))
      result = workflow.job(task_name, "EXEC", inputs.dup).exec
      result = paginate(result, params[:_page]) if params.include? :_page

      workflow_render_result(result, workflow, task_name, visualization_parameters.merge(:inputs => inputs))

      # direct job issuing and redirection when job has all paramteres
    when has_all_inputs(workflow, task_name, inputs)
      params[:jobname] = "WWW" unless params.include? :jobname or params.include? "jobname"

      jobname = get_jobname(params)
      real_jobname = authorized? ? File.join(user, jobname) : jobname

      job = workflow.job(task_name, real_jobname, inputs.dup)

      if type.to_sym == :synchronous
        job.run
      else
        job.fork
      end

      redirection = to(File.join("/", workflow.to_s, task_name, File.basename(job.path)))

      redirection = add_param(redirection, :_format, visualization_parameters[:_format]) if visualization_parameters[:_format] != "html"
      redirection = add_param(redirection, :_page, visualization_parameters[:_page]) if visualization_parameters[:_page] != nil
      redirection = add_param(redirection, :_size, visualization_parameters[:_size]) if visualization_parameters[:_size] != nil
      redirection = add_param(redirection, :_layout, visualization_parameters[:_layout]) unless visualization_parameters[:_layout]

      redirect redirection
    else
      workflow_render("form", workflow, task_name, workflow.task_info(task_name).merge(visualization_parameters))
    end
  end

  # job for task form post
  post File.join("/", workflow_name, task_name) do
    jobname = get_jobname(params)
    real_jobname = authorized? ? File.join(user, jobname) : jobname

    visualization_parameters = get_visualization_parameters(params)
    inputs = get_inputs(workflow, task_name, params)

    case type.to_sym

    when :exec
      result = workflow.job(task_name, real_jobname, inputs.dup).exec
      workflow_render_result(result, workflow, task_name, visualization_parameters.merge(:inputs => inputs).merge(workflow.task_info(task_name)))

    when :synchronous, :asynchronous
      job = workflow.job(task_name, real_jobname, inputs)

      if type.to_sym == :synchronous
        job.run
      else
        email = get_email(params)
        job.fork
        if can_send_email? and not (email.nil? or email.empty?) and valid_email?(email)
          Log.debug "Monitoring #{job.name} completion for email notification"
          Process.fork do
            while not job.done?
              sleep 5
            end
            case
            when job.error?
              send_email(workflow, email, "Job #{job.name} failed", job.messages.last)
            else
              send_email(workflow, email, "Job #{job.name} succeded", "")
            end
          end
        end
      end

      return job.name if visualization_parameters[:_format] == "json"

      redirection = to(File.join("/", workflow.to_s, task_name, job.name.sub(/.*\//,'')))

      redirection = add_param(redirection, :_format, visualization_parameters[:_format]) if visualization_parameters[:_format] != "html"
      redirection = add_param(redirection, :_page, visualization_parameters[:_page]) if visualization_parameters[:_page] != nil
      redirection = add_param(redirection, :_size, visualization_parameters[:_size]) if visualization_parameters[:_size] != nil
      redirection = add_param(redirection, :_layout, visualization_parameters[:_layout]) unless visualization_parameters[:_layout]

      redirect redirection
    end
  end

  # job results
  get File.join("/", workflow.to_s, task_name, ":jobname") do
    jobname = get_jobname(params)

    real_jobname = authorized? ? File.join(user, jobname) : jobname

    visualization_parameters = get_visualization_parameters(params)
    inputs = get_inputs(workflow, task_name, params)

    session[:jobname] = jobname

    step = nil
    [real_jobname, jobname].uniq.each do |jname|
      step = workflow.load_id(File.join(task_name, jname))
      break if File.exists? step.info_file
    end

    case

      # ERROR
    when step.error?

      if visualization_parameters[:_format] == "html"
        content_type "text/html"
        status 500
        return workflow_render("step_error", workflow, task_name, workflow.task_info(task_name).merge(visualization_parameters).merge(:jobname => jobname, :step => step, :inputs => step.info[:inputs]))
      else
        status 500
        content_type "text/plain"
        step.messages.last
      end

      # NOT DONE
    when (not step.done?)

      if visualization_parameters[:_format] == "html"
        content_type "text/html"
        status 202
        if request.xhr?
          visualization_parameters[:_layout] = false
          return workflow_render("wait", workflow, task_name, workflow.task_info(task_name).merge(visualization_parameters).merge(:jobname => jobname, :step => step, :inputs => step.info[:inputs]))
        else
          @rbbt_refresh = 30 unless step.error?
          return workflow_render("wait", workflow, task_name, workflow.task_info(task_name).merge(visualization_parameters).merge(:jobname => jobname, :step => step, :inputs => step.info[:inputs]))
        end
      else
        status 202
        step.status
      end

      # DONE
    else

      cache("Job Result:", 
            :visualization_parameters => visualization_parameters,
            :workflow => workflow.to_s,
            :task_name => task_name,
            :jobname => jobname,
            :user => user
           ) do
             case
             when visualization_parameters[:_format].to_s == "excel"
               require 'rbbt/tsv/excel'
               data = nil
               excel_file = TmpFile.tmp_file
               result = step.load
               result.excel(excel_file, :name => params[:_excel_use_name],:sort_by => params[:_excel_sort_by], :sort_by_cast => params[:_excel_sort_by_cast])
               send_file excel_file, :type => 'application/vnd.ms-excel', :filename => step.clean_name + '.xls'
             when visualization_parameters[:_format] == "html"
               result = step.load
               workflow_render_result(result, workflow, task_name, workflow.task_info(task_name).merge(visualization_parameters).merge(:jobname => jobname, :step => step, :inputs => step.info[:inputs], :result => result))

             when visualization_parameters[:_format].to_s == "json"
               result = step.load
               result = paginate(result, visualization_parameters[:_page]) if visualization_parameters.include? "_page"

               content_type "application/json"

               case
               when TSV === result
                 result.dup.to_json
               when workflow.task_info(task_name)[:result_type] == :annotations
                 Annotated.json(result, true)
               else
                 result.to_json
               end

             when visualization_parameters[:_format].to_s == "raw"
               content_type "text/plain"
               result = step.load

               case
               when workflow.task_info(task_name)[:result_type] == :binary
                 send_file step.path
               when workflow.task_info(task_name)[:result_type] == :annotations
                 Annotated.tsv(result, :all).to_s
               when Array === result
                 result * "\n"
               else
                 result.to_s
               end

             else
               content_type "text/plain"
               result = step.load
               result = paginate(result, visualization_parameters[:_page]) if visualization_parameters.include? "_page"
               case
               when Array === result
                 result * "\n"
               else
                 result.to_s
               end
             end

           end
    end
  end

  # job info
  get File.join("/", workflow.to_s, task_name, ":jobname", 'info') do
    jobname = get_jobname(params)
    real_jobname = authorized? ? File.join(user, jobname) : jobname

    visualization_parameters = get_visualization_parameters(params)

    step = workflow.load_id(File.join(task_name, real_jobname))

    info = step.info

    case
    when visualization_parameters[:_format] == "json"
      content_type "application/json"
      info.to_json
    when visualization_parameters[:_format] == "raw"
      info.collect{|k,v| [k,v] * "\t"} * "\n"
    when visualization_parameters[:_format] == "html"
      workflow_render("info", workflow, task_name, visualization_parameters.merge(:info => info))
    end
  end

  # job dependencies
  get File.join("/", workflow.to_s, task_name, ":jobname", 'dependencies') do
    jobname = get_jobname(params)
    real_jobname = authorized? ? File.join(user, jobname) : jobname

    visualization_parameters = get_visualization_parameters(params)

    step = workflow.load_id(File.join(task_name, real_jobname))

    dependencies = step.info[:dependencies]

    case
    when visualization_parameters[:_format] == "json"
      content_type "application/json"
      depencencies.to_json
    when visualization_parameters[:_format] == "raw"
      dependencies.collect{|task, job| [task, job] * "\t"} * "\n"
    when visualization_parameters[:_format] == "html"
      workflow_render("dependencies", workflow, task_name, visualization_parameters.merge(:dependencies => dependencies, :workflow => workflow, :task_name => task_name, :jobname => jobname))
    end
  end

  # job files
  get File.join("/", workflow.to_s, task_name, ":jobname", 'files') do
    jobname = get_jobname(params)
    real_jobname = authorized? ? File.join(user, jobname) : jobname

    visualization_parameters = get_visualization_parameters(params)

    step = workflow.load_id(File.join(task_name, real_jobname))

    files = step.files

    case
    when visualization_parameters[:_format] == "json"
      content_type "application/json"
      files.to_json
    when visualization_parameters[:_format] == "raw"
      files * "\n"
    when visualization_parameters[:_format] == "html"
      workflow_render("files", workflow, task_name, visualization_parameters.merge(:files => files, :workflow => workflow, :task_name => task_name, :jobname => jobname))
    end
  end

  # job file
  get File.join("/", workflow.to_s, task_name, ":jobname", 'file', '*') do
    jobname = get_jobname(params)
    real_jobname = authorized? ? File.join(user, jobname) : jobname

    visualization_parameters = get_visualization_parameters(params)

    step = workflow.load_id(File.join(task_name, real_jobname))

    filename = params[:splat].first
    filepath = step.file(filename)

    file = step.load_file(filename)

    case
    when visualization_parameters[:_format] == "json"
      content_type "application/json"
      file.to_json
    else
      begin
        content_type `file --mime -b #{filepath}`
      rescue
        if filepath =~ /.xlsx?/
          content_type "application/vnd.ms-excel"
        end
      end

      case
      when Array === file
        file * "\n"
      when TSV === file
        file.to_s
      when Hash === file
        file.collect{|*p| p * "\t"} * "\n"
      else
        file.to_s
      end
    end
  end

  # delete job
  delete File.join("/", workflow.to_s, task_name, ":jobname") do
    jobname = get_jobname(params)
    real_jobname = authorized? ? File.join(user, jobname) : jobname

    visualization_parameters = get_visualization_parameters(params)
    step = workflow.load_id(File.join(task_name, real_jobname))

    step.clean
  end
end

.add_workflows(*workflows) ⇒ Object



891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
# File 'lib/rbbt/workflow/rest.rb', line 891

def self.add_workflows(*workflows)
  self.workflows.concat workflows.select{|wf| Module === wf}
  self.workflows.uniq!
  
  self.workflows.each do |workflow|

    get File.join('/', workflow.to_s) do
      visualization_parameters = get_visualization_parameters(params)

      tasks = workflow.exec_exports + workflow.synchronous_exports + workflow.asynchronous_exports
      if visualization_parameters[:_format] and visualization_parameters[:_format].to_sym == :json
        tasks.to_json
      else
        workflow_render("task_list", workflow, nil, visualization_parameters.merge(:workflow => workflow, :tasks => tasks))
      end
    end

    get File.join("/plugins", workflow.to_s, "*") do
      file = params[:splat].first
      path = RbbtHTMLHelpers.locate_file(workflow, nil, 'plugins/' + file)
      send_file path
    end

    workflow.asynchronous_exports.each do |name|
      WorkflowREST.add_task workflow, name, :asynchronous
    end

    workflow.synchronous_exports.each do |name|
      WorkflowREST.add_task workflow, name, :synchronous
    end

    workflow.exec_exports.each do |name|
      WorkflowREST.add_task workflow, name, :exec
    end

    if workflow.libdir.lib["sinatra.rb"].exists?
      require workflow.libdir.lib["sinatra.rb"].find
    end
  end

  get '/' do
    visualization_parameters = get_visualization_parameters(params)
    workflow_render("front", visualization_parameters.merge(:workflows => WorkflowREST.workflows.select{|w| Module === w}))
  end

end

.setupObject



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
# File 'lib/rbbt/workflow/rest.rb', line 25

def self.setup
  configure do
    Compass.add_project_configuration(Rbbt.share.views.compass["compass.config"].find.dup.to_s)
  end

  Sinatra.module_eval do

    helpers Sinatra::SinatraRbbt

    use Rack::Deflater

    enable :static

    # This would make random secrets that would make the server hang after reload by old cookies. 
    # enable :sessions 
    # Util fix...
    use Rack::Session::Cookie, :key => 'rack.session',
      :path => '/',
      :expire_after => 2592000,
      :secret => 'rbbt_default_secret. Please CHANGE! ' + WorkflowREST.workflows.collect{|w| w.to_s} * ", "

    set :root, ENV["PWD"]
    set :cache_dir, "var/cache" unless settings.respond_to? :cache_dir and settings.cache_dir
    set :persistence_dir, "var/cache/persistence" unless settings.respond_to? :persistence_dir and settings.persistence_dir
    set :file_dir, "var/files" unless settings.respond_to? :file_dir and settings.file_dir
    set :note_dir, "var/notes" unless settings.respond_to? :note_dir and settings.note_dir
    set :user_action_defaults, "var/user_defaults" unless settings.respond_to? :user_action_defaults and settings.user_action_defaults

    # To allow crossorigin with json
    # http://trevorpower.com/blog/faiiure-to-access-json-on-a-remote-domain
    set :protection, :except => :json_csrf

    before do
      @file_cache = {}
      @result_cache = {}
      @js_files = []
      @css_files = []

      headers "AJAX-URL" => request.env["REQUEST_URI"]

      params.delete "_"
      params.delete :_
      cache_control :public, :must_revalidate, :max_age => 60

      # CrossOrigin
      headers 'Access-Control-Allow-Origin' => request.env['HTTP_ORIGIN'],
        'Access-Control-Allow-Methods' => %w(GET POST OPTIONS),
        'Access-Control-Allow-Credentials' => "true",
        'Access-Control-Max-Age' => "1728000"
    end

    after do
      @file_cache.clear if @file_cache
      @result_cache.clear if @result_cache
    end

    helpers do
      def authorization_realm
        "Workflow Explorer"
      end

      def check_user(user)
        
        request.env['REMOTE_USER'] == user || throw(:halt, [ 401, 'Authorization Required' ])
      end
    end

  end

  set :public_folder, RbbtHTMLHelpers.locate_file(nil, nil, 'public/').find

  begin
    set :users, RbbtHTMLHelpers.locate_file(nil, nil, 'config/users').yaml
  rescue
    if Rbbt.etc.web_users.exists?
      set :users, Rbbt.etc.web_users.yaml
    else
      set :users, {}
    end
  end

  followed_users = {}
  all_users = settings.users.keys
  all_users.each do |user|
    followed_users[user] = all_users - [user]
  end
  set :followed_users, followed_users

  error do
    error = request.env['sinatra.error']
    workflow_render('error', :message => error.message)
  end

  #{{{ Files, CSS, and JS

  get '/files/:filename' do
    cache_control :public, :max_age => 36000 if production?
    file = File.join(settings.file_dir, params[:filename])
    send_file file
  end

  get '/stylesheets/entity/:type/:entity/:name.css' do

    entity_type, format = params["type"].split ":"
    entity = setup_entity(entity_type, format, params["entity"])

    file = locate_file(WorkflowREST.workflows.last, nil, File.join("compass", params[:name] + '.sass'), entity)

    content_type 'text/css', :charset => 'utf-8'
    cache_control :public, :max_age => 36000 if production?
    cache_type = production? ? :sync : :none
    cache('css', params.merge(:cache_type => cache_type)) do
      renderer = Sass::Engine.new(file.read, :filename => file.find)
      css_text = renderer.render
      YUI::CssCompressor.new.compress(css_text)
    end
  end

  get '/stylesheets/:name.css' do
    file = locate_file(WorkflowREST.workflows.last, nil, File.join("compass", params[:name] + '.sass'))

    content_type 'text/css', :charset => 'utf-8'
    cache_control :public, :max_age => 36000 if production?
    cache_type = production? ? :sync : :none
    cache('css', params.merge(:cache_type => cache_type)) do
      renderer = Sass::Engine.new(file.read, :filename => file.find)
      css_text = renderer.render
      YUI::CssCompressor.new.compress(css_text)
    end
  end

  get '/js/entity/:type/:entity/:name.js' do
    content_type 'text/css', :charset => 'utf-8'
    cache_control :public, :max_age => 36000 if production?

    entity_type, format = params["type"].split ":"
    entity = setup_entity(entity_type, format, params["entity"])

    send_file locate_file(WorkflowREST.workflows.last, nil, File.join("js", params[:name] + '.js'), entity)
  end

  get '/js/:name.js' do
    content_type 'text/css', :charset => 'utf-8'
    cache_control :public, :max_age => 36000 if production?
    send_file locate_file(WorkflowREST.workflows.last, nil, File.join("js", params[:name] + '.js')).find
  end

  #{{{ entity
  
  get '/entity/:entity_type/:entity' do
    params.delete "captures"
    params.delete "splat"
    entity_type = params[:entity_type]

    visualization_parameters = get_visualization_parameters(params)

    cache_type = params.delete(:_cache_type) || params.delete("_cache_type") || :async
    update = params.delete(:_update) || params.delete("_update") || nil

    cache("Entity [#{ params[:entity] }]:", :update => update, :cache_type => cache_type, :params => params, :visualization_params => visualization_parameters, :user => user) do
      entity_render(entity_type, params.merge(visualization_parameters))
    end
  end

  #{{{ entity_list
  
  get '/entity_list/:user' do
    params.delete "captures"
    params.delete "splat"
    list_user = params.delete(:user) || params.delete("user")

    list_user = user if list_user.to_s == "user"

    if list_user != "public"
      raise "Not allowed to view entity lists for: #{ user }" unless authorized? and user.to_s == list_user
    end

    lists = Entity::REST.list_files(list_user)
    workflow_render('user_lists', params.merge(:lists => lists))
  end

  get '/entity_list/:entity_type/:id' do
    params.delete "captures"
    params.delete "splat"
    entity_type = params.delete "entity_type"
    visualization_parameters = get_visualization_parameters(params)
    list_id = params[:id] = CGI.unescape(params[:id])

    cache_type = params.delete(:_cache_type) || params.delete("_cache_type") || :async
    update = params.delete(:_update) || params.delete("_update") || nil

    check = Entity::REST.list_file(entity_type, list_id, user)
    check = Entity::REST.list_file(entity_type, list_id, :public) if check.nil? or not File.exists? check
    check = Entity::REST.list_file(entity_type, list_id) if check.nil? or not File.exists? check

    raise "List of type #{ entity_type } not found for #{list_id}: #{ check.inspect }" if check.nil? or not File.exists?(check)

    content_type "text/plain" if visualization_parameters[:_format] == 'literal' or visualization_parameters[:_format] == 'tsv' or visualization_parameters[:_format] == 'bed'
    content_type "application/json" if visualization_parameters[:_format] == 'json'

    cache("Entity List:", :check => [check], :update => update, :cache_type => cache_type, :params => params, :entity_type => entity_type, :visualization_params => visualization_parameters, :user => user) do
      case (visualization_parameters[:_format] || 'html').to_s
      when  'json'
        list = Entity::REST.load_list(entity_type, list_id, user)
        list.collect{|elem| [elem.clean_annotations, elem.info.merge(:id => elem.id)]}.to_json
      when  'bed'
        list = Entity::REST.load_list(entity_type, list_id, user)
        type = entity_type.split(":").first
        case type
        when "GenomicMutation"
          list.collect{|elem| ['chr' + elem.chromosome, elem.position, elem.position+1] * "\t"} * "\n"
        else
          raise "Don't know how to produce bed file for entity type: #{ entity_type }"
        end
      when 'tsv'
        list = Entity::REST.load_list(entity_type, list_id, user)
        Annotated.tsv(list, :all).to_s
      when 'literal'
        list = Entity::REST.load_list(entity_type, list_id, user)
        list * "\n"
      else
        entity_list_render(entity_type, params.merge(visualization_parameters))
      end
    end
  end

  post '/entity_list/:entity_type/create/:id' do
    params.delete "captures"
    params.delete "splat"

    list_id = params.delete("id")
    list_id = CGI.unescape(list_id)

    entity_type = params.delete "entity_type"
    entities = params.delete("entities")

    raise "No entities" unless (entities and not entities.empty?)

    entities = entities.split(/[,;|]/)

    options = IndiferentHash.setup(params.dup)
    entities = Misc.prepare_entity(entities, entity_type, options)

    Entity::REST.save_list(entity_type, list_id, entities, user)

    redirect "/entity_list/#{ entity_type }/#{list_id}"
  end

  post '/entity_list/:entity_type/copy/:id' do
    params.delete "captures"
    params.delete "splat"
    entity_type = params.delete "entity_type"

    list_id = params[:id] = CGI.unescape(params[:id])
    list = Entity::REST.load_list(entity_type, list_id, user)
    Entity::REST.save_list(entity_type, params[:new_id], list, user)

    redirect "/entity_list/#{ entity_type }/#{params[:new_id]}"
  end

  post '/entity_list/:entity_type/add/:id' do
    params.delete "captures"
    params.delete "splat"
    entity_type = params.delete "entity_type"

    list_id = params[:id] = CGI.unescape(params[:id])
    list = Entity::REST.load_list(entity_type, list_id, user)
    other_list = Entity::REST.load_list(entity_type, params[:other_list], user)
    list.concat other_list
    list.uniq!
    Entity::REST.save_list(entity_type, list_id, list, user)

    redirect back
  end

  post '/entity_list/:entity_type/remove/:id' do
    params.delete "captures"
    params.delete "splat"
    entity_type = params.delete "entity_type"

    list_id = params[:id] = CGI.unescape(params[:id])
    list = Entity::REST.load_list(entity_type, list_id, user)
    other_list = Entity::REST.load_list(entity_type, params[:other_list], user)
    Entity::REST.save_list(entity_type, list_id, list.remove(other_list), user)

    redirect back
  end

  post '/entity_list/:entity_type/subset/:id' do
    params.delete "captures"
    params.delete "splat"
    entity_type = params.delete "entity_type"

    list_id = params[:id] = CGI.unescape(params[:id])
    list = Entity::REST.load_list(entity_type, list_id, user)
    other_list = Entity::REST.load_list(entity_type, params[:other_list], user)
    Entity::REST.save_list(entity_type, list_id, list.subset(other_list), user)

    redirect back
  end

  get '/entity_list/:entity_type/edit/:id' do
    params.delete "captures"
    params.delete "splat"
    entity_type = params[:entity_type]
    visualization_parameters = get_visualization_parameters(params)

    workflow = WorkflowREST.workflows.last
    list_id = params[:id] = CGI.unescape(params[:id])
    id = list_id
    list = Entity::REST.load_list(entity_type, id, user)
    workflow_render('edit_list', workflow, params.merge(:list => list))
  end

  delete '/entity_list/:entity_type/:id' do
    params.delete "captures"
    params.delete "splat"
    list_id = params[:id] = CGI.unescape(params[:id])
    Entity::REST.delete_list(params[:entity_type], list_id, user)
    redirect '/'
  end
 
  delete '/entity_list/:entity_type/:entity/:id' do
    params.delete "captures"
    params.delete "splat"
    id = params[:id] = CGI.unescape(params[:id])
    Log.debug "Deleting #{params[:entity] } from #{id}"

    list = Entity::REST.load_list(params[:entity_type], id, user)

    list.delete params[:entity]

    Entity::REST.save_list(params[:entity_type], id, list, user)

    redirect back
  end

  post '/entity_list/:entity_type/:entity/:list_id' do
    params.delete "captures"
    params.delete "splat"
    list_id       = params.delete(:list_id) || params.delete("list_id")
    entity        = params.delete(:entity) || params.delete("entity")
    entity_type   = params.delete(:entity_type) || params.delete("entity_type")

    entity_type, entity_format = entity_type.split(":")
    info = {:annotation_types => entity_type, :format => entity_format}
    params.each{|k,v| info[k] = begin JSON.parse(v) rescue v end}
    entity = Annotated.load_tsv_values(entity, [[info.to_json]], "JSON")
    entity.id = params[:entity_id] if params.include? :entity_id

    list = 
      begin
        Entity::REST.load_list(entity_type, list_id, user) || []
      rescue
        []
      end

    
    different = Annotated === list ? entity.info.select{|k,v| not v.nil? and v != list.info[k]}.any? : true

    if different
      new = list.collect{|e| e}
    else
      new = list
    end

    new = new.reject{|e| e.id == entity.id}
    new << entity


    new.first.annotate new if new.collect{|e| e.info}.uniq.length == 1

    Entity::REST.save_list(entity_type, list_id, new, user)

    redirect back unless request.xhr?
  end

  #{{{ entity_action

  get '/entity_action/:entity_type/:action/:entity' do
    params.delete "captures"
    params.delete "splat"
    entity_type = params.delete "entity_type"
    action = params.delete "action" 

    visualization_parameters = get_visualization_parameters(params)

    cache_type = params.delete(:_cache_type) || params.delete("_cache_type") || :async
    update = params.delete(:_update) || params.delete("_update") || nil

    add_user_defaults(user, entity_type, action, params) if authorized?

    html = cache("Entity Action [#{action}][#{ params[:entity] }]", :update => update, :cache_type => cache_type, :params => params, :entity_type => entity_type, :action => action, :visualization_params => visualization_parameters, :user => user) do
      entity_action_render(entity_type, action, params.merge(visualization_parameters))
    end

    save_user_defaults(user, entity_type, action, params) if authorized? and response.status == 200
    clear_user_defaults(user, entity_type, action) if authorized? and response.status == 500

    html
  end

  get '/entity_list_action/:entity_type/:action/:id' do
    params.delete "captures"
    params.delete "splat"
    entity_type = params.delete "entity_type"
    action = params.delete "action" 

    visualization_parameters = get_visualization_parameters(params)

    cache_type = params.delete(:_cache_type) || params.delete("_cache_type") || :async
    update = params.delete(:_update) || params.delete("_update") || nil

    add_user_defaults(user, entity_type, action, params) if authorized?

    list_id = params[:id] = params[:id] = CGI.unescape(params[:id])
    html = cache("Entity List Action [#{action}] [#{ list_id}]", :update => update, :cache_type => cache_type, :params => params, :entity_type => entity_type, :action => action, :visualization_params => visualization_parameters, :user => user) do
      entity_list_action_render(entity_type, action, params.merge(visualization_parameters))
    end

    save_user_defaults(user, entity_type, action, params) if authorized? and response.status == 200
    clear_user_defaults(user, entity_type, action) if authorized? and response.status == 500

    html
  end

  #{{{ Notes

  get '/notes/edit/*' do
    params.delete "captures"
    path = params.delete("splat").last
    markup = get_notes_markup(path, params, user)
    workflow_render('notes', StudyExplorer, nil, :markup => markup)
  end

  post '/notes/edit/*' do
    params.delete "captures"
    path = params.delete("splat").last
    path = path.sub("/notes/edit", "/notes")
    markup = params.delete('markup')
    save_notes(path, params, markup, user)
    redirect request.env["REQUEST_URI"].sub('/notes/edit', '/notes')
  end

  post '/notes/share/*' do
    params.delete "captures"
    path = params.delete("splat").last
    path = path.sub("/notes/edit", "/notes")
    markup = params.delete('markup')
    params.delete("_method")
    share_notes(path, params, markup, user)
    redirect back unless request.xhr?
  end

  post '/notes/unshare/*' do
    params.delete "captures"
    path = params.delete("splat").last
    path = path.sub("/notes/edit", "/notes")
    markup = params.delete('markup')
    params.delete("_method")
    unshare_notes(path, params, markup, user)
    redirect back unless request.xhr?
  end


  delete '/notes/*' do
    params.delete "captures"
    path = params.delete("splat").last
    params.delete("_method")
    markup = delete_notes_markup(path, params, user)
    redirect File.join("/", path)
  end

  get '/notes/shared/:user/*' do
    params.delete "captures"
    path = params.delete("splat").last
    sharing_user = params.delete('user')
    render_notes(path, params, sharing_user, true)
  end

  get '/notes/*' do
    params.delete "captures"
    path = params.delete("splat").last
    render_notes(path, params, user)
  end
end