Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / server / ejb / GSSDAOBean.java @ 065ce8c1

History | View | Annotate | Download (30.2 kB)

1 14ad7326 pastith
/*
2 82248972 Panagiotis Astithas
 * Copyright 2007, 2008, 2009, 2010 Electronic Business Systems Ltd.
3 14ad7326 pastith
 *
4 14ad7326 pastith
 * This file is part of GSS.
5 14ad7326 pastith
 *
6 14ad7326 pastith
 * GSS is free software: you can redistribute it and/or modify
7 14ad7326 pastith
 * it under the terms of the GNU General Public License as published by
8 14ad7326 pastith
 * the Free Software Foundation, either version 3 of the License, or
9 14ad7326 pastith
 * (at your option) any later version.
10 14ad7326 pastith
 *
11 14ad7326 pastith
 * GSS is distributed in the hope that it will be useful,
12 14ad7326 pastith
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 14ad7326 pastith
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14ad7326 pastith
 * GNU General Public License for more details.
15 14ad7326 pastith
 *
16 14ad7326 pastith
 * You should have received a copy of the GNU General Public License
17 14ad7326 pastith
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 14ad7326 pastith
 */
19 14ad7326 pastith
package gr.ebs.gss.server.ejb;
20 14ad7326 pastith
21 01a30cd0 Panagiotis Astithas
import static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
22 14ad7326 pastith
import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
23 8f128261 droutsis
import gr.ebs.gss.server.domain.AccountingInfo;
24 14ad7326 pastith
import gr.ebs.gss.server.domain.FileBody;
25 14ad7326 pastith
import gr.ebs.gss.server.domain.FileHeader;
26 14ad7326 pastith
import gr.ebs.gss.server.domain.FileUploadStatus;
27 14ad7326 pastith
import gr.ebs.gss.server.domain.Folder;
28 14ad7326 pastith
import gr.ebs.gss.server.domain.Group;
29 065ce8c1 koutsoub
import gr.ebs.gss.server.domain.FileLock;
30 82248972 Panagiotis Astithas
import gr.ebs.gss.server.domain.Invitation;
31 14ad7326 pastith
import gr.ebs.gss.server.domain.Nonce;
32 14ad7326 pastith
import gr.ebs.gss.server.domain.User;
33 01a30cd0 Panagiotis Astithas
import gr.ebs.gss.server.domain.UserClass;
34 366f3f31 Natasa Kapravelou
import gr.ebs.gss.server.domain.UserLogin;
35 065ce8c1 koutsoub
import gr.ebs.gss.server.domain.WebDavNonce;
36 14ad7326 pastith
37 be3b26e1 Dimitris Routsis
import java.math.BigInteger;
38 14ad7326 pastith
import java.util.ArrayList;
39 8f128261 droutsis
import java.util.Calendar;
40 8f128261 droutsis
import java.util.Date;
41 8f128261 droutsis
import java.util.GregorianCalendar;
42 14ad7326 pastith
import java.util.HashSet;
43 14ad7326 pastith
import java.util.List;
44 14ad7326 pastith
import java.util.Set;
45 14ad7326 pastith
46 14ad7326 pastith
import javax.ejb.Stateless;
47 14ad7326 pastith
import javax.persistence.EntityManager;
48 14ad7326 pastith
import javax.persistence.NoResultException;
49 14ad7326 pastith
import javax.persistence.PersistenceContext;
50 8e86f905 Christos V. Stathis
import javax.persistence.Query;
51 14ad7326 pastith
52 14ad7326 pastith
import org.apache.commons.lang.StringUtils;
53 14ad7326 pastith
54 14ad7326 pastith
/**
55 14ad7326 pastith
 * The implementation of the GSSDAO interface.
56 14ad7326 pastith
 */
