Class: CPEE::Properties::PutDescription

Inherits:
Riddl::Implementation
  • Object
show all
Defined in:
lib/cpee/implementation_properties.rb

Overview

}}}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.set(id, opts, xml, exposition = [], copy = false) ⇒ Object

}}}



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
# File 'lib/cpee/implementation_properties.rb', line 801

def self::set(id,opts,xml,exposition=[],copy=false)
  dslx, dsl, de, ep = if copy
    PutDescription::transform(
      xml,
      '',
      'copy',
      '',
      'none',
      '',
      'none',
      CPEE::Persistence::extract_item(id,opts,'executionhandler'),
      id,
      opts
    )
  else
    PutDescription::transform(
      xml,
      CPEE::Persistence::extract_item(id,opts,'transformation/description'),
      CPEE::Persistence::extract_item(id,opts,'transformation/description/@type'),
      CPEE::Persistence::extract_item(id,opts,'transformation/dataelements'),
      CPEE::Persistence::extract_item(id,opts,'transformation/dataelements/@type'),
      CPEE::Persistence::extract_item(id,opts,'transformation/endpoints'),
      CPEE::Persistence::extract_item(id,opts,'transformation/endpoints/@type'),
      CPEE::Persistence::extract_item(id,opts,'executionhandler'),
      id,
      opts
    )
  end
  attrs = CPEE::Persistence::extract_list(id,opts,'attributes').to_h
  change_uuid = Digest::SHA1.hexdigest(dslx)
  CPEE::Persistence::set_item(id,opts,'description',
    :description => xml,
    :dslx => dslx,
    :change_uuid => change_uuid,
    :dsl => dsl,
    :dataelements => CPEE::Persistence::extract_list(id,opts,'dataelements').to_h,
    :endpoints => CPEE::Persistence::extract_list(id,opts,'endpoints').to_h,
    :attributes => attrs
  )
  PatchItems::set_hash('dataelements',id,opts,de) unless de.empty?
  PatchItems::set_hash('endpoints',id,opts,ep) unless ep.empty?
  exposition.each do |exp|
    content = {
      :change_uuid => change_uuid,
      :exposition => exp.value.read,
      :attributes => attrs
    }
    CPEE::Message::send(:event,'description/exposition',File.join(opts[:url],'/'),id,attrs['uuid'],attrs['info'],content,opts[:redis])
  end
end

.transform(descxml, tdesc, tdesctype, tdata, tdatatype, tendp, tendptype, hw, id, opts) ⇒ Object

{{{



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
# File 'lib/cpee/implementation_properties.rb', line 701

def self::transform(descxml,tdesc,tdesctype,tdata,tdatatype,tendp,tendptype,hw,id,opts) #{{{
  desc = XML::Smart::string(descxml) rescue nil
  if desc.nil?
    if descxml.empty?
      tdesctype = tdatatype = tendptype = 'clean'
    end
  else
    desc.register_namespace  'p', 'http://cpee.org/ns/description/1.0'
    if desc.root.children.empty?
      tdesctype = tdatatype = tendptype = 'clean'
    end
  end

  dslx = nil
  dsl = nil
  de = {}
  ep = {}

  ### endpoints extraction
  addit = if tendptype == 'rest' && !tendp.empty?
    srv = Riddl::Client.new(tendp)
    status, res = srv.post [
      desc.nil? ? Riddl::Parameter::Complex.new("description","text/plain",descxml) : Riddl::Parameter::Complex.new("description","text/xml",descxml),
      Riddl::Parameter::Simple.new("type","endpoints")
    ]
    if status >= 200 && status < 300
      res
    else
      raise 'Could not extract endpoints'
    end
  elsif tendptype == 'xslt' && !tendp.empty?
    trans = XML::Smart::open_unprotected(tendp.text)
    desc.transform_with(trans)
  elsif tendptype == 'clean'
    []
  else
    nil
  end
  unless addit.nil?
    addit.each_slice(2).each do |k,v|
      ep[k.value.to_sym] = v.value
    end
  end

  ### description transformation, including dslx to dsl
  addit = if tdesctype == 'copy' || tdesc.empty?
    desc || ''
  elsif tdesctype == 'rest' && !tdesc.empty?
    srv = Riddl::Client.new(tdesc)
    status, res = srv.post [
      desc.nil? ? Riddl::Parameter::Complex.new("description","text/plain",descxml) : Riddl::Parameter::Complex.new("description","text/xml",descxml),
      Riddl::Parameter::Simple.new("type","description")
    ]
    if status >= 200 && status < 300
      XML::Smart::string(res[0].value.read)
    else
      raise 'Could not extract dslx'
    end
  elsif tdesctype == 'xslt' && !tdesc.empty?
    trans = XML::Smart::open_unprotected(tdesc)
    desc.transform_with(trans)
  elsif tdesctype == 'clean'
    XML::Smart::open_unprotected(opts[:empty_dslx])
  else
    nil
  end
  unless addit.nil?
    dslx = addit.to_s
    dsl = Object.const_get('CPEE::ExecutionHandler::' + hw.capitalize)::dslx_to_dsl(addit,CPEE::Persistence::extract_list(id,opts,'endpoints').to_h.merge(ep))
  end

  ### dataelements extraction
  addit = if tdatatype == 'rest' && !tdata.empty?
    srv = Riddl::Client.new(tdata)
    status, res = srv.post [
      desc.nil? ? Riddl::Parameter::Complex.new("description","text/plain",descxml) : Riddl::Parameter::Complex.new("description","text/xml",descxml),
      Riddl::Parameter::Simple.new("type","dataelements")
    ]
    if status >= 200 && status < 300
      res
    else
      raise 'Could not extract dataelements'
    end
  elsif tdatatype == 'xslt' && !tdata.empty?
    trans = XML::Smart::open_unprotected(tdata)
    desc.transform_with(trans)
  elsif tdatatype == 'clean'
    []
  else
    nil
  end
  unless addit.nil?
    addit.each_slice(2).each do |k,v|
      de[k.value.to_sym] = v.value
    end
  end

  [dslx, dsl, de, ep]
end

Instance Method Details

#responseObject



852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
# File 'lib/cpee/implementation_properties.rb', line 852

def response
  id = @a[0]
  opts = @a[1]
  copy = @a[2]
  if opts[:statemachine].readonly? id
    @status = 423
  elsif opts[:statemachine].final? id
    @status = 410
  else
    begin
      # force-encoding because johannes managed to sneak in ascii special characters. why the browser is not sanitizing it is beyond me.
      PutDescription::set(id,opts,@p[0].value.read.force_encoding('UTF-8'),@p[1..-1],copy)
    rescue => e
      puts e.message
      puts e.backtrace
      @status = 400
    end
  end
  nil
end