Skip to content

Commit d123d07

Browse files
author
toddn
committed
initial commit
when tags are added to sections, the new tag cannot be added without the new tag name and id. to fix this, tagService addTagsHelper now also returns the tags that were added in addition to the (not_found, error_string.) So now, adding tags to sections returns a list of the new tags in the response, so that the new tag is added to the right div in sectionRubberband.js.
1 parent 24641db commit d123d07

File tree

12 files changed

+121
-33
lines changed

12 files changed

+121
-33
lines changed

app/api/Files.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,7 @@ class Files @Inject()(
12461246
*/
12471247
def addTagsHelper(obj_type: TagCheckObjType, id: UUID, request: UserRequest[JsValue]): SimpleResult = {
12481248

1249-
val (not_found, error_str) = tags.addTagsHelper(obj_type, id, request)
1249+
val (not_found, error_str, tagsAdded) = tags.addTagsHelper(obj_type, id, request)
12501250
files.get(id) match {
12511251
case Some(file) => {
12521252
events.addObjectEvent(request.user, file.id, file.filename, EventType.ADD_TAGS_FILE.toString)

app/api/Sections.scala

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package api
22

33
import javax.inject.{Inject, Singleton}
4-
5-
import models.{Comment, UUID}
4+
import models.{Comment, ResourceRef, UUID}
65
import play.api.Logger
7-
import play.api.libs.json.Json
86
import play.api.libs.json.Json._
9-
import models.{ResourceRef, UUID, Comment}
10-
import services._
11-
import javax.inject.{Inject, Singleton}
7+
import play.api.libs.json.{JsValue, Json}
128
import services._
139

10+
import scala.collection.mutable.ListBuffer
11+
1412
/**
1513
* Files sections.
1614
*/
@@ -99,11 +97,17 @@ class Sections @Inject()(
9997
* Requires that the request body contains a "tags" field of List[String] type.
10098
*/
10199
def addTags(id: UUID) = PermissionAction(Permission.AddTag, Some(ResourceRef(ResourceRef.section, id)))(parse.json) { implicit request =>
102-
val (not_found, error_str) = tags.addTagsHelper(TagCheck_Section, id, request)
100+
val (not_found, error_str, tagsAdded) = tags.addTagsHelper(TagCheck_Section, id, request)
103101

104102
// Now the real work: adding the tags.
103+
//TODO create List[JsValue] for each tag, then add too response
104+
var tagsAddedJsValue : ListBuffer[JsValue] = ListBuffer.empty[JsValue]
105+
for (tag <- tagsAdded) {
106+
val currentTagJson = Json.obj("id"->tag.id.stringify, "name"->tag.name)
107+
tagsAddedJsValue += currentTagJson
108+
}
105109
if ("" == error_str) {
106-
Ok(Json.obj("status" -> "success"))
110+
Ok(Json.obj("status" -> "success", "tags" ->tagsAddedJsValue.toList))
107111
} else {
108112
Logger.error(error_str)
109113
if (not_found) {

app/services/DatasetService.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ package services
33
import java.util.Date
44

55
import api.Permission.Permission
6-
import models._
7-
import play.api.libs.json.JsValue
86
import com.mongodb.casbah.Imports._
9-
import models.File
7+
import models.{File, _}
108

119
/**
1210
* Generic dataset service.
@@ -291,7 +289,7 @@ trait DatasetService {
291289

292290
def removeXMLMetadata(id: UUID, fileId: UUID)
293291

294-
def addTags(id: UUID, userIdStr: Option[String], eid: Option[String], tags: List[String])
292+
def addTags(id: UUID, userIdStr: Option[String], eid: Option[String], tags: List[String]) : List[Tag]
295293

296294
def setUserMetadataWasModified(id: UUID, wasModified: Boolean)
297295

app/services/FileService.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ trait FileService {
185185

186186
def findIntermediates(): List[File]
187187

188-
def addTags(id: UUID, userIdStr: Option[String], eid: Option[String], tags: List[String])
188+
def addTags(id: UUID, userIdStr: Option[String], eid: Option[String], tags: List[String]) : List[Tag]
189189

190190
def removeAllTags(id: UUID)
191191

app/services/SectionService.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ trait SectionService {
1616

1717
def get(ids: List[UUID]): DBResult[Section]
1818

19-
def addTags(id: UUID, userIdStr: Option[String], eid: Option[String], tags: List[String])
19+
def addTags(id: UUID, userIdStr: Option[String], eid: Option[String], tags: List[String]) : List[Tag]
2020

2121
def removeTags(id: UUID, userIdStr: Option[String], eid: Option[String], tags: List[String])
2222

app/services/TagService.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package services
22

33
import api.UserRequest
44
import play.api.libs.json.JsValue
5-
import models.UUID
5+
import models.{Tag, UUID}
66

77
/**
88
* Service to manipulate tags
99
*/
1010
abstract class TagService {
11-
def addTagsHelper(obj_type: TagCheckObjType, id: UUID, request: UserRequest[JsValue]): (Boolean, String)
11+
def addTagsHelper(obj_type: TagCheckObjType, id: UUID, request: UserRequest[JsValue]): (Boolean, String, List[Tag])
1212
def removeTagsHelper(obj_type: TagCheckObjType, id: UUID, request: UserRequest[JsValue]): (Boolean, String)
1313
}
1414

app/services/mongodb/MongoDBDatasetService.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,9 +1033,10 @@ class MongoDBDatasetService @Inject() (
10331033
false, false, WriteConcern.Safe)
10341034
}
10351035

1036-
def addTags(id: UUID, userIdStr: Option[String], eid: Option[String], tags: List[String]) {
1036+
def addTags(id: UUID, userIdStr: Option[String], eid: Option[String], tags: List[String]) : List[Tag] = {
10371037
Logger.debug("Adding tags to dataset " + id + " : " + tags)
10381038
// TODO: Need to check for the owner of the dataset before adding tag
1039+
var tagsAdded : ListBuffer[Tag] = ListBuffer.empty[Tag]
10391040

10401041
val dataset = get(id).get
10411042
val existingTags = dataset.tags.filter(x => userIdStr == x.userId && eid == x.extractor_id).map(_.name)
@@ -1051,9 +1052,11 @@ class MongoDBDatasetService @Inject() (
10511052
// Only add tags with new values.
10521053
if (!existingTags.contains(shortTag)) {
10531054
val tagObj = models.Tag(name = shortTag, userId = userIdStr, extractor_id = eid, created = createdDate)
1055+
tagsAdded += tagObj
10541056
Dataset.update(MongoDBObject("_id" -> new ObjectId(id.stringify)), $addToSet("tags" -> Tag.toDBObject(tagObj)), false, false, WriteConcern.Safe)
10551057
}
10561058
})
1059+
tagsAdded.toList
10571060
}
10581061

10591062
def setUserMetadataWasModified(id: UUID, wasModified: Boolean) {

app/services/mongodb/MongoDBFileService.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,8 +742,10 @@ class MongoDBFileService @Inject() (
742742

743743
// ---------- Tags related code starts ------------------
744744
// Input validation is done in api.Files, so no need to check again.
745-
def addTags(id: UUID, userIdStr: Option[String], eid: Option[String], tags: List[String]) {
745+
def addTags(id: UUID, userIdStr: Option[String], eid: Option[String], tags: List[String]) : List[Tag] = {
746746
Logger.debug("Adding tags to file " + id + " : " + tags)
747+
748+
var tagsAdded : ListBuffer[Tag] = ListBuffer.empty[Tag]
747749
val file = get(id).get
748750
val existingTags = file.tags.filter(x => userIdStr == x.userId && eid == x.extractor_id).map(_.name)
749751
val createdDate = new Date
@@ -758,9 +760,11 @@ class MongoDBFileService @Inject() (
758760
// Only add tags with new values.
759761
if (!existingTags.contains(shortTag)) {
760762
val tagObj = models.Tag(name = shortTag, userId = userIdStr, extractor_id = eid, created = createdDate)
763+
tagsAdded += tagObj
761764
FileDAO.update(MongoDBObject("_id" -> new ObjectId(id.stringify)), $addToSet("tags" -> Tag.toDBObject(tagObj)), false, false, WriteConcern.Safe)
762765
}
763766
})
767+
tagsAdded.toList
764768
}
765769

766770
def removeAllTags(id: UUID) {

app/services/mongodb/MongoDBSectionService.scala

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package services.mongodb
22

33
import org.bson.types.ObjectId
4-
import services.{PreviewService, SectionService, CommentService, FileService, DatasetService, FolderService}
4+
import services.{CommentService, DatasetService, FileService, FolderService, PreviewService, SectionService}
55
import models._
66
import javax.inject.{Inject, Singleton}
77
import java.util.Date
@@ -14,7 +14,7 @@ import com.mongodb.casbah.WriteConcern
1414
import com.mongodb.casbah.Imports._
1515
import 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

app/services/mongodb/MongoDBTagService.scala

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class MongoDBTagService @Inject()(files: FileService, datasets: DatasetService,
113113
* which contains the cause of the error, such as "No 'tags' specified", and
114114
* "The file with id 5272d0d7e4b0c4c9a43e81c8 is not found".
115115
*/
116-
def addTagsHelper(obj_type: TagCheckObjType, id: UUID, request: UserRequest[JsValue]): (Boolean, String) = {
116+
def addTagsHelper(obj_type: TagCheckObjType, id: UUID, request: UserRequest[JsValue]): (Boolean, String, List[Tag]) = {
117117
val tagCheck = checkErrorsForTag(obj_type, id, request)
118118

119119
val error_str = tagCheck.error_str
@@ -122,20 +122,26 @@ class MongoDBTagService @Inject()(files: FileService, datasets: DatasetService,
122122
val extractorOpt = tagCheck.extractorOpt
123123
val tags = tagCheck.tags
124124

125+
var tagsAdded : List[Tag] = List.empty[Tag]
126+
125127
// Now the real work: adding the tags.
126128
if ("" == error_str) {
127129
// Clean up leading, trailing and multiple contiguous white spaces and drop empty tags
128130
val tagsCleaned = tags.get.map(_.trim().replaceAll("\\s+", " ")).filter(!_.isEmpty)
129131
(obj_type) match {
130-
case TagCheck_File => files.addTags(id, userOpt, extractorOpt, tagsCleaned)
132+
case TagCheck_File => {
133+
tagsAdded = files.addTags(id, userOpt, extractorOpt, tagsCleaned)
134+
}
131135
case TagCheck_Dataset => {
132-
datasets.addTags(id, userOpt, extractorOpt, tagsCleaned)
136+
tagsAdded = datasets.addTags(id, userOpt, extractorOpt, tagsCleaned)
133137
datasets.index(id)
134138
}
135-
case TagCheck_Section => sections.addTags(id, userOpt, extractorOpt, tagsCleaned)
139+
case TagCheck_Section => {
140+
tagsAdded = sections.addTags(id, userOpt, extractorOpt, tagsCleaned)
136141
}
137142
}
138-
(not_found, error_str)
143+
}
144+
(not_found, error_str, tagsAdded)
139145
}
140146

141147
def removeTagsHelper(obj_type: TagCheckObjType, id: UUID, request: UserRequest[JsValue]): (Boolean, String) = {

0 commit comments

Comments
 (0)