Skip to content

Commit be78fa6

Browse files
committed
Added API methods for creating and updating extractor labels
1 parent e7ae152 commit be78fa6

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

app/api/Extractions.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,4 +674,26 @@ class Extractions @Inject()(
674674
Ok(toJson("added new event"))
675675
}
676676

677+
def createExtractorsLabel() = ServerAdminAction(parse.json) { implicit request =>
678+
val name = (request.body \ "name").as[String]
679+
val category = (request.body \ "category").asOpt[String]
680+
val assignedExtractors = (request.body \ "extractors").as[List[String]]
681+
682+
extractors.createExtractorsLabel(name, category, assignedExtractors) match {
683+
case Some(lbl) => { Ok(Json.toJson(lbl)) }
684+
case None => { BadRequest("Failed to create new label") }
685+
}
686+
}
687+
688+
def updateExtractorsLabel(id: UUID) = ServerAdminAction(parse.json) { implicit request =>
689+
val name = (request.body \ "name").as[String]
690+
val category = (request.body \ "category").asOpt[String]
691+
val assignedExtractors = (request.body \ "extractors").as[List[String]]
692+
693+
val label = ExtractorsLabel(id, name, category, assignedExtractors)
694+
extractors.updateExtractorsLabel(label) match {
695+
case Some(lbl) => { Ok(Json.toJson(lbl)) }
696+
case None => { BadRequest("Failed to update existing label") }
697+
}
698+
}
677699
}

app/models/Extraction.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,21 @@ case class ExtractionGroup(
185185
latestMsg: String,
186186
allMsgs: Map[UUID, List[Extraction]]
187187
)
188+
189+
case class ExtractorsLabel(
190+
id: UUID,
191+
name: String,
192+
category: Option[String],
193+
extractors: List[String]
194+
)
195+
196+
object ExtractorsLabel {
197+
implicit val extractorsLabelWrites = Json.writes[ExtractorsLabel]
198+
199+
/*implicit val extractorsLabelReads: Reads[ExtractorsLabel] = (
200+
(JsPath \ "id").read[UUID] and
201+
(JsPath \ "name").read[String] and
202+
(JsPath \ "category").read[String] and
203+
(JsPath \ "extractors").read[List[String]].orElse(Reads.pure(List.empty))
204+
)(ExtractorsLabel.apply _)*/
205+
}

app/services/ExtractorService.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,10 @@ trait ExtractorService {
4242
def updateExtractorInfo(e: ExtractorInfo): Option[ExtractorInfo]
4343

4444
def deleteExtractor(extractorName: String)
45+
46+
def listExtractorsLabels(): List[ExtractorsLabel]
47+
48+
def createExtractorsLabel(name: String, category: Option[String], extractors: List[String]): Option[ExtractorsLabel]
49+
50+
def updateExtractorsLabel(label: ExtractorsLabel): Option[ExtractorsLabel]
4551
}

app/services/mongodb/MongoDBExtractorService.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,28 @@ class MongoDBExtractorService extends ExtractorService {
216216

217217
}
218218
}
219+
220+
def listExtractorsLabels(): List[ExtractorsLabel] = {
221+
ExtractorsLabelDAO.findAll().toList
222+
}
223+
224+
def createExtractorsLabel(name: String, category: Option[String], assignedExtractors: List[String]): Option[ExtractorsLabel] = {
225+
val id = UUID.generate()
226+
val label = ExtractorsLabel(id, name, category, assignedExtractors)
227+
ExtractorsLabelDAO.save(label)
228+
Some(label)
229+
}
230+
231+
def updateExtractorsLabel(updated: ExtractorsLabel): Option[ExtractorsLabel] = {
232+
val query = MongoDBObject("id" -> updated.id)
233+
ExtractorsLabelDAO.findOne(query) match {
234+
case Some(old) => {
235+
ExtractorsLabelDAO.update(query, updated, false, false, WriteConcern.Safe)
236+
Some(updated)
237+
}
238+
case None => None
239+
}
240+
}
219241
}
220242

221243
object ExtractorServer extends ModelCompanion[ExtractorServer, ObjectId] {
@@ -259,6 +281,13 @@ object ExtractorInfoDAO extends ModelCompanion[ExtractorInfo, ObjectId] {
259281
}
260282
}
261283

284+
object ExtractorsLabelDAO extends ModelCompanion[ExtractorsLabel, ObjectId] {
285+
val dao = current.plugin[MongoSalatPlugin] match {
286+
case None => throw new RuntimeException("No MongoSalatPlugin");
287+
case Some(x) => new SalatDAO[ExtractorsLabel, ObjectId](collection = x.collection("extractors.labels")) {}
288+
}
289+
}
290+
262291
object ExtractorsForInstanceDAO extends ModelCompanion[ExtractorsForInstance, ObjectId] {
263292
val dao = current.plugin[MongoSalatPlugin] match {
264293
case None => throw new RuntimeException("No MongoSalatPlugin");

0 commit comments

Comments
 (0)