root / src / gr / ebs / gss / server / ejb / GSSDAOBean.java @ 065ce8c1
History | View | Annotate | Download (30.2 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2007, 2008, 2009, 2010 Electronic Business Systems Ltd.
|
3 |
*
|
4 |
* This file is part of GSS.
|
5 |
*
|
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.
|
10 |
*
|
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.
|
15 |
*
|
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/>.
|
18 |
*/
|
19 |
package gr.ebs.gss.server.ejb; |
20 |
|
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; |
36 |
|
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; |
44 |
import java.util.Set; |
45 |
|
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; |
51 |
|
52 |
import org.apache.commons.lang.StringUtils; |
53 |
|
54 |
/**
|
55 |
* The implementation of the GSSDAO interface.
|
56 |
*/
|
57 |
@Stateless
|
58 |
public class GSSDAOBean implements GSSDAO { |
59 |
|
60 |
private static final int BANDWIDTH_TIME_PERIOD_FIELD = Calendar.MONTH; |
61 |
private static final int BANDWIDTH_TIME_PERIOD_AMOUNT = 1; |
62 |
|
63 |
/**
|
64 |
* The entity manager for the persistence unit
|
65 |
*/
|
66 |
@PersistenceContext(unitName = "gss") |
67 |
private EntityManager manager;
|
68 |
|
69 |
@Override
|
70 |
public Long getRootFolderId(final Long userId) throws ObjectNotFoundException { |
71 |
try {
|
72 |
if (userId == null) |
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)
|
77 |
.getSingleResult(); |
78 |
} catch (final NoResultException e) { |
79 |
throw new ObjectNotFoundException("Root folder not found for user with id=" + userId); |
80 |
} |
81 |
} |
82 |
|
83 |
@Override
|
84 |
public Folder getRootFolder(final Long userId) throws ObjectNotFoundException { |
85 |
try {
|
86 |
if (userId == null) |
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)
|
90 |
.getSingleResult(); |
91 |
} catch (final NoResultException e) { |
92 |
throw new ObjectNotFoundException("Root folder not found for user with id=" + userId); |
93 |
} |
94 |
} |
95 |
|
96 |
@Override
|
97 |
public User getUser(final String username) throws ObjectNotFoundException { |
98 |
try {
|
99 |
if (username == null) |
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)
|
103 |
.getSingleResult(); |
104 |
} catch (final NoResultException e) { |
105 |
throw new ObjectNotFoundException("No User found for username=" + username); |
106 |
} |
107 |
} |
108 |
|
109 |
@Override
|
110 |
public void create(final Object obj) { |
111 |
if (obj == null) |
112 |
throw new IllegalArgumentException("No object speficied"); |
113 |
manager.persist(obj); |
114 |
} |
115 |
|
116 |
@Override
|
117 |
public void refresh(final Object obj) { |
118 |
if (obj == null) |
119 |
throw new IllegalArgumentException("No object speficied"); |
120 |
manager.refresh(obj); |
121 |
} |
122 |
|
123 |
@Override
|
124 |
public void update(final Object obj) { |
125 |
if (obj == null) |
126 |
throw new IllegalArgumentException("No object speficied"); |
127 |
manager.merge(obj); |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public void delete(final Object entity) { |
132 |
if (entity == null) |
133 |
throw new IllegalArgumentException("No object speficied"); |
134 |
manager.remove(entity); |
135 |
} |
136 |
|
137 |
@Override
|
138 |
public <T> T getEntityById(final Class<T> _class, final Object _id) throws ObjectNotFoundException { |
139 |
if (_id == null) |
140 |
throw new ObjectNotFoundException("No " + _class.getSimpleName() + " specified"); |
141 |
|
142 |
final T entity = manager.find(_class, _id);
|
143 |
if (entity == null) |
144 |
throw new ObjectNotFoundException(_class.getSimpleName() + " with id=" + _id + " was not found"); |
145 |
|
146 |
return entity;
|
147 |
} |
148 |
|
149 |
@Override
|
150 |
@SuppressWarnings("unchecked") |
151 |
public List<Group> getGroups(final Long userId) throws ObjectNotFoundException { |
152 |
if (userId == null) |
153 |
throw new ObjectNotFoundException("No user specified"); |
154 |
|
155 |
return manager.createQuery("select g from Group g where g.owner.id=:userId").setParameter("userId", userId).getResultList(); |
156 |
} |
157 |
|
158 |
@Override
|
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"); |
163 |
if (userId == null) |
164 |
throw new ObjectNotFoundException("No user specified"); |
165 |
User user = getEntityById(User.class, userId); |
166 |
String query;
|
167 |
if(ignoreDeleted)
|
168 |
query = "select f from FileHeader f where f.folder.id=:folderId and f.deleted=false";
|
169 |
else
|
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);
|
175 |
|
176 |
return retv;
|
177 |
} |
178 |
|
179 |
@Override
|
180 |
@SuppressWarnings("unchecked") |
181 |
public List<User> getUsers(final Long groupId) throws ObjectNotFoundException { |
182 |
if (groupId == null) |
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();
|
186 |
} |
187 |
|
188 |
@Override
|
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"); |
194 |
|
195 |
try {
|
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)
|
200 |
.getSingleResult(); |
201 |
return true; |
202 |
} catch (NoResultException e) {
|
203 |
try {
|
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)
|
208 |
.getSingleResult(); |
209 |
return true; |
210 |
} catch (NoResultException e1) {
|
211 |
return false; |
212 |
} |
213 |
} |
214 |
} |
215 |
|
216 |
@Override
|
217 |
public boolean existsGroup(final Long userId, final String name) throws ObjectNotFoundException { |
218 |
if (userId == null) |
219 |
throw new ObjectNotFoundException("No user specified"); |
220 |
if (StringUtils.isEmpty(name))
|
221 |
throw new ObjectNotFoundException("No group name specified"); |
222 |
try {
|
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)
|
226 |
.getSingleResult(); |
227 |
return true; |
228 |
} catch (final NoResultException e) { |
229 |
return false; |
230 |
} |
231 |
} |
232 |
|
233 |
@Override
|
234 |
public Set<String> getUserTags(final Long userId) throws ObjectNotFoundException { |
235 |
if (userId == null) |
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)
|
239 |
.getResultList()); |
240 |
} |
241 |
|
242 |
@Override
|
243 |
public void flush() { |
244 |
manager.flush(); |
245 |
} |
246 |
|
247 |
@Override
|
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"); |
253 |
|
254 |
try {
|
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)
|
258 |
.getSingleResult(); |
259 |
} catch (final NoResultException e1) { |
260 |
throw new ObjectNotFoundException("File not found"); |
261 |
} |
262 |
} |
263 |
|
264 |
@Override
|
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"); |
270 |
|
271 |
try {
|
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)
|
275 |
.getSingleResult(); |
276 |
} catch (NoResultException e) {
|
277 |
throw new ObjectNotFoundException("Folder not found"); |
278 |
} |
279 |
} |
280 |
|
281 |
@Override
|
282 |
public List<FileHeader> getDeletedFiles(Long userId) throws ObjectNotFoundException { |
283 |
if (userId == null) |
284 |
throw new ObjectNotFoundException("No User specified"); |
285 |
User user = getEntityById(User.class, userId); |
286 |
|
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);
|
293 |
|
294 |
return retv;
|
295 |
} |
296 |
|
297 |
@Override
|
298 |
public List<Folder> getDeletedRootFolders(Long userId) throws ObjectNotFoundException { |
299 |
if (userId == null) |
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();
|
304 |
} |
305 |
|
306 |
@Override
|
307 |
public User findUser(String username) { |
308 |
if (username == null) |
309 |
return 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); |
314 |
} |
315 |
|
316 |
@Override
|
317 |
public User findUserByEmail(String email) { |
318 |
if (email == null) |
319 |
return null; |
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); |
324 |
} |
325 |
|
326 |
@Override
|
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(); |
330 |
} |
331 |
|
332 |
@Override
|
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()))
|
341 |
result.add(f); |
342 |
return result;
|
343 |
} |
344 |
|
345 |
@Override
|
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(); |
350 |
} |
351 |
|
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)
|
356 |
.getResultList(); |
357 |
List<Long> groupIds = new ArrayList<Long>(); |
358 |
for (BigInteger id : groups) |
359 |
groupIds.add(id.longValue()); |
360 |
return groupIds;
|
361 |
} |
362 |
|
363 |
@Override
|
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();
|
373 |
} |
374 |
|
375 |
@Override
|
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();
|
385 |
} |
386 |
|
387 |
@Override
|
388 |
public List<FileHeader> getSharedFilesNotInSharedFolders(Long userId) throws ObjectNotFoundException { |
389 |
if (userId == null) |
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);
|
402 |
|
403 |
return retv;
|
404 |
} |
405 |
|
406 |
@Override
|
407 |
public List<FileHeader> getSharedFiles(Long userId) throws ObjectNotFoundException { |
408 |
if (userId == null) |
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);
|
418 |
|
419 |
return retv;
|
420 |
} |
421 |
|
422 |
@Override
|
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();
|
428 |
return folders;
|
429 |
} |
430 |
|
431 |
@Override
|
432 |
public List<FileHeader> getSharedFiles(Long userId, Long callingUserId) throws ObjectNotFoundException { |
433 |
if (userId == null) |
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);
|
449 |
|
450 |
return retv;
|
451 |
} |
452 |
|
453 |
@Override
|
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).
|
462 |
getResultList(); |
463 |
List<Folder> result = new ArrayList<Folder>(); |
464 |
for(Folder f : folders)
|
465 |
if(!folders.contains(f.getParent()))
|
466 |
result.add(f); |
467 |
return result;
|
468 |
|
469 |
} |
470 |
|
471 |
@Override
|
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(); |
475 |
} |
476 |
|
477 |
@Override
|
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); |
484 |
} |
485 |
|
486 |
@Override
|
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)
|
490 |
.getSingleResult(); |
491 |
return singleResult;
|
492 |
} |
493 |
|
494 |
@Override
|
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)
|
498 |
.getSingleResult(); |
499 |
if(singleResult == null) |
500 |
singleResult = new Long(0L); |
501 |
return singleResult;
|
502 |
|
503 |
} |
504 |
|
505 |
@Override
|
506 |
public List<Long> getAllFileIds() { |
507 |
List<Long> ids = manager.createQuery("select f.id from FileHeader f").getResultList(); |
508 |
return ids;
|
509 |
} |
510 |
|
511 |
@Override
|
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(); |
514 |
if(res.size()>0) |
515 |
return res.get(0); |
516 |
return null; |
517 |
} |
518 |
|
519 |
@Override
|
520 |
public FileBody getFileVersion(Long fileId, int version) throws ObjectNotFoundException { |
521 |
try {
|
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) |
525 |
.getSingleResult(); |
526 |
} catch (NoResultException e) {
|
527 |
throw new ObjectNotFoundException("No version " + version + " found for file #" + fileId); |
528 |
} |
529 |
} |
530 |
|
531 |
@Override
|
532 |
public void updateAccounting(User user, Date date, long bandwidthDiff) { |
533 |
AccountingInfo ai = null;
|
534 |
try {
|
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)
|
539 |
.getSingleResult(); |
540 |
} |
541 |
catch (NoResultException e) {
|
542 |
ai = null;
|
543 |
} |
544 |
|
545 |
if (ai==null) { |
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()); |
551 |
int offset = 0; |
552 |
Calendar dateFrom;
|
553 |
Calendar dateTo;
|
554 |
long timeInMillis = date.getTime();
|
555 |
do {
|
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; |
561 |
} |
562 |
while (!(dateFrom.getTimeInMillis()<=timeInMillis && dateTo.getTimeInMillis()>timeInMillis));
|
563 |
|
564 |
ai = new AccountingInfo(user, dateFrom.getTime(), dateTo.getTime());
|
565 |
manager.persist(ai); |
566 |
} |
567 |
|
568 |
// Do the update.
|
569 |
ai.updateBandwidth(bandwidthDiff); |
570 |
} |
571 |
|
572 |
@Override
|
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(); |
576 |
return ids;
|
577 |
} |
578 |
|
579 |
@Override
|
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(); |
584 |
} |
585 |
|
586 |
@Override
|
587 |
public Invitation findInvite(String code) { |
588 |
if (code == null) |
589 |
return null; |
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); |
594 |
} |
595 |
|
596 |
@Override
|
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); |
606 |
create(couponClass); |
607 |
flush(); |
608 |
results.add(couponClass); |
609 |
} |
610 |
return results.get(0); |
611 |
} |
612 |
|
613 |
@Override
|
614 |
public Long getFileCount(UserClass userClass) { |
615 |
Long result;
|
616 |
if(userClass==null) |
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(); |
619 |
if(result==null) |
620 |
result =0L;
|
621 |
return result;
|
622 |
} |
623 |
|
624 |
@Override
|
625 |
public Long getFileSize(UserClass userClass) { |
626 |
Long result;
|
627 |
if(userClass==null) |
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(); |
630 |
if(result==null) |
631 |
result =0L;
|
632 |
return result;
|
633 |
} |
634 |
|
635 |
@Override
|
636 |
public Long getUserCount(UserClass userClass) { |
637 |
Long result;
|
638 |
if(userClass==null) |
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(); |
641 |
if(result==null) |
642 |
result =0L;
|
643 |
return result;
|
644 |
} |
645 |
|
646 |
@Override
|
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();
|
652 |
} |
653 |
|
654 |
@Override
|
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)
|
659 |
.getResultList(); |
660 |
} |
661 |
|
662 |
@Override
|
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 "
|
668 |
). |
669 |
setParameter("ldate", lastLoginDate).setFirstResult(firstResult).setMaxResults(maxResult).getResultList();
|
670 |
} |
671 |
|
672 |
@Override
|
673 |
public List<User> getInactiveUsers() { |
674 |
return manager.createQuery("select u from User u where u.active=:active").setParameter("active", false).getResultList(); |
675 |
} |
676 |
|
677 |
@Override
|
678 |
public List<FileHeader> searchFileByFilename(String filename) { |
679 |
return manager.createQuery("select f from FileHeader f where f.name=:name").setParameter("name", filename).getResultList(); |
680 |
} |
681 |
|
682 |
@Override
|
683 |
public Long getBandwithUsed(UserClass userClass, Date date) { |
684 |
Long result;
|
685 |
if (userClass == null) |
686 |
result = (Long) manager.createQuery("select sum(ai.bandwidthUsed)" + |
687 |
" from AccountingInfo ai where ai.dateFrom<=:date and " +
|
688 |
"ai.dateTo>:date").
|
689 |
setParameter("date", date).
|
690 |
getSingleResult(); |
691 |
else
|
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()).
|
697 |
getSingleResult(); |
698 |
if (result == null) |
699 |
result = 0L;
|
700 |
return result;
|
701 |
} |
702 |
|
703 |
@Override
|
704 |
public List<AccountingInfo> getAccountingInfo(User user) { |
705 |
List<AccountingInfo> ai = new ArrayList<AccountingInfo>(); |
706 |
try {
|
707 |
ai = manager.createQuery("select ai from AccountingInfo ai " +
|
708 |
"where ai.user=:user")
|
709 |
.setParameter("user", user)
|
710 |
.getResultList(); |
711 |
} |
712 |
catch (NoResultException e) {}
|
713 |
return ai;
|
714 |
} |
715 |
|
716 |
@Override
|
717 |
public AccountingInfo getAccountingInfo(User user, Date date) { |
718 |
AccountingInfo ai = null;
|
719 |
try {
|
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)
|
724 |
.getSingleResult(); |
725 |
} |
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);
|
730 |
} |
731 |
return ai;
|
732 |
} |
733 |
|
734 |
@Override
|
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(); |
739 |
} |
740 |
|
741 |
@Override
|
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();
|
748 |
} |
749 |
|
750 |
@Override
|
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();
|
757 |
return users;
|
758 |
} |
759 |
|
760 |
@Override
|
761 |
public FileHeader getFileForIndexing(Long id) throws ObjectNotFoundException { |
762 |
FileHeader h = getEntityById(FileHeader.class, id); |
763 |
h.getFileTags().size(); |
764 |
h.getPermissions().size(); |
765 |
return h;
|
766 |
} |
767 |
|
768 |
@Override
|
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(); |
772 |
return groups;
|
773 |
} |
774 |
|
775 |
@Override
|
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(); |
778 |
return res;
|
779 |
} |
780 |
@Override
|
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(); |
783 |
} |
784 |
|
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)
|
790 |
.setMaxResults(2)
|
791 |
.getResultList(); |
792 |
return res;
|
793 |
} |
794 |
|
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)
|
799 |
.getResultList(); |
800 |
return res;
|
801 |
} |
802 |
|
803 |
@Override
|
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();
|
807 |
|
808 |
} |
809 |
|
810 |
|
811 |
/** WEBDAV LOCK API **/
|
812 |
@Override
|
813 |
public FileLock getLockById(String id) { |
814 |
return manager.find(FileLock.class, id); |
815 |
} |
816 |
|
817 |
@Override
|
818 |
public FileLock getLockByToken(String tokenId) { |
819 |
return (FileLock) manager.createQuery("select c from GssLock c where c.tokenId=:tokenId").setParameter("tokenId", tokenId).getSingleResult(); |
820 |
} |
821 |
|
822 |
@Override
|
823 |
public void removeLock(FileLock lock) { |
824 |
lock =getLockById(lock.getId()); |
825 |
if(lock!=null) |
826 |
manager.remove(lock); |
827 |
} |
828 |
|
829 |
@Override
|
830 |
public FileLock saveOrUpdateLock(FileLock lock) { |
831 |
if(getLockById(lock.getId())!=null) |
832 |
manager.merge(lock); |
833 |
else
|
834 |
manager.persist(lock); |
835 |
manager.flush(); |
836 |
return lock;
|
837 |
} |
838 |
|
839 |
@Override
|
840 |
public WebDavNonce getWebDavNonce(String tokenId) { |
841 |
return manager.find(WebDavNonce.class, tokenId);
|
842 |
} |
843 |
|
844 |
@Override
|
845 |
public void removeWebDavNonce(WebDavNonce nonce) { |
846 |
nonce =getWebDavNonce(nonce.getId()); |
847 |
if(nonce!=null) |
848 |
manager.remove(nonce); |
849 |
} |
850 |
|
851 |
@Override
|
852 |
public WebDavNonce saveOrUpdateWebDavNonce(WebDavNonce nonce) {
|
853 |
if(getWebDavNonce(nonce.getId())!=null) |
854 |
manager.merge(nonce); |
855 |
else
|
856 |
manager.persist(nonce); |
857 |
manager.flush(); |
858 |
return nonce;
|
859 |
} |
860 |
} |