From c14a58b4138509cb8d72f3422197ff6968bde0b3 Mon Sep 17 00:00:00 2001
From: Bruno Nocera Zanette <bnzanette@inf.ufpr.br>
Date: Mon, 28 Sep 2015 20:01:32 -0300
Subject: [PATCH] Fix merges on LearningObject Repository

---
 .../orient_db/learning_object_repository.rb   | 143 ++++++++----------
 1 file changed, 65 insertions(+), 78 deletions(-)

diff --git a/app/repositories/orient_db/learning_object_repository.rb b/app/repositories/orient_db/learning_object_repository.rb
index 2114a7a23..c31fc0f9a 100644
--- a/app/repositories/orient_db/learning_object_repository.rb
+++ b/app/repositories/orient_db/learning_object_repository.rb
@@ -1,6 +1,7 @@
 module OrientDb
   class LearningObjectRepository < Base
     include OrientDb::Methods::EdgeMethods
+    include RepositoriesProxy
 
     def increment_views(user, learning_object)
       create_edge "Views", user.rid, learning_object.id
@@ -8,7 +9,7 @@ module OrientDb
 
     #
     # Usage:
-    #   repository.for(:learning_objects).like @learning_object, current_user
+    #   repository.for(:learning_objects).like current_user, @learning_object
     #
     def like(user, learning_object)
       create_edge "Likes", user.rid, learning_object.id
@@ -24,73 +25,80 @@ module OrientDb
       learning_objects = build_learning_objects(learning_objects_hash) || []
     end
 
-    # Example:
-    #   list = repository.for(:learning_objects).all
-    #   list.each do |learning_object|
-    #     learning_object.inspect <LearningObject model>
-    #   end
-    def all_ids(limit = 100, offset = 0)
-      learning_objects_hash = connection.query "SELECT @rid FROM LearningObject LIMIT #{limit} OFFSET #{offset}", {limit: limit}
-      learning_objects_ids = []
-      learning_objects_hash.each do |id|
-        learning_objects_ids << id["rid"]
-      end
-      learning_objects_ids
-    end
-
     # Usage:
     #   learning_object = repository.for(:learning_objects).get_by_dspace_id 123
     #
     def get_by_dspace_id(id_dspace)
-      result = connection.query "SELECT FROM LearningObject WHERE id_dspace=#{id_dspace}"
-      build_learning_object result.first
+      result = select_by_property(odb_class, "id_dspace", id_dspace)
+      build_object result.first
     end
 
-    # Usage:
-    #   learning_object = repository.for(:learning_objects).get_by_dspace_id 123
-    #
-    def find(id)
-      result = connection.query "SELECT FROM #{id}"
-      build_learning_object result.first
+    def get_subjects(learning_object)
+      result = get_edges_end("IsAbout", "out", learning_object.id)
+      subject_repository.build_objects(result)
+    end
+
+    def get_attributes(learning_object)
+      result = get_edges_end("HasAttr", "out", learning_object.id)
+      attribute_repository.build_objects(result)
     end
 
-    def create(name, url)
-      connection.command sprintf("INSERT INTO LearningObject (name,URL) VALUES ('%s','%s')", name, url)
+    def create_relations(learning_object)
+      edges = create_edges_from_array("IsAbout", learning_object.id, learning_object.subjects, true)
+      edges << create_edges_from_array("HasAttr", learning_object.id, learning_object.attributes, true)
+      edges.flatten
     end
 
     # Usage:
     #   repository.for(:learning_objects).destroy learning_object
     #
     def destroy(learning_object)
-      connection.command sprintf("DELETE VERTEX LearningObject where @rid = '%s'", learning_object.id)
+      connection.command sprintf("DELETE VERTEX #{odb_class} where @rid = '%s'", learning_object.id)
     end
 
+    ##
+    # To create "index:learningobject_search" on OrientDB, use the following command:
+    #   CREATE INDEX learningobject_search
+    #   ON LearningObject (name, description)
+    #   FULLTEXT ENGINE LUCENE
+    #   METADATA {"analyzer":"org.apache.lucene.analysis.br.BrazilianAnalyzer"}
     def search(qry)
