Subversion Repositories bacoAlunos

Rev

Rev 1310 | Rev 1781 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.domain;

import com.owlike.genson.Genson;
import com.owlike.genson.GensonBuilder;
import jomm.dao.impl.AbstractDao;
import org.json.JSONException;
import org.json.JSONObject;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import pt.estgp.estgweb.domain.utils.JsonView;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author Jorge Machado
 * @date 2/Mar/2008
 * @time 10:56:46
 * @see pt.estgp.estgweb.domain
 */

public abstract class DomainObject implements JsonView
{
    private static final 5+0%2Fdocs%2Fapi+Date">Date MIN_DATE = new 5+0%2Fdocs%2Fapi+Date">Date(0);
    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);

    private Genson genson = null;

    private synchronized void createGensonBuilder()
    {
        if(genson == null)
        {
            GensonBuilder gensonBuilder = new GensonBuilder();

            1.5.0/docs/api/java/beans/BeanInfo.html">BeanInfo bi;
            gensonBuilder.exclude(5+0%2Fdocs%2Fapi+Object">Object.class);
            try {
                bi = 1.5.0/docs/api/java/beans/Introspector.html">Introspector.getBeanInfo(this.getClass());
            } catch (5+0%2Fdocs%2Fapi+IntrospectionException">IntrospectionException e) {
                e.printStackTrace();
                throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException(e);
            }
            List<String> excludedProperties = getJsonExcludedProperties();
            1.5.0/docs/api/java/beans/PropertyDescriptor.html">PropertyDescriptor[] pds = bi.getPropertyDescriptors();
            for (int i = 0; i < pds.length; i++) {


                1.5.0/docs/api/java/lang/String.html">String propName = pds[i].getDisplayName();
                if(excludedProperties == null || !excludedProperties.contains(propName))
                    gensonBuilder.include(propName,this.getClass());
                else
                    gensonBuilder.exclude(propName,this.getClass());
            }
            genson = gensonBuilder.create();
        }
    }

    /**
     * Generic Json Object only for local class methods
     * @return
     * @throws JSONException
     */

    @1.5.0/docs/api/java/lang/Override.html">Override
    public JSONObject toJsonObject() throws JSONException {
        createGensonBuilder();
        return new JSONObject(genson.serialize(this));
    }

    /**
     * * Generic Json Object only for local class methods
     * @return
     * @throws IOException
     */

    @1.5.0/docs/api/java/lang/Override.html">Override
    public 1.5.0/docs/api/java/lang/String.html">String toJson() {
        createGensonBuilder();
        return genson.serialize(this);
    }

    @1.5.0/docs/api/java/lang/Override.html">Override
    public List<String> getJsonExcludedProperties() {
        return null;
    }

    public void searchMyMethodsStartingWith(1.5.0/docs/api/java/lang/String.html">String propNamePrefix, List<String> listMethods) {
        1.5.0/docs/api/java/beans/BeanInfo.html">BeanInfo bi;

        try {
            bi = 1.5.0/docs/api/java/beans/Introspector.html">Introspector.getBeanInfo(this.getClass());
        } catch (5+0%2Fdocs%2Fapi+IntrospectionException">IntrospectionException e) {
            e.printStackTrace();
            throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException(e);
        }
        1.5.0/docs/api/java/beans/PropertyDescriptor.html">PropertyDescriptor[] pds = bi.getPropertyDescriptors();
        for (int i = 0; i < pds.length; i++) {

            1.5.0/docs/api/java/lang/String.html">String propName = pds[i].getDisplayName();
            if(propName.startsWith(propNamePrefix))
                listMethods.add(propName);
        }
    }

    public void setSaveDate(5+0%2Fdocs%2Fapi+Date">Date d)
    {

    }
    public 5+0%2Fdocs%2Fapi+Date">Date getSaveDate()
    {
        return null;
    }


    public 5+0%2Fdocs%2Fapi+Date">Date getStartTime()
    {
        return MIN_DATE;
    }

    public 5+0%2Fdocs%2Fapi+Date">Date getEndTime()
    {
        return MAX_DATE;
    }

    public 1.5.0/docs/api/java/lang/Class.html">Class getReferenceClass()
    {
        return this.getClass();
    }



    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 {

        AbstractDao.getCurrentSession().beginTransaction();
        User c = DaoFactory.getStudentDaoImpl().findAll().get(10);
        1.5.0/docs/api/java/lang/System.html">System.out.println(c.toJson());

    }
   
}