-
Notifications
You must be signed in to change notification settings - Fork 5
/
activity.go
88 lines (72 loc) · 2.07 KB
/
activity.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
/*
* Copyright © 2018. TIBCO Software Inc.
* This file is subject to the license terms contained
* in the license file that is distributed with this file.
*/
package query
// Imports
import (
"fmt"
"strings"
"github.com/TIBCOSoftware/dovetail-contrib/libraries/fabric-go/utils"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/core/data"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
// Constants
const (
ivAssetName = "assetName"
ivData = "queryString"
ivParams = "input"
ivStub = "containerServiceStub"
ovOutput = "output"
)
var logger = shim.NewLogger("FlowCC")
// describes the metadata of the activity as found in the activity.json file
type LedgerActivity struct {
metadata *activity.Metadata
}
func NewActivity(metadata *activity.Metadata) activity.Activity {
return &LedgerActivity{metadata: metadata}
}
func (a *LedgerActivity) Metadata() *activity.Metadata {
return a.metadata
}
// Eval executes the activity
func (a *LedgerActivity) Eval(context activity.Context) (done bool, err error) {
stub, err := utils.GetContainerStub(context)
if err != nil {
return false, err
}
queryString, ok := context.GetInput(ivData).(string)
if !ok {
return false, fmt.Errorf("query string is not initialized")
}
stub.GetLogService().Debug("query string = " + queryString)
params := context.GetInput(ivParams)
var parameters []interface{}
if params != nil {
parameters, err = utils.GetInputData(params.(*data.ComplexObject), false)
if err != nil {
return false, err
}
}
if parameters != nil && len(parameters) > 0 {
inputParam := parameters[0].(map[string]interface{})
for n, p := range inputParam {
queryString = strings.Replace(queryString, "_$"+n, fmt.Sprintf("%v", p), -1)
}
}
result, err := stub.GetDataService().QueryState(queryString)
if err != nil {
return false, err
}
output, err := utils.ParseRecords(result)
if err != nil {
return false, err
}
complexOutput := &data.ComplexObject{}
complexOutput.Value = output
context.SetOutput(ovOutput, complexOutput)
return true, nil
}