Skip to content

Commit 0027028

Browse files
committed
add unlocked tasks only restriction to bundling and lock tasks on bundle
1 parent d4ff1a3 commit 0027028

File tree

1 file changed

+43
-28
lines changed

1 file changed

+43
-28
lines changed

app/org/maproulette/framework/repository/TaskBundleRepository.scala

+43-28
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory
1010
import anorm.ToParameterValue
1111
import anorm._, postgresql._
1212
import javax.inject.{Inject, Singleton}
13+
import org.maproulette.exception.{InvalidException}
1314
import org.maproulette.Config
1415
import org.maproulette.framework.psql.Query
1516
import org.maproulette.framework.psql.filter.BaseParameter
@@ -41,38 +42,52 @@ class TaskBundleRepository @Inject() (
4142
* @param name The name of the task bundle
4243
* @param lockedTasks The tasks to be added to the bundle
4344
*/
44-
def insert(
45-
user: User,
46-
name: String,
47-
taskIds: List[Long],
48-
verifyTasks: (List[Task]) => Unit
49-
): TaskBundle = {
50-
this.withMRTransaction { implicit c =>
51-
val lockedTasks = this.withListLocking(user, Some(TaskType())) { () =>
52-
this.taskDAL.retrieveListById(-1, 0)(taskIds)
53-
}
45+
def insert(
46+
user: User,
47+
name: String,
48+
taskIds: List[Long],
49+
verifyTasks: (List[Task]) => Unit
50+
): TaskBundle = {
51+
this.withMRTransaction { implicit c =>
52+
val lockedTasks = this.withListLocking(user, Some(TaskType())) { () =>
53+
this.taskDAL.retrieveListById(-1, 0)(taskIds)
54+
}
55+
56+
57+
val failedTaskIds = taskIds.diff(lockedTasks.map(_.id))
58+
if (failedTaskIds.nonEmpty) {
59+
throw new InvalidException(s"Bundle creation failed because the following task IDs were locked: ${failedTaskIds.mkString(", ")}")
60+
}
5461

55-
verifyTasks(lockedTasks)
56-
57-
val rowId =
58-
SQL"""INSERT INTO bundles (owner_id, name) VALUES (${user.id}, ${name})""".executeInsert()
59-
rowId match {
60-
case Some(bundleId) =>
61-
val sqlQuery =
62-
s"""INSERT INTO task_bundles (task_id, bundle_id) VALUES ({taskId}, $bundleId)"""
63-
val parameters = lockedTasks.map(task => {
64-
Seq[NamedParameter]("taskId" -> task.id)
65-
})
66-
BatchSql(sqlQuery, parameters.head, parameters.tail: _*).execute()
67-
TaskBundle(bundleId, user.id, lockedTasks.map(task => {
68-
task.id
69-
}), Some(lockedTasks))
70-
71-
case None =>
72-
throw new Exception("Bundle creation failed")
62+
verifyTasks(lockedTasks)
63+
64+
for (task <- lockedTasks) {
65+
try {
66+
this.lockItem(user, task)
67+
} catch {
68+
case e: Exception => this.logger.warn(e.getMessage)
7369
}
7470
}
71+
72+
val rowId =
73+
SQL"""INSERT INTO bundles (owner_id, name) VALUES (${user.id}, ${name})""".executeInsert()
74+
rowId match {
75+
case Some(bundleId) =>
76+
val sqlQuery =
77+
s"""INSERT INTO task_bundles (task_id, bundle_id) VALUES ({taskId}, $bundleId)"""
78+
val parameters = lockedTasks.map(task => {
79+
Seq[NamedParameter]("taskId" -> task.id)
80+
})
81+
BatchSql(sqlQuery, parameters.head, parameters.tail: _*).execute()
82+
TaskBundle(bundleId, user.id, lockedTasks.map(task => {
83+
task.id
84+
}), Some(lockedTasks))
85+
86+
case None =>
87+
throw new Exception("Bundle creation failed")
88+
}
7589
}
90+
}
7691

7792
/**
7893
* Removes tasks from a bundle.

0 commit comments

Comments
 (0)