-      # TO CREATE "index:learningobject_search" ON OrientDB, USE THE COMMAND:
-      #   CREATE INDEX learningobject_search
-      #   ON LearningObject (name, description)
-      #   FULLTEXT ENGINE LUCENE
-      #   METADATA {"analyzer":"org.apache.lucene.analysis.br.BrazilianAnalyzer"}
       learning_objects_hash = connection.query "
                                 SELECT EXPAND(rid)
                                 FROM index:learningobject_search
                                 WHERE key LUCENE '#{qry}'
                               ", limit: -1
-      learning_objects = build_learning_objects(learning_objects_hash) || []
+      build_objects(learning_objects_hash) || []
     end
 
-    #def author_of(rid)
-    #  connection.query "SELECT expand(in) FROM (SELECT expand(out_author_of) FROM User WHERE @rid=#{rid})"
-    #end
-
-    #def has(rid)
-    #  connection.query "SELECT expand(in) FROM (SELECT expand(out_has) FROM User WHERE @rid=#{rid})"
-    #end
+    def build_object(args={})
+      lo = nil
+      unless args.nil?
+        lo = LearningObject.new(:id => args["@rid"],
+                                :name => args["name"],
+                                :description => args["description"],
+                                :thumbnail => args["thumbnail"],
+                                :created_at => args["created_at"],
+                                :id_dspace => args["id_dspace"],
+                                :type => args["type"],
+                                :bitstreams => args["bitstreams"],
+                                :metadata => args["metadata"],
+                                :last_modified => args["last_modified"])
+        lo.likes = args.has_key?("in_Likes") ? args["in_Likes"].size : 0
+        lo.views = args.has_key?("in_Views") ? args["in_Views"].size : 0
+      end
+      lo
+    end
 
-    #def get_likes(id)
-    #  likes = connection.query "select outE('Likes') from #{id}"
-    #  likes[0]["outE"]
-    #end
+    def build_hash
+      hash = super
+      hash.delete("likes")
+      hash.delete("views")
+      hash.delete("downloads")
+      hash.delete("subjects")
+      hash.delete("attributes")
+      hash
+    end
 
     private
 
@@ -98,47 +106,26 @@ module OrientDb
       ['thumbnail']
     end
 
-    def count_likes(learning_object)
-      get_in_edges_count "Likes", learning_object.id
+    def create_edges_from_array(edge_class, id, array, unique=false)
+      edges = []
+      array.each do |o|
+        unless unique && edge_exists?(edge_class, id, o.id)
+          edges << create_edge(edge_class, id, o.id)
+        end
+      end
+      edges
     end
 
-    def count_views(learning_object)
-      get_in_edges_count "Views", learning_object.id
+    def odb_class
+      "LearningObject"
     end
 
-    def build_learning_object(args={})
-      lo = LearningObject.new(:id => args["@rid"],
-                              :name => args["name"],
-                              :description => args["description"],
-                              :thumbnail => args["thumbnail"],
-                              :date_creation => args["date_creation"],
-                              :id_dspace => args["id_dspace"],
-                              :type => args["type"],
-                              :bitstreams => args["bitstreams"],
-                              :metadata => args["metadata"],
-                              :last_modified => args["last_modified"])
-
-      unless args["in_Likes"].nil?
-        lo.likes = args["in_Likes"].count
-      else
-        lo.likes = 0
-      end
-
-      unless args["in_Views"].nil?
-        lo.views = args["in_Views"].count
-      else
-        lo.views = 0
-      end
-
-      lo
+    def count_likes(learning_object)
+      get_in_edges_count "Likes", learning_object.id
     end
 
-    def build_learning_objects(hash=[])
-      learning_objects = []
-      hash.each do |h|
-        learning_objects << build_learning_object(h)
-      end
-      learning_objects
+    def count_views(learning_object)
+      get_in_edges_count "Views", learning_object.id
     end
 
   end
-- 
GitLab