Rev 1310 | Rev 1781 | 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; |
||
5 | import jomm.dao.impl.AbstractDao; |
||
6 | import org.json.JSONException; |
||
7 | import org.json.JSONObject; |
||
8 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
||
9 | import pt.estgp.estgweb.domain.utils.JsonView; |
||
10 | |||
11 | import java.beans.BeanInfo; |
||
12 | import java.beans.IntrospectionException; |
||
13 | import java.beans.Introspector; |
||
14 | import java.beans.PropertyDescriptor; |
||
15 | import java.io.IOException; |
||
1 | fvelez | 16 | import java.util.Date; |
1780 | jmachado | 17 | import java.util.List; |
1 | fvelez | 18 | |
19 | /** |
||
20 | * @author Jorge Machado |
||
21 | * @date 2/Mar/2008 |
||
22 | * @time 10:56:46 |
||
23 | * @see pt.estgp.estgweb.domain |
||
24 | */ |
||
1780 | jmachado | 25 | public abstract class DomainObject implements JsonView |
1 | fvelez | 26 | { |
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 | |||
1780 | jmachado | 30 | private Genson genson = null; |
1 | fvelez | 31 | |
1780 | jmachado | 32 | private synchronized void createGensonBuilder() |
33 | { |
||
34 | if(genson == null) |
||
35 | { |
||
36 | GensonBuilder gensonBuilder = new GensonBuilder(); |
||
58 | jmachado | 37 | |
1780 | jmachado | 38 | 1.5.0/docs/api/java/beans/BeanInfo.html">BeanInfo bi; |
39 | gensonBuilder.exclude(5+0%2Fdocs%2Fapi+Object">Object.class); |
||
40 | try { |
||
41 | bi = 1.5.0/docs/api/java/beans/Introspector.html">Introspector.getBeanInfo(this.getClass()); |
||
42 | } catch (5+0%2Fdocs%2Fapi+IntrospectionException">IntrospectionException e) { |
||
43 | e.printStackTrace(); |
||
44 | throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException(e); |
||
45 | } |
||
46 | List<String> excludedProperties = getJsonExcludedProperties(); |
||
47 | 1.5.0/docs/api/java/beans/PropertyDescriptor.html">PropertyDescriptor[] pds = bi.getPropertyDescriptors(); |
||
48 | for (int i = 0; i < pds.length; i++) { |
||
49 | |||
50 | |||
51 | 1.5.0/docs/api/java/lang/String.html">String propName = pds[i].getDisplayName(); |
||
52 | if(excludedProperties == null || !excludedProperties.contains(propName)) |
||
53 | gensonBuilder.include(propName,this.getClass()); |
||
54 | else |
||
55 | gensonBuilder.exclude(propName,this.getClass()); |
||
56 | } |
||
57 | genson = gensonBuilder.create(); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Generic Json Object only for local class methods |
||
63 | * @return |
||
64 | * @throws JSONException |
||
65 | */ |
||
66 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
67 | public JSONObject toJsonObject() throws JSONException { |
||
68 | createGensonBuilder(); |
||
69 | return new JSONObject(genson.serialize(this)); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * * Generic Json Object only for local class methods |
||
74 | * @return |
||
75 | * @throws IOException |
||
76 | */ |
||
77 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
78 | public 1.5.0/docs/api/java/lang/String.html">String toJson() { |
||
79 | createGensonBuilder(); |
||
80 | return genson.serialize(this); |
||
81 | } |
||
82 | |||
83 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
84 | public List<String> getJsonExcludedProperties() { |
||
85 | return null; |
||
86 | } |
||
87 | |||
88 | public void searchMyMethodsStartingWith(1.5.0/docs/api/java/lang/String.html">String propNamePrefix, List<String> listMethods) { |
||
89 | 1.5.0/docs/api/java/beans/BeanInfo.html">BeanInfo bi; |
||
90 | |||
91 | try { |
||
92 | bi = 1.5.0/docs/api/java/beans/Introspector.html">Introspector.getBeanInfo(this.getClass()); |
||
93 | } catch (5+0%2Fdocs%2Fapi+IntrospectionException">IntrospectionException e) { |
||
94 | e.printStackTrace(); |
||
95 | throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException(e); |
||
96 | } |
||
97 | 1.5.0/docs/api/java/beans/PropertyDescriptor.html">PropertyDescriptor[] pds = bi.getPropertyDescriptors(); |
||
98 | for (int i = 0; i < pds.length; i++) { |
||
99 | |||
100 | 1.5.0/docs/api/java/lang/String.html">String propName = pds[i].getDisplayName(); |
||
101 | if(propName.startsWith(propNamePrefix)) |
||
102 | listMethods.add(propName); |
||
103 | } |
||
104 | } |
||
105 | |||
146 | jmachado | 106 | public void setSaveDate(5+0%2Fdocs%2Fapi+Date">Date d) |
1 | fvelez | 107 | { |
108 | |||
109 | } |
||
146 | jmachado | 110 | public 5+0%2Fdocs%2Fapi+Date">Date getSaveDate() |
1 | fvelez | 111 | { |
44 | fvelez | 112 | return null; |
113 | } |
||
1 | fvelez | 114 | |
44 | fvelez | 115 | |
1 | fvelez | 116 | public 5+0%2Fdocs%2Fapi+Date">Date getStartTime() |
117 | { |
||
118 | return MIN_DATE; |
||
119 | } |
||
120 | |||
121 | public 5+0%2Fdocs%2Fapi+Date">Date getEndTime() |
||
122 | { |
||
123 | return MAX_DATE; |
||
124 | } |
||
125 | |||
126 | public 1.5.0/docs/api/java/lang/Class.html">Class getReferenceClass() |
||
127 | { |
||
128 | return this.getClass(); |
||
129 | } |
||
1780 | jmachado | 130 | |
131 | |||
132 | |||
133 | 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 { |
||
134 | |||
135 | AbstractDao.getCurrentSession().beginTransaction(); |
||
136 | User c = DaoFactory.getStudentDaoImpl().findAll().get(10); |
||
137 | 1.5.0/docs/api/java/lang/System.html">System.out.println(c.toJson()); |
||
138 | |||
139 | } |
||
208 | jmachado | 140 | |
1 | fvelez | 141 | } |