Class Core::Providers::DataStore::Provider
In: core/serviceproviders/data_store.rb
Parent: Object

This provider interfaces with the server to store/retrieve resources (Notes and Nodes)

Methods

Included Modules

Interfaces::ServiceProvider

Public Class methods

=============================================== public methods

[Source]

# File core/serviceproviders/data_store.rb, line 31
        def initialize(params={})
          @knowledge = Knowledge.new
          if params.key?(:controller)
            # TODO: add some checks over the controller to see if it 
            # implements the interface
            @controller = params[:controller]
          end
        end

Public Instance methods

Store a new Node in the server. The expected parameters are:

  • parent_id: The id of the parent Node associated to this one.
  • type_id: Type of the node.
  • label: The text to be displayed in the nodes tree.

All parameters are required.

[Source]

# File core/serviceproviders/data_store.rb, line 161
        def service_model_add_category(*args)
          if (args.size != 1)
            raise ServiceRequestInvalidArgumentError.new('Invalid parameters. Required parameters: name')
          end
          name = args[0]
          c = Category.new(:name => name)
          c.save
          c
        end

Store a new Node in the server. The expected parameters are:

  • parent_id: The id of the parent Node associated to this one.
  • type_id: Type of the node.
  • label: The text to be displayed in the nodes tree.

All parameters are required.

[Source]

# File core/serviceproviders/data_store.rb, line 179
        def service_model_add_node(*args)
          if (args.size != 3)
            raise ServiceRequestInvalidArgumentError.new('Invalid parameters. Required parameters: parent_id, type_id and label')
          end        
        
          parent_id = args[0]
          type_id = args[1]
          label = args[2]
        
          # FIXME: these calls should be made to this object and not to the knowledge.
          if (!parent_id.nil? && @knowledge.node_find(parent_id).id.nil?)
            raise ServiceRequestInvalidArgumentError.new("No Node whose id is #{parent_id} was found.")
          end
          # TODO: verify the type agains the Multiverse recognised types.
          if (label.empty?)
            raise ServiceRequestInvalidArgumentError.new("Please provide a label.")
          end

          node = Node.new(:parent_id => parent_id, :label=> label, :type_id => type_id)
          node.save
          node
        end

Store a new Note in the server. The expected parameters are:

  • node_id: The id of the Node associated to this note.
  • author: The author of the note.
  • category_id: The id of the Category associated to this note.
  • text: The text of the note.

All parameters are required.

[Source]

# File core/serviceproviders/data_store.rb, line 78
        def service_model_add_note(*args)
          if (args.size != 4)
            raise ServiceRequestInvalidArgumentError.new('Invalid parameters. Required parameters: node_id, author, category_id, text')
          end        
        
          node_id = args[0]
          author = args[1]
          category_id = args[2]
          text = args[3]
        
          # FIXME: these calls should be made to this object and not to the knowledge.
          # FIXME: fix this?
          if not Node.exists?(node_id)
            raise ServiceRequestInvalidArgumentError.new("No Node whose id is #{node_id} was found.")
          end
          if (author.empty?)
            raise ServiceRequestInvalidArgumentError.new("Please provide an author.")
          end
          if (@knowledge.category_find(category_id).id.nil?)
            raise ServiceRequestInvalidArgumentError.new("No Category whose id is #{category_id} was found.")
          end
          if (text.empty?)
            raise ServiceRequestInvalidArgumentError.new("Please provide some text for the note.")
          end

        
          Note.new(:author => author, :text => text, :category_id => category_id, :node_id => node_id).save
        end

Return a list of categories from the local cache

[Source]

# File core/serviceproviders/data_store.rb, line 246
        def service_model_categories_list
          @knowledge.categories
        end

Find a category by name in the local cache. The expected parameters are:

  • name: The id of the parent Node associated to this one.

All parameters are required.

[Source]

# File core/serviceproviders/data_store.rb, line 282
        def service_model_find_category_by_name(*args)
          if (args.size != 1)
            raise ServiceRequestInvalidArgumentError.new('Invalid parameters. Required parameters: name')
          end
          name = args[0]
          @knowledge.categories.each do |c|
            return c if c.name == name            
          end
          return nil
        end

Return the Node associated with the note_id passed as first parameter

[Source]

# File core/serviceproviders/data_store.rb, line 270
        def service_model_find_node(*args)
          if (args.size != 1)
            raise ServiceRequestInvalidArgumentError.new('Invalid parameters. Required parameters: node_id')
          end
          node_id = args[0]
          @knowledge.node_find(node_id)
        end

Return the Note associated with the note_id passed as first parameter

[Source]

