Skip to content

Commit ec1cf7f

Browse files
committed
Determine attribute type using the corresponding field
1 parent 300c946 commit ec1cf7f

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

kotlin-insight-client/kotlin-insight-client-api/src/main/kotlin/com/linkedplanet/kotlininsightclient/api/model/Model.kt

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,11 @@ fun InsightObject.getAttribute(id: Int): InsightAttribute? =
4848

4949
fun InsightObject.isReferenceAttribute(id: Int): Boolean =
5050
getAttribute(id)
51-
?.let { it.value.any { value -> value.referencedObject != null } }
52-
?: false
51+
?.attributeType == InsightObjectAttributeType.REFERENCE
5352

5453
fun InsightObject.isValueAttribute(id: Int): Boolean =
5554
getAttribute(id)
56-
?.let { it.value.any { value -> value.value != null } }
57-
?: false
55+
?.attributeType == InsightObjectAttributeType.DEFAULT
5856

5957
fun InsightObject.exists(id: Int): Boolean =
6058
getAttribute(id) != null
@@ -72,7 +70,7 @@ fun InsightObject.getValueList(id: Int): List<Any> =
7270

7371
fun <T> InsightObject.setValueList(id: Int, values: List<T?>) {
7472
if (!exists(id)) {
75-
this.createAttribute(id)
73+
this.createAttribute(id, InsightObjectAttributeType.DEFAULT)
7674
}
7775
getAttribute(id)
7876
?.value = values.map {
@@ -82,7 +80,7 @@ fun <T> InsightObject.setValueList(id: Int, values: List<T?>) {
8280

8381
fun <T> InsightObject.setValue(id: Int, value: T?) {
8482
if (!exists(id)) {
85-
this.createAttribute(id)
83+
this.createAttribute(id, InsightObjectAttributeType.DEFAULT)
8684
}
8785
getAttribute(id)
8886
?.value = listOf(ObjectAttributeValue(value, "", null))
@@ -95,7 +93,7 @@ fun <T> InsightObject.removeValue(id: Int, value: T?) {
9593

9694
fun InsightObject.addValue(id: Int, value: Any?) {
9795
if (!exists(id)) {
98-
this.createAttribute(id)
96+
this.createAttribute(id, InsightObjectAttributeType.DEFAULT)
9997
}
10098
getAttribute(id)
10199
?.apply {
@@ -176,7 +174,7 @@ fun InsightObject.clearReferenceValue(id: Int) {
176174

177175
fun InsightObject.addReference(attributeId: Int, referencedObjectId: Int) {
178176
if (!exists(attributeId)) {
179-
this.createAttribute(attributeId)
177+
this.createAttribute(attributeId, InsightObjectAttributeType.REFERENCE)
180178
}
181179
getAttribute(attributeId)
182180
?.let {
@@ -199,10 +197,16 @@ fun InsightObject.setSingleReference(id: Int, referencedObjectId: Int) {
199197
this.addReference(id, referencedObjectId)
200198
}
201199

200+
fun InsightObject.toEditObjectItem() =
201+
ObjectEditItem(
202+
objectTypeId,
203+
getEditAttributes()
204+
)
205+
202206
fun InsightObject.getEditAttributes() =
203207
this.attributes.map { insightAttr ->
204208
val values = insightAttr.value.map {
205-
if (it.referencedObject != null) {
209+
if (insightAttr.attributeType == InsightObjectAttributeType.REFERENCE) {
206210
ObjectEditItemAttributeValue(
207211
it.referencedObject!!.id
208212
)
@@ -218,9 +222,23 @@ fun InsightObject.getEditAttributes() =
218222
)
219223
}
220224

221-
private fun InsightObject.createAttribute(id: Int) {
225+
/**
226+
* See type attribute in response of https://insight-javadoc.riada.io/insight-javadoc-8.6/insight-rest/#object__id__attributes_get
227+
*/
228+
enum class InsightObjectAttributeType(val value: Int) {
229+
DEFAULT(0),
230+
REFERENCE(1);
231+
232+
companion object {
233+
fun parse(value: Int) =
234+
values().single { it.value == value }
235+
}
236+
}
237+
238+
private fun InsightObject.createAttribute(id: Int, attributeType: InsightObjectAttributeType) {
222239
this.attributes = this.attributes + InsightAttribute(
223240
id,
241+
attributeType,
224242
emptyList()
225243
)
226244
}
@@ -235,6 +253,7 @@ data class InsightReference(
235253

236254
data class InsightAttribute(
237255
val attributeId: Int,
256+
val attributeType: InsightObjectAttributeType,
238257
var value: List<ObjectAttributeValue>
239258
)
240259

@@ -324,13 +343,14 @@ data class ObjectTypeAttribute(
324343
val id: Int,
325344
val name: String,
326345
val referenceObjectTypeId: Int,
327-
val referenceObjectType: InsightMetaObjectType
346+
val referenceObjectType: InsightMetaObjectType,
347+
val type: Int
328348
)
329349

330350
data class ObjectAttributeValue(
331351
var value: Any?,
332352
var displayValue: Any?,
333-
var referencedObject: ReferencedObject?
353+
var referencedObject: ReferencedObject?,
334354
)
335355

336356
data class ReferencedObject(

kotlin-insight-client/kotlin-insight-client-http/src/main/kotlin/com/linkedplanet/kotlininsightclient/http/HttpInsightObjectOperator.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,11 @@ class HttpInsightObjectOperator(private val context: HttpInsightClientContext) :
7171
}
7272

7373
override suspend fun updateObject(obj: InsightObject): Either<InsightClientError, InsightObject> = either {
74-
val attrs = obj.getEditAttributes()
75-
val editItem = ObjectEditItem(
76-
obj.objectTypeId,
77-
attrs
78-
)
79-
8074
context.httpClient.executeRest<ObjectUpdateResponse>(
8175
"PUT",
8276
"rest/insight/1.0/object/${obj.id}",
8377
emptyMap(),
84-
GSON.toJson(editItem),
78+
GSON.toJson(obj.toEditObjectItem()),
8579
"application/json",
8680
ObjectUpdateResponse::class.java
8781
)
@@ -241,8 +235,13 @@ class HttpInsightObjectOperator(private val context: HttpInsightClientContext) :
241235

242236
private fun InsightObjectApiResponse.toValue(): InsightObject {
243237
val attributes = this.attributes.map {
238+
val attributeType =
239+
it.objectTypeAttribute?.type
240+
?.let { type -> InsightObjectAttributeType.parse(type) }
241+
?: InsightObjectAttributeType.DEFAULT
244242
InsightAttribute(
245243
it.objectTypeAttributeId,
244+
attributeType,
246245
it.objectAttributeValues
247246
)
248247
}

kotlin-insight-client/kotlin-insight-client-test-applink/src/test/kotlin/it/InsightApplinkClientTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ class InsightApplinkClientTest constructor(
4141

4242
override val insightObjectOperator get() = HttpInsightObjectOperator(clientContext)
4343
override val insightAttachmentOperator get() = HttpInsightAttachmentOperator(clientContext)
44-
override val insightObjectTypeOperator get() = HttpInsightObjectTypeOperator(clientContext)
44+
override val insightSchemaOperator get() = HttpInsightSchemaOperator(clientContext)
4545
override val insightHistoryOperator get() = HttpInsightHistoryOperator(clientContext)
4646

4747
init {
48-
println("### Starting MainWiredTest")
48+
println("### Starting InsightApplinkClientTest")
4949

5050
println("### AppLinkUrl: ${applicationLinkService.getPrimaryApplicationLink(JiraApplicationType::class.java).displayUrl}")
5151
val serviceUser = userAccessor.getUserByName("admin")
@@ -57,7 +57,7 @@ class InsightApplinkClientTest constructor(
5757

5858
clientContext = HttpInsightClientContext("http://localhost:8080", httpClient)
5959

60-
println("### Starting MainWiredTest")
60+
println("### Starting InsightApplinkClientTest")
6161
}
6262

6363
@Before

0 commit comments

Comments
 (0)