Rev 1703 | Rev 1719 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1670 | jmachado | 1 | package pt.estgp.estgweb.services.data.repositorydocuments; |
2 | |||
3 | import com.owlike.genson.TransformationException; |
||
1692 | jmachado | 4 | import jomm.dao.impl.AbstractDao; |
1675 | jmachado | 5 | import org.json.JSONArray; |
6 | import org.json.JSONException; |
||
7 | import org.json.JSONObject; |
||
8 | import pt.estgp.estgweb.Globals; |
||
1670 | jmachado | 9 | import pt.estgp.estgweb.domain.*; |
10 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
||
1675 | jmachado | 11 | import pt.estgp.estgweb.filters.chains.ResourceAccessControlEnum; |
12 | import pt.estgp.estgweb.filters.exceptions.AccessDeniedException; |
||
13 | import pt.estgp.estgweb.services.data.RepositoryService; |
||
1692 | jmachado | 14 | import pt.estgp.estgweb.services.data.repositorydocuments.interfaces.RepositoryInterface; |
1711 | jmachado | 15 | import pt.estgp.estgweb.services.data.sourcemodules.RepositoryDocumentsSource; |
16 | import pt.estgp.estgweb.services.utils.HttpServices; |
||
1675 | jmachado | 17 | import pt.estgp.estgweb.web.controllers.utils.FileUploaded; |
18 | import pt.estgp.estgweb.web.controllers.utils.FilesUploadResult; |
||
1711 | jmachado | 19 | import pt.estgp.estgweb.web.exceptions.NotAuthorizedException; |
1670 | jmachado | 20 | import pt.utl.ist.berserk.logic.serviceManager.IService; |
21 | |||
1675 | jmachado | 22 | import java.io.FileInputStream; |
1711 | jmachado | 23 | import java.io.FileNotFoundException; |
1670 | jmachado | 24 | import java.io.IOException; |
1692 | jmachado | 25 | import java.util.ArrayList; |
1675 | jmachado | 26 | import java.util.HashSet; |
27 | import java.util.List; |
||
28 | import java.util.Set; |
||
1670 | jmachado | 29 | |
30 | /** |
||
31 | * Created by jorgemachado on 23/02/17. |
||
32 | */ |
||
1675 | jmachado | 33 | public class RepositoryDocumentService implements IService |
34 | { |
||
1670 | jmachado | 35 | |
1675 | jmachado | 36 | public static final 1.5.0/docs/api/java/lang/String.html">String REPOSITORY_DOCS_ROLE = "repositoryDocs"; |
1670 | jmachado | 37 | |
1675 | jmachado | 38 | |
39 | /** |
||
40 | * Public service |
||
41 | * Uses Roles to filter collections |
||
42 | * @param collectionId |
||
43 | * @param userSession |
||
44 | * @return |
||
45 | * @throws IOException |
||
46 | * @throws TransformationException |
||
47 | * @throws JSONException |
||
48 | * @throws AccessDeniedException |
||
49 | */ |
||
50 | public JSONArray openRepositoryCollection(1.5.0/docs/api/java/lang/Long.html">Long collectionId, UserSession userSession) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException, AccessDeniedException { |
||
51 | |||
1679 | jmachado | 52 | boolean onlyVisible = !(userSession.getUser() != null && |
1675 | jmachado | 53 | (userSession.getUser().hasRole(REPOSITORY_DOCS_ROLE) |
54 | || |
||
1679 | jmachado | 55 | userSession.getUser().isSuperuserOrAdmin())); |
1675 | jmachado | 56 | if(collectionId == null) |
57 | { |
||
58 | //finding roots |
||
1679 | jmachado | 59 | List<RepositoryDocumentCollectionImpl> roots = DaoFactory.getRepositoryDocumentCollectionDaoImpl().findRoots(onlyVisible); |
1675 | jmachado | 60 | return RepositoryDocumentCollectionImpl.toJsonArray(roots); |
61 | } |
||
62 | else |
||
63 | { |
||
1679 | jmachado | 64 | List<RepositoryDocumentCollectionImpl> childs = DaoFactory.getRepositoryDocumentCollectionDaoImpl().findChilds(onlyVisible,collectionId); |
1675 | jmachado | 65 | return RepositoryDocumentCollectionImpl.toJsonArray(childs); |
66 | } |
||
67 | } |
||
68 | |||
69 | |||
70 | public JSONArray openRepositoryCollectionDocuments(1.5.0/docs/api/java/lang/Long.html">Long collectionId, UserSession userSession) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException, AccessDeniedException { |
||
71 | |||
1679 | jmachado | 72 | boolean onlyVisible = !(userSession.getUser() != null && |
1675 | jmachado | 73 | (userSession.getUser().hasRole(REPOSITORY_DOCS_ROLE) |
74 | || |
||
1679 | jmachado | 75 | userSession.getUser().isSuperuserOrAdmin())); |
1675 | jmachado | 76 | |
1679 | jmachado | 77 | List<RepositoryDocumentImpl> docs = DaoFactory.getRepositoryDocumentDaoImpl().findDocumentsSortPublishDateDesc(onlyVisible, collectionId); |
1675 | jmachado | 78 | return RepositoryDocumentImpl.toJsonArray(docs); |
79 | } |
||
80 | |||
81 | public JSONObject addRepositoryCollection(1.5.0/docs/api/java/lang/Long.html">Long collectionParentId, UserSession userSession) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException, AccessDeniedException { |
||
82 | RepositoryDocumentCollectionImpl collectionParent = null; |
||
83 | if(collectionParentId != null) |
||
84 | { |
||
85 | collectionParent = (RepositoryDocumentCollectionImpl) DaoFactory.getRepositoryDocumentCollectionDaoImpl().get(collectionParentId); |
||
86 | } |
||
87 | RepositoryDocumentCollectionImpl collectionChild = DomainObjectFactory.createRepositoryDocumentCollectionImpl(); |
||
88 | collectionChild.setParent(collectionParent); |
||
89 | |||
90 | if(collectionParent != null) |
||
91 | collectionParent.getChilds().add(collectionChild); |
||
92 | |||
93 | collectionChild.setVisible(true); |
||
94 | DaoFactory.getRepositoryDocumentCollectionDaoImpl().save(collectionChild); |
||
95 | return collectionChild.toJsonObject(); |
||
96 | } |
||
97 | |||
98 | public JSONObject changeParentRepositoryDocumentCollection(1.5.0/docs/api/java/lang/Long.html">Long collectionId,1.5.0/docs/api/java/lang/Long.html">Long newParentId, UserSession userSession) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException, AccessDeniedException { |
||
99 | RepositoryDocumentCollectionImpl collectionChild = (RepositoryDocumentCollectionImpl) DaoFactory.getRepositoryDocumentCollectionDaoImpl().get(collectionId); |
||
100 | RepositoryDocumentCollectionImpl collectionNewParent = newParentId == null ? null : (RepositoryDocumentCollectionImpl) DaoFactory.getRepositoryDocumentCollectionDaoImpl().get(newParentId); |
||
101 | |||
102 | if(collectionChild.getParent() != null) |
||
103 | { |
||
104 | collectionChild.getParent().getChilds().remove(collectionChild); |
||
105 | } |
||
106 | collectionChild.setParent(collectionNewParent); |
||
107 | if(collectionNewParent != null) |
||
108 | { |
||
109 | collectionNewParent.getChilds().add(collectionChild); |
||
110 | } |
||
111 | |||
112 | |||
113 | return collectionChild.toJsonObject(); |
||
114 | } |
||
115 | |||
116 | |||
117 | |||
118 | public JSONObject updateRepositoryCollection(1.5.0/docs/api/java/lang/String.html">String collection, UserSession userSession) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException, AccessDeniedException { |
||
119 | |||
120 | RepositoryDocumentCollectionImpl collectionChild = RepositoryDocumentCollectionImpl.loadFromJson(collection); |
||
121 | |||
122 | |||
123 | RepositoryDocumentCollectionImpl persistent = (RepositoryDocumentCollectionImpl) DaoFactory.getRepositoryDocumentCollectionDaoImpl().get(collectionChild.getId()); |
||
124 | persistent.setName(collectionChild.getName()); |
||
125 | persistent.setDescription(collectionChild.getDescription()); |
||
126 | persistent.setLegacyUrl(collectionChild.getLegacyUrl()); |
||
127 | persistent.setVisible(collectionChild.isVisible()); |
||
128 | |||
129 | return persistent.toJsonObject(); |
||
130 | } |
||
131 | |||
132 | |||
1711 | jmachado | 133 | public JSONObject saveRepositoryDocument(1.5.0/docs/api/java/lang/String.html">String docJson, FilesUploadResult uploadedFiles, UserSession userSession) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException, AccessDeniedException, NotAuthorizedException { |
1675 | jmachado | 134 | |
1670 | jmachado | 135 | RepositoryDocumentImpl view = RepositoryDocumentImpl.loadFromJson(docJson); |
1711 | jmachado | 136 | |
137 | if(view.getCollection() == null || view.getCollection().getId() <= 0) |
||
138 | { |
||
139 | throw new NotAuthorizedException("Não pode depositar documentos sem colecção"); |
||
140 | } |
||
141 | |||
1670 | jmachado | 142 | RepositoryDocumentImpl persistent = null; |
143 | if(view.getId() <= 0) |
||
144 | { |
||
1711 | jmachado | 145 | 5+0%2Fdocs%2Fapi+List">List dups = DaoFactory.getRepositoryDocumentDaoImpl().findDocumentsByTitleAndCollection(view.getTitle(), view.getCollection().getId()); |
146 | if(dups.size() > 0) |
||
147 | { |
||
148 | throw new NotAuthorizedException("Já existe um documento com o mesmo titulo na colecção"); |
||
149 | } |
||
1670 | jmachado | 150 | persistent = DomainObjectFactory.createRepositoryDocumentImpl(); |
151 | DaoFactory.getRepositoryDocumentDaoImpl().save(persistent); |
||
152 | } |
||
1675 | jmachado | 153 | else |
154 | { |
||
155 | persistent = (RepositoryDocumentImpl) DaoFactory.getRepositoryDocumentDaoImpl().get(view.getId()); |
||
156 | } |
||
1670 | jmachado | 157 | |
158 | persistent.setContributorsJson(view.getContributorsJson()); |
||
159 | persistent.setAuthorsJson(view.getAuthorsJson()); |
||
160 | persistent.setTitle(view.getTitle()); |
||
161 | persistent.setSubtitle(view.getSubtitle()); |
||
162 | persistent.setPublisher(view.getPublisher()); |
||
163 | persistent.setDescription(view.getDescription()); |
||
164 | persistent.setPublishDate(view.getPublishDate()); |
||
165 | persistent.setCloseDate(view.getCloseDate()); |
||
166 | persistent.setInternalIdentifier(view.getInternalIdentifier()); |
||
167 | persistent.setVisible(view.isVisible()); |
||
168 | |||
169 | if(view.getCollection() != null) |
||
170 | { |
||
171 | RepositoryDocumentCollection col = DaoFactory.getRepositoryDocumentCollectionDaoImpl().get(view.getCollection().getId()); |
||
172 | col.getId(); |
||
173 | persistent.setCollection((RepositoryDocumentCollectionImpl) col); |
||
174 | } |
||
175 | |||
1675 | jmachado | 176 | /** |
177 | * atualizar campos dos ficheiros já existentes |
||
178 | */ |
||
179 | if(view.getRepositoryDocumentFiles() != null && view.getRepositoryDocumentFiles().size() > 0) |
||
180 | { |
||
181 | for(RepositoryDocumentFileImpl ficheiroAtualizado : view.getRepositoryDocumentFiles()) |
||
182 | { |
||
183 | for(RepositoryDocumentFileImpl repoFileCandidate: persistent.getRepositoryDocumentFiles() ) |
||
184 | { |
||
185 | if(repoFileCandidate.getId()==ficheiroAtualizado.getId()) |
||
186 | { |
||
187 | //ficheiro encontrado |
||
188 | repoFileCandidate.setTitle(ficheiroAtualizado.getTitle()); |
||
189 | repoFileCandidate.setDescription(ficheiroAtualizado.getDescription()); |
||
190 | repoFileCandidate.setVisible(ficheiroAtualizado.isVisible()); |
||
191 | repoFileCandidate.getRepositoryFileProxy().getRepositoryFile().setAccessControl(ficheiroAtualizado.getRepositoryFileProxy().getRepositoryFile().getAccessControl()); |
||
1679 | jmachado | 192 | if(!repoFileCandidate.isVisible()) |
193 | repoFileCandidate.getRepositoryFileProxy().getRepositoryFile().setAccessControl(ResourceAccessControlEnum.privateDomain.getKey()); |
||
1675 | jmachado | 194 | break; |
195 | } |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 | if(uploadedFiles != null) |
||
200 | { |
||
201 | RepositoryService repositoryService = new RepositoryService(); |
||
202 | for(FileUploaded uploadedF : uploadedFiles.getUploadedFiles()) |
||
203 | { |
||
1711 | jmachado | 204 | if(uploadedF.getAccessControl() == null) |
205 | uploadedF.setAccessControl(ResourceAccessControlEnum.publicDomain.getKey()); |
||
1675 | jmachado | 206 | |
207 | ResourceAccessControlEnum controloAcesso = ResourceAccessControlEnum.parse(uploadedF.getAccessControl()); |
||
208 | |||
209 | RepositoryDocumentFileImpl repoFile = null; |
||
210 | 1.5.0/docs/api/java/lang/String.html">String repositoryStream = uploadedF.getRepositoryStream(); |
||
211 | if(repositoryStream != null) |
||
212 | { |
||
213 | for(RepositoryDocumentFileImpl repoFileCandidate: persistent.getRepositoryDocumentFiles() ) |
||
214 | { |
||
215 | if(repoFileCandidate.getRepositoryFileProxy().getRepositoryStream().equals(repositoryStream)) |
||
216 | { |
||
217 | repoFile = repoFileCandidate; |
||
218 | break; |
||
219 | } |
||
220 | } |
||
221 | if(repoFile == null) |
||
222 | throw new AccessDeniedException("Trying to access to a repoFile not Allowded"); |
||
223 | |||
224 | repositoryService.updateRepositoryFile( |
||
225 | repositoryStream, |
||
226 | new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(new java.io.1.5.0/docs/api/java/io/File.html">File(Globals.TMP_DIR + "/" + uploadedF.getTmpName())), |
||
227 | uploadedF.getContentType(), |
||
228 | uploadedF.getExtension(), |
||
229 | (int)uploadedF.getFileSize(), |
||
230 | uploadedF.getFileName(), |
||
231 | "Institutional Repository Document File", |
||
232 | controloAcesso); |
||
233 | } |
||
234 | else |
||
235 | { |
||
1711 | jmachado | 236 | if(uploadedF.getTmpName().startsWith("http://") || uploadedF.getTmpName().startsWith("ftp://") || uploadedF.getTmpName().startsWith("https://")) |
237 | { |
||
238 | uploadedF.setVisible(true); |
||
239 | |||
240 | FileUploaded dowloadedF = HttpServices.downloadUrl(uploadedF.getTmpName()); |
||
241 | uploadedF.setFileSize(dowloadedF.getFileSize()); |
||
242 | uploadedF.setFileName(dowloadedF.getFileName()); |
||
243 | uploadedF.setExtension(dowloadedF.getExtension()); |
||
244 | uploadedF.setContentType(dowloadedF.getContentType()); |
||
245 | uploadedF.setTmpName(dowloadedF.getTmpName()); |
||
246 | |||
247 | } |
||
248 | else if(!new java.io.1.5.0/docs/api/java/io/File.html">File(Globals.TMP_DIR + "/" + uploadedF.getTmpName()).exists()) |
||
249 | { |
||
250 | throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("Ficheiro com o identificador: " + uploadedF.getTmpName() + " não foi encontrado"); |
||
251 | } |
||
252 | |||
1675 | jmachado | 253 | repositoryStream = repositoryService.storeRepositoryFile( |
254 | new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(new java.io.1.5.0/docs/api/java/io/File.html">File(Globals.TMP_DIR + "/" + uploadedF.getTmpName())), |
||
255 | uploadedF.getContentType(), |
||
256 | uploadedF.getExtension(), |
||
257 | (int)uploadedF.getFileSize(), |
||
258 | uploadedF.getFileName(), |
||
259 | "Institutional Repository Document File", |
||
260 | controloAcesso, |
||
1711 | jmachado | 261 | new RepositoryDocumentsSource(), |
262 | userSession.getUser()); |
||
1675 | jmachado | 263 | Set<RepositoryDocumentFileImpl> files = persistent.getRepositoryDocumentFiles(); |
264 | if(files == null) |
||
265 | { |
||
266 | files = new HashSet<RepositoryDocumentFileImpl>(); |
||
267 | persistent.setRepositoryDocumentFiles(files); |
||
268 | } |
||
269 | repoFile = new RepositoryDocumentFileImpl(); |
||
270 | DaoFactory.getRepositoryDocumentFileDaoImpl().save(repoFile); |
||
271 | files.add(repoFile); |
||
272 | RepositoryFileProxy repositoryFileProxy = new RepositoryFileProxy(); |
||
273 | repositoryFileProxy.setRepositoryStream(repositoryStream); |
||
274 | repoFile.setRepositoryFileProxy(repositoryFileProxy); |
||
275 | } |
||
276 | |||
277 | repoFile.setVisible(uploadedF.isVisible()); |
||
1679 | jmachado | 278 | if(!repoFile.isVisible()) |
279 | repoFile.getRepositoryFileProxy().getRepositoryFile().setAccessControl(ResourceAccessControlEnum.privateDomain.getKey()); |
||
1675 | jmachado | 280 | repoFile.setTitle(uploadedF.getTitle()); |
281 | repoFile.setDescription(uploadedF.getDescription()); |
||
282 | |||
283 | } |
||
284 | |||
285 | } |
||
286 | |||
1670 | jmachado | 287 | 1.5.0/docs/api/java/lang/System.html">System.out.println(view.toJson()); |
1675 | jmachado | 288 | return persistent.toJsonObject(); |
1670 | jmachado | 289 | } |
1692 | jmachado | 290 | |
291 | |||
292 | public JSONArray loadCollectionsOrderedAsTree() throws JSONException, TransformationException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
293 | ArrayList<RepositoryDocumentCollectionImpl> collections = new ArrayList<RepositoryDocumentCollectionImpl>(); |
||
294 | List<RepositoryDocumentCollection> collectionsRoots = DaoFactory.getRepositoryDocumentCollectionDaoImpl().findRoots(); |
||
295 | for(RepositoryDocumentCollection col : collectionsRoots) |
||
296 | { |
||
297 | col.getName(); |
||
298 | collections.add((RepositoryDocumentCollectionImpl) col); |
||
299 | collections.addAll(loadChildsOrderedAsTree(col)); |
||
300 | } |
||
301 | return RepositoryDocumentCollectionImpl.toJsonArray(collections); |
||
302 | } |
||
303 | |||
304 | private ArrayList<RepositoryDocumentCollectionImpl> loadChildsOrderedAsTree(RepositoryDocumentCollection rootCol) |
||
305 | { |
||
306 | ArrayList<RepositoryDocumentCollectionImpl> collections = new ArrayList<RepositoryDocumentCollectionImpl>(); |
||
307 | for(RepositoryDocumentCollection col : rootCol.getChilds()) |
||
308 | { |
||
309 | col.getName(); |
||
310 | collections.add((RepositoryDocumentCollectionImpl) col); |
||
311 | collections.addAll(loadChildsOrderedAsTree(col)); |
||
312 | } |
||
313 | return collections; |
||
314 | } |
||
315 | |||
316 | |||
317 | |||
318 | |||
319 | public JSONObject addRepositoryInterface(UserSession userSession) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException, AccessDeniedException { |
||
320 | RepositoryDocumentInterfaceImpl repositoryDocumentInterface = DomainObjectFactory.createRepositoryDocumentInterfaceImpl(); |
||
321 | repositoryDocumentInterface.setVisible(false); |
||
322 | DaoFactory.getRepositoryDocumentInterfaceDaoImpl().save(repositoryDocumentInterface); |
||
323 | return repositoryDocumentInterface.toJsonObject(); |
||
324 | } |
||
325 | public JSONObject updateRepositoryInterface(1.5.0/docs/api/java/lang/String.html">String jsonObject,UserSession userSession) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException, AccessDeniedException { |
||
326 | |||
327 | RepositoryDocumentInterfaceImpl repositoryDocumentInterface = |
||
328 | RepositoryDocumentInterfaceImpl.loadFromJson(jsonObject); |
||
329 | |||
330 | RepositoryDocumentInterfaceImpl persistent = (RepositoryDocumentInterfaceImpl) DaoFactory.getRepositoryDocumentInterfaceDaoImpl().get(repositoryDocumentInterface.getId()); |
||
331 | persistent.setVisible(repositoryDocumentInterface.isVisible()); |
||
332 | persistent.setAdminDescription(repositoryDocumentInterface.getAdminDescription()); |
||
333 | persistent.setSlug(repositoryDocumentInterface.getSlug()); |
||
334 | |||
335 | return persistent.toJsonObject(); |
||
336 | } |
||
337 | |||
338 | public void removeRepositoryInterface(1.5.0/docs/api/java/lang/Long.html">Long id,UserSession userSession) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException, AccessDeniedException { |
||
339 | |||
340 | RepositoryDocumentInterfaceImpl persistent = (RepositoryDocumentInterfaceImpl) DaoFactory.getRepositoryDocumentInterfaceDaoImpl().get(id); |
||
341 | DaoFactory.getRepositoryDocumentInterfaceDaoImpl().delete(persistent); |
||
342 | } |
||
343 | |||
344 | |||
345 | public void saveRepositoryInterfaceData(1.5.0/docs/api/java/lang/Long.html">Long id,1.5.0/docs/api/java/lang/String.html">String dataJson, UserSession userSession) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException, AccessDeniedException { |
||
346 | |||
347 | RepositoryInterface repositoryInterface = RepositoryInterface.loadFromJson(dataJson); |
||
348 | RepositoryDocumentInterfaceImpl persistent = (RepositoryDocumentInterfaceImpl) DaoFactory.getRepositoryDocumentInterfaceDaoImpl().get(id); |
||
349 | persistent.setRepositoryInterface(repositoryInterface); |
||
350 | } |
||
351 | |||
352 | public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws TransformationException, JSONException, 1.5.0/docs/api/java/io/IOException.html">IOException |
||
353 | { |
||
354 | AbstractDao.getCurrentSession().beginTransaction(); |
||
355 | JSONArray array = new RepositoryDocumentService().loadCollectionsOrderedAsTree(); |
||
356 | 1.5.0/docs/api/java/lang/System.html">System.out.println(array.toString()); |
||
357 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
358 | } |
||
359 | |||
1670 | jmachado | 360 | } |