Subversion Repositories bacoAlunos

Rev

Rev 1675 | Rev 1778 | 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 jomm.dao.impl.AbstractDao;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import pt.estgp.estgweb.services.data.repositorydocuments.DocumentPerson;
import pt.estgp.estgweb.services.data.repositorydocuments.DocumentPersons;
import pt.estgp.estgweb.services.data.repositorydocuments.RepositoryFileProxy;
import pt.estgp.estgweb.web.utils.DatesUtils;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashSet;
import java.util.List;

/**
 * Created by jorgemachado on 30/01/17.
 */

public class RepositoryDocumentImpl extends RepositoryDocument {
    @1.5.0/docs/api/java/lang/Override.html">Override
    public 1.5.0/docs/api/java/io/Serializable.html">Serializable getSerializable() {
        return getId();
    }


    public void setPublishDateStr(1.5.0/docs/api/java/lang/String.html">String date)
    {
        setPublishDate(DatesUtils.getDateFromString(date));
    }

    public void setCloseDateStr(1.5.0/docs/api/java/lang/String.html">String date)
    {
        setCloseDate(DatesUtils.getDateFromString(date));
    }

    public 1.5.0/docs/api/java/lang/String.html">String getPublishDateStr()
    {
        if(getPublishDate() != null)
            return DatesUtils.getStringFromDate(getPublishDate());
        return "";
    }
    public 1.5.0/docs/api/java/lang/String.html">String getCloseDateStr()
    {
        if(getCloseDate() != null)
            return DatesUtils.getStringFromDate(getCloseDate());
        return "";
    }

    static Genson gensonSerialize = new Genson.Builder()
            .exclude(5+0%2Fdocs%2Fapi+Object">Object.class)
            .include(RepositoryDocumentImpl.class)
            .include(DocumentPersons.class)
            .include(DocumentPerson.class)
            .include(RepositoryDocumentCollectionImpl.class)
            .include(RepositoryDocumentFileImpl.class)
            .include("authorsJson", RepositoryDocumentImpl.class)
            .include("contributorsJson", RepositoryDocumentImpl.class)
            .include("persons", DocumentPersons.class)
            .include("contributorsJson")
            .include("collection", RepositoryDocumentImpl.class)
            .include(1.5.0/docs/api/java/lang/String.html">String.class)
            .include("repositoryDocumentFiles", RepositoryDocumentImpl.class)
            .include(RepositoryFileProxy.class)
            .include(RepositoryFileImpl.class)
            .include(RepositoryFileVersionImpl.class)
            .include("repositoryFileProxy", RepositoryDocumentFileImpl.class)
            .include("lastVersion", RepositoryFileImpl.class)
            .exclude("repositoryFile", RepositoryFileVersion.class)
            .exclude("publishDate", RepositoryDocumentImpl.class)
            .exclude("closeDate", RepositoryDocumentImpl.class)
            //O sistema nao constroi o RepositoryFile a partir da classe
            //mas como guarda o stream quando serializar aparece outra vez o ficheiro
            .include("repositoryFile", RepositoryFileProxy.class) //DIFERENCA COM O ANTERIOR
            .create();


    static Genson gensonDeserialize = new Genson.Builder()
            .exclude(5+0%2Fdocs%2Fapi+Object">Object.class)
            .include(RepositoryDocumentImpl.class)
            .include(DocumentPersons.class)
            .include(DocumentPerson.class)
            .include(RepositoryDocumentCollectionImpl.class)
            .include(RepositoryDocumentFileImpl.class)
            .include("authorsJson", RepositoryDocumentImpl.class)
            .include("contributorsJson", RepositoryDocumentImpl.class)
            .include("persons", DocumentPersons.class)
            .include("contributorsJson")
            .include("collection", RepositoryDocumentImpl.class)
            .include(1.5.0/docs/api/java/lang/String.html">String.class)
            .include("repositoryDocumentFiles", RepositoryDocumentImpl.class)
            .include(RepositoryFileProxy.class)
            .include(RepositoryFileImpl.class)
            .include(RepositoryFileVersionImpl.class)
            .include("repositoryFileProxy", RepositoryDocumentFileImpl.class)
            .include("lastVersion", RepositoryFileImpl.class)
            .exclude("repositoryFile", RepositoryFileVersion.class)
            .exclude("publishDate", RepositoryDocumentImpl.class)
            .exclude("closeDate", RepositoryDocumentImpl.class)
            //O sistema nao constroi o RepositoryFile a partir da classe
            //mas como guarda o stream quando serializar aparece outra vez o ficheiro
            .exclude("repositoryFile", RepositoryFileProxy.class) //DIFERENCA COM O ANTERIOR
            .create();




    public 1.5.0/docs/api/java/lang/String.html">String toJson() throws 1.5.0/docs/api/java/io/IOException.html">IOException
    {
        return gensonSerialize.serialize(this);
    }

    public static JSONArray toJsonArray(List<RepositoryDocumentImpl> docs) throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException {
        return new JSONArray(gensonSerialize.serialize(docs));
    }

    public static RepositoryDocumentImpl loadFromJson(1.5.0/docs/api/java/lang/String.html">String json) throws 1.5.0/docs/api/java/io/IOException.html">IOException {
        return gensonDeserialize.deserialize(json, RepositoryDocumentImpl.class);
    }

    public JSONObject toJsonObject() throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException {
        return new JSONObject(toJson());
    }

    public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws 1.5.0/docs/api/java/io/IOException.html">IOException {
        AbstractDao.getCurrentSession().beginTransaction();
        DocumentPerson p = new DocumentPerson();
        p.setFirstNames("jorge");
        p.setLastName("Machado");
        DocumentPersons ps = new DocumentPersons();
        ps.getPersons().add(p);

        RepositoryDocumentCollectionImpl c = (RepositoryDocumentCollectionImpl) DaoFactory.getRepositoryDocumentCollectionDaoImpl().findAll().get(0);

        RepositoryDocumentImpl r = new RepositoryDocumentImpl();
        r.setCollection(c);
        r.setAuthorsJson(ps);
        r.setContributorsJson(ps);


        RepositoryDocumentFileImpl f = new RepositoryDocumentFileImpl();
        //f.setRepositoryStream("241");
        RepositoryFileProxy proxy  =new RepositoryFileProxy();
        proxy.setRepositoryStream("242");


        r.setRepositoryDocumentFiles(new HashSet<RepositoryDocumentFileImpl>());
        r.getRepositoryDocumentFiles().add(f);

        f.setRepositoryFileProxy(proxy);
        DaoFactory.getRepositoryDocumentFileDaoImpl().save(f);
        DaoFactory.getRepositoryDocumentDaoImpl().save(r);
        AbstractDao.getCurrentSession().getTransaction().commit();


        AbstractDao.getCurrentSession().beginTransaction();
        r = (RepositoryDocumentImpl) DaoFactory.getRepositoryDocumentDaoImpl().get(r.getId());
        1.5.0/docs/api/java/lang/System.html">System.out.println(r.toJson());

        1.5.0/docs/api/java/lang/System.html">System.out.println(RepositoryDocumentImpl.loadFromJson(r.toJson()).toJson());
        AbstractDao.getCurrentSession().getTransaction().commit();
    }
}