-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkOrder.go
141 lines (118 loc) · 3.5 KB
/
WorkOrder.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
package aspace
import (
"bufio"
"bytes"
"encoding/csv"
"fmt"
"io"
"strings"
)
// HEADER_ROW is the list of expected work order field names
var HEADER_ROW = []string{"Resource ID", "Ref ID", "URI", "Container Indicator 1", "Container Indicator 2", "Container Indicator 3", "Title", "Component ID"}
// A WorkOrder stores the header and constituent rows for a parsed work order
type WorkOrder struct {
Header []string
Rows []WorkOrderRow
}
// A WorkOrderRow holds the values for a single row in the parsed WorkOrder
type WorkOrderRow struct {
fields []string
}
func (wor WorkOrderRow) String() string {
var b bytes.Buffer
out := csv.NewWriter(bufio.NewWriter(&b))
out.Comma = '\t'
out.Write(wor.fields)
out.Flush()
// the csv writer adds a newline, so we need to trim it
return strings.Trim(b.String(), "\n")
}
// assertHeaderFields ensures that the fields in the the work order being processed match expectations
func (wo *WorkOrder) assertHeaderFields() error {
if len(wo.Header) != len(HEADER_ROW) {
return fmt.Errorf("number of columns in work order header in work order does match expectations")
}
var errors []string
for i, v := range HEADER_ROW {
if wo.Header[i] != v {
errors = append(errors, fmt.Sprintf("header mismatch: expected: '%s' got: '%s'", v, wo.Header[i]))
}
}
if len(errors) != 0 {
return fmt.Errorf("%s", errors)
}
return nil
}
func newWorkOrderRow(record []string) WorkOrderRow {
var wo WorkOrderRow
wo.fields = record
return wo
}
// NewWorkOrder returns a pointer to a newly constructed WorkOrder
func NewWorkOrder() *WorkOrder {
// this function is here in case we need to perform further initialization
return new(WorkOrder)
}
// Load reads and parses data from an io.Reader storing them in the WorkOrder.
// This method returns an error if the header fields do not match expectations.
func (wo *WorkOrder) Load(r io.Reader) error {
wor := csv.NewReader(r)
wor.Comma = rune('\t')
headerRow := true
for {
record, err := wor.Read()
if err != nil {
if err == io.EOF {
break
} else {
return err
}
}
if headerRow {
wo.Header = record
err = wo.assertHeaderFields()
if err != nil {
return err
}
headerRow = false
wo.Header = record
continue
}
// add the row to the work order
wo.Rows = append(wo.Rows, newWorkOrderRow(record))
}
return nil
}
// accessors
// GetResourceID returns the "Resource ID" value for the WorkOrderRow
func (wor *WorkOrderRow) GetResourceID() string {
return wor.fields[0]
}
// GetRefID returns the "Ref ID" value for the WorkOrderRow
func (wor *WorkOrderRow) GetRefID() string {
return wor.fields[1]
}
// GetURI returns the "URI" value for the WorkOrderRow
func (wor *WorkOrderRow) GetURI() string {
return wor.fields[2]
}
// GetContainerIndicator1 returns the "Container Indicator 1" value for the WorkOrderRow
func (wor *WorkOrderRow) GetContainerIndicator1() string {
return wor.fields[3]
}
// GetContainerIndicator2 returns the "Container Indicator 2" value for the WorkOrderRow
func (wor *WorkOrderRow) GetContainerIndicator2() string {
return wor.fields[4]
}
// GetContainerIndicator3 returns the "Container Indicator 3" value for the WorkOrderRow
func (wor *WorkOrderRow) GetContainerIndicator3() string {
return wor.fields[5]
}
// GetTitle returns the "Title" value for the WorkOrderRow
func (wor *WorkOrderRow) GetTitle() string {
return wor.fields[6]
}
// GetComponentID returns the "Component ID" value for the WorkOrderRow
func (wor *WorkOrderRow) GetComponentID() string {
return wor.fields[7]
}