Argument(Class)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
1
/*
2
 * Copyright 2014, Armenak Grigoryan, and individual contributors as indicated
3
 * by the @authors tag. See the copyright.txt in the distribution for a
4
 * full listing of individual contributors.
5
 *
6
 * This is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation; either version 2.1 of
9
 * the License, or (at your option) any later version.
10
 *
11
 * This software is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
 * Lesser General Public License for more details.
15
 */
16
package com.strider.datadefender.requirement.plan;
17
18
import com.strider.datadefender.requirement.ClassAdapter;
19
import com.strider.datadefender.requirement.TypeConverter;
20
import java.lang.reflect.InvocationTargetException;
21
import java.sql.SQLException;
22
import java.util.ArrayList;
23
import java.util.List;
24
import java.util.Objects;
25
import javax.xml.bind.Unmarshaller;
26
27
import javax.xml.bind.annotation.XmlAccessType;
28
import javax.xml.bind.annotation.XmlAccessorType;
29
import javax.xml.bind.annotation.XmlAttribute;
30
import javax.xml.bind.annotation.XmlElement;
31
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
32
33
import lombok.AccessLevel;
34
import lombok.Data;
35
import lombok.Getter;
36
import lombok.extern.log4j.Log4j2;
37
import org.apache.commons.collections.CollectionUtils;
38
39
/**
40
 * JAXB class that defines argument elements in Requirement.xml file
41
 *
42
 * @author Armenak Grigoryan
43
 */
44
@XmlAccessorType(XmlAccessType.NONE)
45
@Data
46
@Log4j2
47
public class Argument {
48
49
    @XmlAttribute
50
    private String name;
51
52
    @XmlJavaTypeAdapter(ClassAdapter.class)
53
    @XmlAttribute
54
    private Class<?> type = String.class;
55
56
    @Getter(AccessLevel.NONE)
57
    @XmlAttribute
58
    private String value;
59
60
    @XmlElement(name = "element")
61
    private List<ArrayElement> elements;
62
    
63
    @Getter(AccessLevel.NONE)
64
    private Object objectValue;
65
66
    /**
67
     * If set to true, "Value" is null, and elements are empty, uses the passed
68
     * value -- either the running value in a function chain (the return value
69
     * from the last call) or the value of the column, or the ResultSet at the
70
     * current row if "Type" is java.sql.ResultSet.
71
     */
72
    @XmlAttribute(name = "pass-current-value")
73
    private Boolean isDynamicValue;
74
75
    public Argument() {
76
    }
77
78
    public Argument(Class<?> type) {
79
        this.type = type;
80
    }
81
82
    public Argument(String name, Class<?> type, String value) {
83
        this(type);
84
        this.name = name;
85
        this.value = value;
86
    }
87
88
    public Argument(String name, Class<?> type, boolean isDynamicValue) {
89
        this(type);
90
        this.name = name;
91
        this.isDynamicValue = isDynamicValue;
92
    }
93
94
    /**
95
     * Sets up the value based on the type.
96
     *
97
     * @param unmarshaller
98
     * @param parent
99
     */
100
    public void afterUnmarshal(Unmarshaller unmarshaller, Object parent)
101
        throws InstantiationException,
102
        IllegalAccessException,
103
        IllegalArgumentException,
104
        InvocationTargetException {
105
        
106
        if (value != null) {
107
            log.debug("Converting value: {} to type: {}", value, type);
108
            objectValue = TypeConverter.convert(value, type);
109
        } else if (CollectionUtils.isNotEmpty(elements)) {
110
            List a = new ArrayList(elements.size());
111
            for (ArrayElement el : elements) {
112
                if (el.getValue() == null) {
113
                    continue;
114
                }
115
                a.add(TypeConverter.convert(el.getValue(), type));
116
            }
117
            objectValue = a;
118
        }
119
    }
120
121
    /**
122
     * Returns the value of the argument.
123
     *
124
     * The method is called with a dynamic value set from a previous invocation
125
     * in a chain of method calls, which is used if IsDynamicValue is set to
126
     * true.
127
     *
128
     * @param rs
129
     * @return
130
     * @throws SQLException
131
     */
132
    public Object getValue(Object lastValue)
133
        throws InstantiationException,
134
        IllegalAccessException,
135
        IllegalArgumentException,
136
        InvocationTargetException {
137
        
138
        if (Objects.equals(Boolean.TRUE, isDynamicValue) && value == null && elements == null) {
139
            log.debug("Using dynamic value for attribute");
140
            if (!type.isInstance(lastValue)) {
141
                log.debug("Converting dynamic attribute value: {} to type: {}", lastValue, type);
142
                return TypeConverter.convert(lastValue, type);
143
            }
144
            return lastValue;
145
        }
146
        return objectValue;
147
    }
148
149
    /**
150
     * Returns the String value of the attribute named "Value".
151
     */
152
    public String getValueAttribute() {
153
        return value;
154
    }
155
}
156