57 14ad7326 pastith
@Stateless
58 14ad7326 pastith
public class GSSDAOBean implements GSSDAO {
59 14ad7326 pastith
60 8f128261 droutsis
        private static final int BANDWIDTH_TIME_PERIOD_FIELD = Calendar.MONTH;
61 8f128261 droutsis
        private static final int BANDWIDTH_TIME_PERIOD_AMOUNT = 1;
62 8f128261 droutsis
63 14ad7326 pastith
        /**
64 14ad7326 pastith
         * The entity manager for the persistence unit
65 14ad7326 pastith
         */
66 14ad7326 pastith
        @PersistenceContext(unitName = "gss")
67 14ad7326 pastith
        private EntityManager manager;
68 14ad7326 pastith
69 023f6f1e Panagiotis Astithas
        @Override
70 966ae42b Dimitris Routsis
        public Long getRootFolderId(final Long userId) throws ObjectNotFoundException {
71 966ae42b Dimitris Routsis
                try {
72 966ae42b Dimitris Routsis
                        if (userId == null)
73 966ae42b Dimitris Routsis
                                throw new ObjectNotFoundException("No user specified");
74 966ae42b Dimitris Routsis
                        return (Long) manager        .createQuery("select f.id from Folder f " +
75 966ae42b Dimitris Routsis
                                        "where f.owner.id=:ownerId and f.parent is null")
76 966ae42b Dimitris Routsis
                                                                        .setParameter("ownerId", userId)
77 966ae42b Dimitris Routsis
                                                                        .getSingleResult();
78 966ae42b Dimitris Routsis
                } catch (final NoResultException e) {
79 966ae42b Dimitris Routsis
                        throw new ObjectNotFoundException("Root folder not found for user with id=" + userId);
80 966ae42b Dimitris Routsis
                }
81 966ae42b Dimitris Routsis
        }
82 966ae42b Dimitris Routsis
        
83 966ae42b Dimitris Routsis
        @Override
84 14ad7326 pastith
        public Folder getRootFolder(final Long userId) throws ObjectNotFoundException {
85 14ad7326 pastith
                try {
86 14ad7326 pastith
                        if (userId == null)
87 14ad7326 pastith
                                throw new ObjectNotFoundException("No user specified");
88 14ad7326 pastith
                        return (Folder) manager        .createQuery("select f from Folder f where f.owner.id=:ownerId and f.parent is null")
89 14ad7326 pastith
                                                                        .setParameter("ownerId", userId)
90 14ad7326 pastith
                                                                        .getSingleResult();
91 14ad7326 pastith
                } catch (final NoResultException e) {
92 14ad7326 pastith
                        throw new ObjectNotFoundException("Root folder not found for user with id=" + userId);
93 14ad7326 pastith
                }
94 14ad7326 pastith
        }
95 14ad7326 pastith
96 023f6f1e Panagiotis Astithas
        @Override
97 14ad7326 pastith
        public User getUser(final String username) throws ObjectNotFoundException {
98 14ad7326 pastith
                try {
99 14ad7326 pastith
                        if (username == null)
100 14ad7326 pastith
                                throw new ObjectNotFoundException("No user specified");
101 14ad7326 pastith
                        return (User) manager        .createQuery("select f from User f where f.username=:username")
102 14ad7326 pastith
                                                                        .setParameter("username", username)
103 14ad7326 pastith
                                                                        .getSingleResult();
104 14ad7326 pastith
                } catch (final NoResultException e) {
105 14ad7326 pastith
                        throw new ObjectNotFoundException("No User found for username=" + username);
106 14ad7326 pastith
                }
107 14ad7326 pastith
        }
108 14ad7326 pastith
109 023f6f1e Panagiotis Astithas
        @Override
110 14ad7326 pastith
        public void create(final Object obj) {
111 14ad7326 pastith
                if (obj == null)
112 14ad7326 pastith
                        throw new IllegalArgumentException("No object speficied");
113 14ad7326 pastith
                manager.persist(obj);
114 14ad7326 pastith
        }
115 14ad7326 pastith
116 023f6f1e Panagiotis Astithas
        @Override
117 14ad7326 pastith
        public void refresh(final Object obj) {
118 14ad7326 pastith
                if (obj == null)
119 14ad7326 pastith
                        throw new IllegalArgumentException("No object speficied");
120 14ad7326 pastith
                manager.refresh(obj);
121 14ad7326 pastith
        }
122 14ad7326 pastith
123 023f6f1e Panagiotis Astithas
        @Override
124 14ad7326 pastith
        public void update(final Object obj) {
125 14ad7326 pastith
                if (obj == null)
126 14ad7326 pastith
                        throw new IllegalArgumentException("No object speficied");
127 14ad7326 pastith
                manager.merge(obj);
128 14ad7326 pastith
        }
129 14ad7326 pastith
130 023f6f1e Panagiotis Astithas
        @Override
131 14ad7326 pastith
        public void delete(final Object entity) {
132 14ad7326 pastith
                if (entity == null)
133 14ad7326 pastith
                        throw new IllegalArgumentException("No object speficied");
134 14ad7326 pastith
                manager.remove(entity);
135 14ad7326 pastith
        }
136 14ad7326 pastith
137 023f6f1e Panagiotis Astithas
        @Override
138 14ad7326 pastith
        public <T> T getEntityById(final Class<T> _class, final Object _id) throws ObjectNotFoundException {
139 14ad7326 pastith
                if (_id == null)
140 14ad7326 pastith
                        throw new ObjectNotFoundException("No " + _class.getSimpleName() + " specified");
141 14ad7326 pastith
142 14ad7326 pastith
                final T entity = manager.find(_class, _id);
143 14ad7326 pastith
                if (entity == null)
144 14ad7326 pastith
                        throw new ObjectNotFoundException(_class.getSimpleName() + " with id=" + _id + " was not found");
145 14ad7326 pastith
146 14ad7326 pastith
                return entity;
147 14ad7326 pastith
        }
148 14ad7326 pastith
149 023f6f1e Panagiotis Astithas
        @Override
150 14ad7326 pastith
        @SuppressWarnings("unchecked")
151 14ad7326 pastith
        public List<Group> getGroups(final Long userId) throws ObjectNotFoundException {
152 14ad7326 pastith
                if (userId == null)
153 14ad7326 pastith
                        throw new ObjectNotFoundException("No user specified");
154 14ad7326 pastith
155 14ad7326 pastith
                return manager.createQuery("select g from Group g where g.owner.id=:userId").setParameter("userId", userId).getResultList();
156 14ad7326 pastith
        }
157 14ad7326 pastith
158 023f6f1e Panagiotis Astithas
        @Override
159 14ad7326 pastith
        @SuppressWarnings("unchecked")
160 cc154eca droutsis
        public List<FileHeader> getFiles(final Long folderId, Long userId, boolean ignoreDeleted) throws ObjectNotFoundException {
161 14ad7326 pastith
                if (folderId == null)
162 14ad7326 pastith
                        throw new ObjectNotFoundException("No folder specified");
163 cc154eca droutsis
                if (userId == null)
164 cc154eca droutsis
                        throw new ObjectNotFoundException("No user specified");
165 cc154eca droutsis
                User user = getEntityById(User.class, userId);
166 3d1b9329 koutsoub
                String query;
167 3d1b9329 koutsoub
                if(ignoreDeleted)
168 3d1b9329 koutsoub
                        query = "select f from FileHeader f where f.folder.id=:folderId  and f.deleted=false";
169 3d1b9329 koutsoub
                else
170 3d1b9329 koutsoub
                        query = "select f from FileHeader f where f.folder.id=:folderId";
171 cc154eca droutsis
                List<FileHeader> tempList = manager.createQuery(query).setParameter("folderId", folderId).getResultList();
172 cc154eca droutsis
                List<FileHeader> retv = new ArrayList<FileHeader>();
173 cc154eca droutsis
                for (FileHeader f: tempList)
174 cc154eca droutsis
                        if (f.hasReadPermission(user)) retv.add(f);
175 cc154eca droutsis
176 cc154eca droutsis
                return retv;
177 14ad7326 pastith
        }
178 14ad7326 pastith
179 023f6f1e Panagiotis Astithas
        @Override
180 14ad7326 pastith
        @SuppressWarnings("unchecked")
181 14ad7326 pastith
        public List<User> getUsers(final Long groupId) throws ObjectNotFoundException {
182 14ad7326 pastith
                if (groupId == null)
183 14ad7326 pastith
                        throw new ObjectNotFoundException("No group specified");
184 14ad7326 pastith
                return manager.createQuery("select u from User u join u.groupsMember g where g.id=:groupId").
185 14ad7326 pastith
                                setParameter("groupId", groupId).getResultList();
186 14ad7326 pastith
        }
187 14ad7326 pastith
188 14ad7326 pastith
        @Override
189 14ad7326 pastith
        public boolean existsFolderOrFile(Long parentId, String name) throws ObjectNotFoundException {
190 14ad7326 pastith
                if (parentId == null)
191 14ad7326 pastith
                        throw new ObjectNotFoundException("No parent folder specified");
192 14ad7326 pastith
                if (StringUtils.isEmpty(name))
193 14ad7326 pastith
                        throw new IllegalArgumentException("No folder name specified");
194 14ad7326 pastith
195 14ad7326 pastith
                try {
196 14ad7326 pastith
                        manager        .createQuery("select f from Folder f " +
197 14ad7326 pastith
                                        "where f.parent.id=:parentId and f.name=:name")
198 14ad7326 pastith
                                        .setParameter("parentId", parentId)
199 14ad7326 pastith
                                        .setParameter("name", name)
200 14ad7326 pastith
                                        .getSingleResult();
201 14ad7326 pastith
                        return true;
202 14ad7326 pastith
                } catch (NoResultException e) {
203 14ad7326 pastith
                        try {
204 14ad7326 pastith
                                manager        .createQuery("select f from FileHeader f " +
205 14ad7326 pastith
                                                "where f.folder.id=:parentId and f.name=:name")
206 14ad7326 pastith
                                                .setParameter("parentId", parentId)
207 14ad7326 pastith
                                                .setParameter("name", name)
208 14ad7326 pastith
                                                .getSingleResult();
209 14ad7326 pastith
                                return true;
210 14ad7326 pastith
                        } catch (NoResultException e1) {
211 14ad7326 pastith
                                return false;
212 14ad7326 pastith
                        }
213 14ad7326 pastith
                }
214 14ad7326 pastith
        }
215 14ad7326 pastith
216 023f6f1e Panagiotis Astithas
        @Override
217 14ad7326 pastith
        public boolean existsGroup(final Long userId, final String name) throws ObjectNotFoundException {
218 14ad7326 pastith
                if (userId == null)
219 14ad7326 pastith
                        throw new ObjectNotFoundException("No user specified");
220 14ad7326 pastith
                if (StringUtils.isEmpty(name))
221 14ad7326 pastith
                        throw new ObjectNotFoundException("No group name specified");
222 14ad7326 pastith
                try {
223 14ad7326 pastith
                        manager        .createQuery("select g from Group g where g.owner.id=:userId and g.name=:name")
224 14ad7326 pastith
                                        .setParameter("userId", userId)
225 14ad7326 pastith
                                        .setParameter("name", name)
226 14ad7326 pastith
                                        .getSingleResult();
227 14ad7326 pastith
                        return true;
228 14ad7326 pastith
                } catch (final NoResultException e) {
229 14ad7326 pastith
                        return false;
230 14ad7326 pastith
                }
231 14ad7326 pastith
        }
232 14ad7326 pastith
233 023f6f1e Panagiotis Astithas
        @Override
234 14ad7326 pastith
        public Set<String> getUserTags(final Long userId) throws ObjectNotFoundException {
235 14ad7326 pastith
                if (userId == null)
236 14ad7326 pastith
                        throw new ObjectNotFoundException("No user specified");
237 14ad7326 pastith
                return new HashSet(manager.createQuery("select t.tag from FileTag t where t.user.id=:userId order by t.tag")
238 14ad7326 pastith
                                                .setParameter("userId", userId)
239 14ad7326 pastith
                                                .getResultList());
240 14ad7326 pastith
        }
241 14ad7326 pastith
242 023f6f1e Panagiotis Astithas
        @Override
243 14ad7326 pastith
        public void flush() {
244 14ad7326 pastith
                manager.flush();
245 14ad7326 pastith
        }
246 14ad7326 pastith
247 023f6f1e Panagiotis Astithas
        @Override
248 14ad7326 pastith
        public FileHeader getFile(Long folderId, String name) throws ObjectNotFoundException {
249 14ad7326 pastith
                if (folderId == null)
250 14ad7326 pastith
                        throw new ObjectNotFoundException("No parent folder specified");
251 14ad7326 pastith
                if (StringUtils.isEmpty(name))
252 14ad7326 pastith
                        throw new IllegalArgumentException("No file name specified");
253 14ad7326 pastith
254 14ad7326 pastith
                try {
255 14ad7326 pastith
                        return (FileHeader) manager.createQuery("select f from FileHeader f where f.folder.id=:parentId and f.name=:name")
256 14ad7326 pastith
                                        .setParameter("parentId", folderId)
257 14ad7326 pastith
                                        .setParameter("name", name)
258 14ad7326 pastith
                                        .getSingleResult();
259 14ad7326 pastith
                } catch (final NoResultException e1) {
260 14ad7326 pastith
                        throw new ObjectNotFoundException("File not found");
261 14ad7326 pastith
                }
262 14ad7326 pastith
        }
263 14ad7326 pastith
264 14ad7326 pastith
        @Override
265 14ad7326 pastith
        public Folder getFolder(Long parentId, String name) throws ObjectNotFoundException {
266 14ad7326 pastith
                if (parentId == null)
267 14ad7326 pastith
                        throw new ObjectNotFoundException("No parent folder specified");
268 14ad7326 pastith
                if (StringUtils.isEmpty(name))
269 14ad7326 pastith
                        throw new IllegalArgumentException("No folder name specified");
270 14ad7326 pastith
271 14ad7326 pastith
                try {
272 14ad7326 pastith
                        return (Folder) manager.createQuery("select f from Folder f where f.parent.id=:parentId and f.name=:name")
273 14ad7326 pastith
                                        .setParameter("parentId", parentId)
274 14ad7326 pastith
                                        .setParameter("name", name)
275 14ad7326 pastith
                                        .getSingleResult();
276 14ad7326 pastith
                } catch (NoResultException e) {
277 14ad7326 pastith
                        throw new ObjectNotFoundException("Folder not found");
278 14ad7326 pastith
                }
279 14ad7326 pastith
        }
280 14ad7326 pastith
281 023f6f1e Panagiotis Astithas
        @Override
282 14ad7326 pastith
        public List<FileHeader> getDeletedFiles(Long userId) throws ObjectNotFoundException {
283 14ad7326 pastith
                if (userId == null)
284 14ad7326 pastith
                        throw new ObjectNotFoundException("No User specified");
285 cc154eca droutsis
                User user = getEntityById(User.class, userId);
286 14ad7326 pastith
287 cc154eca droutsis
                List<FileHeader> tempList = manager.createQuery("select f from FileHeader f where f.owner.id=:userId and " +
288 14ad7326 pastith
                                        "f.deleted=true and f.folder.deleted=false").
289 14ad7326 pastith
                                        setParameter("userId", userId).getResultList();
290 cc154eca droutsis
                List<FileHeader> retv = new ArrayList<FileHeader>();
291 cc154eca droutsis
                for (FileHeader f: tempList)
292 cc154eca droutsis
                        if (f.hasReadPermission(user)) retv.add(f);
293 cc154eca droutsis
294 cc154eca droutsis
                return retv;
295 14ad7326 pastith
        }
296 14ad7326 pastith
297 023f6f1e Panagiotis Astithas
        @Override
298 14ad7326 pastith
        public List<Folder> getDeletedRootFolders(Long userId) throws ObjectNotFoundException {
299 14ad7326 pastith
                if (userId == null)
300 14ad7326 pastith
                        throw new ObjectNotFoundException("No User specified");
301 14ad7326 pastith
                return manager.createQuery("select f from Folder f where f.owner.id=:userId and " +
302 14ad7326 pastith
                                        "f.deleted=true and f.parent.deleted=false").
303 14ad7326 pastith
                                        setParameter("userId", userId).getResultList();
304 14ad7326 pastith
        }
305 14ad7326 pastith
306 14ad7326 pastith
        @Override
307 14ad7326 pastith
        public User findUser(String username) {
308 14ad7326 pastith
                if (username == null)
309 14ad7326 pastith
                        return null;
310 14ad7326 pastith
                List<User> results = manager.createQuery("select u from User u where u.username=:username").
311 14ad7326 pastith
                                setParameter("username", username).getResultList();
312 14ad7326 pastith
                if (results.isEmpty()) return null;
313 14ad7326 pastith
                return results.get(0);
314 14ad7326 pastith
        }
315 14ad7326 pastith
316 3eaf782f pastith
        @Override
317 3eaf782f pastith
        public User findUserByEmail(String email) {
318 3eaf782f pastith
                if (email == null)
319 3eaf782f pastith
                        return null;
320 3eaf782f pastith
                List<User> results = manager.createQuery("select u from User u where u.email=:email").
321 3eaf782f pastith
                                setParameter("email", email).getResultList();
322 3eaf782f pastith
                if (results.isEmpty()) return null;
323 3eaf782f pastith
                return results.get(0);
324 3eaf782f pastith
        }
325 3eaf782f pastith
326 14ad7326 pastith
        @Override
327 14ad7326 pastith
        public List<User> getUsersByUserNameLike(String username) {
328 14ad7326 pastith
                return manager.createQuery("select u from User u where u.username like :username").
329 14ad7326 pastith
                setParameter("username", username+"%").getResultList();
330 14ad7326 pastith
        }
331 14ad7326 pastith
332 14ad7326 pastith
        @Override
333 14ad7326 pastith
        public List<Folder> getSharedRootFolders(Long userId) {
334 14ad7326 pastith
                List<Folder> folders = manager.createQuery("select distinct f from Folder f " +
335 14ad7326 pastith
                                        "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
336 dd127df7 Natasa Kapravelou
                                        "and (p.group.id != null or p.user.id != f.owner.id or f.readForAll=true) ").
337 14ad7326 pastith
                                        setParameter("userId", userId).getResultList();
338 14ad7326 pastith
                List<Folder> result = new ArrayList<Folder>();
339 14ad7326 pastith
                for(Folder f : folders)
340 14ad7326 pastith
                        if(!folders.contains(f.getParent()))
341 14ad7326 pastith
                                result.add(f);
342 14ad7326 pastith
                return result;
343 14ad7326 pastith
        }
344 14ad7326 pastith
345 14ad7326 pastith
        @Override
346 14ad7326 pastith
        public List<Folder> getFoldersPermittedForGroup(Long userId, Long groupId) {
347 14ad7326 pastith
                return manager.createQuery("select distinct f from Folder f LEFT JOIN f.permissions p " +
348 14ad7326 pastith
                                        "where f.owner.id=:userId and f.deleted = false and p.group.id=:groupId ").
349 14ad7326 pastith
                                        setParameter("userId", userId).setParameter("groupId", groupId).getResultList();
350 14ad7326 pastith
        }
351 14ad7326 pastith
352 be3b26e1 Dimitris Routsis
        private List<Long> getGroupIdsForUserId(Long userId) {
353 be3b26e1 Dimitris Routsis
                List<BigInteger> groups = manager.createNativeQuery("select distinct groupsmember_id " +
354 be3b26e1 Dimitris Routsis
                "from GSS_Group_GSS_User where members_id=:userId")
355 be3b26e1 Dimitris Routsis
                        .setParameter("userId", userId)
356 be3b26e1 Dimitris Routsis
                        .getResultList();
357 be3b26e1 Dimitris Routsis
                List<Long> groupIds = new ArrayList<Long>();
358 be3b26e1 Dimitris Routsis
                for (BigInteger id : groups)
359 be3b26e1 Dimitris Routsis
                        groupIds.add(id.longValue());
360 be3b26e1 Dimitris Routsis
                return groupIds;
361 be3b26e1 Dimitris Routsis
        }
362 be3b26e1 Dimitris Routsis
        
363 14ad7326 pastith
        @Override
364 14ad7326 pastith
        public List<User> getUsersSharingFoldersForUser(Long userId) {
365 8e86f905 Christos V. Stathis
        List<Long> groupIds = getGroupIdsForUserId(userId);
366 8e86f905 Christos V. Stathis
                Query q = manager.createQuery("select distinct f.owner from Folder f " +
367 14ad7326 pastith
                                        "LEFT JOIN f.permissions p where f.owner.id != :userId and f.deleted=false " +
368 8e86f905 Christos V. Stathis
                                        "and (p.user.id=:userId "+ (groupIds.isEmpty() ? "" : "or p.group.id in (:groupIds)") +")").
369 8e86f905 Christos V. Stathis
                                        setParameter("userId", userId);
370 8e86f905 Christos V. Stathis
        if (!groupIds.isEmpty())
371 8e86f905 Christos V. Stathis
            q.setParameter("groupIds", groupIds);
372 8e86f905 Christos V. Stathis
        return q.getResultList();            
373 14ad7326 pastith
        }
374 14ad7326 pastith
375 14ad7326 pastith
        @Override
376 14ad7326 pastith
        public List<User> getUsersSharingFilesForUser(Long userId) {
377 8e86f905 Christos V. Stathis
        List<Long> groupIds = getGroupIdsForUserId(userId);
378 8e86f905 Christos V. Stathis
                Query q = manager.createQuery("select distinct f.owner from FileHeader f " +
379 14ad7326 pastith
                                        "LEFT JOIN f.permissions p where f.owner.id != :userId and f.deleted=false " +
380 8e86f905 Christos V. Stathis
                                        "and (p.user.id=:userId " + (groupIds.isEmpty() ? "" : "or p.group.id in (:groupIds)") + ")").
381 8e86f905 Christos V. Stathis
                                        setParameter("userId", userId);
382 8e86f905 Christos V. Stathis
        if (!groupIds.isEmpty())
383 8e86f905 Christos V. Stathis
            q.setParameter("groupIds", groupIds);
384 8e86f905 Christos V. Stathis
        return q.getResultList();
385 14ad7326 pastith
        }
386 14ad7326 pastith
387 14ad7326 pastith
        @Override
388 cc154eca droutsis
        public List<FileHeader> getSharedFilesNotInSharedFolders(Long userId) throws ObjectNotFoundException {
389 cc154eca droutsis
                if (userId == null)
390 cc154eca droutsis
                        throw new ObjectNotFoundException("No user specified");
391 cc154eca droutsis
                User user = getEntityById(User.class, userId);
392 cc154eca droutsis
                List<FileHeader> tempList = manager.createQuery("select distinct f from FileHeader f " +
393 14ad7326 pastith
                                        "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
394 b4c3f47a pastith
                                        "and (f.readForAll=true or p.group.id != null or p.user.id != f.owner.id)" +
395 b4c3f47a pastith
                                        " and f.folder.id not in (select distinct fo.id from Folder fo LEFT JOIN " +
396 b4c3f47a pastith
                                        "fo.permissions po where fo.owner.id=:userId and fo.deleted=false and " +
397 749c03f9 Natasa Kapravelou
                                        "(po.group.id != null or po.user.id != fo.owner.id or fo.readForAll = true))").
398 14ad7326 pastith
                                        setParameter("userId", userId).getResultList();
399 cc154eca droutsis
                List<FileHeader> retv = new ArrayList<FileHeader>();
400 cc154eca droutsis
                for (FileHeader f: tempList)
401 cc154eca droutsis
                        if (f.hasReadPermission(user)) retv.add(f);
402 cc154eca droutsis
403 cc154eca droutsis
                return retv;
404 14ad7326 pastith
        }
405 14ad7326 pastith
406 14ad7326 pastith
        @Override
407 cc154eca droutsis
        public List<FileHeader> getSharedFiles(Long userId) throws ObjectNotFoundException {
408 cc154eca droutsis
                if (userId == null)
409 cc154eca droutsis
                        throw new ObjectNotFoundException("No user specified");
410 cc154eca droutsis
                User user = getEntityById(User.class, userId);
411 cc154eca droutsis
                List<FileHeader> tempList = manager.createQuery("select distinct f from FileHeader f " +
412 14ad7326 pastith
                                        "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
413 14ad7326 pastith
                                        "and (p.group.id != null or p.user.id != f.owner.id)").
414 14ad7326 pastith
                                        setParameter("userId", userId).getResultList();
415 cc154eca droutsis
                List<FileHeader> retv = new ArrayList<FileHeader>();
416 cc154eca droutsis
                for (FileHeader f: tempList)
417 cc154eca droutsis
                        if (f.hasReadPermission(user)) retv.add(f);
418 cc154eca droutsis
419 cc154eca droutsis
                return retv;
420 14ad7326 pastith
        }
421 14ad7326 pastith
422 14ad7326 pastith
        @Override
423 14ad7326 pastith
        public List<Folder> getSharedFolders(Long userId) {
424 14ad7326 pastith
                List<Folder> folders = manager.createQuery("select distinct f from Folder f " +
425 14ad7326 pastith
                                        "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
426 14ad7326 pastith
                                        "and (p.group.id != null or p.user.id != f.owner.id)").
427 14ad7326 pastith
                                        setParameter("userId", userId).getResultList();
428 14ad7326 pastith
                return folders;
429 14ad7326 pastith
        }
430 14ad7326 pastith
431 14ad7326 pastith
        @Override
432 cc154eca droutsis
        public List<FileHeader> getSharedFiles(Long userId, Long callingUserId) throws ObjectNotFoundException {
433 cc154eca droutsis
                if (userId == null)
434 cc154eca droutsis
                        throw new ObjectNotFoundException("No user specified");
435 cc154eca droutsis
                User user = getEntityById(User.class, userId);
436 cc154eca droutsis
                List<FileHeader> tempList = manager.createQuery("select distinct f from FileHeader f " +
437 14ad7326 pastith
                                        "LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
438 14ad7326 pastith
                                        "and p.read=true and (p.user.id=:cuserId or p.group.id in " +
439 14ad7326 pastith
                                        "(select distinct gg.id from Group gg join gg.members memb " +
440 14ad7326 pastith
                                        "where memb.id=:cuserId)) and f.folder.id not in (select distinct fo.id " +
441 14ad7326 pastith
                                        "from Folder fo LEFT JOIN fo.permissions po where fo.owner.id = :userId " +
442 14ad7326 pastith
                                        "and fo.deleted=false and po.read=true and (po.user.id=:cuserId " +
443 14ad7326 pastith
                                        "or po.group.id in (select distinct ggo.id from Group ggo " +
444 14ad7326 pastith
                                        "join ggo.members membo where membo.id=:cuserId)))").
445 14ad7326 pastith
                                        setParameter("userId", userId).setParameter("cuserId", callingUserId).getResultList();
446 cc154eca droutsis
                List<FileHeader> retv = new ArrayList<FileHeader>();
447 cc154eca droutsis
                for (FileHeader f: tempList)
448 cc154eca droutsis
                        if (f.hasReadPermission(user)) retv.add(f);
449 cc154eca droutsis
450 cc154eca droutsis
                return retv;
451 14ad7326 pastith
        }
452 14ad7326 pastith
453 14ad7326 pastith
        @Override
454 14ad7326 pastith
        public List<Folder> getSharedRootFolders(Long userId, Long callingUserId) {
455 14ad7326 pastith
                List<Folder> folders = manager.createQuery("select distinct f from Folder f " +
456 14ad7326 pastith
                                        "LEFT JOIN f.permissions p where f.owner.id = :userId and f.deleted=false " +
457 14ad7326 pastith
                                        "and p.read=true and (p.user.id=:cuserId or p.group.id in " +
458 14ad7326 pastith
                                        "(select distinct gg.id from Group gg join gg.members memb " +
459 14ad7326 pastith
                                        "where memb.id=:cuserId))) ").
460 14ad7326 pastith
                                        setParameter("userId", userId).
461 14ad7326 pastith
                                        setParameter("cuserId", callingUserId).
462 14ad7326 pastith
                                        getResultList();
463 14ad7326 pastith
                List<Folder> result = new ArrayList<Folder>();
464 14ad7326 pastith
                for(Folder f : folders)
465 14ad7326 pastith
                        if(!folders.contains(f.getParent()))
466 14ad7326 pastith
                                result.add(f);
467 14ad7326 pastith
                return result;
468 14ad7326 pastith
469 14ad7326 pastith
        }
470 14ad7326 pastith
471 14ad7326 pastith
        @Override
472 14ad7326 pastith
        public List<FileHeader> searchFiles(Long userId, String query) {
473 14ad7326 pastith
                return manager.createQuery("select f from FileHeader f where f.owner.id=:userId and f.name like :query").
474 14ad7326 pastith
                                setParameter("query", "%"+query+"%").setParameter("userId",userId).getResultList();
475 14ad7326 pastith
        }
476 14ad7326 pastith
477 14ad7326 pastith
        @Override
478 14ad7326 pastith
        public Nonce getNonce(String nonce, Long userId) throws ObjectNotFoundException {
479 14ad7326 pastith
                List<Nonce> results = manager.createQuery("select n from Nonce n where n.userId=:userId and n.encodedNonce=:nonce").
480 14ad7326 pastith
                                setParameter("userId", userId).setParameter("nonce", nonce).getResultList();
481 14ad7326 pastith
                if (results.isEmpty())
482 14ad7326 pastith
                        throw new ObjectNotFoundException("No nonce found");
483 14ad7326 pastith
                return results.get(0);
484 14ad7326 pastith
        }
485 14ad7326 pastith
486 14ad7326 pastith
        @Override
487 14ad7326 pastith
        public Long getFileCount(Long userId) {
488 14ad7326 pastith
                Long singleResult = (Long) manager.createQuery("select count(f) from FileHeader f where f.owner.id=:ownerId")
489 14ad7326 pastith
                .setParameter("ownerId", userId)
490 14ad7326 pastith
                .getSingleResult();
491 14ad7326 pastith
                return singleResult;
492 14ad7326 pastith
        }
493 14ad7326 pastith
494 14ad7326 pastith
        @Override
495 14ad7326 pastith
        public Long getFileSize(Long userId) {
496 14ad7326 pastith
                Long singleResult = (Long) manager.createQuery("select sum(f.fileSize) from FileBody f where f.header.owner.id=:ownerId")
497 14ad7326 pastith
                .setParameter("ownerId", userId)
498 14ad7326 pastith
                .getSingleResult();
499 14ad7326 pastith
                if(singleResult == null)
500 14ad7326 pastith
                        singleResult = new Long(0L);
501 14ad7326 pastith
                return singleResult;
502 14ad7326 pastith
503 14ad7326 pastith
        }
504 14ad7326 pastith
505 14ad7326 pastith
        @Override
506 14ad7326 pastith
        public List<Long> getAllFileIds() {
507 14ad7326 pastith
                List<Long> ids = manager.createQuery("select f.id from FileHeader f").getResultList();
508 14ad7326 pastith
                return ids;
509 14ad7326 pastith
        }
510 14ad7326 pastith
511 14ad7326 pastith
        @Override
512 14ad7326 pastith
        public FileUploadStatus getFileUploadStatus(Long userId, String fileName) {
513 14ad7326 pastith
                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();
514 14ad7326 pastith
                if(res.size()>0)
515 14ad7326 pastith
                        return res.get(0);
516 14ad7326 pastith
                return null;
517 14ad7326 pastith
        }
518 14ad7326 pastith
519 14ad7326 pastith
        @Override
520 14ad7326 pastith
        public FileBody getFileVersion(Long fileId, int version) throws ObjectNotFoundException {
521 14ad7326 pastith
                try {
522 14ad7326 pastith
                        return (FileBody) manager.createQuery("select f from FileBody f " +
523 14ad7326 pastith
                        "where f.header.id=:fileId and f.version=:version")
524 14ad7326 pastith
                        .setParameter("fileId", fileId).setParameter("version", version)
525 14ad7326 pastith
                        .getSingleResult();
526 14ad7326 pastith
                } catch (NoResultException e) {
527 14ad7326 pastith
                        throw new ObjectNotFoundException("No version " + version + " found for file #" + fileId);
528 14ad7326 pastith
                }
529 14ad7326 pastith
        }
530 8f128261 droutsis
531 8f128261 droutsis
        @Override
532 8f128261 droutsis
        public void updateAccounting(User user, Date date, long bandwidthDiff) {
533 8f128261 droutsis
                AccountingInfo ai = null;
534 8f128261 droutsis
                try {
535 8f128261 droutsis
                        ai = (AccountingInfo) manager.createQuery("select ai from AccountingInfo ai " +
536 8f128261 droutsis
                                "where ai.user=:user and ai.dateFrom<=:date and ai.dateTo>:date")
537 8f128261 droutsis
                                .setParameter("user", user)
538 8f128261 droutsis
                                .setParameter("date", date)
539 8f128261 droutsis
                                .getSingleResult();
540 8f128261 droutsis
                }
541 8f128261 droutsis
                catch (NoResultException e) {
542 8f128261 droutsis
                        ai = null;
543 8f128261 droutsis
                }
544 8f128261 droutsis
545 8f128261 droutsis
                if (ai==null) {
546 8f128261 droutsis
                        // The right entry does not exist; must be created.
547 8f128261 droutsis
                        // This is where we set the initial time period.
548 8f128261 droutsis
                        // We now start from the user's creation, we can change this to something else.
549 8f128261 droutsis
                        Calendar creationDate = new GregorianCalendar();
550 8f128261 droutsis
                        creationDate.setTime(user.getAuditInfo().getCreationDate());
551 8f128261 droutsis
                        int offset = 0;
552 8f128261 droutsis
                        Calendar dateFrom;
553 8f128261 droutsis
                        Calendar dateTo;
554 8f128261 droutsis
                        long timeInMillis = date.getTime();
555 8f128261 droutsis
                        do {
556 8f128261 droutsis
                                dateFrom = (Calendar) creationDate.clone();
557 8f128261 droutsis
                                dateFrom.add(BANDWIDTH_TIME_PERIOD_FIELD, offset);
558 8f128261 droutsis
                                dateTo = (Calendar) dateFrom.clone();
559 8f128261 droutsis
                                dateTo.add(BANDWIDTH_TIME_PERIOD_FIELD, 1);
560 8f128261 droutsis
                                offset += BANDWIDTH_TIME_PERIOD_AMOUNT;
561 8f128261 droutsis
                        }
562 8f128261 droutsis
                        while (!(dateFrom.getTimeInMillis()<=timeInMillis && dateTo.getTimeInMillis()>timeInMillis));
563 8f128261 droutsis
564 8f128261 droutsis
                        ai = new AccountingInfo(user, dateFrom.getTime(), dateTo.getTime());
565 8f128261 droutsis
                        manager.persist(ai);
566 8f128261 droutsis
                }
567 8f128261 droutsis
568 8f128261 droutsis
                // Do the update.
569 8f128261 droutsis
                ai.updateBandwidth(bandwidthDiff);
570 8f128261 droutsis
        }
571 8f128261 droutsis
572 82248972 Panagiotis Astithas
        @Override
573 023f6f1e Panagiotis Astithas
        public List<UserClass> getUserClasses() {
574 023f6f1e Panagiotis Astithas
                // Ordering by quota is important here.
575 023f6f1e Panagiotis Astithas
                List<UserClass> ids = manager.createQuery("select uc from UserClass uc order by uc.quota").getResultList();
576 023f6f1e Panagiotis Astithas
                return ids;
577 023f6f1e Panagiotis Astithas
        }
578 023f6f1e Panagiotis Astithas
579 023f6f1e Panagiotis Astithas
        @Override
580 023f6f1e Panagiotis Astithas
        public List<User> getUsersByUserNameOrEmailLike(String query) {
581 023f6f1e Panagiotis Astithas
                return manager.createQuery("select u from User u where " +
582 023f6f1e Panagiotis Astithas
                                        " upper(u.username) like :query or upper(u.email) like :query order by u.username").
583 023f6f1e Panagiotis Astithas
                        setParameter("query", query.toUpperCase()+"%").getResultList();
584 023f6f1e Panagiotis Astithas
        }
585 023f6f1e Panagiotis Astithas
586 023f6f1e Panagiotis Astithas
        @Override
587 82248972 Panagiotis Astithas
        public Invitation findInvite(String code) {
588 82248972 Panagiotis Astithas
                if (code == null)
589 82248972 Panagiotis Astithas
                        return null;
590 82248972 Panagiotis Astithas
                List<Invitation> results = manager.createQuery("select i from Invitation i where i.code=:code").
591 82248972 Panagiotis Astithas
                                setParameter("code", code).getResultList();
592 82248972 Panagiotis Astithas
                if (results.isEmpty()) return null;
593 82248972 Panagiotis Astithas
                return results.get(0);
594 82248972 Panagiotis Astithas
        }
595 82248972 Panagiotis Astithas
596 01a30cd0 Panagiotis Astithas
        @Override
597 01a30cd0 Panagiotis Astithas
        public UserClass findCouponUserClass() {
598 01a30cd0 Panagiotis Astithas
                List<UserClass> results = manager.createQuery("select uc from UserClass uc where uc.name=:name").
599 01a30cd0 Panagiotis Astithas
                                setParameter("name", getConfiguration().getString("couponQuota")).getResultList();
600 01a30cd0 Panagiotis Astithas
                if (results.isEmpty()) {
601 01a30cd0 Panagiotis Astithas
                        // Create the coupon user class on first use.
602 01a30cd0 Panagiotis Astithas
                        UserClass couponClass = new UserClass();
603 01a30cd0 Panagiotis Astithas
                        couponClass.setName("coupon");
604 01a30cd0 Panagiotis Astithas
                        Long couponQuota = getConfiguration().getLong("couponQuota", new Long(52428800L));
605 01a30cd0 Panagiotis Astithas
                        couponClass.setQuota(couponQuota);
606 01a30cd0 Panagiotis Astithas
                        create(couponClass);
607 01a30cd0 Panagiotis Astithas
                        flush();
608 01a30cd0 Panagiotis Astithas
                        results.add(couponClass);
609 01a30cd0 Panagiotis Astithas
                }
610 01a30cd0 Panagiotis Astithas
                return results.get(0);
611 01a30cd0 Panagiotis Astithas
        }
612 023f6f1e Panagiotis Astithas
613 023f6f1e Panagiotis Astithas
        @Override
614 023f6f1e Panagiotis Astithas
        public Long getFileCount(UserClass userClass) {
615 023f6f1e Panagiotis Astithas
                Long result;
616 023f6f1e Panagiotis Astithas
                if(userClass==null)
617 023f6f1e Panagiotis Astithas
                        result=(Long) manager.createQuery("select count(f) from FileHeader f").getSingleResult();
618 023f6f1e Panagiotis Astithas
                else result= (Long) manager.createQuery("select count(f) from FileHeader f where f.owner.userClass.id=:id").setParameter("id", userClass.getId()).getSingleResult();
619 023f6f1e Panagiotis Astithas
                if(result==null)
620 023f6f1e Panagiotis Astithas
                        result =0L;
621 023f6f1e Panagiotis Astithas
                return result;
622 023f6f1e Panagiotis Astithas
        }
623 023f6f1e Panagiotis Astithas
624 023f6f1e Panagiotis Astithas
        @Override
625 023f6f1e Panagiotis Astithas
        public Long getFileSize(UserClass userClass) {
626 023f6f1e Panagiotis Astithas
                Long result;
627 023f6f1e Panagiotis Astithas
                if(userClass==null)
628 023f6f1e Panagiotis Astithas
                        result=(Long) manager.createQuery("select sum(f.currentBody.fileSize) from FileHeader f").getSingleResult();
629 023f6f1e Panagiotis Astithas
                else result=(Long) manager.createQuery("select sum(f.currentBody.fileSize) from FileHeader f where f.owner.userClass.id=:id").setParameter("id", userClass.getId()).getSingleResult();
630 023f6f1e Panagiotis Astithas
                if(result==null)
631 023f6f1e Panagiotis Astithas
                        result =0L;
632 023f6f1e Panagiotis Astithas
                return result;
633 023f6f1e Panagiotis Astithas
        }
634 023f6f1e Panagiotis Astithas
635 023f6f1e Panagiotis Astithas
        @Override
636 023f6f1e Panagiotis Astithas
        public Long getUserCount(UserClass userClass) {
637 023f6f1e Panagiotis Astithas
                Long result;
638 023f6f1e Panagiotis Astithas
                if(userClass==null)
639 023f6f1e Panagiotis Astithas
                        result = (Long) manager.createQuery("select count(u) from User u").getSingleResult();
640 023f6f1e Panagiotis Astithas
                else result = (Long) manager.createQuery("select count(u) from User u where u.userClass.id=:id").setParameter("id", userClass.getId()).getSingleResult();
641 023f6f1e Panagiotis Astithas
                if(result==null)
642 023f6f1e Panagiotis Astithas
                        result =0L;
643 023f6f1e Panagiotis Astithas
                return result;
644 023f6f1e Panagiotis Astithas
        }
645 023f6f1e Panagiotis Astithas
646 023f6f1e Panagiotis Astithas
        @Override
647 023f6f1e Panagiotis Astithas
        public Long getCountUsersByLastLogin(Date lastLoginDate) {
648 930d1307 Natasa Kapravelou
                return (Long) manager.createQuery(
649 403c8ca0 Natasa Kapravelou
                                        " select count(distinct ul.user.id) from UserLogin ul " +                                
650 403c8ca0 Natasa Kapravelou
                                                " where ul.loginDate >=:ldate" 
651 930d1307 Natasa Kapravelou
                                        ).setParameter("ldate", lastLoginDate).getSingleResult();
652 023f6f1e Panagiotis Astithas
        }
653 930d1307 Natasa Kapravelou
        
654 023f6f1e Panagiotis Astithas
        @Override
655 023f6f1e Panagiotis Astithas
        public List<User> getUsersByLastLogin(Date lastLoginDate) {
656 403c8ca0 Natasa Kapravelou
                return manager.createQuery(" select distinct ul.user from UserLogin ul " +                                
657 403c8ca0 Natasa Kapravelou
                                                " where ul.loginDate >=:ldate ")
658 403c8ca0 Natasa Kapravelou
                                                .setParameter("ldate", lastLoginDate)
659 403c8ca0 Natasa Kapravelou
                                                .getResultList();
660 023f6f1e Panagiotis Astithas
        }
661 023f6f1e Panagiotis Astithas
662 023f6f1e Panagiotis Astithas
        @Override
663 023f6f1e Panagiotis Astithas
        public List<User> getUsersByLastLogin(Date lastLoginDate, int firstResult, int maxResult) {
664 930d1307 Natasa Kapravelou
                return manager.createQuery("" +
665 930d1307 Natasa Kapravelou
                                " select ul from UserLogin ul " +
666 930d1307 Natasa Kapravelou
                                " where ul.loginDate >= :ldate " +
667 930d1307 Natasa Kapravelou
                                " order by ul.loginDate desc "
668 930d1307 Natasa Kapravelou
                                ).
669 023f6f1e Panagiotis Astithas
                setParameter("ldate", lastLoginDate).setFirstResult(firstResult).setMaxResults(maxResult).getResultList();
670 023f6f1e Panagiotis Astithas
        }
671 023f6f1e Panagiotis Astithas
672 023f6f1e Panagiotis Astithas
        @Override
673 023f6f1e Panagiotis Astithas
        public List<User> getInactiveUsers() {
674 023f6f1e Panagiotis Astithas
                return manager.createQuery("select u from User u where u.active=:active").setParameter("active", false).getResultList();
675 023f6f1e Panagiotis Astithas
        }
676 023f6f1e Panagiotis Astithas
677 023f6f1e Panagiotis Astithas
        @Override
678 023f6f1e Panagiotis Astithas
        public List<FileHeader> searchFileByFilename(String filename) {
679 023f6f1e Panagiotis Astithas
                return manager.createQuery("select f from FileHeader f where f.name=:name").setParameter("name", filename).getResultList();
680 023f6f1e Panagiotis Astithas
        }
681 023f6f1e Panagiotis Astithas
682 023f6f1e Panagiotis Astithas
        @Override
683 023f6f1e Panagiotis Astithas
        public Long getBandwithUsed(UserClass userClass, Date date) {
684 023f6f1e Panagiotis Astithas
                Long result;
685 023f6f1e Panagiotis Astithas
                if (userClass == null)
686 023f6f1e Panagiotis Astithas
                        result = (Long) manager.createQuery("select sum(ai.bandwidthUsed)" +
687 023f6f1e Panagiotis Astithas
                                        " from AccountingInfo ai where ai.dateFrom<=:date and " +
688 023f6f1e Panagiotis Astithas
                                        "ai.dateTo>:date").
689 023f6f1e Panagiotis Astithas
                                        setParameter("date", date).
690 023f6f1e Panagiotis Astithas
                                        getSingleResult();
691 023f6f1e Panagiotis Astithas
                else
692 023f6f1e Panagiotis Astithas
                        result = (Long) manager.createQuery("select sum(ai.bandwidthUsed)" +
693 023f6f1e Panagiotis Astithas
                                        " from AccountingInfo ai where ai.user.userClass.id=:id " +
694 023f6f1e Panagiotis Astithas
                                        "and ai.dateFrom<=:date and ai.dateTo>:date").
695 023f6f1e Panagiotis Astithas
                                        setParameter("date", date).
696 023f6f1e Panagiotis Astithas
                                        setParameter("id", userClass.getId()).
697 023f6f1e Panagiotis Astithas
                                        getSingleResult();
698 023f6f1e Panagiotis Astithas
                if (result == null)
699 023f6f1e Panagiotis Astithas
                        result = 0L;
700 023f6f1e Panagiotis Astithas
                return result;
701 023f6f1e Panagiotis Astithas
        }
702 023f6f1e Panagiotis Astithas
703 023f6f1e Panagiotis Astithas
        @Override
704 023f6f1e Panagiotis Astithas
        public List<AccountingInfo> getAccountingInfo(User user) {
705 023f6f1e Panagiotis Astithas
                List<AccountingInfo> ai = new ArrayList<AccountingInfo>();
706 023f6f1e Panagiotis Astithas
                try {
707 023f6f1e Panagiotis Astithas
                        ai =  manager.createQuery("select ai from AccountingInfo ai " +
708 023f6f1e Panagiotis Astithas
                                "where ai.user=:user")
709 023f6f1e Panagiotis Astithas
                                .setParameter("user", user)
710 023f6f1e Panagiotis Astithas
                                .getResultList();
711 023f6f1e Panagiotis Astithas
                }
712 023f6f1e Panagiotis Astithas
                catch (NoResultException e) {}
713 023f6f1e Panagiotis Astithas
                return ai;
714 023f6f1e Panagiotis Astithas
        }
715 023f6f1e Panagiotis Astithas
716 023f6f1e Panagiotis Astithas
        @Override
717 023f6f1e Panagiotis Astithas
        public AccountingInfo getAccountingInfo(User user, Date date) {
718 023f6f1e Panagiotis Astithas
                AccountingInfo ai = null;
719 023f6f1e Panagiotis Astithas
                try {
720 023f6f1e Panagiotis Astithas
                        ai = (AccountingInfo) manager.createQuery("select ai from AccountingInfo ai " +
721 023f6f1e Panagiotis Astithas
                                "where ai.user=:user and ai.dateFrom<=:date and ai.dateTo>:date")
722 023f6f1e Panagiotis Astithas
                                .setParameter("user", user)
723 023f6f1e Panagiotis Astithas
                                .setParameter("date", date)
724 023f6f1e Panagiotis Astithas
                                .getSingleResult();
725 023f6f1e Panagiotis Astithas
                }
726 023f6f1e Panagiotis Astithas
                catch (NoResultException e) {
727 023f6f1e Panagiotis Astithas
                        // If not found, that means that there is no accounting info noted
728 023f6f1e Panagiotis Astithas
                        // for given time. So return a 0 as an answer.
729 023f6f1e Panagiotis Astithas
                        ai = new AccountingInfo(user, date, date);
730 023f6f1e Panagiotis Astithas
                }
731 023f6f1e Panagiotis Astithas
                return ai;
732 023f6f1e Panagiotis Astithas
        }
733 b7565ade Natasa Kapravelou
734 b7565ade Natasa Kapravelou
        @Override
735 b7565ade Natasa Kapravelou
        public List<FileHeader> getFilesPermittedForGroup(Long userId, Long groupId) {
736 b7565ade Natasa Kapravelou
                return manager.createQuery("select distinct f from FileHeader f LEFT JOIN f.permissions p " +
737 b7565ade Natasa Kapravelou
                                        "where f.owner.id=:userId and f.deleted = false and p.group.id=:groupId ").
738 b7565ade Natasa Kapravelou
                                        setParameter("userId", userId).setParameter("groupId", groupId).getResultList();
739 b7565ade Natasa Kapravelou
        }
740 50cd9e9c koutsoub
        
741 50cd9e9c koutsoub
        @Override
742 50cd9e9c koutsoub
        public List<Folder> getSharingFoldersForUser(Long userId) {
743 50cd9e9c koutsoub
                return manager.createQuery("select distinct f from Folder f " +
744 50cd9e9c koutsoub
                                        "LEFT JOIN f.permissions p where  " +
745 50cd9e9c koutsoub
                                        " (p.user.id=:userId or p.group.id in (select distinct gg.id " +
746 50cd9e9c koutsoub
                                        "from Group gg join gg.members memb where memb.id=:userId))) ").
747 50cd9e9c koutsoub
                                        setParameter("userId", userId).getResultList();
748 50cd9e9c koutsoub
        }
749 50cd9e9c koutsoub
        
750 50cd9e9c koutsoub
        @Override
751 50cd9e9c koutsoub
        public List<FileHeader> getSharingFilesForUser(Long userId) {
752 50cd9e9c koutsoub
                List<FileHeader> users = manager.createQuery("select distinct f from FileHeader f " +
753 50cd9e9c koutsoub
                                        "LEFT JOIN f.permissions p where " +
754 50cd9e9c koutsoub
                                        " (p.user.id=:userId or p.group.id in (select distinct gg.id from Group gg " +
755 50cd9e9c koutsoub
                                        "join gg.members memb where memb.id=:userId)))").
756 50cd9e9c koutsoub
                                        setParameter("userId", userId).getResultList();
757 50cd9e9c koutsoub
                return users;
758 9268f2cb Christos V. Stathis
    }
759 376d0ebf Christos V. Stathis
760 376d0ebf Christos V. Stathis
    @Override
761 376d0ebf Christos V. Stathis
    public FileHeader getFileForIndexing(Long id) throws ObjectNotFoundException {
762 376d0ebf Christos V. Stathis
        FileHeader h = getEntityById(FileHeader.class, id);
763 376d0ebf Christos V. Stathis
        h.getFileTags().size();
764 a17a48ae Christos V. Stathis
        h.getPermissions().size();
765 376d0ebf Christos V. Stathis
        return h;
766 376d0ebf Christos V. Stathis
    }
767 50cd9e9c koutsoub
        
768 50cd9e9c koutsoub
        @Override 
769 50cd9e9c koutsoub
        public List<Group> getGroupsContainingUser(Long userId){
770 50cd9e9c koutsoub
                List<Group> groups = manager.createQuery("select distinct gg " +
771 50cd9e9c koutsoub
                                        "from Group gg join gg.members memb where memb.id=:userId)").setParameter("userId", userId).getResultList();
772 50cd9e9c koutsoub
                return groups;
773 50cd9e9c koutsoub
        }
774 50cd9e9c koutsoub
        
775 50cd9e9c koutsoub
        @Override
776 50cd9e9c koutsoub
        public List<FileUploadStatus> getUploadStatus(Long userId){
777 50cd9e9c koutsoub
                List<FileUploadStatus> res = manager.createQuery("select f from FileUploadStatus f where f.owner.id=:userId").setParameter("userId", userId).getResultList();
778 50cd9e9c koutsoub
                return res;
779 50cd9e9c koutsoub
        }
780 50cd9e9c koutsoub
        @Override
781 50cd9e9c koutsoub
        public int deletePermissionsNotCorrespondingToFilesAndFolders(Long userId){
782 50cd9e9c koutsoub
                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 50cd9e9c koutsoub
        }
784 366f3f31 Natasa Kapravelou
        
785 366f3f31 Natasa Kapravelou
        public List<UserLogin> getLoginsForUser (Long userId){
786 366f3f31 Natasa Kapravelou
                List<UserLogin> res = manager
787 a3d193a5 Natasa Kapravelou
                                                                .createQuery("select ul from UserLogin ul where ul.user.id=:userId " +
788 a3d193a5 Natasa Kapravelou
                                                                                        " order by ul.loginDate desc")                                                                                        
789 366f3f31 Natasa Kapravelou
                                                                .setParameter("userId", userId)
790 a3d193a5 Natasa Kapravelou
                                                                .setMaxResults(2)                                                                
791 366f3f31 Natasa Kapravelou
                                                                .getResultList();
792 a3d193a5 Natasa Kapravelou
                return res;                                                                        
793 366f3f31 Natasa Kapravelou
        }
794 f5684a19 Christos V. Stathis
795 403c8ca0 Natasa Kapravelou
        public List<UserLogin> getAllLoginsForUser (Long userId){
796 403c8ca0 Natasa Kapravelou
                List<UserLogin> res = manager
797 403c8ca0 Natasa Kapravelou
                                                                .createQuery("select ul from UserLogin ul where ul.user.id=:userId ")                                
798 403c8ca0 Natasa Kapravelou
                                                                .setParameter("userId", userId)                                                                                                        
799 403c8ca0 Natasa Kapravelou
                                                                .getResultList();
800 403c8ca0 Natasa Kapravelou
                return res;                                                                        
801 403c8ca0 Natasa Kapravelou
        }
802 921693ed koutsoub
        
803 921693ed koutsoub
        @Override
804 921693ed koutsoub
        public User getUserByUserName(String username) {
805 921693ed koutsoub
                return (User) manager.createQuery("select u from User u where u.username=:username").
806 921693ed koutsoub
                setParameter("username", username).getSingleResult();
807 921693ed koutsoub
                
808 921693ed koutsoub
        }
809 d2127957 koutsoub
        
810 d2127957 koutsoub
        
811 d2127957 koutsoub
        /** WEBDAV LOCK API **/
812 d2127957 koutsoub
        @Override
813 065ce8c1 koutsoub
        public FileLock getLockById(String id) {
814 065ce8c1 koutsoub
                return manager.find(FileLock.class, id);
815 d2127957 koutsoub
        }
816 d2127957 koutsoub
817 d2127957 koutsoub
        @Override
818 065ce8c1 koutsoub
        public FileLock getLockByToken(String tokenId) {
819 065ce8c1 koutsoub
                return (FileLock) manager.createQuery("select c from GssLock c where c.tokenId=:tokenId").setParameter("tokenId", tokenId).getSingleResult();
820 d2127957 koutsoub
        }
821 d2127957 koutsoub
822 d2127957 koutsoub
        @Override
823 065ce8c1 koutsoub
        public void removeLock(FileLock lock) {
824 d2127957 koutsoub
                lock =getLockById(lock.getId());
825 d2127957 koutsoub
                if(lock!=null)
826 d2127957 koutsoub
                        manager.remove(lock);                
827 d2127957 koutsoub
        }
828 d2127957 koutsoub
829 d2127957 koutsoub
        @Override
830 065ce8c1 koutsoub
        public FileLock saveOrUpdateLock(FileLock lock) {
831 d2127957 koutsoub
                if(getLockById(lock.getId())!=null)
832 d2127957 koutsoub
                        manager.merge(lock);
833 d2127957 koutsoub
                else
834 d2127957 koutsoub
                        manager.persist(lock);
835 d2127957 koutsoub
                manager.flush();
836 d2127957 koutsoub
                return lock;
837 d2127957 koutsoub
        }
838 065ce8c1 koutsoub
        
839 065ce8c1 koutsoub
        @Override
840 065ce8c1 koutsoub
        public WebDavNonce getWebDavNonce(String tokenId) {
841 065ce8c1 koutsoub
                return manager.find(WebDavNonce.class, tokenId);
842 065ce8c1 koutsoub
        }
843 065ce8c1 koutsoub
844 065ce8c1 koutsoub
        @Override
845 065ce8c1 koutsoub
        public void removeWebDavNonce(WebDavNonce nonce) {
846 065ce8c1 koutsoub
                nonce =getWebDavNonce(nonce.getId());
847 065ce8c1 koutsoub
                if(nonce!=null)
848 065ce8c1 koutsoub
                        manager.remove(nonce);                
849 065ce8c1 koutsoub
        }
850 065ce8c1 koutsoub
851 065ce8c1 koutsoub
        @Override
852 065ce8c1 koutsoub
        public WebDavNonce saveOrUpdateWebDavNonce(WebDavNonce nonce) {
853 065ce8c1 koutsoub
                if(getWebDavNonce(nonce.getId())!=null)
854 065ce8c1 koutsoub
                        manager.merge(nonce);
855 065ce8c1 koutsoub
                else
856 065ce8c1 koutsoub
                        manager.persist(nonce);
857 065ce8c1 koutsoub
                manager.flush();
858 065ce8c1 koutsoub
                return nonce;
859 065ce8c1 koutsoub
        }
860 14ad7326 pastith
}