def process_services
      # todo: persist mm_md5
      xml= @app||get_app
      if defined? session
        md5= Digest::MD5.hexdigest(xml.to_s)
        if session[:mm_md5]
          return if session[:mm_md5]==md5
        else
          session[:mm_md5]= md5
        end
      end
      protected_services = []
      protected_modules = []
      mseq= 0
      @services= xml.elements["//node[@TEXT='services']"] || REXML::Document.new
      @services.each_element('node') do |m|
        ss= m.attributes["TEXT"]
        code, name= ss.split(':', 2)
        next if code.blank?
        next if code.comment?
        module_code= code.to_code
        # create or update to GmaModule
        ma_module= Mindapp::Module.find_or_create_by :code=>module_code
        ma_module.update_attributes :uid=>ma_module.id.to_s
        protected_modules << ma_module.uid
        name = module_code if name.blank?
        ma_module.update_attributes :name=> name.strip, :seq=> mseq
        mseq += 1
        seq= 0
        m.each_element('node') do |s|
          service_name= s.attributes["TEXT"].to_s
          scode, sname= service_name.split(':', 2)
          sname ||= scode; sname.strip!
          scode= scode.to_code
          if scode=="role"
            ma_module.update_attribute :role, sname
            next
          end
          if scode.downcase=="link"
            role= get_option_xml("role", s) || ""
            rule= get_option_xml("rule", s) || ""
            ma_service= Mindapp::Service.find_or_create_by :module_code=> ma_module.code, :code=> scode, :name=> sname
            ma_service.update_attributes :xml=>s.to_s, :name=>sname,
              :list=>listed(s), :ma_secured=>ma_secured?(s),
              :module_id=>ma_module.id, :seq => seq,
              :confirm=> get_option_xml("confirm", xml),
              :role => role, :rule => rule, :uid=> ma_service.id.to_s
            seq += 1
            protected_services << ma_service.uid
          else
            # normal service
            step1 = s.elements['node']
            role= get_option_xml("role", step1) || ""
            rule= get_option_xml("rule", step1) || ""
            ma_service= Mindapp::Service.find_or_create_by :module_code=> ma_module.code, :code=> scode
            ma_service.update_attributes :xml=>s.to_s, :name=>sname,
              :list=>listed(s), :ma_secured=>ma_secured?(s),
              :module_id=>ma_module.id, :seq => seq,
              :confirm=> get_option_xml("confirm", xml),
              :role => role, :rule => rule, :uid=> ma_service.id.to_s
            seq += 1
            protected_services << ma_service.uid
          end
        end
      end
      Mindapp::Module.not_in(:uid=>protected_modules).delete_all
      Mindapp::Service.not_in(:uid=>protected_services).delete_all
    end
def process_services
      # todo: persist mm_md5
      xml= @app||get_app
      if defined? session
        md5= Digest::MD5.hexdigest(xml.to_s)
        if session[:mm_md5]
          return if session[:mm_md5]==md5
        else
          session[:mm_md5]= md5
        end
      end
      protected_services = []
      protected_modules = []
      mseq= 0
      @services= xml.elements["//node[@TEXT='services']"] || REXML::Document.new
      @services.each_element('node') do |m|
        ss= m.attributes["TEXT"]
        code, name= ss.split(':', 2)
        next if code.blank?
        next if code.comment?
        module_code= code.to_code
        # create or update to GmaModule
        ma_module= Mindapp::Module.find_or_create_by :code=>module_code
        ma_module.update_attributes :uid=>ma_module.id.to_s
        protected_modules << ma_module.uid
        name = module_code if name.blank?
        ma_module.update_attributes :name=> name.strip, :seq=> mseq
        mseq += 1
        seq= 0
        m.each_element('node') do |s|
          service_name= s.attributes["TEXT"].to_s
          scode, sname= service_name.split(':', 2)
          sname ||= scode; sname.strip!
          scode= scode.to_code
          if scode=="role"
            ma_module.update_attribute :role, sname
            next
          end
          if scode.downcase=="link"
            role= get_option_xml("role", s) || ""
            rule= get_option_xml("rule", s) || ""
            ma_service= Mindapp::Service.find_or_create_by :module_code=> ma_module.code, :code=> scode, :name=> sname
            ma_service.update_attributes :xml=>s.to_s, :name=>sname,
              :list=>listed(s), :ma_secured=>ma_secured?(s),
              :module_id=>ma_module.id, :seq => seq,
              :confirm=> get_option_xml("confirm", xml),
              :role => role, :rule => rule, :uid=> ma_service.id.to_s
            seq += 1
            protected_services << ma_service.uid
          else
            # normal service
            step1 = s.elements['node']
            role= get_option_xml("role", step1) || ""
            rule= get_option_xml("rule", step1) || ""
            ma_service= Mindapp::Service.find_or_create_by :module_code=> ma_module.code, :code=> scode
            ma_service.update_attributes :xml=>s.to_s, :name=>sname,
              :list=>listed(s), :ma_secured=>ma_secured?(s),
              :module_id=>ma_module.id, :seq => seq,
              :confirm=> get_option_xml("confirm", xml),
              :role => role, :rule => rule, :uid=> ma_service.id.to_s
            seq += 1
            protected_services << ma_service.uid
          end
        end
      end
      Mindapp::Module.not_in(:uid=>protected_modules).delete_all
      Mindapp::Service.not_in(:uid=>protected_services).delete_all
    end
def handle_ma_notice
   if Mindapp::Notice.recent.count>0
     notice= Mindapp::Notice.recent.last
     notice.update_attribute :unread, false
     "<script>notice('#{notice.message}');</script>"
   else
     ""
   end
 end