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