|
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; |
|
17
|
|
|
|
|
18
|
|
|
import com.strider.datadefender.requirement.plan.Plan; |
|
19
|
|
|
import com.strider.datadefender.requirement.plan.PlanRef; |
|
20
|
|
|
import java.lang.reflect.InvocationTargetException; |
|
21
|
|
|
import java.sql.ResultSet; |
|
22
|
|
|
import java.sql.SQLException; |
|
23
|
|
|
import java.util.ArrayList; |
|
24
|
|
|
import java.util.List; |
|
25
|
|
|
|
|
26
|
|
|
import javax.xml.bind.annotation.XmlAccessType; |
|
27
|
|
|
import javax.xml.bind.annotation.XmlAccessorType; |
|
28
|
|
|
import javax.xml.bind.annotation.XmlAttribute; |
|
29
|
|
|
import javax.xml.bind.annotation.XmlElement; |
|
30
|
|
|
import javax.xml.bind.annotation.XmlElementWrapper; |
|
31
|
|
|
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; |
|
32
|
|
|
|
|
33
|
|
|
import org.apache.commons.collections4.CollectionUtils; |
|
34
|
|
|
import org.apache.commons.lang3.ClassUtils; |
|
35
|
|
|
|
|
36
|
|
|
import lombok.Data; |
|
37
|
|
|
import lombok.extern.log4j.Log4j2; |
|
38
|
|
|
|
|
39
|
|
|
import static java.util.Collections.unmodifiableList; |
|
40
|
|
|
import org.apache.commons.beanutils.ConvertUtils; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* JAXB class that defines column elements in Requirement.xml file |
|
44
|
|
|
* |
|
45
|
|
|
* @author Armenak Grigoryan |
|
46
|
|
|
*/ |
|
47
|
|
|
@Log4j2 |
|
48
|
|
|
@Data |
|
49
|
|
|
@XmlAccessorType(XmlAccessType.NONE) |
|
50
|
|
|
public class Column { |
|
51
|
|
|
|
|
52
|
|
|
@XmlAttribute |
|
53
|
|
|
private String name; |
|
54
|
|
|
|
|
55
|
|
|
@XmlAttribute(name = "skip-empty") |
|
56
|
|
|
private boolean ignoreEmpty = true; |
|
57
|
|
|
|
|
58
|
|
|
@XmlJavaTypeAdapter(ClassAdapter.class) |
|
59
|
|
|
@XmlAttribute |
|
60
|
|
|
private Class<?> type = String.class; |
|
61
|
|
|
|
|
62
|
|
|
@XmlElement |
|
63
|
|
|
private Plan plan; |
|
64
|
|
|
|
|
65
|
|
|
@XmlElement(name = "plan-ref") |
|
66
|
|
|
private PlanRef planRef; |
|
67
|
|
|
|
|
68
|
|
|
@XmlElementWrapper(name = "exclusions") |
|
69
|
|
|
@XmlElement(name = "exclude") |
|
70
|
|
|
private List<Exclude> exclusions; |
|
71
|
|
|
|
|
72
|
|
|
public Column() { |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public Column(String name) { |
|
76
|
|
|
this.name = name; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns a list of exclusions |
|
81
|
|
|
* |
|
82
|
|
|
* @return List<Exclude> |
|
83
|
|
|
*/ |
|
84
|
|
|
public List<Exclude> getExclusions() { |
|
85
|
|
|
final List<Exclude> lst = new ArrayList<>(); |
|
86
|
|
|
if (!CollectionUtils.isEmpty(exclusions)) { |
|
87
|
|
|
lst.addAll(exclusions); |
|
88
|
|
|
} |
|
89
|
|
|
if (ignoreEmpty) { |
|
90
|
|
|
lst.add(new Exclude(true)); |
|
91
|
|
|
} |
|
92
|
|
|
return unmodifiableList(lst); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns the plan used by this column, either the one defined under it, |
|
97
|
|
|
* or the one referenced by planRef. |
|
98
|
|
|
* |
|
99
|
|
|
* @return |
|
100
|
|
|
*/ |
|
101
|
|
|
public Plan getResolvedPlan() { |
|
102
|
|
|
if (planRef != null) { |
|
103
|
|
|
return planRef.getRef(); |
|
104
|
|
|
} |
|
105
|
|
|
return plan; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Calls all functions defined under Functions in order. |
|
110
|
|
|
* |
|
111
|
|
|
* @param rs ResultSet |
|
112
|
|
|
* @return |
|
113
|
|
|
* @throws SQLException |
|
114
|
|
|
* @throws IllegalAccessException |
|
115
|
|
|
* @throws InvocationTargetException |
|
116
|
|
|
* @throws java.lang.InstantiationException |
|
117
|
|
|
*/ |
|
118
|
|
|
public Object invokeFunctionChain(ResultSet rs) |
|
119
|
|
|
throws SQLException, |
|
120
|
|
|
IllegalAccessException, |
|
121
|
|
|
InvocationTargetException, |
|
122
|
|
|
InstantiationException { |
|
123
|
|
|
|
|
124
|
|
|
Object startingValue = null; |
|
125
|
|
|
Class<?> argType = getResolvedPlan().getDynamicArgumentType(); |
|
126
|
|
|
if (argType != null && ClassUtils.isAssignable(ResultSet.class, argType)) { |
|
127
|
|
|
startingValue = rs; |
|
128
|
|
|
} else { |
|
129
|
|
|
startingValue = rs.getObject(name, type); |
|
130
|
|
|
} |
|
131
|
|
|
return ConvertUtils.convert(getResolvedPlan().invoke(startingValue), type); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|