2 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
6 * GSS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
19 package gr.ebs.gss.server.ejb;
21 import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
22 import gr.ebs.gss.server.domain.FileBody;
23 import gr.ebs.gss.server.domain.FileHeader;
24 import gr.ebs.gss.server.domain.FileUploadStatus;
25 import gr.ebs.gss.server.domain.Folder;
26 import gr.ebs.gss.server.domain.Group;
27 import gr.ebs.gss.server.domain.Nonce;
28 import gr.ebs.gss.server.domain.User;
30 import java.util.ArrayList;
31 import java.util.HashSet;
32 import java.util.List;
35 import javax.ejb.Stateless;
36 import javax.persistence.EntityManager;
37 import javax.persistence.NoResultException;
38 import javax.persistence.PersistenceContext;
40 import org.apache.commons.lang.StringUtils;
43 * The implementation of the GSSDAO interface.
46 public class GSSDAOBean implements GSSDAO {
49 * The entity manager for the persistence unit
51 @PersistenceContext(unitName = "gss")
52 private EntityManager manager;
57 * @see gr.ebs.gss.server.ejb.GSSDAO#getRootFolder(java.lang.Long)
59 public Folder getRootFolder(final Long userId) throws ObjectNotFoundException {
62 throw new ObjectNotFoundException("No user specified");
63 return (Folder) manager .createQuery("select f from Folder f where f.owner.id=:ownerId and f.parent is null")
64 .setParameter("ownerId", userId)
66 } catch (final NoResultException e) {
67 throw new ObjectNotFoundException("Root folder not found for user with id=" + userId);
74 * @see gr.ebs.gss.server.ejb.GSSDAO#getUser(java.lang.String)
76 public User getUser(final String username) throws ObjectNotFoundException {
79 throw new ObjectNotFoundException("No user specified");
80 return (User) manager .createQuery("select f from User f where f.username=:username")
81 .setParameter("username", username)
83 } catch (final NoResultException e) {
84 throw new ObjectNotFoundException("No User found for username=" + username);
91 * @see gr.ebs.gss.server.ejb.GSSDAO#create(java.lang.Object)
93 public void create(final Object obj) {
95 throw new IllegalArgumentException("No object speficied");
102 * @see gr.ebs.gss.server.ejb.GSSDAO#refresh(java.lang.Object)
104 public void refresh(final Object obj) {
106 throw new IllegalArgumentException("No object speficied");
107 manager.refresh(obj);
113 * @see gr.ebs.gss.server.ejb.GSSDAO#update(java.lang.Object)
115 public void update(final Object obj) {
117 throw new IllegalArgumentException("No object speficied");
124 * @see gr.ebs.gss.server.ejb.GSSDAO#delete(java.lang.Object)
126 public void delete(final Object entity) {
128 throw new IllegalArgumentException("No object speficied");
129 manager.remove(entity);
135 * @see gr.ebs.gss.server.ejb.GSSDAO#getEntityById(java.lang.Class,
138 public <T> T getEntityById(final Class<T> _class, final Object _id) throws ObjectNotFoundException {
140 throw new ObjectNotFoundException("No " + _class.getSimpleName() + " specified");
142 final T entity = manager.find(_class, _id);
144 throw new ObjectNotFoundException(_class.getSimpleName() + " with id=" + _id + " was not found");
152 * @see gr.ebs.gss.server.ejb.GSSDAO#getGroups(java.lang.Long)
154 @SuppressWarnings("unchecked")
155 public List<Group> getGroups(final Long userId) throws ObjectNotFoundException {
157 throw new ObjectNotFoundException("No user specified");
159 return manager.createQuery("select g from Group g where g.owner.id=:userId").setParameter("userId", userId).getResultList();
165 * @see gr.ebs.gss.server.ejb.GSSDAO#getFiles(java.lang.Long, java.lang.Boolean)
167 @SuppressWarnings("unchecked")
168 public List<FileHeader> getFiles(final Long folderId, boolean ignoreDeleted) throws ObjectNotFoundException {
169 if (folderId == null)
170 throw new ObjectNotFoundException("No folder specified");
173 query = "select f from FileHeader f where f.folder.id=:folderId and f.deleted=false";
175 query = "select f from FileHeader f where f.folder.id=:folderId";
176 return manager.createQuery(query).setParameter("folderId", folderId).getResultList();
182 * @see gr.ebs.gss.server.ejb.GSSDAO#getUsers(java.lang.Long)
184 @SuppressWarnings("unchecked")
185 public List<User> getUsers(final Long groupId) throws ObjectNotFoundException {
187 throw new ObjectNotFoundException("No group specified");
188 return manager.createQuery("select u from User u join u.groupsMember g where g.id=:groupId").
189 setParameter("groupId", groupId).getResultList();
193 public boolean existsFolderOrFile(Long parentId, String name) throws ObjectNotFoundException {
194 if (parentId == null)
195 throw new ObjectNotFoundException("No parent folder specified");
196 if (StringUtils.isEmpty(name))
197 throw new IllegalArgumentException("No folder name specified");
200 manager .createQuery("select f from Folder f " +
201 "where f.parent.id=:parentId and f.name=:name")
202 .setParameter("parentId", parentId)
203 .setParameter("name", name)
206 } catch (NoResultException e) {
208 manager .createQuery("select f from FileHeader f " +
209 "where f.folder.id=:parentId and f.name=:name")
210 .setParameter("parentId", parentId)
211 .setParameter("name", name)
214 } catch (NoResultException e1) {
223 * @see gr.ebs.gss.server.ejb.GSSDAO#existsGroup(java.lang.Long,
226 public boolean existsGroup(final Long userId, final String name) throws ObjectNotFoundException {
228 throw new ObjectNotFoundException("No user specified");
229 if (StringUtils.isEmpty(name))
230 throw new ObjectNotFoundException("No group name specified");
232 manager .createQuery("select g from Group g where g.owner.id=:userId and g.name=:name")
233 .setParameter("userId", userId)
234 .setParameter("name", name)
237 } catch (final NoResultException e) {
243 * @see gr.ebs.gss.server.ejb.GSSDAO#getUserTags(java.lang.Long)
245 public Set<String> getUserTags(final Long userId) throws ObjectNotFoundException {
247 throw new ObjectNotFoundException("No user specified");
248 return new HashSet(manager.createQuery("select t.tag from FileTag t where t.user.id=:userId order by t.tag")
249 .setParameter("userId", userId)
254 * @see gr.ebs.gss.server.ejb.GSSDAO#flush()
256 public void flush() {
261 * @see gr.ebs.gss.server.ejb.GSSDAO#getFile(java.lang.Long, java.lang.String)
263 public FileHeader getFile(Long folderId, String name) throws ObjectNotFoundException {
264 if (folderId == null)
265 throw new ObjectNotFoundException("No parent folder specified");
266 if (StringUtils.isEmpty(name))
267 throw new IllegalArgumentException("No file name specified");
270 return (FileHeader) manager.createQuery("select f from FileHeader f where f.folder.id=:parentId and f.name=:name")
271 .setParameter("parentId", folderId)
272 .setParameter("name", name)
274 } catch (final NoResultException e1) {
275 throw new ObjectNotFoundException("File not found");
280 public Folder getFolder(Long parentId, String name) throws ObjectNotFoundException {
281 if (parentId == null)
282 throw new ObjectNotFoundException("No parent folder specified");
283 if (StringUtils.isEmpty(name))
284 throw new IllegalArgumentException("No folder name specified");
287 return (Folder) manager.createQuery("select f from Folder f where f.parent.id=:parentId and f.name=:name")
288 .setParameter("parentId", parentId)
289 .setParameter("name", name)
291 } catch (NoResultException e) {
292 throw new ObjectNotFoundException("Folder not found");
297 * @see gr.ebs.gss.server.ejb.GSSDAO#getDeletedFiles(java.lang.Long)
299 public List<FileHeader> getDeletedFiles(Long userId) throws ObjectNotFoundException {
301 throw new ObjectNotFoundException("No User specified");
303 return manager.createQuery("select f from FileHeader f where f.owner.id=:userId and " +
304 "f.deleted=true and f.folder.deleted=false").
305 setParameter("userId", userId).getResultList();
309 * @see gr.ebs.gss.server.ejb.GSSDAO#getDeletedRootFolders(java.lang.Long)
311 public List<Folder> getDeletedRootFolders(Long userId) throws ObjectNotFoundException {
313 throw new ObjectNotFoundException("No User specified");
314 return manager.createQuery("select f from Folder f where f.owner.id=:userId and " +
315 "f.deleted=true and f.parent.deleted=false").
316 setParameter("userId", userId).getResultList();
320 public User findUser(String username) {
321 if (username == null)
323 List<User> results = manager.createQuery("select u from User u where u.username=:username").
324 setParameter("username", username).getResultList();
325 if (results.isEmpty()) return null;
326 return results.get(0);
330 * @see gr.ebs.gss.server.ejb.GSSDAO#getUsersByUserNameLike(java.lang.String)
333 public List<User> getUsersByUserNameLike(String username) {
334 return manager.createQuery("select u from User u where u.username like :username").
335 setParameter("username", username+"%").getResultList();
339 * @see gr.ebs.gss.server.ejb.GSSDAO#getSharedRootFolders(java.lang.Long)
342 public List<Folder> getSharedRootFolders(Long userId) {
343 List<Folder> folders = manager.createQuery("select distinct f from Folder f " +
344 "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
345 "and (p.group.id != null or p.user.id != f.owner.id) ").
346 setParameter("userId", userId).getResultList();
347 List<Folder> result = new ArrayList<Folder>();
348 for(Folder f : folders)
349 if(!folders.contains(f.getParent()))
355 * @see gr.ebs.gss.server.ejb.GSSDAO#getFoldersPermittedForGroup(java.lang.Long, java.lang.Long)
358 public List<Folder> getFoldersPermittedForGroup(Long userId, Long groupId) {
359 return manager.createQuery("select distinct f from Folder f LEFT JOIN f.permissions p " +
360 "where f.owner.id=:userId and f.deleted = false and p.group.id=:groupId ").
361 setParameter("userId", userId).setParameter("groupId", groupId).getResultList();
365 * @see gr.ebs.gss.server.ejb.GSSDAO#getUsersSharingFilesForUser(java.lang.Long)
368 public List<User> getUsersSharingFoldersForUser(Long userId) {
369 return manager.createQuery("select distinct f.owner from Folder f " +
370 "LEFT JOIN f.permissions p where f.owner.id != :userId and f.deleted=false " +
371 "and (p.user.id=:userId or p.group.id in (select distinct gg.id " +
372 "from Group gg join gg.members memb where memb.id=:userId))) ").
373 setParameter("userId", userId).getResultList();
377 * @see gr.ebs.gss.server.ejb.GSSDAO#getUsersSharingFilesForUser(java.lang.Long)
380 public List<User> getUsersSharingFilesForUser(Long userId) {
381 List<User> users = manager.createQuery("select distinct f.owner from FileHeader f " +
382 "LEFT JOIN f.permissions p where f.owner.id != :userId and f.deleted=false " +
383 "and (p.user.id=:userId or p.group.id in (select distinct gg.id from Group gg " +
384 "join gg.members memb where memb.id=:userId)))").
385 setParameter("userId", userId).getResultList();
391 public List<FileHeader> getSharedFilesNotInSharedFolders(Long userId) {
392 List<FileHeader> files = manager.createQuery("select distinct f from FileHeader f " +
393 "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
394 "and (f.readForAll=true or p.group.id != null or p.user.id != f.owner.id)" +
395 " and f.folder.id not in (select distinct fo.id from Folder fo LEFT JOIN " +
396 "fo.permissions po where fo.owner.id=:userId and fo.deleted=false and " +
397 "(po.group.id != null or po.user.id != fo.owner.id))").
398 setParameter("userId", userId).getResultList();
403 public List<FileHeader> getSharedFiles(Long userId) {
404 List<FileHeader> files = manager.createQuery("select distinct f from FileHeader f " +
405 "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
406 "and (p.group.id != null or p.user.id != f.owner.id)").
407 setParameter("userId", userId).getResultList();
412 public List<Folder> getSharedFolders(Long userId) {
413 List<Folder> folders = manager.createQuery("select distinct f from Folder f " +
414 "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
415 "and (p.group.id != null or p.user.id != f.owner.id)").
416 setParameter("userId", userId).getResultList();
421 * @see gr.ebs.gss.server.ejb.GSSDAO#getSharedFiles(java.lang.Long, java.lang.Long)
424 public List<FileHeader> getSharedFiles(Long userId, Long callingUserId) {
425 List<FileHeader> files = manager.createQuery("select distinct f from FileHeader f " +
426 "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
427 "and p.read=true and (p.user.id=:cuserId or p.group.id in " +
428 "(select distinct gg.id from Group gg join gg.members memb " +
429 "where memb.id=:cuserId)) and f.folder.id not in (select distinct fo.id " +
430 "from Folder fo LEFT JOIN fo.permissions po where fo.owner.id = :userId " +
431 "and fo.deleted=false and po.read=true and (po.user.id=:cuserId " +
432 "or po.group.id in (select distinct ggo.id from Group ggo " +
433 "join ggo.members membo where membo.id=:cuserId)))").
434 setParameter("userId", userId).setParameter("cuserId", callingUserId).getResultList();
439 * @see gr.ebs.gss.server.ejb.GSSDAO#getSharedRootFolders(java.lang.Long, java.lang.Long)
442 public List<Folder> getSharedRootFolders(Long userId, Long callingUserId) {
443 List<Folder> folders = manager.createQuery("select distinct f from Folder f " +
444 "LEFT JOIN f.permissions p where f.owner.id = :userId and f.deleted=false " +
445 "and p.read=true and (p.user.id=:cuserId or p.group.id in " +
446 "(select distinct gg.id from Group gg join gg.members memb " +
447 "where memb.id=:cuserId))) ").
448 setParameter("userId", userId).
449 setParameter("cuserId", callingUserId).
451 List<Folder> result = new ArrayList<Folder>();
452 for(Folder f : folders)
453 if(!folders.contains(f.getParent()))
460 * @see gr.ebs.gss.server.ejb.GSSDAO#searchFiles(java.lang.Long, java.lang.String)
463 public List<FileHeader> searchFiles(Long userId, String query) {
464 return manager.createQuery("select f from FileHeader f where f.owner.id=:userId and f.name like :query").
465 setParameter("query", "%"+query+"%").setParameter("userId",userId).getResultList();
469 public Nonce getNonce(String nonce, Long userId) throws ObjectNotFoundException {
470 List<Nonce> results = manager.createQuery("select n from Nonce n where n.userId=:userId and n.encodedNonce=:nonce").
471 setParameter("userId", userId).setParameter("nonce", nonce).getResultList();
472 if (results.isEmpty())
473 throw new ObjectNotFoundException("No nonce found");
474 return results.get(0);
478 * @see gr.ebs.gss.server.ejb.GSSDAO#getFileForIndexing(java.lang.Long)
481 public FileHeader getFileForIndexing(Long id) throws ObjectNotFoundException {
482 FileHeader h = getEntityById(FileHeader.class, id);
483 h.getFileTags().size();
488 * @see gr.ebs.gss.server.ejb.GSSDAO#getFileCount(java.lang.Long)
491 public Long getFileCount(Long userId) {
492 Long singleResult = (Long) manager.createQuery("select count(f) from FileHeader f where f.owner.id=:ownerId")
493 .setParameter("ownerId", userId)
499 * @see gr.ebs.gss.server.ejb.GSSDAO#getFileSize(java.lang.Long)
502 public Long getFileSize(Long userId) {
503 Long singleResult = (Long) manager.createQuery("select sum(f.fileSize) from FileBody f where f.header.owner.id=:ownerId")
504 .setParameter("ownerId", userId)
506 if(singleResult == null)
507 singleResult = new Long(0L);
513 * @see gr.ebs.gss.server.ejb.GSSDAO#getAllFileIds()
516 public List<Long> getAllFileIds() {
517 List<Long> ids = manager.createQuery("select f.id from FileHeader f").getResultList();
522 * @see gr.ebs.gss.server.ejb.GSSDAO#getFileUploadStatus(java.lang.Long, java.lang.String)
525 public FileUploadStatus getFileUploadStatus(Long userId, String fileName) {
526 List<FileUploadStatus> res = manager.createQuery(" select f from FileUploadStatus f where f.owner.id=:userId and f.filename=:filename").setParameter("userId", userId).setParameter("filename", fileName).getResultList();
533 public FileBody getFileVersion(Long fileId, int version) throws ObjectNotFoundException {
535 return (FileBody) manager.createQuery("select f from FileBody f " +
536 "where f.header.id=:fileId and f.version=:version")
537 .setParameter("fileId", fileId).setParameter("version", version)
539 } catch (NoResultException e) {
540 throw new ObjectNotFoundException("No version " + version + " found for file #" + fileId);