| 1 |
package element |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strconv" |
| 6 |
|
| 7 |
"github.com/mitch000001/go-hbci/charset" |
| 8 |
"github.com/mitch000001/go-hbci/domain" |
| 9 |
) |
| 10 |
|
| 11 |
// These represent HBCI acknowledgement codes. Codes starting with 3 are meant |
| 12 |
// to be warnings. |
| 13 |
const ( |
| 14 |
AcknowledgementAdditionalInformation = 3040 |
| 15 |
AcknowledgementSupportedSecurityFunction = 3920 |
| 16 |
) |
| 17 |
|
| 18 |
// NewAcknowledgement returns a new acknowledgement DataElement |
| 19 |
func NewAcknowledgement(acknowledgement domain.Acknowledgement) *AcknowledgementDataElement { |
| 20 |
a := &AcknowledgementDataElement{ |
| 21 |
Code: NewDigit(acknowledgement.Code, 4), |
| 22 |
ReferenceDataElement: NewAlphaNumeric(acknowledgement.ReferenceDataElement, 7), |
| 23 |
Text: NewAlphaNumeric(acknowledgement.Text, 80), |
| 24 |
Params: NewParams(10, 10, acknowledgement.Params...), |
| 25 |
} |
| 26 |
a.DataElement = NewDataElementGroup(acknowledgementDEG, 4, a) |
| 27 |
return a |
| 28 |
} |
| 29 |
|
| 30 |
// An AcknowledgementDataElement defines a group of DataElements used to |
| 31 |
// transmit information from the bank institute to the client. |
| 32 |
type AcknowledgementDataElement struct { |
| 33 |
DataElement |
| 34 |
Code *DigitDataElement |
| 35 |
ReferenceDataElement *AlphaNumericDataElement |
| 36 |
Text *AlphaNumericDataElement |
| 37 |
Params *ParamsDataElement |
| 38 |
referencingMessage domain.MessageReference |
| 39 |
referencingSegmentPosition int |
| 40 |
typ string |
| 41 |
} |
| 42 |
|
| 43 |
// SetReferencingMessage is used by MessageAcknowledgements to set the reference |
| 44 |
// to the previously sent message. This is needed to identify the message within |
| 45 |
// an ongoing dialog. |
| 46 |
func (a *AcknowledgementDataElement) SetReferencingMessage(reference domain.MessageReference) { |
| 47 |
a.referencingMessage = reference |
| 48 |
} |
| 49 |
|
| 50 |
// SetReferencingSegmentPosition is a setter for setting the Segment position this |
| 51 |
// Acknowledgement is referring to. |
| 52 |
func (a *AcknowledgementDataElement) SetReferencingSegmentPosition(position int) { |
| 53 |
a.referencingSegmentPosition = position |
| 54 |
} |
| 55 |
|
| 56 |
// SetType sets the type of the Acknowledgement. There are only two types of |
| 57 |
// Acknowledgements, message related ones and segment related ones. |
| 58 |
// |
| 59 |
// See domain.MessageAcknowledgements or domain.SegmentAcknowledgement for |
| 60 |
// details on semantics. |
| 61 |
func (a *AcknowledgementDataElement) SetType(acknowledgementType string) { |
| 62 |
a.typ = acknowledgementType |
| 63 |
} |
| 64 |
|
| 65 |
// Val returns the underlying value of the acknowledgement. |
| 66 |
func (a *AcknowledgementDataElement) Val() domain.Acknowledgement { |
| 67 |
return domain.Acknowledgement{ |
| 68 |
Code: a.Code.Val(), |
| 69 |
ReferenceDataElement: a.ReferenceDataElement.Val(), |
| 70 |
Text: a.Text.Val(), |
| 71 |
Params: a.Params.Val(), |
| 72 |
Type: a.typ, |
| 73 |
ReferencingMessage: a.referencingMessage, |
| 74 |
ReferencingSegmentNumber: a.referencingSegmentPosition, |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// IsValid returns true if the acknowledgment code and text is set and the |
| 79 |
// underlying value is valid. |
| 80 |
func (a *AcknowledgementDataElement) IsValid() bool { |
| 81 |
if a.Code == nil || a.Text == nil { |
| 82 |
return false |
| 83 |
} |
| 84 |
return a.DataElement.IsValid() |
| 85 |
} |
| 86 |
|
| 87 |
// GroupDataElements returns all grouped elements within the acknowledgment. |
| 88 |
func (a *AcknowledgementDataElement) GroupDataElements() []DataElement { |
| 89 |
return []DataElement{ |
| 90 |
a.Code, |
| 91 |
a.ReferenceDataElement, |
| 92 |
a.Text, |
| 93 |
a.Params, |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// UnmarshalHBCI unmarshals the value into an acknowledgment. |
| 98 |
func (a *AcknowledgementDataElement) UnmarshalHBCI(value []byte) error { |
| 99 |
acknowledgement := domain.Acknowledgement{} |
| 100 |
chunks, err := ExtractElements(value) |
| 101 |
if err != nil { |
| 102 |
return err |
| 103 |
} |
| 104 |
if len(chunks) < 3 { |
| 105 |
return fmt.Errorf("Malformed acknowledgment to unmarshal") |
| 106 |
} |
| 107 |
code, err := strconv.Atoi(charset.ToUTF8(chunks[0])) |
| 108 |
if err != nil { |
| 109 |
return fmt.Errorf("%T: Malformed code", a) |
| 110 |
} |
| 111 |
acknowledgement.Code = code |
| 112 |
acknowledgement.ReferenceDataElement = charset.ToUTF8(chunks[1]) |
| 113 |
acknowledgement.Text = charset.ToUTF8(chunks[2]) |
| 114 |
if len(chunks) > 3 { |
| 115 |
params := make([]string, len(chunks[3:])) |
| 116 |
for i, chunk := range chunks[3:] { |
| 117 |
params[i] = charset.ToUTF8(chunk) |
| 118 |
} |
| 119 |
acknowledgement.Params = params |
| 120 |
} |
| 121 |
*a = *NewAcknowledgement(acknowledgement) |
| 122 |
return nil |
| 123 |
} |
| 124 |
|
| 125 |
// NewParams returns a new ParamsDataElement. |
| 126 |
func NewParams(min, max int, params ...string) *ParamsDataElement { |
| 127 |
var paramDE []DataElement |
| 128 |
for _, p := range params { |
| 129 |
paramDE = append(paramDE, NewAlphaNumeric(p, 35)) |
| 130 |
} |
| 131 |
return &ParamsDataElement{arrayElementGroup: newArrayElementGroup(acknowlegdementParamsGDEG, min, max, paramDE)} |
| 132 |
} |
| 133 |
|
| 134 |
// ParamsDataElement defines a DataElement describing generic parameters |
| 135 |
type ParamsDataElement struct { |
| 136 |
*arrayElementGroup |
| 137 |
} |
| 138 |
|
| 139 |
// Val returns the underlying value of the DataElement. |
| 140 |
func (p *ParamsDataElement) Val() []string { |
| 141 |
params := make([]string, len(p.array)) |
| 142 |
for i, de := range p.array { |
| 143 |
params[i] = de.Value().(string) |
| 144 |
} |
| 145 |
return params |
| 146 |
} |
| 147 |
|
| 148 |
// UnmarshalHBCI unmarshals the value into a ParamsDataElement |
| 149 |
func (p *ParamsDataElement) UnmarshalHBCI(value []byte) error { |
| 150 |
elements, err := ExtractElements(value) |
| 151 |
if err != nil { |
| 152 |
return nil |
| 153 |
} |
| 154 |
if len(elements) > 10 { |
| 155 |
return fmt.Errorf("Malformed params") |
| 156 |
} |
| 157 |
dataElements := make([]DataElement, len(elements)) |
| 158 |
for i, elem := range elements { |
| 159 |
dataElements[i] = NewAlphaNumeric(charset.ToUTF8(elem), 35) |
| 160 |
} |
| 161 |
p.arrayElementGroup = newArrayElementGroup(acknowlegdementParamsGDEG, 10, 10, dataElements) |
| 162 |
return nil |
| 163 |
} |