Rev 1785 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | fvelez | 1 | package pt.estgp.estgweb.domain; |
2 | |||
1780 | jmachado | 3 | import com.owlike.genson.Genson; |
4 | import com.owlike.genson.GensonBuilder; |
||
1782 | jmachado | 5 | import org.apache.log4j.Logger; |
1780 | jmachado | 6 | import org.json.JSONException; |
7 | import org.json.JSONObject; |
||
8 | import pt.estgp.estgweb.domain.utils.JsonView; |
||
9 | |||
10 | import java.beans.BeanInfo; |
||
11 | import java.beans.IntrospectionException; |
||
12 | import java.beans.Introspector; |
||
13 | import java.beans.PropertyDescriptor; |
||
14 | import java.io.IOException; |
||
1 | fvelez | 15 | import java.util.Date; |
1780 | jmachado | 16 | import java.util.List; |
1 | fvelez | 17 | |
18 | /** |
||
19 | * @author Jorge Machado |
||
20 | * @date 2/Mar/2008 |
||
21 | * @time 10:56:46 |
||
22 | * @see pt.estgp.estgweb.domain |
||
23 | */ |
||
1780 | jmachado | 24 | public abstract class DomainObject implements JsonView |
1 | fvelez | 25 | { |
1782 | jmachado | 26 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(DomainObject.class); |
1 | fvelez | 27 | private static final 5+0%2Fdocs%2Fapi+Date">Date MIN_DATE = new 5+0%2Fdocs%2Fapi+Date">Date(0); |
28 | private static final 5+0%2Fdocs%2Fapi+Date">Date MAX_DATE = new 5+0%2Fdocs%2Fapi+Date">Date(1.5.0/docs/api/java/lang/Integer.html">Integer.MAX_VALUE); |
||
29 | |||
1781 | jmachado | 30 | private static Genson gensonGenericLoad; |
31 | static{ |
||
1782 | jmachado | 32 | gensonGenericLoad = new GensonBuilder() |
33 | //Usa ClassMetadata nos objectos contidos se houver |
||
34 | // porque no principal já foi buscar antes fazendo parse ao JSON no metodo fromJson e fazendo get("@class") |
||
35 | //porque no genson o @class tem de vir em primeiro e pode não |
||
36 | // se nos outros não vier info da class ele falha |
||
37 | .useMethods(true) //only bean proerties |
||
38 | .useFields(true) //pode existir uma var com campos publicos esses ele serializa |
||
39 | .useRuntimeType(true) |
||
40 | .useClassMetadata(true) |
||
41 | .useDateAsTimestamp(true) |
||
1785 | jmachado | 42 | .exclude(5+0%2Fdocs%2Fapi+Date">Date.class) |
43 | .exclude(java.sql.5+0%2Fdocs%2Fapi+Timestamp">Timestamp.class) |
||
44 | .exclude(java.sql.5+0%2Fdocs%2Fapi+Date">Date.class) |
||
1782 | jmachado | 45 | .create(); |
1781 | jmachado | 46 | } |
47 | |||
48 | public static DomainObject fromJson(1.5.0/docs/api/java/lang/String.html">String json) |
||
49 | { |
||
1782 | jmachado | 50 | try { |
51 | 1.5.0/docs/api/java/lang/String.html">String className = (1.5.0/docs/api/java/lang/String.html">String) new JSONObject(json).get("@class"); |
||
52 | if(className != null) |
||
53 | return (DomainObject) gensonGenericLoad.deserialize(json,1.5.0/docs/api/java/lang/Class.html">Class.forName(className)); |
||
54 | return gensonGenericLoad.deserialize(json,DomainObject.class); |
||
55 | } catch (1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException e) { |
||
56 | logger.error(e,e); |
||
57 | throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException("Fail Json Object:",e); |
||
58 | } catch (JSONException e) { |
||
59 | logger.error(e, e); |
||
60 | throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException("Fail Json Object:",e); |
||
61 | } |
||
1781 | jmachado | 62 | } |
63 | |||
1780 | jmachado | 64 | private Genson genson = null; |
1 | fvelez | 65 | |
1780 | jmachado | 66 | private synchronized void createGensonBuilder() |
67 | { |
||
68 | if(genson == null) |
||
69 | { |
||
58 | jmachado | 70 | |
1782 | jmachado | 71 | |
1780 | jmachado | 72 | 1.5.0/docs/api/java/beans/BeanInfo.html">BeanInfo bi; |
1782 | jmachado | 73 | GensonBuilder gensonBuilder = new GensonBuilder() |
74 | .useClassMetadata(true) |
||
75 | .exclude(5+0%2Fdocs%2Fapi+Object">Object.class); |
||
76 | |||
1781 | jmachado | 77 | //.useFields(false, VisibilityFilter.NONE); |
78 | |||
79 | |||
1780 | jmachado | 80 | try { |
81 | bi = 1.5.0/docs/api/java/beans/Introspector.html">Introspector.getBeanInfo(this.getClass()); |
||
82 | } catch (5+0%2Fdocs%2Fapi+IntrospectionException">IntrospectionException e) { |
||
83 | e.printStackTrace(); |
||
84 | throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException(e); |
||
85 | } |
||
86 | List<String> excludedProperties = getJsonExcludedProperties(); |
||
87 | 1.5.0/docs/api/java/beans/PropertyDescriptor.html">PropertyDescriptor[] pds = bi.getPropertyDescriptors(); |
||
88 | for (int i = 0; i < pds.length; i++) { |
||
89 | |||
90 | |||
91 | 1.5.0/docs/api/java/lang/String.html">String propName = pds[i].getDisplayName(); |
||
92 | if(excludedProperties == null || !excludedProperties.contains(propName)) |
||
1782 | jmachado | 93 | gensonBuilder = gensonBuilder.include(propName,this.getClass()); |
1780 | jmachado | 94 | else |
1782 | jmachado | 95 | gensonBuilder = gensonBuilder.exclude(propName, this.getClass()); |
1780 | jmachado | 96 | } |
97 | genson = gensonBuilder.create(); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Generic Json Object only for local class methods |
||
103 | * @return |
||
104 | * @throws JSONException |
||
105 | */ |
||
106 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
1782 | jmachado | 107 | public JSONObject toJsonObject() throws JSONException |
108 | { |
||
109 | return new JSONObject(toJson()); |
||
1780 | jmachado | 110 | } |
111 | |||
112 | /** |
||
113 | * * Generic Json Object only for local class methods |
||
114 | * @return |
||
115 | * @throws IOException |
||
116 | */ |
||
117 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
118 | public 1.5.0/docs/api/java/lang/String.html">String toJson() { |
||
119 | createGensonBuilder(); |
||
120 | return genson.serialize(this); |
||
121 | } |
||
122 | |||
123 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
124 | public List<String> getJsonExcludedProperties() { |
||
125 | return null; |
||
126 | } |
||
127 | |||
128 | public void searchMyMethodsStartingWith(1.5.0/docs/api/java/lang/String.html">String propNamePrefix, List<String> listMethods) { |
||
129 | 1.5.0/docs/api/java/beans/BeanInfo.html">BeanInfo bi; |
||
130 | |||
131 | try { |
||
132 | bi = 1.5.0/docs/api/java/beans/Introspector.html">Introspector.getBeanInfo(this.getClass()); |
||
133 | } catch (5+0%2Fdocs%2Fapi+IntrospectionException">IntrospectionException e) { |
||
134 | e.printStackTrace(); |
||
135 | throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException(e); |
||
136 | } |
||
137 | 1.5.0/docs/api/java/beans/PropertyDescriptor.html">PropertyDescriptor[] pds = bi.getPropertyDescriptors(); |
||
138 | for (int i = 0; i < pds.length; i++) { |
||
139 | |||
140 | 1.5.0/docs/api/java/lang/String.html">String propName = pds[i].getDisplayName(); |
||
141 | if(propName.startsWith(propNamePrefix)) |
||
142 | listMethods.add(propName); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | jmachado | 146 | public void setSaveDate(5+0%2Fdocs%2Fapi+Date">Date d) |
1 | fvelez | 147 | { |
148 | |||
149 | } |
||
146 | jmachado | 150 | public 5+0%2Fdocs%2Fapi+Date">Date getSaveDate() |
1 | fvelez | 151 | { |
44 | fvelez | 152 | return null; |
153 | } |
||
1 | fvelez | 154 | |
44 | fvelez | 155 | |
1 | fvelez | 156 | public 5+0%2Fdocs%2Fapi+Date">Date getStartTime() |
157 | { |
||
158 | return MIN_DATE; |
||
159 | } |
||
160 | |||
161 | public 5+0%2Fdocs%2Fapi+Date">Date getEndTime() |
||
162 | { |
||
163 | return MAX_DATE; |
||
164 | } |
||
165 | |||
166 | public 1.5.0/docs/api/java/lang/Class.html">Class getReferenceClass() |
||
167 | { |
||
168 | return this.getClass(); |
||
169 | } |
||
1780 | jmachado | 170 | |
171 | |||
172 | |||
173 | public static void main(1.5.0/docs/api/java/lang/String.html">String[] argv) throws 1.5.0/docs/api/java/lang/Exception.html">Exception { |
||
174 | |||
1782 | jmachado | 175 | // AbstractDao.getCurrentSession().beginTransaction(); |
1780 | jmachado | 176 | |
1782 | jmachado | 177 | 1.5.0/docs/api/java/lang/System.html">System.out.println(DomainObject.fromJson("{\n" + |
178 | " \"flowComponentId\": 67,\n" + |
||
179 | " \"@class\": \"pt.estgp.estgweb.domain.BpmnFlowConnectorImpl\",\n" + |
||
180 | " \"encaminhadorTarefaVarHolder\": null,\n" + |
||
1781 | jmachado | 181 | |
1782 | jmachado | 182 | " \"updateDate\": 1497716649000,\n" + |
183 | " \"id\": 14,\n" + |
||
184 | " \"msgEmail\": null,\n" + |
||
185 | " \"nameConnection\": \"kn\",\n" + |
||
186 | " \"sendEmail\": false,\n" + |
||
187 | " \"targetTypes\": null,\n" + |
||
188 | " \"encaminhadorTarefaVarHolderType\": null,\n" + |
||
189 | " \"saveDate\": 1497716649083,\n" + |
||
190 | " \"keyValue\": null,\n" + |
||
191 | " \"selected\": true,\n" + |
||
192 | " \"dirty\": true\n" + |
||
193 | " }").toJson()); |
||
1791 | jmachado | 194 | } |
1782 | jmachado | 195 | |
196 | |||
208 | jmachado | 197 | |
1 | fvelez | 198 | } |