P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package foundation.object.reader;
 
import java.lang.reflect.Method;
import java.util.Date;
 
import foundation.server.config.DBaseType;
import foundation.translator.ITranslator;
import foundation.translator.Translator;
 
public class PropertyReader {
 
    protected String name;
    protected Class<?> type;    
    protected Method getMethod;
    protected Method setMethod;
    protected ITranslator translator;
    protected IEntityReader valueReader;
    
    public PropertyReader(String property, Class<?> propertyType) throws Exception {
        name = property;
        type = propertyType;
        
        translator = Translator.getInstance(propertyType);
    }
 
    public void setDataString(Object entity, String value) throws Exception {
        if (setMethod == null) {
            return;
        }
        
        if (translator == null) {
            throw new Exception("can not set string value to " + type);
        }
        
        Object arg = translator.toTranslatorTypeValue(value);
        setMethod.invoke(entity, arg);
    }
 
    public String getString(Object entity) {
        if (getMethod == null) {
            return null;
        }
        
        try {
            Object value = getMethod.invoke(entity);
            
            if (translator != null) {
                return translator.toString(value);
            }
            else {
                return valueReader.getString(value);
            }
        }
        catch (Exception e) {
            return "error";
        }
    }
 
    public String getJSONString(Object entity) throws Exception {
        if (getMethod == null) {
            return null;
        }
        
        Object value = getMethod.invoke(entity);
        
        if (translator != null) {
            return translator.toJSONString(value);
        }
        else {
            if (valueReader == null) {
                valueReader = EntityReader.getInstance(value.getClass());
            }
            
            return valueReader.getJSONString(value);
        }
    }
 
    public String getSQLString(DBaseType dbaseType, Object entity) throws Exception {
        if (getMethod == null) {
            return null;
        }
        
        if (translator == null) {
            throw new Exception("can not get sql string from " + type);
        }
        
        Object value = getMethod.invoke(entity);
        return translator.toSqlString(dbaseType, value);
    }
 
    public Integer getInteger(Object entity) throws Exception {
        if (getMethod == null) {
            return null;
        }
        
        if (translator == null) {
            throw new Exception("can not get int from " + type);
        }
        
        Object value = getMethod.invoke(entity);
        return translator.toInteger(value);
    }
 
    public Double getDouble(Object entity) throws Exception {
        if (getMethod == null) {
            return null;
        }
        
        if (translator == null) {
            throw new Exception("can not get double from " + type);
        }
        
        Object value = getMethod.invoke(entity);
        return translator.toDouble(value);    
    }
 
    public Boolean getBoolean(Object entity) throws Exception {
        if (getMethod == null) {
            return null;
        }
        
        if (translator == null) {
            throw new Exception("can not get boolean from " + type);
        }
        
        Object value = getMethod.invoke(entity);
        return translator.toBoolean(value);    
    }
 
    public Date getDate(Object entity) throws Exception {
        if (getMethod == null) {
            return null;
        }
        
        if (translator == null) {
            throw new Exception("can not get date from " + type);
        }
        
        Object value = getMethod.invoke(entity);
        return translator.toDate(value);    
    }
    
    public void setSetMethod(Method setMethod) {
        this.setMethod = setMethod;
    }
 
    public void setGetMethod(Method getMethod) {
        this.getMethod = getMethod;
    }
 
    public void setData(Object entity, Object value) throws Exception {
        if (setMethod == null) {
            return;
        }
        
        setMethod.invoke(entity, value);
    }
    
    public Object getObject(Object entity) throws Exception {
        if (getMethod == null) {
            return null;
        }
        
        return getMethod.invoke(entity);
    }
 
    public String getName() {
        return name;
    }
 
}