11package services .mongodb
22
33import org .bson .types .ObjectId
4- import services .{PreviewService , SectionService , CommentService , FileService , DatasetService , FolderService }
4+ import services .{CommentService , DatasetService , FileService , FolderService , PreviewService , SectionService }
55import models ._
66import javax .inject .{Inject , Singleton }
77import java .util .Date
@@ -14,7 +14,7 @@ import com.mongodb.casbah.WriteConcern
1414import com .mongodb .casbah .Imports ._
1515import play .api .libs .json .{JsValue , Json }
1616
17- import scala .collection .mutable .ArrayBuffer
17+ import scala .collection .mutable .{ ArrayBuffer , ListBuffer }
1818
1919/**
2020 * USe MongoDB to store sections
@@ -26,8 +26,11 @@ class MongoDBSectionService @Inject() (comments: CommentService, previews: Previ
2626 SectionDAO .findAll.toList
2727 }
2828
29- def addTags (id : UUID , userIdStr : Option [String ], eid : Option [String ], tags : List [String ]) {
29+ def addTags (id : UUID , userIdStr : Option [String ], eid : Option [String ], tags : List [String ]) : List [ Tag ] = {
3030 Logger .debug(" Adding tags to section " + id + " : " + tags)
31+
32+ var tagsAdded : ListBuffer [Tag ] = ListBuffer .empty[Tag ]
33+
3134 val section = SectionDAO .findOneById(new ObjectId (id.stringify)).get
3235 val existingTags = section.tags.filter(x => userIdStr == x.userId && eid == x.extractor_id).map(_.name)
3336 val createdDate = new Date
@@ -42,19 +45,22 @@ class MongoDBSectionService @Inject() (comments: CommentService, previews: Previ
4245 // Only add tags with new values.
4346 if (! existingTags.contains(shortTag)) {
4447 val tagObj = models.Tag (name = shortTag, userId = userIdStr, extractor_id = eid, created = createdDate)
48+ tagsAdded += tagObj
4549 SectionDAO .dao.collection.update(MongoDBObject (" _id" -> new ObjectId (id.stringify)), $addToSet(" tags" -> Tag .toDBObject(tagObj)), false , false , WriteConcern .Safe )
4650 }
4751 })
52+ tagsAdded.toList
4853 }
4954
5055 def removeTags (id : UUID , userIdStr : Option [String ], eid : Option [String ], tags : List [String ]) {
5156 Logger .debug(" Removing tags in section " + id + " : " + tags + " , userId: " + userIdStr + " , eid: " + eid)
5257 val section = SectionDAO .findOneById(new ObjectId (id.stringify)).get
53- val existingTags = section.tags.filter(x => userIdStr == x.userId && eid == x.extractor_id).map(_.name )
58+ val existingTags = section.tags.filter(x => userIdStr == x.userId && eid == x.extractor_id).map(_.id.toString() )
5459 Logger .debug(" existingTags after user and extractor filtering: " + existingTags.toString)
5560 // Only remove existing tags.
5661 tags.intersect(existingTags).map { tag =>
57- SectionDAO .dao.collection.update(MongoDBObject (" _id" -> new ObjectId (id.stringify)), $pull(" tags" -> MongoDBObject (" name" -> tag)), false , false , WriteConcern .Safe )
62+ Logger .info(tag)
63+ SectionDAO .dao.collection.update(MongoDBObject (" _id" -> new ObjectId (id.stringify)), $pull(" tags" -> MongoDBObject (" _id" -> new ObjectId (tag))), false , false , WriteConcern .Safe )
5864 }
5965 }
6066
0 commit comments