# File core/serviceproviders/data_store.rb, line 251
        def service_model_find_note(*args)
          if (args.size != 1)
            raise ServiceRequestInvalidArgumentError.new('Invalid parameters. Required parameters: note_id')
          end
        
          note_id = args[0]
          if @sorted_notes.size.zero?
            @knowledge.nodes.each do |node|
                node.notes.each do |n|
                  @sorted_notes[n.id] = n
                end
            end
          end
          return @sorted_notes.fetch(note_id, Note.new(:text => 'error, this note does not exist in the server!!'))
        end

[Source]

# File core/serviceproviders/data_store.rb, line 241
        def service_model_knowledge_get
          @knowledge
        end

Check the revision number of the remote server against the revison number of the locally cached copy of the Knowledge. If the remote revision is newer, returns true, otherwise, returns false

[Source]

# File core/serviceproviders/data_store.rb, line 49
        def service_model_knowledge_new_revision?
          @knowledge.revision < Configuration.find(:first).value.to_i
        end

Refresh the local cache of Knowledge. ActiveResources are pulled from the server. The local revision number is updated.

[Source]

# File core/serviceproviders/data_store.rb, line 55
        def service_model_knowledge_refresh
          updated = false
          if self.service_model_knowledge_new_revision?
            $logger.debug{ 'Refreshing the Knowledge..' }
            @knowledge.categories = Category.find(:all)
            @knowledge.nodes = Node.find(:all)
            @knowledge.revision = Configuration.find(:first).value.to_i
            updated = true
            @sorted_notes = {}
            $logger.debug{ 'done.'}
          end
          return updated        
        end

Update the attributes of a Node in the server. The expected parameters are:

  • node_id: The id of the Note
  • parent_id: The id of the parent Node associated to this one.
  • type_id: Type of the node.
  • label: The text to be displayed in the nodes tree.

All parameters are required.

[Source]

# File core/serviceproviders/data_store.rb, line 210
        def service_model_update_node(*args)
          if (args.size != 4)
            raise ServiceRequestInvalidArgumentError.new('Invalid parameters. Required parameters: node_id, parent_id, type_id and label')
          end        

          node_id = args[0]
          parent_id = args[1]
          type_id = args[2]
          label = args[3]
        
          # FIXME: these calls should be made to this object and not to the knowledge.
          if (@knowledge.node_find(node_id).id.nil?)
            raise ServiceRequestInvalidArgumentError.new("No Node whose id is #{node_id} was found.")
          end
          if ( !parent_id.nil? &&  @knowledge.node_find(parent_id).id.nil?)
            raise ServiceRequestInvalidArgumentError.new("No Node whose id is #{parent_id} was found.")
          end  
          # TODO: validate the type_id against the Multiverse
          if (label.empty?)
            raise ServiceRequestInvalidArgumentError.new("Please provide a label.")
          end

          node = @knowledge.node_find(node_id)
          node.parent_id = parent_id
          node.type = type_id
          node.label = label
          node.save
        end

Update the attributes of a Note in the server. The expected parameters are:

  • node_id: The id of the Node to which this note is associated
  • note_id: The id of the Note
  • author: The author of the note.
  • category_id: The id of the Category associated to this note.
  • text: The text of the note.

All parameters are required.

[Source]

# File core/serviceproviders/data_store.rb, line 116
        def service_model_update_note(*args)
          if (args.size != 5)
            raise ServiceRequestInvalidArgumentError.new('Invalid parameters. Required parameters: node_id, note_id, author, category_id, text')
          end        
        
          node_id = args[0]
          note_id = args[1]
          author = args[2]
          category_id = args[3]
          text = args[4]
        
          #if (service_model_find_note(note_id).id.nil?)
          # FIXME: fix this?
          if not Note.exists?(note_id, :params => { :node_id => node_id })
            raise ServiceRequestInvalidArgumentError.new("No Note whose id is #{note_id} was found.")
          end
          if (author.empty?)
            raise ServiceRequestInvalidArgumentError.new("Please provide an author.")
          end
          if (@knowledge.category_find(category_id).id.nil?)
            raise ServiceRequestInvalidArgumentError.new("No Category whose id is #{category_id} was found.")
          end
          if (text.empty?)
            raise ServiceRequestInvalidArgumentError.new("Please provide some text for the note.")
          end

          # FIXME: This is changed because the Extended Node hides the 
          # property notes from the method
          #note = service_model_find_note(note_id)
          note = Note.find(note_id, :params => { :node_id => node_id })
          note.author = author
          note.text = text
          note.category_id = category_id
          note.node_id = node_id
          note.save        
        end

[Validate]