2 * Copyright 2007, 2008, 2009, 2010 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 static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
22 import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
23 import gr.ebs.gss.server.domain.AccountingInfo;
24 import gr.ebs.gss.server.domain.FileBody;
25 import gr.ebs.gss.server.domain.FileHeader;
26 import gr.ebs.gss.server.domain.FileUploadStatus;
27 import gr.ebs.gss.server.domain.Folder;
28 import gr.ebs.gss.server.domain.Group;
29 import gr.ebs.gss.server.domain.Invitation;
30 import gr.ebs.gss.server.domain.Nonce;
31 import gr.ebs.gss.server.domain.User;
32 import gr.ebs.gss.server.domain.UserClass;
33 import gr.ebs.gss.server.domain.UserLogin;
35 import java.math.BigInteger;
36 import java.util.ArrayList;
37 import java.util.Calendar;
38 import java.util.Date;
39 import java.util.GregorianCalendar;
40 import java.util.HashSet;
41 import java.util.List;
44 import javax.ejb.Stateless;
45 import javax.persistence.EntityManager;
46 import javax.persistence.NoResultException;
47 import javax.persistence.PersistenceContext;
48 import javax.persistence.Query;
50 import org.apache.commons.lang.StringUtils;
53 * The implementation of the GSSDAO interface.
56 public class GSSDAOBean implements GSSDAO {
58 private static final int BANDWIDTH_TIME_PERIOD_FIELD = Calendar.MONTH;
59 private static final int BANDWIDTH_TIME_PERIOD_AMOUNT = 1;
62 * The entity manager for the persistence unit
64 @PersistenceContext(unitName = "gss")
65 private EntityManager manager;
68 public Long getRootFolderId(final Long userId) throws ObjectNotFoundException {
71 throw new ObjectNotFoundException("No user specified");
72 return (Long) manager .createQuery("select f.id from Folder f " +
73 "where f.owner.id=:ownerId and f.parent is null")
74 .setParameter("ownerId", userId)
76 } catch (final NoResultException e) {
77 throw new ObjectNotFoundException("Root folder not found for user with id=" + userId);
82 public Folder getRootFolder(final Long userId) throws ObjectNotFoundException {
85 throw new ObjectNotFoundException("No user specified");
86 return (Folder) manager .createQuery("select f from Folder f where f.owner.id=:ownerId and f.parent is null")
87 .setParameter("ownerId", userId)
89 } catch (final NoResultException e) {
90 throw new ObjectNotFoundException("Root folder not found for user with id=" + userId);
95 public User getUser(final String username) throws ObjectNotFoundException {
98 throw new ObjectNotFoundException("No user specified");
99 return (User) manager .createQuery("select f from User f where f.username=:username")
100 .setParameter("username", username)
102 } catch (final NoResultException e) {
103 throw new ObjectNotFoundException("No User found for username=" + username);
108 public void create(final Object obj) {
110 throw new IllegalArgumentException("No object speficied");
111 manager.persist(obj);
115 public void refresh(final Object obj) {
117 throw new IllegalArgumentException("No object speficied");
118 manager.refresh(obj);
122 public void update(final Object obj) {
124 throw new IllegalArgumentException("No object speficied");
129 public void delete(final Object entity) {
131 throw new IllegalArgumentException("No object speficied");
132 manager.remove(entity);
136 public <T> T getEntityById(final Class<T> _class, final Object _id) throws ObjectNotFoundException {
138 throw new ObjectNotFoundException("No " + _class.getSimpleName() + " specified");
140 final T entity = manager.find(_class, _id);
142 throw new ObjectNotFoundException(_class.getSimpleName() + " with id=" + _id + " was not found");
148 @SuppressWarnings("unchecked")
149 public List<Group> getGroups(final Long userId) throws ObjectNotFoundException {
151 throw new ObjectNotFoundException("No user specified");
153 return manager.createQuery("select g from Group g where g.owner.id=:userId").setParameter("userId", userId).getResultList();
157 @SuppressWarnings("unchecked")
158 public List<FileHeader> getFiles(final Long folderId, Long userId, boolean ignoreDeleted) throws ObjectNotFoundException {
159 if (folderId == null)
160 throw new ObjectNotFoundException("No folder specified");
162 throw new ObjectNotFoundException("No user specified");
163 User user = getEntityById(User.class, userId);
166 query = "select f from FileHeader f where f.folder.id=:folderId and f.deleted=false";
168 query = "select f from FileHeader f where f.folder.id=:folderId";
169 List<FileHeader> tempList = manager.createQuery(query).setParameter("folderId", folderId).getResultList();
170 List<FileHeader> retv = new ArrayList<FileHeader>();
171 for (FileHeader f: tempList)
172 if (f.hasReadPermission(user)) retv.add(f);
178 @SuppressWarnings("unchecked")
179 public List<User> getUsers(final Long groupId) throws ObjectNotFoundException {
181 throw new ObjectNotFoundException("No group specified");
182 return manager.createQuery("select u from User u join u.groupsMember g where g.id=:groupId").
183 setParameter("groupId", groupId).getResultList();
187 public boolean existsFolderOrFile(Long parentId, String name) throws ObjectNotFoundException {
188 if (parentId == null)
189 throw new ObjectNotFoundException("No parent folder specified");
190 if (StringUtils.isEmpty(name))
191 throw new IllegalArgumentException("No folder name specified");
194 manager .createQuery("select f from Folder f " +
195 "where f.parent.id=:parentId and f.name=:name")
196 .setParameter("parentId", parentId)
197 .setParameter("name", name)
200 } catch (NoResultException e) {
202 manager .createQuery("select f from FileHeader f " +
203 "where f.folder.id=:parentId and f.name=:name")
204 .setParameter("parentId", parentId)
205 .setParameter("name", name)
208 } catch (NoResultException e1) {
215 public boolean existsGroup(final Long userId, final String name) throws ObjectNotFoundException {
217 throw new ObjectNotFoundException("No user specified");
218 if (StringUtils.isEmpty(name))
219 throw new ObjectNotFoundException("No group name specified");
221 manager .createQuery("select g from Group g where g.owner.id=:userId and g.name=:name")
222 .setParameter("userId", userId)
223 .setParameter("name", name)
226 } catch (final NoResultException e) {
232 public Set<String> getUserTags(final Long userId) throws ObjectNotFoundException {
234 throw new ObjectNotFoundException("No user specified");
235 return new HashSet(manager.createQuery("select t.tag from FileTag t where t.user.id=:userId order by t.tag")
236 .setParameter("userId", userId)
241 public void flush() {
246 public FileHeader getFile(Long folderId, String name) throws ObjectNotFoundException {
247 if (folderId == null)
248 throw new ObjectNotFoundException("No parent folder specified");
249 if (StringUtils.isEmpty(name))
250 throw new IllegalArgumentException("No file name specified");
253 return (FileHeader) manager.createQuery("select f from FileHeader f where f.folder.id=:parentId and f.name=:name")
254 .setParameter("parentId", folderId)
255 .setParameter("name", name)
257 } catch (final NoResultException e1) {
258 throw new ObjectNotFoundException("File not found");
263 public Folder getFolder(Long parentId, String name) throws ObjectNotFoundException {
264 if (parentId == null)
265 throw new ObjectNotFoundException("No parent folder specified");
266 if (StringUtils.isEmpty(name))
267 throw new IllegalArgumentException("No folder name specified");
270 return (Folder) manager.createQuery("select f from Folder f where f.parent.id=:parentId and f.name=:name")
271 .setParameter("parentId", parentId)
272 .setParameter("name", name)
274 } catch (NoResultException e) {
275 throw new ObjectNotFoundException("Folder not found");
280 public List<FileHeader> getDeletedFiles(Long userId) throws ObjectNotFoundException {
282 throw new ObjectNotFoundException("No User specified");
283 User user = getEntityById(User.class, userId);
285 List<FileHeader> tempList = manager.createQuery("select f from FileHeader f where f.owner.id=:userId and " +
286 "f.deleted=true and f.folder.deleted=false").
287 setParameter("userId", userId).getResultList();
288 List<FileHeader> retv = new ArrayList<FileHeader>();
289 for (FileHeader f: tempList)
290 if (f.hasReadPermission(user)) retv.add(f);
296 public List<Folder> getDeletedRootFolders(Long userId) throws ObjectNotFoundException {
298 throw new ObjectNotFoundException("No User specified");
299 return manager.createQuery("select f from Folder f where f.owner.id=:userId and " +
300 "f.deleted=true and f.parent.deleted=false").
301 setParameter("userId", userId).getResultList();
305 public User findUser(String username) {
306 if (username == null)
308 List<User> results = manager.createQuery("select u from User u where u.username=:username").
309 setParameter("username", username).getResultList();
310 if (results.isEmpty()) return null;
311 return results.get(0);
315 public User findUserByEmail(String email) {
318 List<User> results = manager.createQuery("select u from User u where u.email=:email").
319 setParameter("email", email).getResultList();
320 if (results.isEmpty()) return null;
321 return results.get(0);
325 public List<User> getUsersByUserNameLike(String username) {
326 return manager.createQuery("select u from User u where u.username like :username").
327 setParameter("username", username+"%").getResultList();
331 public List<Folder> getSharedRootFolders(Long userId) {
332 List<Folder> folders = manager.createQuery("select distinct f from Folder f " +
333 "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
334 "and (p.group.id != null or p.user.id != f.owner.id or f.readForAll=true) ").
335 setParameter("userId", userId).getResultList();
336 List<Folder> result = new ArrayList<Folder>();
337 for(Folder f : folders)
338 if(!folders.contains(f.getParent()))
344 public List<Folder> getFoldersPermittedForGroup(Long userId, Long groupId) {
345 return manager.createQuery("select distinct f from Folder f LEFT JOIN f.permissions p " +
346 "where f.owner.id=:userId and f.deleted = false and p.group.id=:groupId ").
347 setParameter("userId", userId).setParameter("groupId", groupId).getResultList();
350 private List<Long> getGroupIdsForUserId(Long userId) {
351 List<BigInteger> groups = manager.createNativeQuery("select distinct groupsmember_id " +
352 "from GSS_Group_GSS_User where members_id=:userId")
353 .setParameter("userId", userId)
355 List<Long> groupIds = new ArrayList<Long>();
356 for (BigInteger id : groups)
357 groupIds.add(id.longValue());
362 public List<User> getUsersSharingFoldersForUser(Long userId) {
363 List<Long> groupIds = getGroupIdsForUserId(userId);
364 Query q = manager.createQuery("select distinct f.owner from Folder f " +
365 "LEFT JOIN f.permissions p where f.owner.id != :userId and f.deleted=false " +
366 "and (p.user.id=:userId "+ (groupIds.isEmpty() ? "" : "or p.group.id in (:groupIds)") +")").
367 setParameter("userId", userId);
368 if (!groupIds.isEmpty())
369 q.setParameter("groupIds", groupIds);
370 return q.getResultList();
374 public List<User> getUsersSharingFilesForUser(Long userId) {
375 List<Long> groupIds = getGroupIdsForUserId(userId);
376 Query q = manager.createQuery("select distinct f.owner from FileHeader f " +
377 "LEFT JOIN f.permissions p where f.owner.id != :userId and f.deleted=false " +
378 "and (p.user.id=:userId " + (groupIds.isEmpty() ? "" : "or p.group.id in (:groupIds)") + ")").
379 setParameter("userId", userId);
380 if (!groupIds.isEmpty())
381 q.setParameter("groupIds", groupIds);
382 return q.getResultList();
386 public List<FileHeader> getSharedFilesNotInSharedFolders(Long userId) throws ObjectNotFoundException {
388 throw new ObjectNotFoundException("No user specified");
389 User user = getEntityById(User.class, userId);
390 List<FileHeader> tempList = manager.createQuery("select distinct f from FileHeader f " +
391 "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
392 "and (f.readForAll=true or p.group.id != null or p.user.id != f.owner.id)" +
393 " and f.folder.id not in (select distinct fo.id from Folder fo LEFT JOIN " +
394 "fo.permissions po where fo.owner.id=:userId and fo.deleted=false and " +
395 "(po.group.id != null or po.user.id != fo.owner.id or fo.readForAll = true))").
396 setParameter("userId", userId).getResultList();
397 List<FileHeader> retv = new ArrayList<FileHeader>();
398 for (FileHeader f: tempList)
399 if (f.hasReadPermission(user)) retv.add(f);
405 public List<FileHeader> getSharedFiles(Long userId) throws ObjectNotFoundException {
407 throw new ObjectNotFoundException("No user specified");
408 User user = getEntityById(User.class, userId);
409 List<FileHeader> tempList = manager.createQuery("select distinct f from FileHeader f " +
410 "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
411 "and (p.group.id != null or p.user.id != f.owner.id)").
412 setParameter("userId", userId).getResultList();
413 List<FileHeader> retv = new ArrayList<FileHeader>();
414 for (FileHeader f: tempList)
415 if (f.hasReadPermission(user)) retv.add(f);
421 public List<Folder> getSharedFolders(Long userId) {
422 List<Folder> folders = manager.createQuery("select distinct f from Folder f " +
423 "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
424 "and (p.group.id != null or p.user.id != f.owner.id)").
425 setParameter("userId", userId).getResultList();
430 public List<FileHeader> getSharedFiles(Long userId, Long callingUserId) throws ObjectNotFoundException {
432 throw new ObjectNotFoundException("No user specified");
433 User user = getEntityById(User.class, userId);
434 List<FileHeader> tempList = manager.createQuery("select distinct f from FileHeader f " +
435 "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
436 "and p.read=true and (p.user.id=:cuserId or p.group.id in " +
437 "(select distinct gg.id from Group gg join gg.members memb " +
438 "where memb.id=:cuserId)) and f.folder.id not in (select distinct fo.id " +
439 "from Folder fo LEFT JOIN fo.permissions po where fo.owner.id = :userId " +
440 "and fo.deleted=false and po.read=true and (po.user.id=:cuserId " +
441 "or po.group.id in (select distinct ggo.id from Group ggo " +
442 "join ggo.members membo where membo.id=:cuserId)))").
443 setParameter("userId", userId).setParameter("cuserId", callingUserId).getResultList();
444 List<FileHeader> retv = new ArrayList<FileHeader>();
445 for (FileHeader f: tempList)
446 if (f.hasReadPermission(user)) retv.add(f);
452 public List<Folder> getSharedRootFolders(Long userId, Long callingUserId) {
453 List<Folder> folders = manager.createQuery("select distinct f from Folder f " +
454 "LEFT JOIN f.permissions p where f.owner.id = :userId and f.deleted=false " +
455 "and p.read=true and (p.user.id=:cuserId or p.group.id in " +
456 "(select distinct gg.id from Group gg join gg.members memb " +
457 "where memb.id=:cuserId))) ").
458 setParameter("userId", userId).
459 setParameter("cuserId", callingUserId).
461 List<Folder> result = new ArrayList<Folder>();
462 for(Folder f : folders)
463 if(!folders.contains(f.getParent()))
470 public List<FileHeader> searchFiles(Long userId, String query) {
471 return manager.createQuery("select f from FileHeader f where f.owner.id=:userId and f.name like :query").
472 setParameter("query", "%"+query+"%").setParameter("userId",userId).getResultList();
476 public Nonce getNonce(String nonce, Long userId) throws ObjectNotFoundException {
477 List<Nonce> results = manager.createQuery("select n from Nonce n where n.userId=:userId and n.encodedNonce=:nonce").
478 setParameter("userId", userId).setParameter("nonce", nonce).getResultList();
479 if (results.isEmpty())
480 throw new ObjectNotFoundException("No nonce found");
481 return results.get(0);
485 public Long getFileCount(Long userId) {
486 Long singleResult = (Long) manager.createQuery("select count(f) from FileHeader f where f.owner.id=:ownerId")
487 .setParameter("ownerId", userId)
493 public Long getFileSize(Long userId) {
494 Long singleResult = (Long) manager.createQuery("select sum(f.fileSize) from FileBody f where f.header.owner.id=:ownerId")
495 .setParameter("ownerId", userId)
497 if(singleResult == null)
498 singleResult = new Long(0L);
504 public List<Long> getAllFileIds() {
505 List<Long> ids = manager.createQuery("select f.id from FileHeader f").getResultList();
510 public FileUploadStatus getFileUploadStatus(Long userId, String fileName) {
511 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();
518 public FileBody getFileVersion(Long fileId, int version) throws ObjectNotFoundException {
520 return (FileBody) manager.createQuery("select f from FileBody f " +
521 "where f.header.id=:fileId and f.version=:version")
522 .setParameter("fileId", fileId).setParameter("version", version)
524 } catch (NoResultException e) {
525 throw new ObjectNotFoundException("No version " + version + " found for file #" + fileId);
530 public void updateAccounting(User user, Date date, long bandwidthDiff) {
531 AccountingInfo ai = null;
533 ai = (AccountingInfo) manager.createQuery("select ai from AccountingInfo ai " +
534 "where ai.user=:user and ai.dateFrom<=:date and ai.dateTo>:date")
535 .setParameter("user", user)
536 .setParameter("date", date)
539 catch (NoResultException e) {
544 // The right entry does not exist; must be created.
545 // This is where we set the initial time period.
546 // We now start from the user's creation, we can change this to something else.
547 Calendar creationDate = new GregorianCalendar();
548 creationDate.setTime(user.getAuditInfo().getCreationDate());
552 long timeInMillis = date.getTime();
554 dateFrom = (Calendar) creationDate.clone();
555 dateFrom.add(BANDWIDTH_TIME_PERIOD_FIELD, offset);
556 dateTo = (Calendar) dateFrom.clone();
557 dateTo.add(BANDWIDTH_TIME_PERIOD_FIELD, 1);
558 offset += BANDWIDTH_TIME_PERIOD_AMOUNT;
560 while (!(dateFrom.getTimeInMillis()<=timeInMillis && dateTo.getTimeInMillis()>timeInMillis));
562 ai = new AccountingInfo(user, dateFrom.getTime(), dateTo.getTime());
567 ai.updateBandwidth(bandwidthDiff);
571 public List<UserClass> getUserClasses() {
572 // Ordering by quota is important here.
573 List<UserClass> ids = manager.createQuery("select uc from UserClass uc order by uc.quota").getResultList();
578 public List<User> getUsersByUserNameOrEmailLike(String query) {
579 return manager.createQuery("select u from User u where " +
580 " upper(u.username) like :query or upper(u.email) like :query order by u.username").
581 setParameter("query", query.toUpperCase()+"%").getResultList();
585 public Invitation findInvite(String code) {
588 List<Invitation> results = manager.createQuery("select i from Invitation i where i.code=:code").
589 setParameter("code", code).getResultList();
590 if (results.isEmpty()) return null;
591 return results.get(0);
595 public UserClass findCouponUserClass() {
596 List<UserClass> results = manager.createQuery("select uc from UserClass uc where uc.name=:name").
597 setParameter("name", getConfiguration().getString("couponQuota")).getResultList();
598 if (results.isEmpty()) {
599 // Create the coupon user class on first use.
600 UserClass couponClass = new UserClass();
601 couponClass.setName("coupon");
602 Long couponQuota = getConfiguration().getLong("couponQuota", new Long(52428800L));
603 couponClass.setQuota(couponQuota);
606 results.add(couponClass);
608 return results.get(0);
612 public Long getFileCount(UserClass userClass) {
615 result=(Long) manager.createQuery("select count(f) from FileHeader f").getSingleResult();
616 else result= (Long) manager.createQuery("select count(f) from FileHeader f where f.owner.userClass.id=:id").setParameter("id", userClass.getId()).getSingleResult();
623 public Long getFileSize(UserClass userClass) {
626 result=(Long) manager.createQuery("select sum(f.currentBody.fileSize) from FileHeader f").getSingleResult();
627 else result=(Long) manager.createQuery("select sum(f.currentBody.fileSize) from FileHeader f where f.owner.userClass.id=:id").setParameter("id", userClass.getId()).getSingleResult();
634 public Long getUserCount(UserClass userClass) {
637 result = (Long) manager.createQuery("select count(u) from User u").getSingleResult();
638 else result = (Long) manager.createQuery("select count(u) from User u where u.userClass.id=:id").setParameter("id", userClass.getId()).getSingleResult();
645 public Long getCountUsersByLastLogin(Date lastLoginDate) {
646 return (Long) manager.createQuery(
647 " select count(distinct ul.user.id) from UserLogin ul " +
648 " where ul.loginDate >=:ldate"
649 ).setParameter("ldate", lastLoginDate).getSingleResult();
653 public List<User> getUsersByLastLogin(Date lastLoginDate) {
654 return manager.createQuery(" select distinct ul.user from UserLogin ul " +
655 " where ul.loginDate >=:ldate ")
656 .setParameter("ldate", lastLoginDate)
661 public List<User> getUsersByLastLogin(Date lastLoginDate, int firstResult, int maxResult) {
662 return manager.createQuery("" +
663 " select ul from UserLogin ul " +
664 " where ul.loginDate >= :ldate " +
665 " order by ul.loginDate desc "
667 setParameter("ldate", lastLoginDate).setFirstResult(firstResult).setMaxResults(maxResult).getResultList();
671 public List<User> getInactiveUsers() {
672 return manager.createQuery("select u from User u where u.active=:active").setParameter("active", false).getResultList();
676 public List<FileHeader> searchFileByFilename(String filename) {
677 return manager.createQuery("select f from FileHeader f where f.name=:name").setParameter("name", filename).getResultList();
681 public Long getBandwithUsed(UserClass userClass, Date date) {
683 if (userClass == null)
684 result = (Long) manager.createQuery("select sum(ai.bandwidthUsed)" +
685 " from AccountingInfo ai where ai.dateFrom<=:date and " +
687 setParameter("date", date).
690 result = (Long) manager.createQuery("select sum(ai.bandwidthUsed)" +
691 " from AccountingInfo ai where ai.user.userClass.id=:id " +
692 "and ai.dateFrom<=:date and ai.dateTo>:date").
693 setParameter("date", date).
694 setParameter("id", userClass.getId()).
702 public List<AccountingInfo> getAccountingInfo(User user) {
703 List<AccountingInfo> ai = new ArrayList<AccountingInfo>();
705 ai = manager.createQuery("select ai from AccountingInfo ai " +
706 "where ai.user=:user")
707 .setParameter("user", user)
710 catch (NoResultException e) {}
715 public AccountingInfo getAccountingInfo(User user, Date date) {
716 AccountingInfo ai = null;
718 ai = (AccountingInfo) manager.createQuery("select ai from AccountingInfo ai " +
719 "where ai.user=:user and ai.dateFrom<=:date and ai.dateTo>:date")
720 .setParameter("user", user)
721 .setParameter("date", date)
724 catch (NoResultException e) {
725 // If not found, that means that there is no accounting info noted
726 // for given time. So return a 0 as an answer.
727 ai = new AccountingInfo(user, date, date);
733 public List<FileHeader> getFilesPermittedForGroup(Long userId, Long groupId) {
734 return manager.createQuery("select distinct f from FileHeader f LEFT JOIN f.permissions p " +
735 "where f.owner.id=:userId and f.deleted = false and p.group.id=:groupId ").
736 setParameter("userId", userId).setParameter("groupId", groupId).getResultList();
740 public List<Folder> getSharingFoldersForUser(Long userId) {
741 return manager.createQuery("select distinct f from Folder f " +
742 "LEFT JOIN f.permissions p where " +
743 " (p.user.id=:userId or p.group.id in (select distinct gg.id " +
744 "from Group gg join gg.members memb where memb.id=:userId))) ").
745 setParameter("userId", userId).getResultList();
749 public List<FileHeader> getSharingFilesForUser(Long userId) {
750 List<FileHeader> users = manager.createQuery("select distinct f from FileHeader f " +
751 "LEFT JOIN f.permissions p where " +
752 " (p.user.id=:userId or p.group.id in (select distinct gg.id from Group gg " +
753 "join gg.members memb where memb.id=:userId)))").
754 setParameter("userId", userId).getResultList();
759 public FileHeader getFileForIndexing(Long id) throws ObjectNotFoundException {
760 FileHeader h = getEntityById(FileHeader.class, id);
761 h.getFileTags().size();
762 h.getPermissions().size();
767 public List<Group> getGroupsContainingUser(Long userId){
768 List<Group> groups = manager.createQuery("select distinct gg " +
769 "from Group gg join gg.members memb where memb.id=:userId)").setParameter("userId", userId).getResultList();
774 public List<FileUploadStatus> getUploadStatus(Long userId){
775 List<FileUploadStatus> res = manager.createQuery("select f from FileUploadStatus f where f.owner.id=:userId").setParameter("userId", userId).getResultList();
779 public int deletePermissionsNotCorrespondingToFilesAndFolders(Long userId){
780 return manager.createNativeQuery("delete from permission where user_id=:userId and id not in(select permissions_id from fileheader_permission) and id not in(select permissions_id from folder_permission)").setParameter("userId", userId).executeUpdate();
783 public List<UserLogin> getLoginsForUser (Long userId){
784 List<UserLogin> res = manager
785 .createQuery("select ul from UserLogin ul where ul.user.id=:userId " +
786 " order by ul.loginDate desc")
787 .setParameter("userId", userId)
793 public List<UserLogin> getAllLoginsForUser (Long userId){
794 List<UserLogin> res = manager
795 .createQuery("select ul from UserLogin ul where ul.user.id=:userId ")
796 .setParameter("userId", userId)
802 public User getUserByUserName(String username) {
803 return (User) manager.createQuery("select u from User u where u.username=:username").
804 setParameter("username", username).getSingleResult();