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.FileLock;
30 import gr.ebs.gss.server.domain.Invitation;
31 import gr.ebs.gss.server.domain.Nonce;
32 import gr.ebs.gss.server.domain.User;
33 import gr.ebs.gss.server.domain.UserClass;
34 import gr.ebs.gss.server.domain.UserLogin;
35 import gr.ebs.gss.server.domain.WebDavNonce;
37 import java.math.BigInteger;
38 import java.util.ArrayList;
39 import java.util.Calendar;
40 import java.util.Date;
41 import java.util.GregorianCalendar;
42 import java.util.HashSet;
43 import java.util.List;
46 import javax.ejb.Stateless;
47 import javax.persistence.EntityManager;
48 import javax.persistence.NoResultException;
49 import javax.persistence.PersistenceContext;
50 import javax.persistence.Query;
52 import org.apache.commons.lang.StringUtils;
55 * The implementation of the GSSDAO interface.
58 public class GSSDAOBean implements GSSDAO {
60 private static final int BANDWIDTH_TIME_PERIOD_FIELD = Calendar.MONTH;
61 private static final int BANDWIDTH_TIME_PERIOD_AMOUNT = 1;
64 * The entity manager for the persistence unit
66 @PersistenceContext(unitName = "gss")
67 private EntityManager manager;
70 public Long getRootFolderId(final Long userId) throws ObjectNotFoundException {
73 throw new ObjectNotFoundException("No user specified");
74 return (Long) manager .createQuery("select f.id from Folder f " +
75 "where f.owner.id=:ownerId and f.parent is null")
76 .setParameter("ownerId", userId)
78 } catch (final NoResultException e) {
79 throw new ObjectNotFoundException("Root folder not found for user with id=" + userId);
84 public Folder getRootFolder(final Long userId) throws ObjectNotFoundException {
87 throw new ObjectNotFoundException("No user specified");
88 return (Folder) manager .createQuery("select f from Folder f where f.owner.id=:ownerId and f.parent is null")
89 .setParameter("ownerId", userId)
91 } catch (final NoResultException e) {
92 throw new ObjectNotFoundException("Root folder not found for user with id=" + userId);
97 public User getUser(final String username) throws ObjectNotFoundException {
100 throw new ObjectNotFoundException("No user specified");
101 return (User) manager .createQuery("select f from User f where f.username=:username")
102 .setParameter("username", username)
104 } catch (final NoResultException e) {
105 throw new ObjectNotFoundException("No User found for username=" + username);
110 public void create(final Object obj) {
112 throw new IllegalArgumentException("No object speficied");
113 manager.persist(obj);
117 public void refresh(final Object obj) {
119 throw new IllegalArgumentException("No object speficied");
120 manager.refresh(obj);
124 public void update(final Object obj) {
126 throw new IllegalArgumentException("No object speficied");
131 public void delete(final Object entity) {
133 throw new IllegalArgumentException("No object speficied");
134 manager.remove(entity);
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");
150 @SuppressWarnings("unchecked")
151 public List<Group> getGroups(final Long userId) throws ObjectNotFoundException {
153 throw new ObjectNotFoundException("No user specified");
155 return manager.createQuery("select g from Group g where g.owner.id=:userId").setParameter("userId", userId).getResultList();
159 @SuppressWarnings("unchecked")
160 public List<FileHeader> getFiles(final Long folderId, Long userId, boolean ignoreDeleted) throws ObjectNotFoundException {
161 if (folderId == null)
162 throw new ObjectNotFoundException("No folder specified");
164 throw new ObjectNotFoundException("No user specified");
165 User user = getEntityById(User.class, userId);
168 query = "select f from FileHeader f where f.folder.id=:folderId and f.deleted=false";
170 query = "select f from FileHeader f where f.folder.id=:folderId";
171 List<FileHeader> tempList = manager.createQuery(query).setParameter("folderId", folderId).getResultList();
172 List<FileHeader> retv = new ArrayList<FileHeader>();
173 for (FileHeader f: tempList)
174 if (f.hasReadPermission(user)) retv.add(f);
180 @SuppressWarnings("unchecked")
181 public List<User> getUsers(final Long groupId) throws ObjectNotFoundException {
183 throw new ObjectNotFoundException("No group specified");
184 return manager.createQuery("select u from User u join u.groupsMember g where g.id=:groupId").
185 setParameter("groupId", groupId).getResultList();
189 public boolean existsFolderOrFile(Long parentId, String name) throws ObjectNotFoundException {
190 if (parentId == null)
191 throw new ObjectNotFoundException("No parent folder specified");
192 if (StringUtils.isEmpty(name))
193 throw new IllegalArgumentException("No folder name specified");
196 manager .createQuery("select f from Folder f " +
197 "where f.parent.id=:parentId and f.name=:name")
198 .setParameter("parentId", parentId)
199 .setParameter("name", name)
202 } catch (NoResultException e) {
204 manager .createQuery("select f from FileHeader f " +
205 "where f.folder.id=:parentId and f.name=:name")
206 .setParameter("parentId", parentId)
207 .setParameter("name", name)
210 } catch (NoResultException e1) {
217 public boolean existsGroup(final Long userId, final String name) throws ObjectNotFoundException {
219 throw new ObjectNotFoundException("No user specified");
220 if (StringUtils.isEmpty(name))
221 throw new ObjectNotFoundException("No group name specified");
223 manager .createQuery("select g from Group g where g.owner.id=:userId and g.name=:name")
224 .setParameter("userId", userId)
225 .setParameter("name", name)
228 } catch (final NoResultException e) {
234 public Set<String> getUserTags(final Long userId) throws ObjectNotFoundException {
236 throw new ObjectNotFoundException("No user specified");
237 return new HashSet(manager.createQuery("select t.tag from FileTag t where t.user.id=:userId order by t.tag")
238 .setParameter("userId", userId)
243 public void flush() {
248 public FileHeader getFile(Long folderId, String name) throws ObjectNotFoundException {
249 if (folderId == null)
250 throw new ObjectNotFoundException("No parent folder specified");
251 if (StringUtils.isEmpty(name))
252 throw new IllegalArgumentException("No file name specified");
255 return (FileHeader) manager.createQuery("select f from FileHeader f where f.folder.id=:parentId and f.name=:name")
256 .setParameter("parentId", folderId)
257 .setParameter("name", name)
259 } catch (final NoResultException e1) {
260 throw new ObjectNotFoundException("File not found");
265 public Folder getFolder(Long parentId, String name) throws ObjectNotFoundException {
266 if (parentId == null)
267 throw new ObjectNotFoundException("No parent folder specified");
268 if (StringUtils.isEmpty(name))
269 throw new IllegalArgumentException("No folder name specified");
272 return (Folder) manager.createQuery("select f from Folder f where f.parent.id=:parentId and f.name=:name")
273 .setParameter("parentId", parentId)
274 .setParameter("name", name)
276 } catch (NoResultException e) {
277 throw new ObjectNotFoundException("Folder not found");
282 public List<FileHeader> getDeletedFiles(Long userId) throws ObjectNotFoundException {
284 throw new ObjectNotFoundException("No User specified");
285 User user = getEntityById(User.class, userId);
287 List<FileHeader> tempList = manager.createQuery("select f from FileHeader f where f.owner.id=:userId and " +
288 "f.deleted=true and f.folder.deleted=false").
289 setParameter("userId", userId).getResultList();
290 List<FileHeader> retv = new ArrayList<FileHeader>();
291 for (FileHeader f: tempList)
292 if (f.hasReadPermission(user)) retv.add(f);
298 public List<Folder> getDeletedRootFolders(Long userId) throws ObjectNotFoundException {
300 throw new ObjectNotFoundException("No User specified");
301 return manager.createQuery("select f from Folder f where f.owner.id=:userId and " +
302 "f.deleted=true and f.parent.deleted=false").
303 setParameter("userId", userId).getResultList();
307 public User findUser(String username) {
308 if (username == null)
310 List<User> results = manager.createQuery("select u from User u where u.username=:username").
311 setParameter("username", username).getResultList();
312 if (results.isEmpty()) return null;
313 return results.get(0);
317 public User findUserByEmail(String email) {
320 List<User> results = manager.createQuery("select u from User u where u.email=:email").
321 setParameter("email", email).getResultList();
322 if (results.isEmpty()) return null;
323 return results.get(0);
327 public List<User> getUsersByUserNameLike(String username) {
328 return manager.createQuery("select u from User u where u.username like :username").
329 setParameter("username", username+"%").getResultList();
333 public List<Folder> getSharedRootFolders(Long userId) {
334 List<Folder> folders = manager.createQuery("select distinct f from Folder f " +
335 "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
336 "and (p.group.id != null or p.user.id != f.owner.id or f.readForAll=true) ").
337 setParameter("userId", userId).getResultList();
338 List<Folder> result = new ArrayList<Folder>();
339 for(Folder f : folders)
340 if(!folders.contains(f.getParent()))
346 public List<Folder> getFoldersPermittedForGroup(Long userId, Long groupId) {
347 return manager.createQuery("select distinct f from Folder f LEFT JOIN f.permissions p " +
348 "where f.owner.id=:userId and f.deleted = false and p.group.id=:groupId ").
349 setParameter("userId", userId).setParameter("groupId", groupId).getResultList();
352 private List<Long> getGroupIdsForUserId(Long userId) {
353 List<BigInteger> groups = manager.createNativeQuery("select distinct groupsmember_id " +
354 "from GSS_Group_GSS_User where members_id=:userId")
355 .setParameter("userId", userId)
357 List<Long> groupIds = new ArrayList<Long>();
358 for (BigInteger id : groups)
359 groupIds.add(id.longValue());
364 public List<User> getUsersSharingFoldersForUser(Long userId) {
365 List<Long> groupIds = getGroupIdsForUserId(userId);
366 Query q = manager.createQuery("select distinct f.owner from Folder f " +
367 "LEFT JOIN f.permissions p where f.owner.id != :userId and f.deleted=false " +
368 "and (p.user.id=:userId "+ (groupIds.isEmpty() ? "" : "or p.group.id in (:groupIds)") +")").
369 setParameter("userId", userId);
370 if (!groupIds.isEmpty())
371 q.setParameter("groupIds", groupIds);
372 return q.getResultList();
376 public List<User> getUsersSharingFilesForUser(Long userId) {
377 List<Long> groupIds = getGroupIdsForUserId(userId);
378 Query q = manager.createQuery("select distinct f.owner from FileHeader f " +
379 "LEFT JOIN f.permissions p where f.owner.id != :userId and f.deleted=false " +
380 "and (p.user.id=:userId " + (groupIds.isEmpty() ? "" : "or p.group.id in (:groupIds)") + ")").
381 setParameter("userId", userId);
382 if (!groupIds.isEmpty())
383 q.setParameter("groupIds", groupIds);
384 return q.getResultList();
388 public List<FileHeader> getSharedFilesNotInSharedFolders(Long userId) throws ObjectNotFoundException {
390 throw new ObjectNotFoundException("No user specified");
391 User user = getEntityById(User.class, userId);
392 List<FileHeader> tempList = 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 or fo.readForAll = true))").
398 setParameter("userId", userId).getResultList();
399 List<FileHeader> retv = new ArrayList<FileHeader>();
400 for (FileHeader f: tempList)
401 if (f.hasReadPermission(user)) retv.add(f);
407 public List<FileHeader> getSharedFiles(Long userId) throws ObjectNotFoundException {
409 throw new ObjectNotFoundException("No user specified");
410 User user = getEntityById(User.class, userId);
411 List<FileHeader> tempList = manager.createQuery("select distinct f from FileHeader f " +
412 "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
413 "and (p.group.id != null or p.user.id != f.owner.id)").
414 setParameter("userId", userId).getResultList();
415 List<FileHeader> retv = new ArrayList<FileHeader>();
416 for (FileHeader f: tempList)
417 if (f.hasReadPermission(user)) retv.add(f);
423 public List<Folder> getSharedFolders(Long userId) {
424 List<Folder> folders = manager.createQuery("select distinct f from Folder f " +
425 "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
426 "and (p.group.id != null or p.user.id != f.owner.id)").
427 setParameter("userId", userId).getResultList();
432 public List<FileHeader> getSharedFiles(Long userId, Long callingUserId) throws ObjectNotFoundException {
434 throw new ObjectNotFoundException("No user specified");
435 User user = getEntityById(User.class, userId);
436 List<FileHeader> tempList = manager.createQuery("select distinct f from FileHeader f " +
437 "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
438 "and p.read=true and (p.user.id=:cuserId or p.group.id in " +
439 "(select distinct gg.id from Group gg join gg.members memb " +
440 "where memb.id=:cuserId)) and f.folder.id not in (select distinct fo.id " +
441 "from Folder fo LEFT JOIN fo.permissions po where fo.owner.id = :userId " +
442 "and fo.deleted=false and po.read=true and (po.user.id=:cuserId " +
443 "or po.group.id in (select distinct ggo.id from Group ggo " +
444 "join ggo.members membo where membo.id=:cuserId)))").
445 setParameter("userId", userId).setParameter("cuserId", callingUserId).getResultList();
446 List<FileHeader> retv = new ArrayList<FileHeader>();
447 for (FileHeader f: tempList)
448 if (f.hasReadPermission(user)) retv.add(f);
454 public List<Folder> getSharedRootFolders(Long userId, Long callingUserId) {
455 List<Folder> folders = manager.createQuery("select distinct f from Folder f " +
456 "LEFT JOIN f.permissions p where f.owner.id = :userId and f.deleted=false " +
457 "and p.read=true and (p.user.id=:cuserId or p.group.id in " +
458 "(select distinct gg.id from Group gg join gg.members memb " +
459 "where memb.id=:cuserId))) ").
460 setParameter("userId", userId).
461 setParameter("cuserId", callingUserId).
463 List<Folder> result = new ArrayList<Folder>();
464 for(Folder f : folders)
465 if(!folders.contains(f.getParent()))
472 public List<FileHeader> searchFiles(Long userId, String query) {
473 return manager.createQuery("select f from FileHeader f where f.owner.id=:userId and f.name like :query").
474 setParameter("query", "%"+query+"%").setParameter("userId",userId).getResultList();
478 public Nonce getNonce(String nonce, Long userId) throws ObjectNotFoundException {
479 List<Nonce> results = manager.createQuery("select n from Nonce n where n.userId=:userId and n.encodedNonce=:nonce").
480 setParameter("userId", userId).setParameter("nonce", nonce).getResultList();
481 if (results.isEmpty())
482 throw new ObjectNotFoundException("No nonce found");
483 return results.get(0);
487 public Long getFileCount(Long userId) {
488 Long singleResult = (Long) manager.createQuery("select count(f) from FileHeader f where f.owner.id=:ownerId")
489 .setParameter("ownerId", userId)
495 public Long getFileSize(Long userId) {
496 Long singleResult = (Long) manager.createQuery("select sum(f.fileSize) from FileBody f where f.header.owner.id=:ownerId")
497 .setParameter("ownerId", userId)
499 if(singleResult == null)
500 singleResult = new Long(0L);
506 public List<Long> getAllFileIds() {
507 List<Long> ids = manager.createQuery("select f.id from FileHeader f").getResultList();
512 public FileUploadStatus getFileUploadStatus(Long userId, String fileName) {
513 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();
520 public FileBody getFileVersion(Long fileId, int version) throws ObjectNotFoundException {
522 return (FileBody) manager.createQuery("select f from FileBody f " +
523 "where f.header.id=:fileId and f.version=:version")
524 .setParameter("fileId", fileId).setParameter("version", version)
526 } catch (NoResultException e) {
527 throw new ObjectNotFoundException("No version " + version + " found for file #" + fileId);
532 public void updateAccounting(User user, Date date, long bandwidthDiff) {
533 AccountingInfo ai = null;
535 ai = (AccountingInfo) manager.createQuery("select ai from AccountingInfo ai " +
536 "where ai.user=:user and ai.dateFrom<=:date and ai.dateTo>:date")
537 .setParameter("user", user)
538 .setParameter("date", date)
541 catch (NoResultException e) {
546 // The right entry does not exist; must be created.
547 // This is where we set the initial time period.
548 // We now start from the user's creation, we can change this to something else.
549 Calendar creationDate = new GregorianCalendar();
550 creationDate.setTime(user.getAuditInfo().getCreationDate());
554 long timeInMillis = date.getTime();
556 dateFrom = (Calendar) creationDate.clone();
557 dateFrom.add(BANDWIDTH_TIME_PERIOD_FIELD, offset);
558 dateTo = (Calendar) dateFrom.clone();
559 dateTo.add(BANDWIDTH_TIME_PERIOD_FIELD, 1);
560 offset += BANDWIDTH_TIME_PERIOD_AMOUNT;
562 while (!(dateFrom.getTimeInMillis()<=timeInMillis && dateTo.getTimeInMillis()>timeInMillis));
564 ai = new AccountingInfo(user, dateFrom.getTime(), dateTo.getTime());
569 ai.updateBandwidth(bandwidthDiff);
573 public List<UserClass> getUserClasses() {
574 // Ordering by quota is important here.
575 List<UserClass> ids = manager.createQuery("select uc from UserClass uc order by uc.quota").getResultList();
580 public List<User> getUsersByUserNameOrEmailLike(String query) {
581 return manager.createQuery("select u from User u where " +
582 " upper(u.username) like :query or upper(u.email) like :query order by u.username").
583 setParameter("query", query.toUpperCase()+"%").getResultList();
587 public Invitation findInvite(String code) {
590 List<Invitation> results = manager.createQuery("select i from Invitation i where i.code=:code").
591 setParameter("code", code).getResultList();
592 if (results.isEmpty()) return null;
593 return results.get(0);
597 public UserClass findCouponUserClass() {
598 List<UserClass> results = manager.createQuery("select uc from UserClass uc where uc.name=:name").
599 setParameter("name", getConfiguration().getString("couponQuota")).getResultList();
600 if (results.isEmpty()) {
601 // Create the coupon user class on first use.
602 UserClass couponClass = new UserClass();
603 couponClass.setName("coupon");
604 Long couponQuota = getConfiguration().getLong("couponQuota", new Long(52428800L));
605 couponClass.setQuota(couponQuota);
608 results.add(couponClass);
610 return results.get(0);
614 public Long getFileCount(UserClass userClass) {
617 result=(Long) manager.createQuery("select count(f) from FileHeader f").getSingleResult();
618 else result= (Long) manager.createQuery("select count(f) from FileHeader f where f.owner.userClass.id=:id").setParameter("id", userClass.getId()).getSingleResult();
625 public Long getFileSize(UserClass userClass) {
628 result=(Long) manager.createQuery("select sum(f.currentBody.fileSize) from FileHeader f").getSingleResult();
629 else result=(Long) manager.createQuery("select sum(f.currentBody.fileSize) from FileHeader f where f.owner.userClass.id=:id").setParameter("id", userClass.getId()).getSingleResult();
636 public Long getUserCount(UserClass userClass) {
639 result = (Long) manager.createQuery("select count(u) from User u").getSingleResult();
640 else result = (Long) manager.createQuery("select count(u) from User u where u.userClass.id=:id").setParameter("id", userClass.getId()).getSingleResult();
647 public Long getCountUsersByLastLogin(Date lastLoginDate) {
648 return (Long) manager.createQuery(
649 " select count(distinct ul.user.id) from UserLogin ul " +
650 " where ul.loginDate >=:ldate"
651 ).setParameter("ldate", lastLoginDate).getSingleResult();
655 public List<User> getUsersByLastLogin(Date lastLoginDate) {
656 return manager.createQuery(" select distinct ul.user from UserLogin ul " +
657 " where ul.loginDate >=:ldate ")
658 .setParameter("ldate", lastLoginDate)
663 public List<User> getUsersByLastLogin(Date lastLoginDate, int firstResult, int maxResult) {
664 return manager.createQuery("" +
665 " select ul from UserLogin ul " +
666 " where ul.loginDate >= :ldate " +
667 " order by ul.loginDate desc "
669 setParameter("ldate", lastLoginDate).setFirstResult(firstResult).setMaxResults(maxResult).getResultList();
673 public List<User> getInactiveUsers() {
674 return manager.createQuery("select u from User u where u.active=:active").setParameter("active", false).getResultList();
678 public List<FileHeader> searchFileByFilename(String filename) {
679 return manager.createQuery("select f from FileHeader f where f.name=:name").setParameter("name", filename).getResultList();
683 public Long getBandwithUsed(UserClass userClass, Date date) {
685 if (userClass == null)
686 result = (Long) manager.createQuery("select sum(ai.bandwidthUsed)" +
687 " from AccountingInfo ai where ai.dateFrom<=:date and " +
689 setParameter("date", date).
692 result = (Long) manager.createQuery("select sum(ai.bandwidthUsed)" +
693 " from AccountingInfo ai where ai.user.userClass.id=:id " +
694 "and ai.dateFrom<=:date and ai.dateTo>:date").
695 setParameter("date", date).
696 setParameter("id", userClass.getId()).
704 public List<AccountingInfo> getAccountingInfo(User user) {
705 List<AccountingInfo> ai = new ArrayList<AccountingInfo>();
707 ai = manager.createQuery("select ai from AccountingInfo ai " +
708 "where ai.user=:user")
709 .setParameter("user", user)
712 catch (NoResultException e) {}
717 public AccountingInfo getAccountingInfo(User user, Date date) {
718 AccountingInfo ai = null;
720 ai = (AccountingInfo) manager.createQuery("select ai from AccountingInfo ai " +
721 "where ai.user=:user and ai.dateFrom<=:date and ai.dateTo>:date")
722 .setParameter("user", user)
723 .setParameter("date", date)
726 catch (NoResultException e) {
727 // If not found, that means that there is no accounting info noted
728 // for given time. So return a 0 as an answer.
729 ai = new AccountingInfo(user, date, date);
735 public List<FileHeader> getFilesPermittedForGroup(Long userId, Long groupId) {
736 return manager.createQuery("select distinct f from FileHeader f LEFT JOIN f.permissions p " +
737 "where f.owner.id=:userId and f.deleted = false and p.group.id=:groupId ").
738 setParameter("userId", userId).setParameter("groupId", groupId).getResultList();
742 public List<Folder> getSharingFoldersForUser(Long userId) {
743 return manager.createQuery("select distinct f from Folder f " +
744 "LEFT JOIN f.permissions p where " +
745 " (p.user.id=:userId or p.group.id in (select distinct gg.id " +
746 "from Group gg join gg.members memb where memb.id=:userId))) ").
747 setParameter("userId", userId).getResultList();
751 public List<FileHeader> getSharingFilesForUser(Long userId) {
752 List<FileHeader> users = manager.createQuery("select distinct f from FileHeader f " +
753 "LEFT JOIN f.permissions p where " +
754 " (p.user.id=:userId or p.group.id in (select distinct gg.id from Group gg " +
755 "join gg.members memb where memb.id=:userId)))").
756 setParameter("userId", userId).getResultList();
761 public FileHeader getFileForIndexing(Long id) throws ObjectNotFoundException {
762 FileHeader h = getEntityById(FileHeader.class, id);
763 h.getFileTags().size();
764 h.getPermissions().size();
769 public List<Group> getGroupsContainingUser(Long userId){
770 List<Group> groups = manager.createQuery("select distinct gg " +
771 "from Group gg join gg.members memb where memb.id=:userId)").setParameter("userId", userId).getResultList();
776 public List<FileUploadStatus> getUploadStatus(Long userId){
777 List<FileUploadStatus> res = manager.createQuery("select f from FileUploadStatus f where f.owner.id=:userId").setParameter("userId", userId).getResultList();
781 public int deletePermissionsNotCorrespondingToFilesAndFolders(Long userId){
782 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();
785 public List<UserLogin> getLoginsForUser (Long userId){
786 List<UserLogin> res = manager
787 .createQuery("select ul from UserLogin ul where ul.user.id=:userId " +
788 " order by ul.loginDate desc")
789 .setParameter("userId", userId)
795 public List<UserLogin> getAllLoginsForUser (Long userId){
796 List<UserLogin> res = manager
797 .createQuery("select ul from UserLogin ul where ul.user.id=:userId ")
798 .setParameter("userId", userId)
804 public User getUserByUserName(String username) {
805 return (User) manager.createQuery("select u from User u where u.username=:username").
806 setParameter("username", username).getSingleResult();
811 /** WEBDAV LOCK API **/
813 public FileLock getLockById(String id) {
814 return manager.find(FileLock.class, id);
818 public FileLock getLockByToken(String tokenId) {
819 return (FileLock) manager.createQuery("select c from FileLock c where c.tokenId=:tokenId").setParameter("tokenId", tokenId).getSingleResult();
823 public void removeLock(FileLock lock) {
824 lock =getLockById(lock.getId());
826 manager.remove(lock);
830 public FileLock saveOrUpdateLock(FileLock lock) {
831 if(getLockById(lock.getId())!=null)
834 manager.persist(lock);
840 public WebDavNonce getWebDavNonce(String tokenId) {
841 return manager.find(WebDavNonce.class, tokenId);
845 public void removeWebDavNonce(WebDavNonce nonce) {
846 nonce =getWebDavNonce(nonce.getId());
848 manager.remove(nonce);
852 public WebDavNonce saveOrUpdateWebDavNonce(WebDavNonce nonce) {
853 if(getWebDavNonce(nonce.getId())!=null)
854 manager.merge(nonce);
856 manager.persist(nonce);