@@ -28,7 +28,7 @@ trait ArtifactAttributes { _: AttributeDef ⇒
28
28
val artifactId : A [String ] = attribute(" _id" , F .stringFmt, " Artifact id" , O .model)
29
29
val data : A [Option [String ]] = optionalAttribute(" data" , F .stringFmt, " Content of the artifact" , O .readonly)
30
30
val dataType : A [String ] = attribute(" dataType" , F .listEnumFmt(" artifactDataType" )(dblists), " Type of the artifact" , O .readonly)
31
- val message : A [String ] = attribute (" message" , F .textFmt, " Description of the artifact in the context of the case" )
31
+ val message : A [Option [ String ]] = optionalAttribute (" message" , F .textFmt, " Description of the artifact in the context of the case" )
32
32
val startDate : A [Date ] = attribute(" startDate" , F .dateFmt, " Creation date" , new Date )
33
33
val attachment : A [Option [Attachment ]] = optionalAttribute(" attachment" , F .attachmentFmt, " Artifact file content" , O .readonly)
34
34
val tlp : A [Long ] = attribute(" tlp" , F .numberFmt, " TLP level" , 2L )
@@ -47,21 +47,46 @@ class ArtifactModel @Inject() (
47
47
implicit val ec : ExecutionContext ) extends ChildModelDef [ArtifactModel , Artifact , CaseModel , Case ](caseModel, " case_artifact" ) with ArtifactAttributes with AuditedModel {
48
48
override val removeAttribute : JsObject = Json .obj(" status" → ArtifactStatus .Deleted )
49
49
50
- override def apply (attributes : JsObject ) = {
50
+ override def apply (attributes : JsObject ): Artifact = {
51
51
val tags = (attributes \ " tags" ).asOpt[Seq [JsString ]].getOrElse(Nil ).distinct
52
52
new Artifact (this , attributes + (" tags" → JsArray (tags)))
53
53
}
54
54
55
55
// this method modify request in order to hash artifact and manager file upload
56
56
override def creationHook (parent : Option [BaseEntity ], attrs : JsObject ): Future [JsObject ] = {
57
57
val keys = attrs.keys
58
+ println(s " keys= $keys" )
59
+ println(s " attrs= $attrs" )
60
+ if (! keys.contains(" message" ) && (attrs \ " tags" ).asOpt[Seq [JsValue ]].forall(_.isEmpty))
61
+ throw BadRequestError (s " Artifact must contain a message or on ore more tags " )
58
62
if (keys.contains(" data" ) == keys.contains(" attachment" ))
59
63
throw BadRequestError (s " Artifact must contain data or attachment (but not both) " )
60
64
computeId(parent, attrs).map { id ⇒
61
65
attrs + (" _id" → JsString (id))
62
66
}
63
67
}
64
68
69
+ override def updateHook (entity : BaseEntity , updateAttrs : JsObject ): Future [JsObject ] = {
70
+ entity match {
71
+ case artifact : Artifact ⇒
72
+ val removeMessage = (updateAttrs \ " message" ).toOption.exists {
73
+ case JsNull ⇒ true
74
+ case JsArray (Nil ) ⇒ true
75
+ case _ ⇒ false
76
+ }
77
+ val removeTags = (updateAttrs \ " tags" ).toOption.exists {
78
+ case JsNull ⇒ true
79
+ case JsArray (Nil ) ⇒ true
80
+ case _ ⇒ false
81
+ }
82
+ if ((removeMessage && removeTags) ||
83
+ (removeMessage && artifact.tags().isEmpty) ||
84
+ (removeTags && artifact.message().isEmpty))
85
+ Future .failed(BadRequestError (s " Artifact must contain a message or on ore more tags " ))
86
+ else
87
+ Future .successful(updateAttrs)
88
+ }
89
+ }
65
90
def computeId (parent : Option [BaseEntity ], attrs : JsObject ): Future [String ] = {
66
91
// in order to make sure that there is no duplicated artifact, calculate its id from its content (dataType, data, attachment and parent)
67
92
val mm = new MultiHash (" MD5" )
0 commit comments