root / src / gr / ebs / gss / server / ejb / GSSDAOBean.java @ 376d0ebf
History | View | Annotate | Download (25.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.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 Long getFileCount(Long userId) { |
454 |
Long singleResult = (Long) manager.createQuery("select count(f) from FileHeader f where f.owner.id=:ownerId") |
455 |
.setParameter("ownerId", userId)
|
456 |
.getSingleResult(); |
457 |
return singleResult;
|
458 |
} |
459 |
|
460 |
@Override
|
461 |
public Long getFileSize(Long userId) { |
462 |
Long singleResult = (Long) manager.createQuery("select sum(f.fileSize) from FileBody f where f.header.owner.id=:ownerId") |
463 |
.setParameter("ownerId", userId)
|
464 |
.getSingleResult(); |
465 |
if(singleResult == null) |
466 |
singleResult = new Long(0L); |
467 |
return singleResult;
|
468 |
|
469 |
} |
470 |
|
471 |
@Override
|
472 |
public List<Long> getAllFileIds() { |
473 |
List<Long> ids = manager.createQuery("select f.id from FileHeader f").getResultList(); |
474 |
return ids;
|
475 |
} |
476 |
|
477 |
@Override
|
478 |
public FileUploadStatus getFileUploadStatus(Long userId, String fileName) { |
479 |
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(); |
480 |
if(res.size()>0) |
481 |
return res.get(0); |
482 |
return null; |
483 |
} |
484 |
|
485 |
@Override
|
486 |
public FileBody getFileVersion(Long fileId, int version) throws ObjectNotFoundException { |
487 |
try {
|
488 |
return (FileBody) manager.createQuery("select f from FileBody f " + |
489 |
"where f.header.id=:fileId and f.version=:version")
|
490 |
.setParameter("fileId", fileId).setParameter("version", version) |
491 |
.getSingleResult(); |
492 |
} catch (NoResultException e) {
|
493 |
throw new ObjectNotFoundException("No version " + version + " found for file #" + fileId); |
494 |
} |
495 |
} |
496 |
|
497 |
@Override
|
498 |
public void updateAccounting(User user, Date date, long bandwidthDiff) { |
499 |
AccountingInfo ai = null;
|
500 |
try {
|
501 |
ai = (AccountingInfo) manager.createQuery("select ai from AccountingInfo ai " +
|
502 |
"where ai.user=:user and ai.dateFrom<=:date and ai.dateTo>:date")
|
503 |
.setParameter("user", user)
|
504 |
.setParameter("date", date)
|
505 |
.getSingleResult(); |
506 |
} |
507 |
catch (NoResultException e) {
|
508 |
ai = null;
|
509 |
} |
510 |
|
511 |
if (ai==null) { |
512 |
// The right entry does not exist; must be created.
|
513 |
// This is where we set the initial time period.
|
514 |
// We now start from the user's creation, we can change this to something else.
|
515 |
Calendar creationDate = new GregorianCalendar(); |
516 |
creationDate.setTime(user.getAuditInfo().getCreationDate()); |
517 |
int offset = 0; |
518 |
Calendar dateFrom;
|
519 |
Calendar dateTo;
|
520 |
long timeInMillis = date.getTime();
|
521 |
do {
|
522 |
dateFrom = (Calendar) creationDate.clone();
|
523 |
dateFrom.add(BANDWIDTH_TIME_PERIOD_FIELD, offset); |
524 |
dateTo = (Calendar) dateFrom.clone();
|
525 |
dateTo.add(BANDWIDTH_TIME_PERIOD_FIELD, 1);
|
526 |
offset += BANDWIDTH_TIME_PERIOD_AMOUNT; |
527 |
} |
528 |
while (!(dateFrom.getTimeInMillis()<=timeInMillis && dateTo.getTimeInMillis()>timeInMillis));
|
529 |
|
530 |
ai = new AccountingInfo(user, dateFrom.getTime(), dateTo.getTime());
|
531 |
manager.persist(ai); |
532 |
} |
533 |
|
534 |
// Do the update.
|
535 |
ai.updateBandwidth(bandwidthDiff); |
536 |
} |
537 |
|
538 |
@Override
|
539 |
public List<UserClass> getUserClasses() { |
540 |
// Ordering by quota is important here.
|
541 |
List<UserClass> ids = manager.createQuery("select uc from UserClass uc order by uc.quota").getResultList(); |
542 |
return ids;
|
543 |
} |
544 |
|
545 |
@Override
|
546 |
public List<User> getUsersByUserNameOrEmailLike(String query) { |
547 |
return manager.createQuery("select u from User u where " + |
548 |
" upper(u.username) like :query or upper(u.email) like :query order by u.username").
|
549 |
setParameter("query", query.toUpperCase()+"%").getResultList(); |
550 |
} |
551 |
|
552 |
@Override
|
553 |
public Invitation findInvite(String code) { |
554 |
if (code == null) |
555 |
return null; |
556 |
List<Invitation> results = manager.createQuery("select i from Invitation i where i.code=:code"). |
557 |
setParameter("code", code).getResultList();
|
558 |
if (results.isEmpty()) return null; |
559 |
return results.get(0); |
560 |
} |
561 |
|
562 |
@Override
|
563 |
public UserClass findCouponUserClass() {
|
564 |
List<UserClass> results = manager.createQuery("select uc from UserClass uc where uc.name=:name"). |
565 |
setParameter("name", getConfiguration().getString("couponQuota")).getResultList(); |
566 |
if (results.isEmpty()) {
|
567 |
// Create the coupon user class on first use.
|
568 |
UserClass couponClass = new UserClass();
|
569 |
couponClass.setName("coupon");
|
570 |
Long couponQuota = getConfiguration().getLong("couponQuota", new Long(52428800L)); |
571 |
couponClass.setQuota(couponQuota); |
572 |
create(couponClass); |
573 |
flush(); |
574 |
results.add(couponClass); |
575 |
} |
576 |
return results.get(0); |
577 |
} |
578 |
|
579 |
@Override
|
580 |
public Long getFileCount(UserClass userClass) { |
581 |
Long result;
|
582 |
if(userClass==null) |
583 |
result=(Long) manager.createQuery("select count(f) from FileHeader f").getSingleResult(); |
584 |
else result= (Long) manager.createQuery("select count(f) from FileHeader f where f.owner.userClass.id=:id").setParameter("id", userClass.getId()).getSingleResult(); |
585 |
if(result==null) |
586 |
result =0L;
|
587 |
return result;
|
588 |
} |
589 |
|
590 |
@Override
|
591 |
public Long getFileSize(UserClass userClass) { |
592 |
Long result;
|
593 |
if(userClass==null) |
594 |
result=(Long) manager.createQuery("select sum(f.currentBody.fileSize) from FileHeader f").getSingleResult(); |
595 |
else result=(Long) manager.createQuery("select sum(f.currentBody.fileSize) from FileHeader f where f.owner.userClass.id=:id").setParameter("id", userClass.getId()).getSingleResult(); |
596 |
if(result==null) |
597 |
result =0L;
|
598 |
return result;
|
599 |
} |
600 |
|
601 |
@Override
|
602 |
public Long getUserCount(UserClass userClass) { |
603 |
Long result;
|
604 |
if(userClass==null) |
605 |
result = (Long) manager.createQuery("select count(u) from User u").getSingleResult(); |
606 |
else result = (Long) manager.createQuery("select count(u) from User u where u.userClass.id=:id").setParameter("id", userClass.getId()).getSingleResult(); |
607 |
if(result==null) |
608 |
result =0L;
|
609 |
return result;
|
610 |
} |
611 |
|
612 |
@Override
|
613 |
public Long getCountUsersByLastLogin(Date lastLoginDate) { |
614 |
return (Long) manager.createQuery("select count(u) from User u where " + |
615 |
" u.lastLogin >= :ldate ").setParameter("ldate", lastLoginDate).getSingleResult(); |
616 |
} |
617 |
|
618 |
@Override
|
619 |
public List<User> getUsersByLastLogin(Date lastLoginDate) { |
620 |
return manager.createQuery("select u from User u where " + |
621 |
" u.lastLogin >= :ldate order by u.lastLogin desc").
|
622 |
setParameter("ldate", lastLoginDate).getResultList();
|
623 |
} |
624 |
|
625 |
@Override
|
626 |
public List<User> getUsersByLastLogin(Date lastLoginDate, int firstResult, int maxResult) { |
627 |
return manager.createQuery("select u from User u where " + |
628 |
" u.lastLogin >= :ldate order by u.lastLogin desc").
|
629 |
setParameter("ldate", lastLoginDate).setFirstResult(firstResult).setMaxResults(maxResult).getResultList();
|
630 |
} |
631 |
|
632 |
@Override
|
633 |
public List<User> getInactiveUsers() { |
634 |
return manager.createQuery("select u from User u where u.active=:active").setParameter("active", false).getResultList(); |
635 |
} |
636 |
|
637 |
@Override
|
638 |
public List<FileHeader> searchFileByFilename(String filename) { |
639 |
return manager.createQuery("select f from FileHeader f where f.name=:name").setParameter("name", filename).getResultList(); |
640 |
} |
641 |
|
642 |
@Override
|
643 |
public Long getBandwithUsed(UserClass userClass, Date date) { |
644 |
Long result;
|
645 |
if (userClass == null) |
646 |
result = (Long) manager.createQuery("select sum(ai.bandwidthUsed)" + |
647 |
" from AccountingInfo ai where ai.dateFrom<=:date and " +
|
648 |
"ai.dateTo>:date").
|
649 |
setParameter("date", date).
|
650 |
getSingleResult(); |
651 |
else
|
652 |
result = (Long) manager.createQuery("select sum(ai.bandwidthUsed)" + |
653 |
" from AccountingInfo ai where ai.user.userClass.id=:id " +
|
654 |
"and ai.dateFrom<=:date and ai.dateTo>:date").
|
655 |
setParameter("date", date).
|
656 |
setParameter("id", userClass.getId()).
|
657 |
getSingleResult(); |
658 |
if (result == null) |
659 |
result = 0L;
|
660 |
return result;
|
661 |
} |
662 |
|
663 |
@Override
|
664 |
public List<AccountingInfo> getAccountingInfo(User user) { |
665 |
List<AccountingInfo> ai = new ArrayList<AccountingInfo>(); |
666 |
try {
|
667 |
ai = manager.createQuery("select ai from AccountingInfo ai " +
|
668 |
"where ai.user=:user")
|
669 |
.setParameter("user", user)
|
670 |
.getResultList(); |
671 |
} |
672 |
catch (NoResultException e) {}
|
673 |
return ai;
|
674 |
} |
675 |
|
676 |
@Override
|
677 |
public AccountingInfo getAccountingInfo(User user, Date date) { |
678 |
AccountingInfo ai = null;
|
679 |
try {
|
680 |
ai = (AccountingInfo) manager.createQuery("select ai from AccountingInfo ai " +
|
681 |
"where ai.user=:user and ai.dateFrom<=:date and ai.dateTo>:date")
|
682 |
.setParameter("user", user)
|
683 |
.setParameter("date", date)
|
684 |
.getSingleResult(); |
685 |
} |
686 |
catch (NoResultException e) {
|
687 |
// If not found, that means that there is no accounting info noted
|
688 |
// for given time. So return a 0 as an answer.
|
689 |
ai = new AccountingInfo(user, date, date);
|
690 |
} |
691 |
return ai;
|
692 |
} |
693 |
|
694 |
@Override
|
695 |
public List<FileHeader> getFilesPermittedForGroup(Long userId, Long groupId) { |
696 |
return manager.createQuery("select distinct f from FileHeader f LEFT JOIN f.permissions p " + |
697 |
"where f.owner.id=:userId and f.deleted = false and p.group.id=:groupId ").
|
698 |
setParameter("userId", userId).setParameter("groupId", groupId).getResultList(); |
699 |
} |
700 |
|
701 |
@Override
|
702 |
public FileHeader getFileForIndexing(Long id) throws ObjectNotFoundException { |
703 |
FileHeader h = getEntityById(FileHeader.class, id); |
704 |
h.getFileTags().size(); |
705 |
return h;
|
706 |
} |
707 |
} |