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