Rev 1637 | Rev 1675 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1637 | jmachado | 1 | package pt.estgp.estgweb.domain; |
2 | |||
1670 | jmachado | 3 | import com.owlike.genson.Genson; |
4 | import com.owlike.genson.TransformationException; |
||
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.services.data.repositorydocuments.DocumentPerson; |
||
10 | import pt.estgp.estgweb.services.data.repositorydocuments.DocumentPersons; |
||
11 | import pt.estgp.estgweb.services.data.repositorydocuments.RepositoryFileProxy; |
||
12 | import pt.estgp.estgweb.web.utils.DatesUtils; |
||
13 | |||
14 | import java.io.IOException; |
||
1637 | jmachado | 15 | import java.io.Serializable; |
1670 | jmachado | 16 | import java.util.HashSet; |
1637 | jmachado | 17 | |
18 | /** |
||
19 | * Created by jorgemachado on 30/01/17. |
||
20 | */ |
||
21 | public class RepositoryDocumentImpl extends RepositoryDocument { |
||
22 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
23 | public 1.5.0/docs/api/java/io/Serializable.html">Serializable getSerializable() { |
||
24 | return getId(); |
||
25 | } |
||
1670 | jmachado | 26 | |
27 | |||
28 | public void setPublishDateStr(1.5.0/docs/api/java/lang/String.html">String date) |
||
29 | { |
||
30 | setPublishDate(DatesUtils.getDateFromString(date)); |
||
31 | } |
||
32 | |||
33 | public void setCloseDateStr(1.5.0/docs/api/java/lang/String.html">String date) |
||
34 | { |
||
35 | setCloseDate(DatesUtils.getDateFromString(date)); |
||
36 | } |
||
37 | |||
38 | public 1.5.0/docs/api/java/lang/String.html">String getPublishDateStr() |
||
39 | { |
||
40 | if(getPublishDate() != null) |
||
41 | return DatesUtils.getStringFromDate(getPublishDate()); |
||
42 | return ""; |
||
43 | } |
||
44 | public 1.5.0/docs/api/java/lang/String.html">String getCloseDateStr() |
||
45 | { |
||
46 | if(getCloseDate() != null) |
||
47 | return DatesUtils.getStringFromDate(getCloseDate()); |
||
48 | return ""; |
||
49 | } |
||
50 | |||
51 | static Genson gensonSerialize = new Genson.Builder() |
||
52 | .exclude(5+0%2Fdocs%2Fapi+Object">Object.class) |
||
53 | .include(RepositoryDocumentImpl.class) |
||
54 | .include(DocumentPersons.class) |
||
55 | .include(DocumentPerson.class) |
||
56 | .include(RepositoryDocumentCollectionImpl.class) |
||
57 | .include(RepositoryDocumentFileImpl.class) |
||
58 | .include("authorsJson", RepositoryDocumentImpl.class) |
||
59 | .include("contributorsJson", RepositoryDocumentImpl.class) |
||
60 | .include("persons", DocumentPersons.class) |
||
61 | .include("contributorsJson") |
||
62 | .include("collection", RepositoryDocumentImpl.class) |
||
63 | .include(1.5.0/docs/api/java/lang/String.html">String.class) |
||
64 | .include("repositoryDocumentFiles", RepositoryDocumentImpl.class) |
||
65 | .include(RepositoryFileProxy.class) |
||
66 | .include(RepositoryFileImpl.class) |
||
67 | .include(RepositoryFileVersionImpl.class) |
||
68 | .include("repositoryFileProxy", RepositoryDocumentFileImpl.class) |
||
69 | .include("lastVersion", RepositoryFileImpl.class) |
||
70 | .exclude("repositoryFile", RepositoryFileVersion.class) |
||
71 | .exclude("publishDate", RepositoryDocumentImpl.class) |
||
72 | .exclude("closeDate", RepositoryDocumentImpl.class) |
||
73 | //O sistema nao constroi o RepositoryFile a partir da classe |
||
74 | //mas como guarda o stream quando serializar aparece outra vez o ficheiro |
||
75 | .include("repositoryFile", RepositoryFileProxy.class) //DIFERENCA COM O ANTERIOR |
||
76 | .create(); |
||
77 | |||
78 | |||
79 | static Genson gensonDeserialize = new Genson.Builder() |
||
80 | .exclude(5+0%2Fdocs%2Fapi+Object">Object.class) |
||
81 | .include(RepositoryDocumentImpl.class) |
||
82 | .include(DocumentPersons.class) |
||
83 | .include(DocumentPerson.class) |
||
84 | .include(RepositoryDocumentCollectionImpl.class) |
||
85 | .include(RepositoryDocumentFileImpl.class) |
||
86 | .include("authorsJson", RepositoryDocumentImpl.class) |
||
87 | .include("contributorsJson", RepositoryDocumentImpl.class) |
||
88 | .include("persons", DocumentPersons.class) |
||
89 | .include("contributorsJson") |
||
90 | .include("collection", RepositoryDocumentImpl.class) |
||
91 | .include(1.5.0/docs/api/java/lang/String.html">String.class) |
||
92 | .include("repositoryDocumentFiles", RepositoryDocumentImpl.class) |
||
93 | .include(RepositoryFileProxy.class) |
||
94 | .include(RepositoryFileImpl.class) |
||
95 | .include(RepositoryFileVersionImpl.class) |
||
96 | .include("repositoryFileProxy", RepositoryDocumentFileImpl.class) |
||
97 | .include("lastVersion", RepositoryFileImpl.class) |
||
98 | .exclude("repositoryFile", RepositoryFileVersion.class) |
||
99 | .exclude("publishDate", RepositoryDocumentImpl.class) |
||
100 | .exclude("closeDate", RepositoryDocumentImpl.class) |
||
101 | //O sistema nao constroi o RepositoryFile a partir da classe |
||
102 | //mas como guarda o stream quando serializar aparece outra vez o ficheiro |
||
103 | .exclude("repositoryFile", RepositoryFileProxy.class) //DIFERENCA COM O ANTERIOR |
||
104 | .create(); |
||
105 | |||
106 | |||
107 | |||
108 | |||
109 | public 1.5.0/docs/api/java/lang/String.html">String toJson() throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException |
||
110 | { |
||
111 | return gensonSerialize.serialize(this); |
||
112 | } |
||
113 | |||
114 | 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, TransformationException { |
||
115 | return gensonDeserialize.deserialize(json, RepositoryDocumentImpl.class); |
||
116 | } |
||
117 | |||
118 | public JSONObject toJsonObject() throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException { |
||
119 | return new JSONObject(toJson()); |
||
120 | } |
||
121 | |||
122 | 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, TransformationException { |
||
123 | AbstractDao.getCurrentSession().beginTransaction(); |
||
124 | DocumentPerson p = new DocumentPerson(); |
||
125 | p.setFirstNames("jorge"); |
||
126 | p.setLastName("Machado"); |
||
127 | DocumentPersons ps = new DocumentPersons(); |
||
128 | ps.getPersons().add(p); |
||
129 | |||
130 | RepositoryDocumentCollectionImpl c = (RepositoryDocumentCollectionImpl) DaoFactory.getRepositoryDocumentCollectionDaoImpl().findAll().get(0); |
||
131 | |||
132 | RepositoryDocumentImpl r = new RepositoryDocumentImpl(); |
||
133 | r.setCollection(c); |
||
134 | r.setAuthorsJson(ps); |
||
135 | r.setContributorsJson(ps); |
||
136 | |||
137 | |||
138 | RepositoryDocumentFileImpl f = new RepositoryDocumentFileImpl(); |
||
139 | //f.setRepositoryStream("241"); |
||
140 | RepositoryFileProxy proxy =new RepositoryFileProxy(); |
||
141 | proxy.setRepositoryStream("242"); |
||
142 | |||
143 | |||
144 | r.setRepositoryDocumentFiles(new HashSet<RepositoryDocumentFileImpl>()); |
||
145 | r.getRepositoryDocumentFiles().add(f); |
||
146 | |||
147 | f.setRepositoryFileProxy(proxy); |
||
148 | DaoFactory.getRepositoryDocumentFileDaoImpl().save(f); |
||
149 | DaoFactory.getRepositoryDocumentDaoImpl().save(r); |
||
150 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
151 | |||
152 | |||
153 | AbstractDao.getCurrentSession().beginTransaction(); |
||
154 | r = (RepositoryDocumentImpl) DaoFactory.getRepositoryDocumentDaoImpl().get(r.getId()); |
||
155 | 1.5.0/docs/api/java/lang/System.html">System.out.println(r.toJson()); |
||
156 | |||
157 | 1.5.0/docs/api/java/lang/System.html">System.out.println(RepositoryDocumentImpl.loadFromJson(r.toJson()).toJson()); |
||
158 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
159 | } |
||
1637 | jmachado | 160 | } |