Subversion Repositories bacoAlunos

Rev

Rev 1310 | Rev 1781 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1310 Rev 1780
Line 1... Line 1...
1
package pt.estgp.estgweb.domain;
1
package pt.estgp.estgweb.domain;
2
 
2
 
-
 
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;
3
import java.util.Date;
16
import java.util.Date;
-
 
17
import java.util.List;
4
 
18
 
5
/**
19
/**
6
 * @author Jorge Machado
20
 * @author Jorge Machado
7
 * @date 2/Mar/2008
21
 * @date 2/Mar/2008
8
 * @time 10:56:46
22
 * @time 10:56:46
9
 * @see pt.estgp.estgweb.domain
23
 * @see pt.estgp.estgweb.domain
10
 */
24
 */
11
public abstract class DomainObject
25
public abstract class DomainObject implements JsonView
12
{
26
{
13
    private static final 5+0%2Fdocs%2Fapi+Date">Date MIN_DATE = new 5+0%2Fdocs%2Fapi+Date">Date(0);
27
    private static final 5+0%2Fdocs%2Fapi+Date">Date MIN_DATE = new 5+0%2Fdocs%2Fapi+Date">Date(0);
14
    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);
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);
15
 
29
 
-
 
30
    private Genson genson = null;
16
 
31
 
-
 
32
    private synchronized void createGensonBuilder()
-
 
33
    {
-
 
34
        if(genson == null)
-
 
35
        {
-
 
36
            GensonBuilder gensonBuilder = new GensonBuilder();
-
 
37
 
-
 
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
    }
17
 
105
 
18
    public void setSaveDate(5+0%2Fdocs%2Fapi+Date">Date d)
106
    public void setSaveDate(5+0%2Fdocs%2Fapi+Date">Date d)
19
    {
107
    {
20
 
108
 
21
    }
109
    }
Line 37... Line 125...
37
 
125
 
38
    public 1.5.0/docs/api/java/lang/Class.html">Class getReferenceClass()
126
    public 1.5.0/docs/api/java/lang/Class.html">Class getReferenceClass()
39
    {
127
    {
40
        return this.getClass();
128
        return this.getClass();
41
    }
129
    }
-
 
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
    }
42
   
140
   
43
}
141
}