/
activity.go
executable file
·138 lines (111 loc) · 3.25 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
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
/*
* Copyright © 2020. TIBCO Software Inc.
* This file is subject to the license terms contained
* in the license file that is distributed with this file.
*/
package graphtofile
import (
"encoding/json"
"fmt"
"os"
"sync"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/logger"
"github.com/TIBCOSoftware/labs-graphbuilder-lib/util"
)
const (
OUTPUT_FILE = "outputFile"
)
var log = logger.GetLogger("tibco-activity-graphtofile")
type TGDBUpsertActivity struct {
metadata *activity.Metadata
outputFiles map[string]string
mux sync.Mutex
}
func NewActivity(metadata *activity.Metadata) activity.Activity {
return &TGDBUpsertActivity{
metadata: metadata,
outputFiles: make(map[string]string),
}
}
func (a *TGDBUpsertActivity) Metadata() *activity.Metadata {
return a.metadata
}
func (a *TGDBUpsertActivity) Eval(context activity.Context) (done bool, err error) {
log.Info("Graph to file entering ......... ")
graph, exists := context.GetInput("Graph").(map[string]interface{})["graph"].(map[string]interface{})
if !exists {
return false, fmt.Errorf("Unable to get data from input!!")
}
outputFile, err := a.getOutputFile(graph["id"].(string), context)
if nil != err {
return false, err
}
a.mux.Lock()
defer a.mux.Unlock()
log.Debug("File name : ", outputFile)
f, err := os.OpenFile(outputFile, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
defer f.Close()
nodes := graph["nodes"].(map[string]interface{})
for _, node := range nodes {
nodeDetail := node.(map[string]interface{})
jsonString, _ := json.Marshal(nodeDetail)
log.Debug("node -> " + string(jsonString))
f.WriteString(string(jsonString) + "\r\n")
}
edges := graph["edges"].(map[string]interface{})
for _, edge := range edges {
edgeDetail := edge.(map[string]interface{})
jsonString, _ := json.Marshal(edgeDetail)
log.Debug("edge -> " + string(jsonString))
f.WriteString(string(jsonString) + "\r\n")
}
//Set Message ID in the output
//context.SetOutput(ovMessageId, *response.MessageId)
log.Info("Update graph to file exit ......... ")
return true, nil
}
func (a *TGDBUpsertActivity) getOutputFile(graphId string, context activity.Context) (string, error) {
myId := util.ActivityId(context)
outputFile := a.outputFiles[myId]
if "" == outputFile {
a.mux.Lock()
defer a.mux.Unlock()
outputFile = a.outputFiles[myId]
if "" == outputFile {
log.Info("Initializing output folder start ...")
outputFileSetup, _ := context.GetSetting(OUTPUT_FILE)
outputFile = util.ReplaceParameter(outputFileSetup.(string), util.GRAPH_ID, graphId)
outputFolderPath, _ := util.SplitFilename(outputFile)
err := os.MkdirAll(outputFolderPath, os.ModePerm)
if nil != err {
log.Error("Unable to create folder ...")
return "", err
}
fileExist := true
_, err = os.Stat(outputFile)
if nil != err {
if os.IsNotExist(err) {
fileExist = false
}
}
if fileExist {
err = os.Remove(outputFile)
if err != nil {
log.Error(err)
}
}
_, err = os.Create(outputFile)
if nil != err {
log.Error("Unable to create file ...")
return "", err
}
log.Info("Initializing TGDB Service end ...")
a.outputFiles[myId] = outputFile
}
}
return outputFile, nil
}