Subversion Repositories bacoAlunos

Rev

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