root / src / gr / ebs / gss / server / ejb / GSSDAOBean.java @ 16b54aa8
History | View | Annotate | Download (27.3 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.Invitation; |
30 |
import gr.ebs.gss.server.domain.Nonce; |
31 |
import gr.ebs.gss.server.domain.User; |
32 |
import gr.ebs.gss.server.domain.UserClass; |
33 |
|
34 |
import java.util.ArrayList; |
35 |
import java.util.Calendar; |
36 |
import java.util.Date; |
37 |
import java.util.GregorianCalendar; |
38 |
import java.util.HashSet; |
39 |
import java.util.List; |
40 |
import java.util.Set; |
41 |
|
42 |
import javax.ejb.Stateless; |
43 |
import javax.persistence.EntityManager; |
44 |
import javax.persistence.NoResultException; |
45 |
import javax.persistence.PersistenceContext; |
46 |
|
47 |
import org.apache.commons.lang.StringUtils; |
48 |
|
49 |
/**
|
50 |
* The implementation of the GSSDAO interface.
|
51 |
*/
|
52 |
@Stateless
|
53 |
public class GSSDAOBean implements GSSDAO { |
54 |
|
55 |
private static final int BANDWIDTH_TIME_PERIOD_FIELD = Calendar.MONTH; |
56 |
private static final int BANDWIDTH_TIME_PERIOD_AMOUNT = 1; |
57 |
|
58 |
/**
|
59 |
* The entity manager for the persistence unit
|
60 |
*/
|
61 |
@PersistenceContext(unitName = "gss") |
62 |
private EntityManager manager;
|
63 |
|
64 |
@Override
|
65 |
public Folder getRootFolder(final Long userId) throws ObjectNotFoundException { |
66 |
try {
|
67 |
if (userId == null) |
68 |
throw new ObjectNotFoundException("No user specified"); |
69 |
return (Folder) manager .createQuery("select f from Folder f where f.owner.id=:ownerId and f.parent is null") |
70 |
.setParameter("ownerId", userId)
|
71 |
.getSingleResult(); |
72 |
} catch (final NoResultException e) { |
73 |
throw new ObjectNotFoundException("Root folder not found for user with id=" + userId); |
74 |
} |
75 |
} |
76 |
|
77 |
@Override
|
78 |
public User getUser(final String username) throws ObjectNotFoundException { |
79 |
try {
|
80 |
if (username == null) |
81 |
throw new ObjectNotFoundException("No user specified"); |
82 |
return (User) manager .createQuery("select f from User f where f.username=:username") |
83 |
.setParameter("username", username)
|
84 |
.getSingleResult(); |
85 |
} catch (final NoResultException e) { |
86 |
throw new ObjectNotFoundException("No User found for username=" + username); |
87 |
} |
88 |
} |
89 |
|
90 |
@Override
|
91 |
public void create(final Object obj) { |
92 |
if (obj == null) |
93 |
throw new IllegalArgumentException("No object speficied"); |
94 |
manager.persist(obj); |
95 |
} |
96 |
|
97 |
@Override
|
98 |
public void refresh(final Object obj) { |
99 |
if (obj == null) |
100 |
throw new IllegalArgumentException("No object speficied"); |
101 |
manager.refresh(obj); |
102 |
} |
103 |
|
104 |
@Override
|
105 |
public void update(final Object obj) { |
106 |
if (obj == null) |
107 |
throw new IllegalArgumentException("No object speficied"); |
108 |
manager.merge(obj); |
109 |
} |
110 |
|
111 |
@Override
|
112 |
public void delete(final Object entity) { |
113 |
if (entity == null) |
114 |
throw new IllegalArgumentException("No object speficied"); |
115 |
manager.remove(entity); |
116 |
} |
117 |
|
118 |
@Override
|
119 |
public <T> T getEntityById(final Class<T> _class, final Object _id) throws ObjectNotFoundException { |
120 |
if (_id == null) |
121 |
throw new ObjectNotFoundException("No " + _class.getSimpleName() + " specified"); |
122 |
|
123 |
final T entity = manager.find(_class, _id);
|
124 |
if (entity == null) |
125 |
throw new ObjectNotFoundException(_class.getSimpleName() + " with id=" + _id + " was not found"); |
126 |
|
127 |
return entity;
|
128 |
} |
129 |
|
130 |
@Override
|
131 |
@SuppressWarnings("unchecked") |
132 |
public List<Group> getGroups(final Long userId) throws ObjectNotFoundException { |
133 |
if (userId == null) |
134 |
throw new ObjectNotFoundException("No user specified"); |
135 |
|
136 |
return manager.createQuery("select g from Group g where g.owner.id=:userId").setParameter("userId", userId).getResultList(); |
137 |
} |
138 |
|
139 |
@Override
|
140 |
@SuppressWarnings("unchecked") |
141 |
public List<FileHeader> getFiles(final Long folderId, Long userId, boolean ignoreDeleted) throws ObjectNotFoundException { |
142 |
if (folderId == null) |
143 |
throw new ObjectNotFoundException("No folder specified"); |
144 |
if (userId == null) |
145 |
throw new ObjectNotFoundException("No user specified"); |
146 |
User user = getEntityById(User.class, userId); |
147 |
String query;
|
148 |
if(ignoreDeleted)
|
149 |
query = "select f from FileHeader f where f.folder.id=:folderId and f.deleted=false";
|
150 |
else
|
151 |
query = "select f from FileHeader f where f.folder.id=:folderId";
|
152 |
List<FileHeader> tempList = manager.createQuery(query).setParameter("folderId", folderId).getResultList(); |
153 |
List<FileHeader> retv = new ArrayList<FileHeader>(); |
154 |
for (FileHeader f: tempList)
|
155 |
if (f.hasReadPermission(user)) retv.add(f);
|
156 |
|
157 |
return retv;
|
158 |
} |
159 |
|
160 |
@Override
|
161 |
@SuppressWarnings("unchecked") |
162 |
public List<User> getUsers(final Long groupId) throws ObjectNotFoundException { |
163 |
if (groupId == null) |
164 |
throw new ObjectNotFoundException("No group specified"); |
165 |
return manager.createQuery("select u from User u join u.groupsMember g where g.id=:groupId"). |
166 |
setParameter("groupId", groupId).getResultList();
|
167 |
} |
168 |
|
169 |
@Override
|
170 |
public boolean existsFolderOrFile(Long parentId, String name) throws ObjectNotFoundException { |
171 |
if (parentId == null) |
172 |
throw new ObjectNotFoundException("No parent folder specified"); |
173 |
if (StringUtils.isEmpty(name))
|
174 |
throw new IllegalArgumentException("No folder name specified"); |
175 |
|
176 |
try {
|
177 |
manager .createQuery("select f from Folder f " +
|
178 |
"where f.parent.id=:parentId and f.name=:name")
|
179 |
.setParameter("parentId", parentId)
|
180 |
.setParameter("name", name)
|
181 |
.getSingleResult(); |
182 |
return true; |
183 |
} catch (NoResultException e) {
|
184 |
try {
|
185 |
manager .createQuery("select f from FileHeader f " +
|
186 |
"where f.folder.id=:parentId and f.name=:name")
|
187 |
.setParameter("parentId", parentId)
|
188 |
.setParameter("name", name)
|
189 |
.getSingleResult(); |
190 |
return true; |
191 |
} catch (NoResultException e1) {
|
192 |
return false; |
193 |
} |
194 |
} |
195 |
} |
196 |
|
197 |
@Override
|
198 |
public boolean existsGroup(final Long userId, final String name) throws ObjectNotFoundException { |
199 |
if (userId == null) |
200 |
throw new ObjectNotFoundException("No user specified"); |
201 |
if (StringUtils.isEmpty(name))
|
202 |
throw new ObjectNotFoundException("No group name specified"); |
203 |
try {
|
204 |
manager .createQuery("select g from Group g where g.owner.id=:userId and g.name=:name")
|
205 |
.setParameter("userId", userId)
|
206 |
.setParameter("name", name)
|
207 |
.getSingleResult(); |
208 |
return true; |
209 |
} catch (final NoResultException e) { |
210 |
return false; |
211 |
} |
212 |
} |
213 |
|
214 |
@Override
|
215 |
public Set<String> getUserTags(final Long userId) throws ObjectNotFoundException { |
216 |
if (userId == null) |
217 |
throw new ObjectNotFoundException("No user specified"); |
218 |
return new HashSet(manager.createQuery("select t.tag from FileTag t where t.user.id=:userId order by t.tag") |
219 |
.setParameter("userId", userId)
|
220 |
.getResultList()); |
221 |
} |
222 |
|
223 |
@Override
|
224 |
public void flush() { |
225 |
manager.flush(); |
226 |
} |
227 |
|
228 |
@Override
|
229 |
public FileHeader getFile(Long folderId, String name) throws ObjectNotFoundException { |
230 |
if (folderId == null) |
231 |
throw new ObjectNotFoundException("No parent folder specified"); |
232 |
if (StringUtils.isEmpty(name))
|
233 |
throw new IllegalArgumentException("No file name specified"); |
234 |
|
235 |
try {
|
236 |
return (FileHeader) manager.createQuery("select f from FileHeader f where f.folder.id=:parentId and f.name=:name") |
237 |
.setParameter("parentId", folderId)
|
238 |
.setParameter("name", name)
|
239 |
.getSingleResult(); |
240 |
} catch (final NoResultException e1) { |
241 |
throw new ObjectNotFoundException("File not found"); |
242 |
} |
243 |
} |
244 |
|
245 |
@Override
|
246 |
public Folder getFolder(Long parentId, String name) throws ObjectNotFoundException { |
247 |
if (parentId == null) |
248 |
throw new ObjectNotFoundException("No parent folder specified"); |
249 |
if (StringUtils.isEmpty(name))
|
250 |
throw new IllegalArgumentException("No folder name specified"); |
251 |
|
252 |
try {
|
253 |
return (Folder) manager.createQuery("select f from Folder f where f.parent.id=:parentId and f.name=:name") |
254 |
.setParameter("parentId", parentId)
|
255 |
.setParameter("name", name)
|
256 |
.getSingleResult(); |
257 |
} catch (NoResultException e) {
|
258 |
throw new ObjectNotFoundException("Folder not found"); |
259 |
} |
260 |
} |
261 |
|
262 |
@Override
|
263 |
public List<FileHeader> getDeletedFiles(Long userId) throws ObjectNotFoundException { |
264 |
if (userId == null) |
265 |
throw new ObjectNotFoundException("No User specified"); |
266 |
User user = getEntityById(User.class, userId); |
267 |
|
268 |
List<FileHeader> tempList = manager.createQuery("select f from FileHeader f where f.owner.id=:userId and " + |
269 |
"f.deleted=true and f.folder.deleted=false").
|
270 |
setParameter("userId", userId).getResultList();
|
271 |
List<FileHeader> retv = new ArrayList<FileHeader>(); |
272 |
for (FileHeader f: tempList)
|
273 |
if (f.hasReadPermission(user)) retv.add(f);
|
274 |
|
275 |
return retv;
|
276 |
} |
277 |
|
278 |
@Override
|
279 |
public List<Folder> getDeletedRootFolders(Long userId) throws ObjectNotFoundException { |
280 |
if (userId == null) |
281 |
throw new ObjectNotFoundException("No User specified"); |
282 |
return manager.createQuery("select f from Folder f where f.owner.id=:userId and " + |
283 |
"f.deleted=true and f.parent.deleted=false").
|
284 |
setParameter("userId", userId).getResultList();
|
285 |
} |
286 |
|
287 |
@Override
|
288 |
public User findUser(String username) { |
289 |
if (username == null) |
290 |
return null; |
291 |
List<User> results = manager.createQuery("select u from User u where u.username=:username"). |
292 |
setParameter("username", username).getResultList();
|
293 |
if (results.isEmpty()) return null; |
294 |
return results.get(0); |
295 |
} |
296 |
|
297 |
@Override
|
298 |
public User findUserByEmail(String email) { |
299 |
if (email == null) |
300 |
return null; |
301 |
List<User> results = manager.createQuery("select u from User u where u.email=:email"). |
302 |
setParameter("email", email).getResultList();
|
303 |
if (results.isEmpty()) return null; |
304 |
return results.get(0); |
305 |
} |
306 |
|
307 |
@Override
|
308 |
public List<User> getUsersByUserNameLike(String username) { |
309 |
return manager.createQuery("select u from User u where u.username like :username"). |
310 |
setParameter("username", username+"%").getResultList(); |
311 |
} |
312 |
|
313 |
@Override
|
314 |
public List<Folder> getSharedRootFolders(Long userId) { |
315 |
List<Folder> folders = manager.createQuery("select distinct f from Folder f " + |
316 |
"LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
|
317 |
"and (p.group.id != null or p.user.id != f.owner.id or f.readForAll=true) ").
|
318 |
setParameter("userId", userId).getResultList();
|
319 |
List<Folder> result = new ArrayList<Folder>(); |
320 |
for(Folder f : folders)
|
321 |
if(!folders.contains(f.getParent()))
|
322 |
result.add(f); |
323 |
return result;
|
324 |
} |
325 |
|
326 |
@Override
|
327 |
public List<Folder> getFoldersPermittedForGroup(Long userId, Long groupId) { |
328 |
return manager.createQuery("select distinct f from Folder f LEFT JOIN f.permissions p " + |
329 |
"where f.owner.id=:userId and f.deleted = false and p.group.id=:groupId ").
|
330 |
setParameter("userId", userId).setParameter("groupId", groupId).getResultList(); |
331 |
} |
332 |
|
333 |
@Override
|
334 |
public List<User> getUsersSharingFoldersForUser(Long userId) { |
335 |
return manager.createQuery("select distinct f.owner from Folder f " + |
336 |
"LEFT JOIN f.permissions p where f.owner.id != :userId and f.deleted=false " +
|
337 |
"and (p.user.id=:userId or p.group.id in (select distinct gg.id " +
|
338 |
"from Group gg join gg.members memb where memb.id=:userId))) ").
|
339 |
setParameter("userId", userId).getResultList();
|
340 |
} |
341 |
|
342 |
@Override
|
343 |
public List<User> getUsersSharingFilesForUser(Long userId) { |
344 |
List<User> users = manager.createQuery("select distinct f.owner from FileHeader f " + |
345 |
"LEFT JOIN f.permissions p where f.owner.id != :userId and f.deleted=false " +
|
346 |
"and (p.user.id=:userId or p.group.id in (select distinct gg.id from Group gg " +
|
347 |
"join gg.members memb where memb.id=:userId)))").
|
348 |
setParameter("userId", userId).getResultList();
|
349 |
return users;
|
350 |
|
351 |
} |
352 |
|
353 |
@Override
|
354 |
public List<FileHeader> getSharedFilesNotInSharedFolders(Long userId) throws ObjectNotFoundException { |
355 |
if (userId == null) |
356 |
throw new ObjectNotFoundException("No user specified"); |
357 |
User user = getEntityById(User.class, userId); |
358 |
List<FileHeader> tempList = manager.createQuery("select distinct f from FileHeader f " + |
359 |
"LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
|
360 |
"and (f.readForAll=true or p.group.id != null or p.user.id != f.owner.id)" +
|
361 |
" and f.folder.id not in (select distinct fo.id from Folder fo LEFT JOIN " +
|
362 |
"fo.permissions po where fo.owner.id=:userId and fo.deleted=false and " +
|
363 |
"(po.group.id != null or po.user.id != fo.owner.id or fo.readForAll = true))").
|
364 |
setParameter("userId", userId).getResultList();
|
365 |
List<FileHeader> retv = new ArrayList<FileHeader>(); |
366 |
for (FileHeader f: tempList)
|
367 |
if (f.hasReadPermission(user)) retv.add(f);
|
368 |
|
369 |
return retv;
|
370 |
} |
371 |
|
372 |
@Override
|
373 |
public List<FileHeader> getSharedFiles(Long userId) throws ObjectNotFoundException { |
374 |
if (userId == null) |
375 |
throw new ObjectNotFoundException("No user specified"); |
376 |
User user = getEntityById(User.class, userId); |
377 |
List<FileHeader> tempList = manager.createQuery("select distinct f from FileHeader f " + |
378 |
"LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
|
379 |
"and (p.group.id != null or p.user.id != f.owner.id)").
|
380 |
setParameter("userId", userId).getResultList();
|
381 |
List<FileHeader> retv = new ArrayList<FileHeader>(); |
382 |
for (FileHeader f: tempList)
|
383 |
if (f.hasReadPermission(user)) retv.add(f);
|
384 |
|
385 |
return retv;
|
386 |
} |
387 |
|
388 |
@Override
|
389 |
public List<Folder> getSharedFolders(Long userId) { |
390 |
List<Folder> folders = manager.createQuery("select distinct f from Folder f " + |
391 |
"LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
|
392 |
"and (p.group.id != null or p.user.id != f.owner.id)").
|
393 |
setParameter("userId", userId).getResultList();
|
394 |
return folders;
|
395 |
} |
396 |
|
397 |
@Override
|
398 |
public List<FileHeader> getSharedFiles(Long userId, Long callingUserId) throws ObjectNotFoundException { |
399 |
if (userId == null) |
400 |
throw new ObjectNotFoundException("No user specified"); |
401 |
User user = getEntityById(User.class, userId); |
402 |
List<FileHeader> tempList = manager.createQuery("select distinct f from FileHeader f " + |
403 |
"LEFT JOIN f.permissions p where f.owner.id=:userId and f.deleted=false " +
|
404 |
"and p.read=true and (p.user.id=:cuserId or p.group.id in " +
|
405 |
"(select distinct gg.id from Group gg join gg.members memb " +
|
406 |
"where memb.id=:cuserId)) and f.folder.id not in (select distinct fo.id " +
|
407 |
"from Folder fo LEFT JOIN fo.permissions po where fo.owner.id = :userId " +
|
408 |
"and fo.deleted=false and po.read=true and (po.user.id=:cuserId " +
|
409 |
"or po.group.id in (select distinct ggo.id from Group ggo " +
|
410 |
"join ggo.members membo where membo.id=:cuserId)))").
|
411 |
setParameter("userId", userId).setParameter("cuserId", callingUserId).getResultList(); |
412 |
List<FileHeader> retv = new ArrayList<FileHeader>(); |
413 |
for (FileHeader f: tempList)
|
414 |
if (f.hasReadPermission(user)) retv.add(f);
|
415 |
|
416 |
return retv;
|
417 |
} |
418 |
|
419 |
@Override
|
420 |
public List<Folder> getSharedRootFolders(Long userId, Long callingUserId) { |
421 |
List<Folder> folders = manager.createQuery("select distinct f from Folder f " + |
422 |
"LEFT JOIN f.permissions p where f.owner.id = :userId and f.deleted=false " +
|
423 |
"and p.read=true and (p.user.id=:cuserId or p.group.id in " +
|
424 |
"(select distinct gg.id from Group gg join gg.members memb " +
|
425 |
"where memb.id=:cuserId))) ").
|
426 |
setParameter("userId", userId).
|
427 |
setParameter("cuserId", callingUserId).
|
428 |
getResultList(); |
429 |
List<Folder> result = new ArrayList<Folder>(); |
430 |
for(Folder f : folders)
|
431 |
if(!folders.contains(f.getParent()))
|
432 |
result.add(f); |
433 |
return result;
|
434 |
|
435 |
} |
436 |
|
437 |
@Override
|
438 |
public List<FileHeader> searchFiles(Long userId, String query) { |
439 |
return manager.createQuery("select f from FileHeader f where f.owner.id=:userId and f.name like :query"). |
440 |
setParameter("query", "%"+query+"%").setParameter("userId",userId).getResultList(); |
441 |
} |
442 |
|
443 |
@Override
|
444 |
public Nonce getNonce(String nonce, Long userId) throws ObjectNotFoundException { |
445 |
List<Nonce> results = manager.createQuery("select n from Nonce n where n.userId=:userId and n.encodedNonce=:nonce"). |
446 |
setParameter("userId", userId).setParameter("nonce", nonce).getResultList(); |
447 |
if (results.isEmpty())
|
448 |
throw new ObjectNotFoundException("No nonce found"); |
449 |
return results.get(0); |
450 |
} |
451 |
|
452 |
@Override
|
453 |
public FileHeader getFileForIndexing(Long id) throws ObjectNotFoundException { |
454 |
FileHeader h = getEntityById(FileHeader.class, id); |
455 |
h.getFileTags().size(); |
456 |
return h;
|
457 |
} |
458 |
|
459 |
@Override
|
460 |
public Long getFileCount(Long userId) { |
461 |
Long singleResult = (Long) manager.createQuery("select count(f) from FileHeader f where f.owner.id=:ownerId") |
462 |
.setParameter("ownerId", userId)
|
463 |
.getSingleResult(); |
464 |
return singleResult;
|
465 |
} |
466 |
|
467 |
@Override
|
468 |
public Long getFileSize(Long userId) { |
469 |
Long singleResult = (Long) manager.createQuery("select sum(f.fileSize) from FileBody f where f.header.owner.id=:ownerId") |
470 |
.setParameter("ownerId", userId)
|
471 |
.getSingleResult(); |
472 |
if(singleResult == null) |
473 |
singleResult = new Long(0L); |
474 |
return singleResult;
|
475 |
|
476 |
} |
477 |
|
478 |
@Override
|
479 |
public List<Long> getAllFileIds() { |
480 |
List<Long> ids = manager.createQuery("select f.id from FileHeader f").getResultList(); |
481 |
return ids;
|
482 |
} |
483 |
|
484 |
@Override
|
485 |
public FileUploadStatus getFileUploadStatus(Long userId, String fileName) { |
486 |
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(); |
487 |
if(res.size()>0) |
488 |
return res.get(0); |
489 |
return null; |
490 |
} |
491 |
|
492 |
@Override
|
493 |
public FileBody getFileVersion(Long fileId, int version) throws ObjectNotFoundException { |
494 |
try {
|
495 |
return (FileBody) manager.createQuery("select f from FileBody f " + |
496 |
"where f.header.id=:fileId and f.version=:version")
|
497 |
.setParameter("fileId", fileId).setParameter("version", version) |
498 |
.getSingleResult(); |
499 |
} catch (NoResultException e) {
|
500 |
throw new ObjectNotFoundException("No version " + version + " found for file #" + fileId); |
501 |
} |
502 |
} |
503 |
|
504 |
@Override
|
505 |
public void updateAccounting(User user, Date date, long bandwidthDiff) { |
506 |
AccountingInfo ai = null;
|
507 |
try {
|
508 |
ai = (AccountingInfo) manager.createQuery("select ai from AccountingInfo ai " +
|
509 |
"where ai.user=:user and ai.dateFrom<=:date and ai.dateTo>:date")
|
510 |
.setParameter("user", user)
|
511 |
.setParameter("date", date)
|
512 |
.getSingleResult(); |
513 |
} |
514 |
catch (NoResultException e) {
|
515 |
ai = null;
|
516 |
} |
517 |
|
518 |
if (ai==null) { |
519 |
// The right entry does not exist; must be created.
|
520 |
// This is where we set the initial time period.
|
521 |
// We now start from the user's creation, we can change this to something else.
|
522 |
Calendar creationDate = new GregorianCalendar(); |
523 |
creationDate.setTime(user.getAuditInfo().getCreationDate()); |
524 |
int offset = 0; |
525 |
Calendar dateFrom;
|
526 |
Calendar dateTo;
|
527 |
long timeInMillis = date.getTime();
|
528 |
do {
|
529 |
dateFrom = (Calendar) creationDate.clone();
|
530 |
dateFrom.add(BANDWIDTH_TIME_PERIOD_FIELD, offset); |
531 |
dateTo = (Calendar) dateFrom.clone();
|
532 |
dateTo.add(BANDWIDTH_TIME_PERIOD_FIELD, 1);
|
533 |
offset += BANDWIDTH_TIME_PERIOD_AMOUNT; |
534 |
} |
535 |
while (!(dateFrom.getTimeInMillis()<=timeInMillis && dateTo.getTimeInMillis()>timeInMillis));
|
536 |
|
537 |
ai = new AccountingInfo(user, dateFrom.getTime(), dateTo.getTime());
|
538 |
manager.persist(ai); |
539 |
} |
540 |
|
541 |
// Do the update.
|
542 |
ai.updateBandwidth(bandwidthDiff); |
543 |
} |
544 |
|
545 |
@Override
|
546 |
public List<UserClass> getUserClasses() { |
547 |
// Ordering by quota is important here.
|
548 |
List<UserClass> ids = manager.createQuery("select uc from UserClass uc order by uc.quota").getResultList(); |
549 |
return ids;
|
550 |
} |
551 |
|
552 |
@Override
|
553 |
public List<User> getUsersByUserNameOrEmailLike(String query) { |
554 |
return manager.createQuery("select u from User u where " + |
555 |
" upper(u.username) like :query or upper(u.email) like :query order by u.username").
|
556 |
setParameter("query", query.toUpperCase()+"%").getResultList(); |
557 |
} |
558 |
|
559 |
@Override
|
560 |
public Invitation findInvite(String code) { |
561 |
if (code == null) |
562 |
return null; |
563 |
List<Invitation> results = manager.createQuery("select i from Invitation i where i.code=:code"). |
564 |
setParameter("code", code).getResultList();
|
565 |
if (results.isEmpty()) return null; |
566 |
return results.get(0); |
567 |
} |
568 |
|
569 |
@Override
|
570 |
public UserClass findCouponUserClass() {
|
571 |
List<UserClass> results = manager.createQuery("select uc from UserClass uc where uc.name=:name"). |
572 |
setParameter("name", getConfiguration().getString("couponQuota")).getResultList(); |
573 |
if (results.isEmpty()) {
|
574 |
// Create the coupon user class on first use.
|
575 |
UserClass couponClass = new UserClass();
|
576 |
couponClass.setName("coupon");
|
577 |
Long couponQuota = getConfiguration().getLong("couponQuota", new Long(52428800L)); |
578 |
couponClass.setQuota(couponQuota); |
579 |
create(couponClass); |
580 |
flush(); |
581 |
results.add(couponClass); |
582 |
} |
583 |
return results.get(0); |
584 |
} |
585 |
|
586 |
@Override
|
587 |
public Long getFileCount(UserClass userClass) { |
588 |
Long result;
|
589 |
if(userClass==null) |
590 |
result=(Long) manager.createQuery("select count(f) from FileHeader f").getSingleResult(); |
591 |
else result= (Long) manager.createQuery("select count(f) from FileHeader f where f.owner.userClass.id=:id").setParameter("id", userClass.getId()).getSingleResult(); |
592 |
if(result==null) |
593 |
result =0L;
|
594 |
return result;
|
595 |
} |
596 |
|
597 |
@Override
|
598 |
public Long getFileSize(UserClass userClass) { |
599 |
Long result;
|
600 |
if(userClass==null) |
601 |
result=(Long) manager.createQuery("select sum(f.currentBody.fileSize) from FileHeader f").getSingleResult(); |
602 |
else result=(Long) manager.createQuery("select sum(f.currentBody.fileSize) from FileHeader f where f.owner.userClass.id=:id").setParameter("id", userClass.getId()).getSingleResult(); |
603 |
if(result==null) |
604 |
result =0L;
|
605 |
return result;
|
606 |
} |
607 |
|
608 |
@Override
|
609 |
public Long getUserCount(UserClass userClass) { |
610 |
Long result;
|
611 |
if(userClass==null) |
612 |
result = (Long) manager.createQuery("select count(u) from User u").getSingleResult(); |
613 |
else result = (Long) manager.createQuery("select count(u) from User u where u.userClass.id=:id").setParameter("id", userClass.getId()).getSingleResult(); |
614 |
if(result==null) |
615 |
result =0L;
|
616 |
return result;
|
617 |
} |
618 |
|
619 |
@Override
|
620 |
public Long getCountUsersByLastLogin(Date lastLoginDate) { |
621 |
return (Long) manager.createQuery("select count(u) from User u where " + |
622 |
" u.lastLogin >= :ldate ").setParameter("ldate", lastLoginDate).getSingleResult(); |
623 |
} |
624 |
|
625 |
@Override
|
626 |
public List<User> getUsersByLastLogin(Date lastLoginDate) { |
627 |
return manager.createQuery("select u from User u where " + |
628 |
" u.lastLogin >= :ldate order by u.lastLogin desc").
|
629 |
setParameter("ldate", lastLoginDate).getResultList();
|
630 |
} |
631 |
|
632 |
@Override
|
633 |
public List<User> getUsersByLastLogin(Date lastLoginDate, int firstResult, int maxResult) { |
634 |
return manager.createQuery("select u from User u where " + |
635 |
" u.lastLogin >= :ldate order by u.lastLogin desc").
|
636 |
setParameter("ldate", lastLoginDate).setFirstResult(firstResult).setMaxResults(maxResult).getResultList();
|
637 |
} |
638 |
|
639 |
@Override
|
640 |
public List<User> getInactiveUsers() { |
641 |
return manager.createQuery("select u from User u where u.active=:active").setParameter("active", false).getResultList(); |
642 |
} |
643 |
|
644 |
@Override
|
645 |
public List<FileHeader> searchFileByFilename(String filename) { |
646 |
return manager.createQuery("select f from FileHeader f where f.name=:name").setParameter("name", filename).getResultList(); |
647 |
} |
648 |
|
649 |
@Override
|
650 |
public Long getBandwithUsed(UserClass userClass, Date date) { |
651 |
Long result;
|
652 |
if (userClass == null) |
653 |
result = (Long) manager.createQuery("select sum(ai.bandwidthUsed)" + |
654 |
" from AccountingInfo ai where ai.dateFrom<=:date and " +
|
655 |
"ai.dateTo>:date").
|
656 |
setParameter("date", date).
|
657 |
getSingleResult(); |
658 |
else
|
659 |
result = (Long) manager.createQuery("select sum(ai.bandwidthUsed)" + |
660 |
" from AccountingInfo ai where ai.user.userClass.id=:id " +
|
661 |
"and ai.dateFrom<=:date and ai.dateTo>:date").
|
662 |
setParameter("date", date).
|
663 |
setParameter("id", userClass.getId()).
|
664 |
getSingleResult(); |
665 |
if (result == null) |
666 |
result = 0L;
|
667 |
return result;
|
668 |
} |
669 |
|
670 |
@Override
|
671 |
public List<AccountingInfo> getAccountingInfo(User user) { |
672 |
List<AccountingInfo> ai = new ArrayList<AccountingInfo>(); |
673 |
try {
|
674 |
ai = manager.createQuery("select ai from AccountingInfo ai " +
|
675 |
"where ai.user=:user")
|
676 |
.setParameter("user", user)
|
677 |
.getResultList(); |
678 |
} |
679 |
catch (NoResultException e) {}
|
680 |
return ai;
|
681 |
} |
682 |
|
683 |
@Override
|
684 |
public AccountingInfo getAccountingInfo(User user, Date date) { |
685 |
AccountingInfo ai = null;
|
686 |
try {
|
687 |
ai = (AccountingInfo) manager.createQuery("select ai from AccountingInfo ai " +
|
688 |
"where ai.user=:user and ai.dateFrom<=:date and ai.dateTo>:date")
|
689 |
.setParameter("user", user)
|
690 |
.setParameter("date", date)
|
691 |
.getSingleResult(); |
692 |
} |
693 |
catch (NoResultException e) {
|
694 |
// If not found, that means that there is no accounting info noted
|
695 |
// for given time. So return a 0 as an answer.
|
696 |
ai = new AccountingInfo(user, date, date);
|
697 |
} |
698 |
return ai;
|
699 |
} |
700 |
|
701 |
@Override
|
702 |
public List<FileHeader> getFilesPermittedForGroup(Long userId, Long groupId) { |
703 |
return manager.createQuery("select distinct f from FileHeader f LEFT JOIN f.permissions p " + |
704 |
"where f.owner.id=:userId and f.deleted = false and p.group.id=:groupId ").
|
705 |
setParameter("userId", userId).setParameter("groupId", groupId).getResultList(); |
706 |
} |
707 |
|
708 |
@Override
|
709 |
public List<Folder> getSharingFoldersForUser(Long userId) { |
710 |
return manager.createQuery("select distinct f from Folder f " + |
711 |
"LEFT JOIN f.permissions p where " +
|
712 |
" (p.user.id=:userId or p.group.id in (select distinct gg.id " +
|
713 |
"from Group gg join gg.members memb where memb.id=:userId))) ").
|
714 |
setParameter("userId", userId).getResultList();
|
715 |
} |
716 |
|
717 |
@Override
|
718 |
public List<FileHeader> getSharingFilesForUser(Long userId) { |
719 |
List<FileHeader> users = manager.createQuery("select distinct f from FileHeader f " + |
720 |
"LEFT JOIN f.permissions p where " +
|
721 |
" (p.user.id=:userId or p.group.id in (select distinct gg.id from Group gg " +
|
722 |
"join gg.members memb where memb.id=:userId)))").
|
723 |
setParameter("userId", userId).getResultList();
|
724 |
return users;
|
725 |
|
726 |
} |
727 |
|
728 |
@Override
|
729 |
public List<Group> getGroupsContainingUser(Long userId){ |
730 |
List<Group> groups = manager.createQuery("select distinct gg " + |
731 |
"from Group gg join gg.members memb where memb.id=:userId)").setParameter("userId", userId).getResultList(); |
732 |
return groups;
|
733 |
} |
734 |
|
735 |
@Override
|
736 |
public List<FileUploadStatus> getUploadStatus(Long userId){ |
737 |
List<FileUploadStatus> res = manager.createQuery("select f from FileUploadStatus f where f.owner.id=:userId").setParameter("userId", userId).getResultList(); |
738 |
return res;
|
739 |
} |
740 |
@Override
|
741 |
public int deletePermissionsNotCorrespondingToFilesAndFolders(Long userId){ |
742 |
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(); |
743 |
} |
744 |
@Override
|
745 |
public void fixSharedFlagForAllFoldersAndFiles(){ |
746 |
List<Folder> folders = manager.createQuery("select f from Folder f").getResultList(); |
747 |
for(Folder f : folders){
|
748 |
if(f.isReadForAll()||f.getPermissions().size()>1){ |
749 |
f.setShared(true);
|
750 |
update(f); |
751 |
} |
752 |
} |
753 |
List<FileHeader> files = manager.createQuery("select f from FileHeader f").getResultList(); |
754 |
for(FileHeader f : files){
|
755 |
if(f.isReadForAll()||f.getPermissions().size()>1){ |
756 |
f.setShared(true);
|
757 |
update(f); |
758 |
} |
759 |
} |
760 |
} |
761 |
} |