@@ -51,7 +51,8 @@ class TaskBundleRepository @Inject() (
51
51
taskIds : List [Long ],
52
52
verifyTasks : (List [Task ]) => Unit
53
53
): TaskBundle = {
54
- this .withMRTransaction { implicit c =>
54
+ // First transaction: verify tasks and create bundle
55
+ val bundleId = this .withMRTransaction { implicit c =>
55
56
val lockedTasks = this .withListLocking(user, Some (TaskType ())) { () =>
56
57
this .taskDAL.retrieveListById(- 1 , 0 )(taskIds)
57
58
}
@@ -70,54 +71,35 @@ class TaskBundleRepository @Inject() (
70
71
SQL """ INSERT INTO bundles (owner_id, name) VALUES ( ${user.id}, ${name}) """ .executeInsert()
71
72
72
73
rowId match {
73
- case Some (bundleId : Long ) =>
74
- // Update the task object to bind it to the bundle
75
- SQL (s """ UPDATE tasks SET bundle_id = $bundleId
76
- WHERE id IN ({inList}) """ )
77
- .on(
78
- " inList" -> taskIds
79
- )
80
- .executeUpdate()
81
-
82
- primaryId match {
83
- case Some (id) =>
84
- val sqlQuery = s """ UPDATE tasks SET is_bundle_primary = true WHERE id = $id"""
85
- SQL (sqlQuery).executeUpdate()
86
- case None => // Handle the case where primaryId is None
87
- }
88
-
89
- val sqlInsertTaskBundles =
90
- s """ INSERT INTO task_bundles (task_id, bundle_id) VALUES ({taskId}, $bundleId) """
91
- val parameters = lockedTasks.map(task => Seq [NamedParameter ](" taskId" -> task.id))
92
- BatchSql (sqlInsertTaskBundles, parameters.head, parameters.tail: _* ).execute()
93
-
94
- // Lock each of the new tasks to indicate they are part of the bundle
95
- for (task <- lockedTasks) {
96
- try {
97
- this .lockItem(user, task)
98
- } catch {
99
- case e : Exception => this .logger.warn(e.getMessage)
100
- }
101
- taskRepository.cacheManager.cache.remove(task.id)
74
+ case Some (id : Long ) =>
75
+ // Set primary task if specified
76
+ primaryId.foreach { pid =>
77
+ SQL """ UPDATE tasks SET is_bundle_primary = true WHERE id = $pid""" .executeUpdate()
102
78
}
103
-
104
- TaskBundle (bundleId, user.id, lockedTasks.map(task => {
105
- task.id
106
- }), Some (lockedTasks))
107
-
79
+ id
108
80
case None =>
109
81
throw new Exception (" Bundle creation failed" )
110
82
}
111
83
}
84
+
85
+ // Second transaction: add tasks to bundle
86
+ this .bundleTasks(user, bundleId, taskIds)
87
+
88
+ val lockedTasks = this .withListLocking(user, Some (TaskType ())) { () =>
89
+ this .taskDAL.retrieveListById(- 1 , 0 )(taskIds)
90
+ }
91
+
92
+ // Return the created bundle
93
+ TaskBundle (bundleId, user.id, taskIds, Some (lockedTasks))
112
94
}
113
95
114
96
/**
115
- * Resets the bundle to the tasks provided, and unlock all tasks removed from current bundle
97
+ * Sets the bundle to the tasks provided, and unlock all tasks removed from current bundle
116
98
*
117
99
* @param bundleId The id of the bundle
118
100
* @param taskIds The task ids the bundle will reset to
119
101
*/
120
- def resetTaskBundle (
102
+ def updateTaskBundle (
121
103
user : User ,
122
104
bundleId : Long ,
123
105
taskIds : List [Long ]
@@ -134,7 +116,7 @@ class TaskBundleRepository @Inject() (
134
116
val tasksToRemove = currentTaskIds.filter(taskId => ! taskIds.contains(taskId))
135
117
136
118
if (tasksToRemove.nonEmpty) {
137
- this .unbundleTasks(user, bundleId, tasksToRemove, List .empty )
119
+ this .unbundleTasks(user, bundleId, tasksToRemove)
138
120
}
139
121
140
122
// Filter for tasks that need to be added back to the bundle.
@@ -207,12 +189,6 @@ class TaskBundleRepository @Inject() (
207
189
}
208
190
209
191
lockedTasks.foreach { task =>
210
- try {
211
- this .lockItem(user, task)
212
- } catch {
213
- case e : Exception =>
214
- this .logger.warn(e.getMessage)
215
- }
216
192
taskRepository.cacheManager.cache.remove(task.id)
217
193
}
218
194
}
@@ -226,8 +202,7 @@ class TaskBundleRepository @Inject() (
226
202
def unbundleTasks (
227
203
user : User ,
228
204
bundleId : Long ,
229
- taskIds : List [Long ],
230
- preventTaskIdUnlocks : List [Long ]
205
+ taskIds : List [Long ]
231
206
): Unit = {
232
207
this .withMRConnection { implicit c =>
233
208
val tasks = this .retrieveTasks(
@@ -265,13 +240,6 @@ class TaskBundleRepository @Inject() (
265
240
)
266
241
.executeUpdate()
267
242
268
- if (! preventTaskIdUnlocks.contains(task.id)) {
269
- try {
270
- this .unlockItem(user, task)
271
- } catch {
272
- case e : Exception => this .logger.warn(e.getMessage)
273
- }
274
- }
275
243
taskRepository.cacheManager.cache.remove(task.id)
276
244
case None => // do nothing
277
245
}
@@ -306,13 +274,19 @@ class TaskBundleRepository @Inject() (
306
274
.on(" bundleId" -> bundleId)
307
275
.executeUpdate()
308
276
277
+ // Update cache for each task
309
278
tasks.foreach { task =>
310
279
if (! task.isBundlePrimary.getOrElse(false )) {
311
- try {
312
- this .unlockItem(user, task)
313
- } catch {
314
- case e : Exception => this .logger.warn(e.getMessage)
315
- }
280
+ SQL (
281
+ """ UPDATE tasks
282
+ SET status = {status}
283
+ WHERE id = {taskId}
284
+ """
285
+ ).on(
286
+ " taskId" -> task.id,
287
+ " status" -> STATUS_CREATED
288
+ )
289
+ .executeUpdate()
316
290
}
317
291
taskRepository.cacheManager.cache.remove(task.id)
318
292
}
0 commit comments