Rev 1732 | Rev 1736 | 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; |
||
1725 | jmachado | 14 | import pt.estgp.estgweb.services.data.repositorydocuments.interfaces.InterfaceBlock; |
15 | import pt.estgp.estgweb.services.data.repositorydocuments.interfaces.InterfaceColumn; |
||
16 | import pt.estgp.estgweb.services.data.repositorydocuments.interfaces.InterfaceRow; |
||
1692 | jmachado | 17 | import pt.estgp.estgweb.services.data.repositorydocuments.interfaces.RepositoryInterface; |
1711 | jmachado | 18 | import pt.estgp.estgweb.services.data.sourcemodules.RepositoryDocumentsSource; |
1725 | jmachado | 19 | import pt.estgp.estgweb.services.expceptions.ServiceException; |
1711 | jmachado | 20 | import pt.estgp.estgweb.services.utils.HttpServices; |
1675 | jmachado | 21 | import pt.estgp.estgweb.web.controllers.utils.FileUploaded; |
22 | import pt.estgp.estgweb.web.controllers.utils.FilesUploadResult; |
||
1711 | jmachado | 23 | import pt.estgp.estgweb.web.exceptions.NotAuthorizedException; |
1670 | jmachado | 24 | import pt.utl.ist.berserk.logic.serviceManager.IService; |
25 | |||
1675 | jmachado | 26 | import java.io.FileInputStream; |
1711 | jmachado | 27 | import java.io.FileNotFoundException; |
1670 | jmachado | 28 | import java.io.IOException; |
1735 | jmachado | 29 | import java.util.*; |
1670 | jmachado | 30 | |
31 | /** |
||
32 | * Created by jorgemachado on 23/02/17. |
||
33 | */ |
||
1675 | jmachado | 34 | public class RepositoryDocumentService implements IService |
35 | { |
||
1670 | jmachado | 36 | |
1675 | jmachado | 37 | public static final 1.5.0/docs/api/java/lang/String.html">String REPOSITORY_DOCS_ROLE = "repositoryDocs"; |
1670 | jmachado | 38 | |
1727 | jmachado | 39 | /** |
40 | * |
||
41 | * @param newCollectionId new parent Collection Id |
||
42 | * @param documentIds documentIds: [LONG,LONG, ...] |
||
43 | * @param userSession |
||
44 | * @throws IOException |
||
45 | * @throws TransformationException |
||
46 | * @throws JSONException |
||
47 | * @throws AccessDeniedException |
||
48 | */ |
||
49 | public void changeDocumentCollection(1.5.0/docs/api/java/lang/Long.html">Long newCollectionId , JSONArray documentIds, UserSession userSession) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException, AccessDeniedException |
||
50 | { |
||
51 | RepositoryDocumentCollection newCollection = DaoFactory.getRepositoryDocumentCollectionDaoImpl().get(newCollectionId); |
||
52 | for(int i = 0; i < documentIds.length();i++) |
||
53 | { |
||
54 | long documentId = documentIds.getLong(i); |
||
55 | RepositoryDocument document = DaoFactory.getRepositoryDocumentDaoImpl().get(documentId); |
||
56 | document.setCollection((RepositoryDocumentCollectionImpl) newCollection); |
||
57 | } |
||
58 | } |
||
1675 | jmachado | 59 | |
60 | /** |
||
61 | * Public service |
||
62 | * Uses Roles to filter collections |
||
63 | * @param collectionId |
||
64 | * @param userSession |
||
65 | * @return |
||
66 | * @throws IOException |
||
67 | * @throws TransformationException |
||
68 | * @throws JSONException |
||
69 | * @throws AccessDeniedException |
||
70 | */ |
||
71 | 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 { |
||
72 | |||
1679 | jmachado | 73 | boolean onlyVisible = !(userSession.getUser() != null && |
1675 | jmachado | 74 | (userSession.getUser().hasRole(REPOSITORY_DOCS_ROLE) |
75 | || |
||
1679 | jmachado | 76 | userSession.getUser().isSuperuserOrAdmin())); |
1675 | jmachado | 77 | if(collectionId == null) |
78 | { |
||
79 | //finding roots |
||
1679 | jmachado | 80 | List<RepositoryDocumentCollectionImpl> roots = DaoFactory.getRepositoryDocumentCollectionDaoImpl().findRoots(onlyVisible); |
1675 | jmachado | 81 | return RepositoryDocumentCollectionImpl.toJsonArray(roots); |
82 | } |
||
83 | else |
||
84 | { |
||
1679 | jmachado | 85 | List<RepositoryDocumentCollectionImpl> childs = DaoFactory.getRepositoryDocumentCollectionDaoImpl().findChilds(onlyVisible,collectionId); |
1675 | jmachado | 86 | return RepositoryDocumentCollectionImpl.toJsonArray(childs); |
87 | } |
||
88 | } |
||
89 | |||
90 | |||
91 | 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 { |
||
92 | |||
1679 | jmachado | 93 | boolean onlyVisible = !(userSession.getUser() != null && |
1675 | jmachado | 94 | (userSession.getUser().hasRole(REPOSITORY_DOCS_ROLE) |
95 | || |
||
1679 | jmachado | 96 | userSession.getUser().isSuperuserOrAdmin())); |
1675 | jmachado | 97 | |
1679 | jmachado | 98 | List<RepositoryDocumentImpl> docs = DaoFactory.getRepositoryDocumentDaoImpl().findDocumentsSortPublishDateDesc(onlyVisible, collectionId); |
1675 | jmachado | 99 | return RepositoryDocumentImpl.toJsonArray(docs); |
100 | } |
||
101 | |||
102 | 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 { |
||
103 | RepositoryDocumentCollectionImpl collectionParent = null; |
||
104 | if(collectionParentId != null) |
||
105 | { |
||
106 | collectionParent = (RepositoryDocumentCollectionImpl) DaoFactory.getRepositoryDocumentCollectionDaoImpl().get(collectionParentId); |
||
107 | } |
||
108 | RepositoryDocumentCollectionImpl collectionChild = DomainObjectFactory.createRepositoryDocumentCollectionImpl(); |
||
109 | collectionChild.setParent(collectionParent); |
||
110 | |||
111 | if(collectionParent != null) |
||
112 | collectionParent.getChilds().add(collectionChild); |
||
113 | |||
114 | collectionChild.setVisible(true); |
||
115 | DaoFactory.getRepositoryDocumentCollectionDaoImpl().save(collectionChild); |
||
116 | return collectionChild.toJsonObject(); |
||
117 | } |
||
118 | |||
119 | 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 { |
||
120 | RepositoryDocumentCollectionImpl collectionChild = (RepositoryDocumentCollectionImpl) DaoFactory.getRepositoryDocumentCollectionDaoImpl().get(collectionId); |
||
121 | RepositoryDocumentCollectionImpl collectionNewParent = newParentId == null ? null : (RepositoryDocumentCollectionImpl) DaoFactory.getRepositoryDocumentCollectionDaoImpl().get(newParentId); |
||
122 | |||
123 | if(collectionChild.getParent() != null) |
||
124 | { |
||
125 | collectionChild.getParent().getChilds().remove(collectionChild); |
||
126 | } |
||
127 | collectionChild.setParent(collectionNewParent); |
||
128 | if(collectionNewParent != null) |
||
129 | { |
||
130 | collectionNewParent.getChilds().add(collectionChild); |
||
131 | } |
||
132 | |||
133 | |||
134 | return collectionChild.toJsonObject(); |
||
135 | } |
||
136 | |||
137 | |||
138 | |||
139 | 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 { |
||
140 | |||
141 | RepositoryDocumentCollectionImpl collectionChild = RepositoryDocumentCollectionImpl.loadFromJson(collection); |
||
142 | |||
143 | |||
144 | RepositoryDocumentCollectionImpl persistent = (RepositoryDocumentCollectionImpl) DaoFactory.getRepositoryDocumentCollectionDaoImpl().get(collectionChild.getId()); |
||
145 | persistent.setName(collectionChild.getName()); |
||
146 | persistent.setDescription(collectionChild.getDescription()); |
||
147 | persistent.setLegacyUrl(collectionChild.getLegacyUrl()); |
||
148 | persistent.setVisible(collectionChild.isVisible()); |
||
149 | |||
150 | return persistent.toJsonObject(); |
||
151 | } |
||
152 | |||
1727 | jmachado | 153 | public void deleteCollection(long collectionId, UserSession userSession) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException, AccessDeniedException, NotAuthorizedException |
154 | { |
||
155 | RepositoryDocumentCollection collection = DaoFactory.getRepositoryDocumentCollectionDaoImpl().get(collectionId); |
||
156 | if(collection.getChilds().size() > 0) |
||
157 | { |
||
158 | throw new NotAuthorizedException("A colecção " + collection.getName() + " não está vazia, tem colecções, limpe a colecção"); |
||
159 | } |
||
160 | List<RepositoryDocumentImpl> docs = DaoFactory.getRepositoryDocumentDaoImpl().findDocuments(false, collectionId); |
||
161 | if(docs.size() > 0) |
||
162 | { |
||
163 | throw new NotAuthorizedException("A colecção " + collection.getName() + " não está vazia, tem documentos, limpe a colecção"); |
||
164 | } |
||
165 | DaoFactory.getRepositoryDocumentCollectionDaoImpl().delete(collection); |
||
166 | } |
||
1675 | jmachado | 167 | |
1727 | jmachado | 168 | |
1719 | jmachado | 169 | public JSONArray checkRepositoryDocumentExistence(1.5.0/docs/api/java/lang/String.html">String docJson, UserSession userSession) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException, AccessDeniedException, NotAuthorizedException |
170 | { |
||
171 | JSONArray found = new JSONArray(); |
||
172 | RepositoryDocumentImpl view = RepositoryDocumentImpl.loadFromJson(docJson); |
||
173 | List<RepositoryDocumentImpl> documents = DaoFactory.getRepositoryDocumentDaoImpl().findDocumentsByTitle(view.getTitle()); |
||
174 | if(documents.size() > 0) |
||
175 | { |
||
176 | for(RepositoryDocumentImpl doc: documents) |
||
177 | { |
||
178 | found.put(doc.toJsonObject()); |
||
179 | } |
||
180 | } |
||
181 | return found; |
||
182 | } |
||
183 | |||
1725 | jmachado | 184 | 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, ServiceException { |
1675 | jmachado | 185 | |
1670 | jmachado | 186 | RepositoryDocumentImpl view = RepositoryDocumentImpl.loadFromJson(docJson); |
1711 | jmachado | 187 | |
188 | if(view.getCollection() == null || view.getCollection().getId() <= 0) |
||
189 | { |
||
190 | throw new NotAuthorizedException("Não pode depositar documentos sem colecção"); |
||
191 | } |
||
192 | |||
1670 | jmachado | 193 | RepositoryDocumentImpl persistent = null; |
194 | if(view.getId() <= 0) |
||
195 | { |
||
1711 | jmachado | 196 | 5+0%2Fdocs%2Fapi+List">List dups = DaoFactory.getRepositoryDocumentDaoImpl().findDocumentsByTitleAndCollection(view.getTitle(), view.getCollection().getId()); |
197 | if(dups.size() > 0) |
||
198 | { |
||
199 | throw new NotAuthorizedException("Já existe um documento com o mesmo titulo na colecção"); |
||
200 | } |
||
1670 | jmachado | 201 | persistent = DomainObjectFactory.createRepositoryDocumentImpl(); |
202 | DaoFactory.getRepositoryDocumentDaoImpl().save(persistent); |
||
1735 | jmachado | 203 | persistent.setOwner(userSession.getUser()); |
1670 | jmachado | 204 | } |
1675 | jmachado | 205 | else |
206 | { |
||
207 | persistent = (RepositoryDocumentImpl) DaoFactory.getRepositoryDocumentDaoImpl().get(view.getId()); |
||
1735 | jmachado | 208 | 1.5.0/docs/api/java/lang/String.html">String history = persistent.getHistoryEdit(); |
209 | if(history == null) |
||
210 | history = ""; |
||
211 | else |
||
212 | history = history + ";\n"; |
||
213 | history = history + "Edited by " + userSession.getUser().getUsername() + " : " + userSession.getUser().getName() + " : " + userSession.getUser().getId() + " - at " + new 5+0%2Fdocs%2Fapi+Date">Date().toString(); |
||
214 | persistent.setHistoryEdit(history); |
||
1675 | jmachado | 215 | } |
1670 | jmachado | 216 | |
217 | persistent.setContributorsJson(view.getContributorsJson()); |
||
218 | persistent.setAuthorsJson(view.getAuthorsJson()); |
||
219 | persistent.setTitle(view.getTitle()); |
||
220 | persistent.setSubtitle(view.getSubtitle()); |
||
221 | persistent.setPublisher(view.getPublisher()); |
||
222 | persistent.setDescription(view.getDescription()); |
||
223 | persistent.setPublishDate(view.getPublishDate()); |
||
224 | persistent.setCloseDate(view.getCloseDate()); |
||
225 | persistent.setInternalIdentifier(view.getInternalIdentifier()); |
||
226 | persistent.setVisible(view.isVisible()); |
||
227 | |||
228 | if(view.getCollection() != null) |
||
229 | { |
||
230 | RepositoryDocumentCollection col = DaoFactory.getRepositoryDocumentCollectionDaoImpl().get(view.getCollection().getId()); |
||
231 | col.getId(); |
||
232 | persistent.setCollection((RepositoryDocumentCollectionImpl) col); |
||
233 | } |
||
234 | |||
1675 | jmachado | 235 | /** |
236 | * atualizar campos dos ficheiros já existentes |
||
237 | */ |
||
238 | if(view.getRepositoryDocumentFiles() != null && view.getRepositoryDocumentFiles().size() > 0) |
||
239 | { |
||
240 | for(RepositoryDocumentFileImpl ficheiroAtualizado : view.getRepositoryDocumentFiles()) |
||
241 | { |
||
242 | for(RepositoryDocumentFileImpl repoFileCandidate: persistent.getRepositoryDocumentFiles() ) |
||
243 | { |
||
244 | if(repoFileCandidate.getId()==ficheiroAtualizado.getId()) |
||
245 | { |
||
246 | //ficheiro encontrado |
||
247 | repoFileCandidate.setTitle(ficheiroAtualizado.getTitle()); |
||
248 | repoFileCandidate.setDescription(ficheiroAtualizado.getDescription()); |
||
249 | repoFileCandidate.setVisible(ficheiroAtualizado.isVisible()); |
||
250 | repoFileCandidate.getRepositoryFileProxy().getRepositoryFile().setAccessControl(ficheiroAtualizado.getRepositoryFileProxy().getRepositoryFile().getAccessControl()); |
||
1679 | jmachado | 251 | if(!repoFileCandidate.isVisible()) |
252 | repoFileCandidate.getRepositoryFileProxy().getRepositoryFile().setAccessControl(ResourceAccessControlEnum.privateDomain.getKey()); |
||
1675 | jmachado | 253 | break; |
254 | } |
||
255 | } |
||
256 | } |
||
257 | } |
||
258 | if(uploadedFiles != null) |
||
259 | { |
||
1725 | jmachado | 260 | |
261 | |||
262 | //VERIFICACAO DE CONSISTENCIA DE FICHEIROS |
||
263 | for(FileUploaded uploadedF : uploadedFiles.getUploadedFiles()) |
||
264 | { |
||
265 | if(uploadedF.getTmpName() == null || uploadedF.getTmpName().trim().length() == 0) |
||
266 | throw new ServiceException("Ficheiro não definido: " + uploadedF.getTitle()); |
||
267 | } |
||
268 | |||
1675 | jmachado | 269 | RepositoryService repositoryService = new RepositoryService(); |
1725 | jmachado | 270 | |
1675 | jmachado | 271 | for(FileUploaded uploadedF : uploadedFiles.getUploadedFiles()) |
272 | { |
||
1711 | jmachado | 273 | if(uploadedF.getAccessControl() == null) |
274 | uploadedF.setAccessControl(ResourceAccessControlEnum.publicDomain.getKey()); |
||
1675 | jmachado | 275 | |
276 | ResourceAccessControlEnum controloAcesso = ResourceAccessControlEnum.parse(uploadedF.getAccessControl()); |
||
277 | |||
278 | RepositoryDocumentFileImpl repoFile = null; |
||
279 | 1.5.0/docs/api/java/lang/String.html">String repositoryStream = uploadedF.getRepositoryStream(); |
||
280 | if(repositoryStream != null) |
||
281 | { |
||
282 | for(RepositoryDocumentFileImpl repoFileCandidate: persistent.getRepositoryDocumentFiles() ) |
||
283 | { |
||
284 | if(repoFileCandidate.getRepositoryFileProxy().getRepositoryStream().equals(repositoryStream)) |
||
285 | { |
||
286 | repoFile = repoFileCandidate; |
||
287 | break; |
||
288 | } |
||
289 | } |
||
290 | if(repoFile == null) |
||
291 | throw new AccessDeniedException("Trying to access to a repoFile not Allowded"); |
||
292 | |||
293 | repositoryService.updateRepositoryFile( |
||
294 | repositoryStream, |
||
295 | 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())), |
||
296 | uploadedF.getContentType(), |
||
297 | uploadedF.getExtension(), |
||
298 | (int)uploadedF.getFileSize(), |
||
299 | uploadedF.getFileName(), |
||
300 | "Institutional Repository Document File", |
||
301 | controloAcesso); |
||
302 | } |
||
303 | else |
||
304 | { |
||
1711 | jmachado | 305 | if(uploadedF.getTmpName().startsWith("http://") || uploadedF.getTmpName().startsWith("ftp://") || uploadedF.getTmpName().startsWith("https://")) |
306 | { |
||
307 | uploadedF.setVisible(true); |
||
308 | |||
309 | FileUploaded dowloadedF = HttpServices.downloadUrl(uploadedF.getTmpName()); |
||
310 | uploadedF.setFileSize(dowloadedF.getFileSize()); |
||
311 | uploadedF.setFileName(dowloadedF.getFileName()); |
||
312 | uploadedF.setExtension(dowloadedF.getExtension()); |
||
313 | uploadedF.setContentType(dowloadedF.getContentType()); |
||
314 | uploadedF.setTmpName(dowloadedF.getTmpName()); |
||
315 | |||
316 | } |
||
317 | else if(!new java.io.1.5.0/docs/api/java/io/File.html">File(Globals.TMP_DIR + "/" + uploadedF.getTmpName()).exists()) |
||
318 | { |
||
319 | throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("Ficheiro com o identificador: " + uploadedF.getTmpName() + " não foi encontrado"); |
||
320 | } |
||
321 | |||
1675 | jmachado | 322 | repositoryStream = repositoryService.storeRepositoryFile( |
323 | 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())), |
||
324 | uploadedF.getContentType(), |
||
325 | uploadedF.getExtension(), |
||
326 | (int)uploadedF.getFileSize(), |
||
327 | uploadedF.getFileName(), |
||
328 | "Institutional Repository Document File", |
||
329 | controloAcesso, |
||
1711 | jmachado | 330 | new RepositoryDocumentsSource(), |
331 | userSession.getUser()); |
||
1675 | jmachado | 332 | Set<RepositoryDocumentFileImpl> files = persistent.getRepositoryDocumentFiles(); |
333 | if(files == null) |
||
334 | { |
||
335 | files = new HashSet<RepositoryDocumentFileImpl>(); |
||
336 | persistent.setRepositoryDocumentFiles(files); |
||
337 | } |
||
338 | repoFile = new RepositoryDocumentFileImpl(); |
||
339 | DaoFactory.getRepositoryDocumentFileDaoImpl().save(repoFile); |
||
340 | files.add(repoFile); |
||
341 | RepositoryFileProxy repositoryFileProxy = new RepositoryFileProxy(); |
||
342 | repositoryFileProxy.setRepositoryStream(repositoryStream); |
||
343 | repoFile.setRepositoryFileProxy(repositoryFileProxy); |
||
344 | } |
||
345 | |||
346 | repoFile.setVisible(uploadedF.isVisible()); |
||
1679 | jmachado | 347 | if(!repoFile.isVisible()) |
348 | repoFile.getRepositoryFileProxy().getRepositoryFile().setAccessControl(ResourceAccessControlEnum.privateDomain.getKey()); |
||
1675 | jmachado | 349 | repoFile.setTitle(uploadedF.getTitle()); |
350 | repoFile.setDescription(uploadedF.getDescription()); |
||
351 | |||
352 | } |
||
353 | |||
354 | } |
||
355 | |||
1670 | jmachado | 356 | 1.5.0/docs/api/java/lang/System.html">System.out.println(view.toJson()); |
1675 | jmachado | 357 | return persistent.toJsonObject(); |
1670 | jmachado | 358 | } |
1692 | jmachado | 359 | |
360 | |||
361 | public JSONArray loadCollectionsOrderedAsTree() throws JSONException, TransformationException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
362 | ArrayList<RepositoryDocumentCollectionImpl> collections = new ArrayList<RepositoryDocumentCollectionImpl>(); |
||
363 | List<RepositoryDocumentCollection> collectionsRoots = DaoFactory.getRepositoryDocumentCollectionDaoImpl().findRoots(); |
||
364 | for(RepositoryDocumentCollection col : collectionsRoots) |
||
365 | { |
||
366 | col.getName(); |
||
367 | collections.add((RepositoryDocumentCollectionImpl) col); |
||
368 | collections.addAll(loadChildsOrderedAsTree(col)); |
||
369 | } |
||
1732 | jmachado | 370 | return RepositoryDocumentCollectionImpl.toJsonArraySimpleFields(collections); |
1692 | jmachado | 371 | } |
372 | |||
373 | private ArrayList<RepositoryDocumentCollectionImpl> loadChildsOrderedAsTree(RepositoryDocumentCollection rootCol) |
||
374 | { |
||
375 | ArrayList<RepositoryDocumentCollectionImpl> collections = new ArrayList<RepositoryDocumentCollectionImpl>(); |
||
376 | for(RepositoryDocumentCollection col : rootCol.getChilds()) |
||
377 | { |
||
378 | col.getName(); |
||
379 | collections.add((RepositoryDocumentCollectionImpl) col); |
||
380 | collections.addAll(loadChildsOrderedAsTree(col)); |
||
381 | } |
||
382 | return collections; |
||
383 | } |
||
384 | |||
385 | |||
386 | |||
387 | |||
388 | public JSONObject addRepositoryInterface(UserSession userSession) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException, AccessDeniedException { |
||
389 | RepositoryDocumentInterfaceImpl repositoryDocumentInterface = DomainObjectFactory.createRepositoryDocumentInterfaceImpl(); |
||
390 | repositoryDocumentInterface.setVisible(false); |
||
391 | DaoFactory.getRepositoryDocumentInterfaceDaoImpl().save(repositoryDocumentInterface); |
||
392 | return repositoryDocumentInterface.toJsonObject(); |
||
393 | } |
||
394 | 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 { |
||
395 | |||
396 | RepositoryDocumentInterfaceImpl repositoryDocumentInterface = |
||
397 | RepositoryDocumentInterfaceImpl.loadFromJson(jsonObject); |
||
398 | |||
399 | RepositoryDocumentInterfaceImpl persistent = (RepositoryDocumentInterfaceImpl) DaoFactory.getRepositoryDocumentInterfaceDaoImpl().get(repositoryDocumentInterface.getId()); |
||
400 | persistent.setVisible(repositoryDocumentInterface.isVisible()); |
||
401 | persistent.setAdminDescription(repositoryDocumentInterface.getAdminDescription()); |
||
402 | persistent.setSlug(repositoryDocumentInterface.getSlug()); |
||
403 | |||
404 | return persistent.toJsonObject(); |
||
405 | } |
||
406 | |||
407 | 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 { |
||
408 | |||
409 | RepositoryDocumentInterfaceImpl persistent = (RepositoryDocumentInterfaceImpl) DaoFactory.getRepositoryDocumentInterfaceDaoImpl().get(id); |
||
410 | DaoFactory.getRepositoryDocumentInterfaceDaoImpl().delete(persistent); |
||
411 | } |
||
412 | |||
1725 | jmachado | 413 | 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, ServiceException { |
1692 | jmachado | 414 | |
415 | RepositoryInterface repositoryInterface = RepositoryInterface.loadFromJson(dataJson); |
||
1725 | jmachado | 416 | for(InterfaceRow row: repositoryInterface.getRows()) |
417 | { |
||
418 | for(InterfaceColumn col : row.getColumns()) |
||
419 | { |
||
420 | for(InterfaceBlock block: col.getBlocks()) |
||
421 | { |
||
422 | if(block.getCollectionIds() == null || block.getCollectionIds().size() == 0) |
||
423 | { |
||
424 | throw new ServiceException("Existem blocos sem colecção associada, corrija a situação por favor, tem de clicar no + para adicionar após escolher colecção"); |
||
425 | } |
||
426 | } |
||
427 | } |
||
428 | } |
||
1692 | jmachado | 429 | RepositoryDocumentInterfaceImpl persistent = (RepositoryDocumentInterfaceImpl) DaoFactory.getRepositoryDocumentInterfaceDaoImpl().get(id); |
430 | persistent.setRepositoryInterface(repositoryInterface); |
||
431 | } |
||
432 | |||
1732 | jmachado | 433 | public void addManagerToCollection(long collectionId, long managerId, UserSession sess) |
434 | { |
||
435 | RepositoryDocumentCollection col = DaoFactory.getRepositoryDocumentCollectionDaoImpl().get(collectionId); |
||
436 | User u = DaoFactory.getUserDaoImpl().get(managerId); |
||
437 | col.getCollectionManagers().add((UserImpl) u); |
||
438 | } |
||
439 | |||
440 | public void removeManagerInCollection(long collectionId, long managerId, UserSession sess) |
||
441 | { |
||
442 | RepositoryDocumentCollection col = DaoFactory.getRepositoryDocumentCollectionDaoImpl().get(collectionId); |
||
443 | for(User user : col.getCollectionManagers()) |
||
444 | { |
||
445 | if(user.getId() == managerId) |
||
446 | { |
||
447 | col.getCollectionManagers().remove(user); |
||
448 | break; |
||
449 | } |
||
450 | } |
||
451 | } |
||
452 | |||
1692 | jmachado | 453 | 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 |
454 | { |
||
455 | AbstractDao.getCurrentSession().beginTransaction(); |
||
456 | JSONArray array = new RepositoryDocumentService().loadCollectionsOrderedAsTree(); |
||
457 | 1.5.0/docs/api/java/lang/System.html">System.out.println(array.toString()); |
||
458 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
459 | } |
||
460 | |||
1670 | jmachado | 461 | } |