-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extractor.java
134 lines (122 loc) · 3.67 KB
/
Extractor.java
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
package com.dushyant.services;
import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.dushyant.annotation.DBField;
/**
* @param <E>
*/
public class Extractor<E extends DBTable> extends ExtractorService<E> {
/**
* @param oyeClass
* @param resultSet
* @return
*/
public static <E extends DBTable> ExtractorService<E> createExtractor(Class<E> oyeClass,
ResultSet resultSet) {
return new Extractor<E>(oyeClass, resultSet, true);
}
/**
* @param oyeClass
* @param resultSet
* @param closeOnExit
* @return
*/
public static <E extends DBTable> ExtractorService<E> createExtractor(Class<E> oyeClass,
ResultSet resultSet, boolean closeOnExit) {
return new Extractor<E>(oyeClass, resultSet, closeOnExit);
}
final ResultSet resultSet;
final Class<E> myType;
final boolean closeOnExit;
/**
* @param oyeClass
* @param resultSet
* @param closeOnExit
*/
private Extractor(Class<E> oyeClass, ResultSet resultSet, boolean closeOnExit) {
this.myType = oyeClass;
this.resultSet = resultSet;
this.closeOnExit = closeOnExit;
}
/**
* @param o
* @return
*/
private List<Field> extractFields(Object o) {
System.out.println(o);
List<Field> fieldList = null;
if (o == null) {
// NullPointerException
return fieldList;
}
fieldList = new ArrayList<Field>();
Class<?> class2 = o.getClass();
System.out.println(class2);
do {
fieldList.addAll(Arrays.asList(class2.getDeclaredFields()));
class2 = class2.getSuperclass();
} while (!class2.equals(Object.class));
return fieldList;
}
/*
* (non-Javadoc)
*
* @see com.dushyant.services.ExtractorService#extract()
*/
@Override
public List<E> extract() throws SQLException, SecurityException, IllegalArgumentException,
IllegalAccessException, InstantiationException {
if (resultSet == null || resultSet.isClosed()) {
throw new SQLException("Invalid ResultSet");
}
List<E> objects = null;
List<Field> declaredFields = null;
try {
objects = new ArrayList<E>();
while (resultSet.next()) {
E newInstance = myType.newInstance();
declaredFields = extractFields(newInstance);
System.out.println(declaredFields);
for (Field field : declaredFields) {
field.setAccessible(true);
Class<?> type = field.getType();
int index = 0;
try {
index =
resultSet.findColumn(field.isAnnotationPresent(DBField.class) ? field
.getAnnotation(DBField.class).field() : field.getName());
} catch (SQLException ex) {
index = -1;
}
if (index != -1) {
Object object = resultSet.getObject(index);
try {
Object raw = type.isPrimitive() && object == null ? 0 : object;
Object castedObject = type.cast(raw);
field.set(newInstance, castedObject);
} catch (ClassCastException e) {
throw new ClassCastException(String.format("%s for field %s",
e.getLocalizedMessage(), field));
}
}
}
objects.add(newInstance);
}
} catch (Exception e) {
throw e;
} finally {
if (closeOnExit) {
if (resultSet != null) {
try {
resultSet.close();
} catch (Exception e) {}
}
}
}
return objects;
}
}