Skip to content

Commit 5ca3026

Browse files
committed
Fix validation, error messages
1 parent d558a78 commit 5ca3026

File tree

5 files changed

+69
-21
lines changed

5 files changed

+69
-21
lines changed

app/api/Extractions.scala

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import play.api.libs.json.Json._
1717
import play.api.libs.json._
1818
import play.api.libs.ws.{Response, WS}
1919
import play.api.libs.functional.syntax._
20-
import play.api.mvc.MultipartFormData
20+
import play.api.mvc.{Action, MultipartFormData, Result, SimpleResult}
2121
import services._
2222

2323
import scala.collection.mutable.ListBuffer
@@ -678,23 +678,53 @@ class Extractions @Inject()(
678678
// Fetch parameters from request body
679679
val (name, category, assignedExtractors) = parseExtractorsLabel(request)
680680

681-
// Validate and create new label
682-
validateExtractorsLabel(name, category, assignedExtractors)
683-
val label = extractors.createExtractorsLabel(name, category, assignedExtractors)
681+
// Validate that name is not empty
682+
if (name.isEmpty) {
683+
BadRequest("Label Name cannot be empty")
684+
} else {
685+
// Validate that name is unique
686+
extractors.getExtractorsLabel(name) match {
687+
case Some(lbl) => Conflict("Label name is already in use: " + lbl.name)
688+
case None => {
689+
// Create the new label
690+
val label = extractors.createExtractorsLabel(name, category, assignedExtractors)
691+
Ok(Json.toJson(label))
692+
}
693+
}
694+
695+
}
684696

685-
Ok(Json.toJson(label))
686697
}
687698

688699
def updateExtractorsLabel(id: UUID) = ServerAdminAction(parse.json) { implicit request =>
689700
// Fetch parameters from request body
690701
val (name, category, assignedExtractors) = parseExtractorsLabel(request)
691702

692-
// Validate and perform update
693-
validateExtractorsLabel(name, category, assignedExtractors)
694-
val label = ExtractorsLabel(id, name, category, assignedExtractors)
695-
val updatedLabel = extractors.updateExtractorsLabel(label)
696-
697-
Ok(Json.toJson(updatedLabel))
703+
// Validate that name is not empty
704+
if (name.isEmpty) {
705+
BadRequest("Label Name cannot be empty")
706+
} else {
707+
// Validate that name is still unique
708+
extractors.getExtractorsLabel(name) match {
709+
case Some(lbl) => {
710+
// Exclude current id (in case name hasn't changed)
711+
if (lbl.id != id) {
712+
Conflict("Label name is already in use: " + lbl.name)
713+
} else {
714+
// Update the label
715+
val label = ExtractorsLabel(id, name, category, assignedExtractors)
716+
val updatedLabel = extractors.updateExtractorsLabel(label)
717+
Ok(Json.toJson(updatedLabel))
718+
}
719+
}
720+
case None => {
721+
// Update the label
722+
val label = ExtractorsLabel(id, name, category, assignedExtractors)
723+
val updatedLabel = extractors.updateExtractorsLabel(label)
724+
Ok(Json.toJson(updatedLabel))
725+
}
726+
}
727+
}
698728
}
699729

700730
def deleteExtractorsLabel(id: UUID) = ServerAdminAction { implicit request =>
@@ -715,10 +745,4 @@ class Extractions @Inject()(
715745

716746
(name, category, assignedExtractors)
717747
}
718-
719-
def validateExtractorsLabel(name: String, category: Option[String], assignedExtractors: List[String]): Unit = {
720-
if (name.isEmpty) {
721-
BadRequest("Label Name cannot be empty")
722-
}
723-
}
724748
}

app/services/ExtractorService.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ trait ExtractorService {
4545

4646
def listExtractorsLabels(): List[ExtractorsLabel]
4747

48+
def getExtractorsLabel(name: String): Option[ExtractorsLabel]
49+
4850
def getExtractorsLabel(id: UUID): Option[ExtractorsLabel]
4951

5052
def createExtractorsLabel(name: String, category: Option[String], extractors: List[String]): ExtractorsLabel

app/services/mongodb/MongoDBExtractorService.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ class MongoDBExtractorService extends ExtractorService {
223223
ExtractorsLabelDAO.findAll().toList
224224
}
225225

226+
def getExtractorsLabel(name: String): Option[ExtractorsLabel] = {
227+
ExtractorsLabelDAO.findOne(MongoDBObject("name" -> name))
228+
}
229+
226230
def getExtractorsLabel(id: UUID): Option[ExtractorsLabel] = {
227231
ExtractorsLabelDAO.findOne(MongoDBObject("_id" -> new ObjectId(id.stringify)))
228232
}

app/views/updateExtractors.scala.html

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ <h1>Extractor Catalog</h1>
4545
<div class="btn-group" data-toggle="buttons">
4646
@for(label <- catLabels) {
4747
<label class="btn btn-primary active">
48-
<input type="checkbox" autocomplete="off" checked> @label.name
48+
<input id="[email protected]" class="label-checkbox" type="checkbox" autocomplete="off" checked> @label.name
4949
</label>
5050
}
5151
</div>
@@ -348,9 +348,20 @@ <h1>Extractor Catalog</h1>
348348
<script>
349349
$('document').ready(function () {
350350
var params = new URLSearchParams(window.location.search);
351-
var targetParameter = "processTriggerSearchFilter"
352-
if (params.has(targetParameter)) {
353-
document.getElementById(targetParameter).value = params.get(targetParameter);
351+
var processTriggerParam = "processTriggerSearchFilter"
352+
if (params.has(processTriggerParam)) {
353+
document.getElementById(processTriggerParam).value = params.get(processTriggerParam);
354+
}
355+
var labelsParam = "labelsFilter"
356+
if (params.has(labelsParam)) {
357+
// If we have a label param, uncheck all other labels
358+
var labelNames = params.get(labelsParam).split(',');
359+
if (labels => 1) { $('.label-checkbox').prop("checked", false); }
360+
361+
// Check only the labels in the query string
362+
labelNames.forEach(function(labelName) {
363+
$('label-' + labelName).prop("checked", true);
364+
});
354365
}
355366
});
356367
</script>

public/javascripts/extractors/extractors.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ function saveExtractorsLabel(label) {
137137

138138
request.fail(function (jqXHR, textStatus, errorThrown){
139139
console.error("The following error occured: " + textStatus, errorThrown);
140+
var operation = label.id ? "update" : "create";
141+
var specificError = jqXHR.responseText;
142+
var msg = "Failed to " + operation + " label: " + specificError;
143+
notify(msg, "error");
140144
});
141145

142146
return request;
@@ -155,6 +159,9 @@ function deleteExtractorsLabel(id) {
155159

156160
request.fail(function (jqXHR, textStatus, errorThrown){
157161
console.error("The following error occured: " + textStatus, errorThrown);
162+
var specificError = jqXHR.responseText;
163+
var msg = "Failed to delete label: " + specificError
164+
notify(msg, "error");
158165
});
159166

160167
return request;

0 commit comments

Comments
 (0)