-
Notifications
You must be signed in to change notification settings - Fork 0
/
exploitationreport.go
441 lines (380 loc) · 18 KB
/
exploitationreport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package main
import (
"fmt"
"reflect"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
var getExploitationReportsForQueryString = getObjectByQueryFromLedger
/*
* generateExploitationReports function contains business logic to insert new Exploitation Reports to the Ledger
*
* @params {Array} args
* @property {string} 0 - stringified JSON array of exploitation report.
* @return {pb.Response} - peer Response
*/
func generateExploitationReports(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var methodName = "addExploitationReports"
logger.Info("ENTERING >", methodName, args)
type ExploitationReportResponse struct {
ExploitationReportUUID string `json:"exploitationReportUUID"`
Message string `json:"message"`
Success bool `json:"success"`
}
type ExploitationReportOutput struct {
SuccessCount int `json:"successCount"`
FailureCount int `json:"failureCount"`
ExploitationReportResponses []ExploitationReportResponse `json:"exploitationReportResponses"`
RoyaltyStatements []RoyaltyStatement `json:"royaltyStatements"`
ExploitationReports []ExploitationReport `json:"exploitationReports"`
}
// check if array length is greater than 0
if len(args) < 1 {
return getErrorResponse("Missing arguments: Array of Exploitation Report objects is required")
}
exploitationReportOutput := ExploitationReportOutput{}
exploitationReportOutput.RoyaltyStatements = []RoyaltyStatement{}
exploitationReports := &[]ExploitationReport{}
exploitationReportResponses := []ExploitationReportResponse{}
// unmarshal the args input to an array of exploitation report records
err := jsonToObject([]byte(args[0]), exploitationReports)
if err != nil {
return getErrorResponse(err.Error())
}
// iterate over exploitation reports
for _, exploitationReport := range *exploitationReports {
royaltyStatements := []RoyaltyStatement{}
exploitationReport.DocType = EXPLOITATIONREPORT
exploitationReport.State = INITIAL
exploitationReportResponse := ExploitationReportResponse{}
exploitationReportResponse.ExploitationReportUUID = exploitationReport.ExploitationReportUUID
exploitationReportResponse.Success = true
// check if exploitation report with the UUID exists on the ledger.
exploitationReportExistingBytes, _ := stub.GetState(exploitationReport.ExploitationReportUUID)
if exploitationReportExistingBytes != nil {
exploitationReportResponse.Success = false
exploitationReportResponse.Message = "Exploitation Report already exists!"
exploitationReportResponses = append(exploitationReportResponses, exploitationReportResponse)
exploitationReportOutput.FailureCount++
continue
}
// create exploitation report parameters to evaluate the selector expressions
exploitationReportParameters, _ := getEvaluableParameters(&exploitationReport)
// query copyright data reports
queryString := fmt.Sprintf("{\"selector\":{\"docType\":\"%s\",\"isrc\":\"%s\", \"startDate\": { \"$lte\": \"%s\" }, \"endDate\": { \"$gte\": \"%s\" }}}", COPYRIGHTDATAREPORT, exploitationReport.Isrc, exploitationReport.ExploitationDate, exploitationReport.ExploitationDate)
copyrightDataReports, _ := queryCopyrightDataReports(stub, queryString)
// set the percentage. used for calculating incomplete royalty statement splits
totalPercentage := 0.0
for _, copyrightDataReport := range copyrightDataReports {
// get all the right holders and evaluate againist right holder selector expression
for _, rightHolder := range copyrightDataReport.RightHolders {
isSelectorValid := false
// generate royalty statements for copy right holders with empty selector
if rightHolder.Selector == "" {
isSelectorValid = true
} else {
isSelectorValidResult, err := evaluate(rightHolder.Selector, exploitationReportParameters)
if err != nil {
logger.Errorf("%s - Failed to get a valid evaluator for right holder ipi %s, selector %s. Error: %s", methodName, rightHolder.IPI, rightHolder.Selector, err.Error())
}
//to prevent a crash when test cases are run.
if reflect.ValueOf(isSelectorValidResult).IsValid() {
isSelectorValid = isSelectorValidResult.(bool)
}
}
if isSelectorValid {
// generate royalty statment
royaltyStatement := RoyaltyStatement{}
// set the royalty statment right holder
royaltyStatement.RightHolder = rightHolder.IPI
// set the right type to OWNERSHIP as the royalty statement is between DSP and owner adminsitrator
royaltyStatement.RightType = OWNERSHIP
royaltyStatement.Amount = toFixed(exploitationReport.Amount*rightHolder.Percent*0.01, 2)
royaltyStatements = append(royaltyStatements, royaltyStatement)
totalPercentage += rightHolder.Percent
}
}
}
// check the total percentage
if totalPercentage > 100 {
// if totalPercentage > 100, do not generate royalty reports
exploitationReport.State = INCONSISTENT_COPYRIGHT_SPLIT
} else {
// if totalPercentage < 100, set the exploitation report state as incomplete
if len(royaltyStatements) == 0 {
exploitationReport.State = UNKOWN_ISRC
} else if totalPercentage == 0 { // if royal statements exisys and the total percentage is 0
exploitationReport.State = INCOMPLETE_COPYRIGHT_SPLIT
} else if totalPercentage < 100 { // totalPercentage < 100 if there are missing copyright holders
exploitationReport.State = MISSING_COPYRIGHT_HOLDER
}
// for all the royalty statments, find the owner administrator and affiliation
for _, royaltyStatement := range royaltyStatements {
royaltyStatement.DocType = ROYALTYSTATEMENT
royaltyStatement.Source = exploitationReport.Source
royaltyStatement.SongTitle = exploitationReport.SongTitle
royaltyStatement.Isrc = exploitationReport.Isrc
royaltyStatement.ExploitationReportUUID = exploitationReport.ExploitationReportUUID
royaltyStatement.ExploitationDate = exploitationReport.ExploitationDate
royaltyStatement.WriterName = exploitationReport.WriterName
royaltyStatement.Units = exploitationReport.Units
royaltyStatement.Territory = exploitationReport.Territory
royaltyStatement.UsageType = exploitationReport.UsageType
royaltyStatement.Administrator = ""
royaltyStatement.Collector = ""
//removing the following since owner administrations and affiliations are deprecated.
// query owner administrations
/*queryString := fmt.Sprintf("{\"selector\":{\"docType\":\"%s\",\"owner\":\"%s\", \"startDate\": { \"$lte\": \"%s\" }, \"endDate\": { \"$gte\": \"%s\" }}}", OWNERADMINISTRATION, royaltyStatement.RightHolder, exploitationReport.ExploitationDate, exploitationReport.ExploitationDate)
ownerAdministrations, _ := queryOwnerAdministrations(stub, queryString)
royaltyStatement.State = MISSING_REPRESENTATIVE
for _, ownerAdministration := range ownerAdministrations {
for _, representation := range ownerAdministration.Representations {
isSelectorValid := false
// set owner representation for a royalty statement with empty selector
if representation.Selector == "" {
isSelectorValid = true
} else {
isSelectorValidResult, _ := evaluate(representation.Selector, exploitationReportParameters)
isSelectorValid = isSelectorValidResult.(bool)
}
if isSelectorValid {
royaltyStatement.State = MISSING_AFFILIATE
// set the royalty statement administrator
royaltyStatement.Administrator = representation.Representative
// query owner administrations
queryString := fmt.Sprintf("{\"selector\":{\"docType\":\"%s\",\"administrator\":\"%s\", \"startDate\": { \"$lte\": \"%s\" }, \"endDate\": { \"$gte\": \"%s\" }}}", ADMINISTRATORAFFILIATION, representation.Representative, exploitationReport.ExploitationDate, exploitationReport.ExploitationDate)
administratorAffiliations, _ := queryAdministratorAffiliations(stub, queryString)
for _, administratorAffiliation := range administratorAffiliations {
for _, affiliation := range administratorAffiliation.Affiliations {
isSelectorValid = false
// set administrator affiliation for a royalty statement with empty selector
if affiliation.Selector == "" {
isSelectorValid = true
} else {
isSelectorValidResult, _ := evaluate(affiliation.Selector, exploitationReportParameters)
isSelectorValid = isSelectorValidResult.(bool)
}
if isSelectorValid {
royaltyStatement.State = INITIAL
// set the royalty statement afflliation
royaltyStatement.Collector = affiliation.Affiliate
break
}
}
}
break
}
}
}*/
// add the royalty statement to output
exploitationReportOutput.RoyaltyStatements = append(exploitationReportOutput.RoyaltyStatements, royaltyStatement)
}
}
// record exploitation report on ledger
// exploitationReportBytes, err := objectToJSON(exploitationReport)
// if err != nil {
// exploitationReportResponse.Success = false
// exploitationReportResponse.Message = err.Error()
// exploitationReportResponses = append(exploitationReportResponses, exploitationReportResponse)
// exploitationReportOutput.FailureCount++
// continue
// }
exploitationReportOutput.ExploitationReports = append(exploitationReportOutput.ExploitationReports, exploitationReport)
// err = stub.PutState(exploitationReport.ExploitationReportUUID, exploitationReportBytes)
// if err != nil {
// exploitationReportResponse.Success = false
// exploitationReportResponse.Message = err.Error()
// }
if exploitationReportResponse.Success {
exploitationReportOutput.SuccessCount++
} else {
exploitationReportResponses = append(exploitationReportResponses, exploitationReportResponse)
exploitationReportOutput.FailureCount++
}
}
exploitationReportOutput.ExploitationReportResponses = exploitationReportResponses
objBytes, _ := objectToJSON(exploitationReportOutput)
logger.Info("EXITING <", methodName, exploitationReportOutput)
return shim.Success(objBytes)
}
/*
* updateExploitationReports function contains business logic to update Exploitation Reports to the Ledger
*
* @params {Array} args
* @property {string} 0 - stringified JSON array of exploitation report.
* @return {pb.Response} - peer Response
*/
func updateExploitationReports(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var methodName = "updateExploitationReports"
logger.Info("ENTERING >", methodName, args)
type ExploitationReportResponse struct {
ExploitationReportUUID string `json:"exploitationReportUUID"`
Message string `json:"message"`
Success bool `json:"success"`
}
type ExploitationReportOutput struct {
SuccessCount int `json:"successCount"`
FailureCount int `json:"failureCount"`
ExploitationReports []ExploitationReportResponse `json:"exploitationReports"`
}
//Check if array length is greater than 0
if len(args) < 1 {
return getErrorResponse("Missing arguments: Array of Exploitation Report objects is required")
}
exploitationReportOutput := ExploitationReportOutput{}
exploitationReports := &[]ExploitationReport{}
exploitationReportResponses := []ExploitationReportResponse{}
//Unmarshal the args input to an array of exploitation report records
err := jsonToObject([]byte(args[0]), exploitationReports)
if err != nil {
return getErrorResponse(err.Error())
}
// iterate over Exploitation Reports
for _, exploitationReport := range *exploitationReports {
exploitationReport.DocType = EXPLOITATIONREPORT
exploitationReportResponse := ExploitationReportResponse{}
exploitationReportResponse.ExploitationReportUUID = exploitationReport.ExploitationReportUUID
exploitationReportResponse.Success = true
// convert exploitation report to bytes
exploitationReportBytes, err := objectToJSON(exploitationReport)
if err != nil {
exploitationReportResponse.Success = false
exploitationReportResponse.Message = err.Error()
exploitationReportResponses = append(exploitationReportResponses, exploitationReportResponse)
exploitationReportOutput.FailureCount++
continue
}
// check if exploitation report with the UUID exists on the ledger.
exploitationReportExistingBytes, err := stub.GetState(exploitationReport.ExploitationReportUUID)
if exploitationReportExistingBytes == nil {
exploitationReportResponse.Success = false
exploitationReportResponse.Message = "Exploitation Report does not exist!"
exploitationReportResponses = append(exploitationReportResponses, exploitationReportResponse)
exploitationReportOutput.FailureCount++
continue
}
// update exploitation report on ledger
err = stub.PutState(exploitationReport.ExploitationReportUUID, exploitationReportBytes)
if err != nil {
exploitationReportResponse.Success = false
exploitationReportResponse.Message = err.Error()
}
if exploitationReportResponse.Success {
exploitationReportOutput.SuccessCount++
} else {
exploitationReportResponses = append(exploitationReportResponses, exploitationReportResponse)
exploitationReportOutput.FailureCount++
}
}
exploitationReportOutput.ExploitationReports = exploitationReportResponses
objBytes, _ := objectToJSON(exploitationReportOutput)
logger.Info("EXITING <", methodName, exploitationReportOutput)
return shim.Success(objBytes)
}
/*
* insertExploitationReports function contains business logic to insert Exploitation Reports to the Ledger
*
* @params {Array} args
* @property {string} 0 - stringified JSON array of exploitation report.
* @return {pb.Response} - peer Response
*/
func insertExploitationReports(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var methodName = "insertExploitationReports"
logger.Info("ENTERING >", methodName, args)
type ExploitationReportResponse struct {
ExploitationReportUUID string `json:"exploitationReportUUID"`
Message string `json:"message"`
Success bool `json:"success"`
}
type ExploitationReportOutput struct {
SuccessCount int `json:"successCount"`
FailureCount int `json:"failureCount"`
ExploitationReports []ExploitationReportResponse `json:"exploitationReports"`
}
//Check if array length is greater than 0
if len(args) < 1 {
return getErrorResponse("Missing arguments: Array of Exploitation Report objects is required")
}
exploitationReportOutput := ExploitationReportOutput{}
exploitationReports := &[]ExploitationReport{}
exploitationReportResponses := []ExploitationReportResponse{}
//Unmarshal the args input to an array of exploitation report records
err := jsonToObject([]byte(args[0]), exploitationReports)
if err != nil {
return getErrorResponse(err.Error())
}
// iterate over Exploitation Reports
for _, exploitationReport := range *exploitationReports {
exploitationReport.DocType = EXPLOITATIONREPORT
exploitationReportResponse := ExploitationReportResponse{}
exploitationReportResponse.ExploitationReportUUID = exploitationReport.ExploitationReportUUID
exploitationReportResponse.Success = true
// check if exploitation report with the UUID exists on the ledger.
exploitationReportExistingBytes, _ := stub.GetState(exploitationReport.ExploitationReportUUID)
if exploitationReportExistingBytes != nil {
exploitationReportResponse.Success = false
exploitationReportResponse.Message = "Exploitation Report already exists!"
exploitationReportResponses = append(exploitationReportResponses, exploitationReportResponse)
exploitationReportOutput.FailureCount++
continue
}
// convert exploitation report to bytes
exploitationReportBytes, err := objectToJSON(exploitationReport)
if err != nil {
exploitationReportResponse.Success = false
exploitationReportResponse.Message = err.Error()
exploitationReportResponses = append(exploitationReportResponses, exploitationReportResponse)
exploitationReportOutput.FailureCount++
continue
}
// update exploitation report on ledger
err = stub.PutState(exploitationReport.ExploitationReportUUID, exploitationReportBytes)
if err != nil {
exploitationReportResponse.Success = false
exploitationReportResponse.Message = err.Error()
}
if exploitationReportResponse.Success {
exploitationReportOutput.SuccessCount++
} else {
exploitationReportResponses = append(exploitationReportResponses, exploitationReportResponse)
exploitationReportOutput.FailureCount++
}
}
exploitationReportOutput.ExploitationReports = exploitationReportResponses
objBytes, _ := objectToJSON(exploitationReportOutput)
logger.Info("EXITING <", methodName, exploitationReportOutput)
return shim.Success(objBytes)
}
/*
* getExploitationReports function contains business logic to retrieve Exploitation Reports
*
* @params {Array} args
* @property {string} 0 - the query string.
* @return {pb.Response} - peer Response
*/
func getExploitationReports(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var methodName = "getExploitationReports"
logger.Info("ENTERING >", methodName, args)
queryString := fmt.Sprintf("{\"selector\":{\"docType\":\"%s\"}}", EXPLOITATIONREPORT)
if len(args) == 1 {
queryString = args[0]
}
logger.Info("%s - executing rich query : %s.", methodName, queryString)
// get exploitation reports based on the rich query selector
queryResult, err := getExploitationReportsForQueryString(stub, queryString) //getQueryResultInBytes(stub, queryString)
if err != nil {
return getErrorResponse(err.Error())
}
var resultExploitationReports []ExploitationReport
err = sliceToStruct(queryResult, &resultExploitationReports)
if err != nil {
return getErrorResponse(err.Error())
}
queryResultBytes, err := objectToJSON(resultExploitationReports)
if err != nil {
return getErrorResponse(err.Error())
}
logger.Info("result(s) received from couch db: %s", string(queryResultBytes))
//return bytes as result
return shim.Success(queryResultBytes)
}