1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.model.mapper;
19
20 import java.io.Serializable;
21 import java.lang.reflect.Method;
22 import java.util.HashSet;
23 import java.util.Set;
24
25 import pl.edu.agh.cast.model.mapper.annotation.MapAttribute;
26 import pl.edu.agh.cast.model.mapper.annotation.Mapping;
27 import pl.edu.agh.cast.model.mapper.internal.Helper;
28
29
30
31
32
33
34
35
36 @Mapping()
37 public abstract class AbstractMappable implements Mappable, Serializable, Cloneable {
38
39 private static final long serialVersionUID = 8223254560632380336L;
40
41
42
43
44 private long __id = 0;
45
46
47
48
49
50 protected String id = "";
51
52
53
54
55
56
57
58 @MapAttribute(typeName = "mappableId")
59 public String getId() {
60 return id;
61 }
62
63
64
65
66
67
68
69 public void setId(String id) {
70 this.id = id;
71 }
72
73
74
75
76
77
78
79 public final long getMid() {
80 return __id;
81 }
82
83
84
85
86
87
88
89 public final void setMid(long mid) {
90 __id = mid;
91 }
92
93
94
95
96
97
98
99
100 public final Object getAttribute(String name) {
101 Class klass = getClass();
102 try {
103 Method getter = klass.getMethod("get" + name, new Class[0]);
104 return getter.invoke(this, new Object[0]);
105 } catch (Exception e) {
106
107 System.err.println("Warning getAttribute: " + e);
108
109 }
110 return null;
111 }
112
113
114
115
116
117
118
119 public final void setAttribute(String name, Object value) {
120 Class klass = getClass();
121 try {
122 Method getter = klass.getMethod("set" + name, new Class[] { value
123 .getClass() });
124 getter.invoke(this, new Object[] { value });
125 } catch (Exception e) {
126 System.err.println("Warning setAttribute: " + e);
127
128 throw new IllegalArgumentException("Problem occured while executing set" +
129 name + " method", e);
130 }
131 }
132
133
134
135
136
137
138
139 public final Class getAttributeType(String name) {
140
141 return null;
142 }
143
144
145
146
147
148
149 public final String[] getAttributeNames() {
150 Set<String> result = new HashSet<String>();
151 getAttributeNames(result, getClass());
152 return result.toArray(new String[0]);
153 }
154
155 private void getAttributeNames(Set<String> result, Class klass) {
156 for (Method method : Helper.mappedAttributeMethods(klass)) {
157 result.add(method.getName().substring(3));
158
159 }
160 if (Helper.isSuperMapped(klass)) {
161 getAttributeNames(result, klass.getSuperclass());
162 }
163 }
164
165
166
167
168
169
170
171 public String getType() {
172 return Helper.mappedTypeName(getClass());
173 }
174
175
176
177
178
179
180
181 @Override
182 public String toString() {
183 return getType() + "[" + getId() + "]";
184 }
185
186
187
188
189
190
191 @Override
192 public AbstractMappable clone() {
193 try {
194 return (AbstractMappable)super.clone();
195 } catch (CloneNotSupportedException e) {
196
197 e.printStackTrace();
198 return null;
199 }
200 }
201 }