do not lock resource if entity object does not exist
[pithos] / src / gr / ebs / gss / server / ejb / GSSDAO.java
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 gr.ebs.gss.client.exceptions.ObjectNotFoundException;
22 import gr.ebs.gss.server.domain.AccountingInfo;
23 import gr.ebs.gss.server.domain.FileBody;
24 import gr.ebs.gss.server.domain.FileHeader;
25 import gr.ebs.gss.server.domain.FileUploadStatus;
26 import gr.ebs.gss.server.domain.Folder;
27 import gr.ebs.gss.server.domain.Group;
28 import gr.ebs.gss.server.domain.FileLock;
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 import gr.ebs.gss.server.domain.WebDavNonce;
35
36 import java.util.Date;
37 import java.util.List;
38 import java.util.Set;
39
40 import javax.ejb.Local;
41
42 /**
43  * This class serves as a facade in front of the persistence library so client
44  * classes can be independent of the persistence implementation.
45  *
46  * @author past
47  */
48 @Local
49 public interface GSSDAO {
50
51         /**
52          * Creates the given object in the persistent storage.
53          *
54          * @param obj The object to be saved or updated
55          */
56         public void create(Object obj);
57
58         /**
59          * Updates by re-attaching the given object to the persistence context.
60          *
61          * @param obj The object to be updated
62          */
63         public void update(Object obj);
64
65         /**
66          * Refreshes an object by re-attaching it to the persistence context.
67          *
68          * @param obj the supplied object
69          */
70         public void refresh(Object obj);
71
72         /**
73          * Deletes the specified entity from the persistent storage.
74          *
75          * @param entity the object to be deleted
76          */
77         public void delete(Object entity);
78
79         /**
80          * Returns an Entity of the specified class with the specified id
81          *
82          * @param <T> The type of the entity
83          * @param _class the Class of the entity
84          * @param _id the id of the entity
85          * @return the Object found
86          * @throws ObjectNotFoundException if the Object was not found
87          */
88         public <T> T getEntityById(Class<T> _class, Object _id) throws ObjectNotFoundException;
89
90         /**
91          * Returns the list of Groups that belong to a particular User.
92          *
93          * @param userId the ID of the specified User
94          * @return a List of Group objects
95          * @throws ObjectNotFoundException
96          */
97         public List<Group> getGroups(Long userId) throws ObjectNotFoundException;
98
99         /**
100          * Retrieves the root folder id for the specified user. The caller must ensure
101          * that the userId exists.
102          *
103          * @param userId
104          * @return Long The id
105          * @throws ObjectNotFoundException if no Folder was found
106          */
107         public Long getRootFolderId(final Long userId) throws ObjectNotFoundException;
108         
109         /**
110          * Retrieves the root folder for the specified user. The caller must ensure
111          * that the userId exists.
112          *
113          * @param userId
114          * @return Folder
115          * @throws ObjectNotFoundException if no Folder was found
116          */
117         public Folder getRootFolder(Long userId) throws gr.ebs.gss.client.exceptions.ObjectNotFoundException;
118
119         /**
120          * Retrieves the user for the requested username.
121          *
122          * @param username the username specified
123          * @return the user object
124          * @throws ObjectNotFoundException if no user was found
125          */
126         public User getUser(final String username) throws ObjectNotFoundException;
127
128         /**
129          * Returns a list of files contained in the folder specified by its id, CAUTION: it does not return files marked as deleted
130          *
131          * @param folderId
132          * @param userId
133          * @param ignoreDeleted
134          * @return List<FileHeader>
135          * @throws ObjectNotFoundException
136          */
137         @SuppressWarnings("unchecked")
138         public List<FileHeader> getFiles(Long folderId, Long userId, boolean ignoreDeleted) throws ObjectNotFoundException;
139
140         /**
141          * Returns a list of deleted files of user specified by userId
142          *
143          * @param userId
144          * @return List<FileHeader>
145          * @throws ObjectNotFoundException
146          */
147         @SuppressWarnings("unchecked")
148         public List<FileHeader> getDeletedFiles(Long userId) throws ObjectNotFoundException;
149
150         /**
151          * Returns a list of deleted root folders of user specified by userId
152          *
153          * @param userId
154          * @return List<Folder>
155          * @throws ObjectNotFoundException
156          */
157         @SuppressWarnings("unchecked")
158         public List<Folder> getDeletedRootFolders(Long userId) throws ObjectNotFoundException;
159
160         /**
161          * Returns a list of users for the specified group
162          *
163          * @param groupId
164          * @return List<User>
165          * @throws ObjectNotFoundException
166          */
167         public List<User> getUsers(Long groupId) throws ObjectNotFoundException;
168
169         /**
170          * Checks if a folder or file with the specified name exists under the
171          * specified parent.
172          *
173          * @param parentId
174          * @param name
175          * @return boolean
176          * @throws ObjectNotFoundException
177          * @throws ObjectNotFoundException
178          */
179         public boolean existsFolderOrFile(Long parentId, String name) throws ObjectNotFoundException;
180
181         /**
182          * Checks if a folder with the specified name exists for the specified user.
183          *
184          * @param userId the owner of the group
185          * @param name the name of the group
186          * @return true if a group with the same name exists
187          * @throws ObjectNotFoundException
188          */
189         public boolean existsGroup(Long userId, String name) throws ObjectNotFoundException;
190
191         /**
192          * Retrieves all tags defined by the specified user
193          *
194          * @param userId
195          * @return Set<String> A set of string tags
196          * @throws ObjectNotFoundException if the user was null
197          */
198         public Set<String> getUserTags(final Long userId) throws ObjectNotFoundException;
199
200         /**
201          * Flushes the persistence context
202          */
203         public void flush();
204
205         /**
206          * Retrieve the file with the supplied name that is contained
207          * in a folder with the specified ID.
208          *
209          * @param folderId the ID of the parent folder
210          * @param name the name of the file
211          * @return the file found
212          * @throws ObjectNotFoundException if the file or parent folder was not found,
213          *                      with the exception message mentioning the precise problem
214          */
215         public FileHeader getFile(Long folderId, String name) throws ObjectNotFoundException;
216
217         /**
218          * Retrieve the folder with the supplied name that is contained
219          * in a folder with the specified ID.
220          *
221          * @param parentId the ID of the parent folder
222          * @param name the name of the folder
223          * @return the folder found
224          * @throws ObjectNotFoundException if the folder or parent was not found,
225          *                      with the exception message mentioning the precise problem
226          */
227         public Folder getFolder(Long parentId, String name) throws ObjectNotFoundException;
228
229         /**
230          * Search the system for a user with the specified username.
231          * If no such user is found, the method returns null.
232          *
233          * @param username the username to search for
234          * @return the User object with the specified username
235          */
236         public User findUser(String username);
237
238         /**
239          * Search the system for a user with the specified email address.
240          * If no such user is found, the method returns null.
241          */
242         public User findUserByEmail(String email);
243
244         /**
245          * Returns a list of users matching specified username
246          *
247          * @param username the email of the User
248          * @return List<User>
249          */
250         public List<User> getUsersByUserNameLike(String username);
251
252         /**
253          * Returns a list of users matching specified username or email
254          * @param query
255          * @return List<User>
256          */
257         public List<User> getUsersByUserNameOrEmailLike(String query);
258
259         /**
260          * Returns a list of All Shared root folders of a user.
261          *
262          * @param userId the ID of the User
263          * @return the list of shared root folders
264          * @throws ObjectNotFoundException if the user cannot be found
265          */
266         public List<Folder> getSharedRootFolders(Long userId) throws ObjectNotFoundException;
267
268         /**
269          * Returns a list of all shared files of a user, not contained in a shared folder.
270          *
271          * @param userId the ID of the User
272          * @return the list of shared files
273          * @throws ObjectNotFoundException if the user cannot be found
274          */
275         public List<FileHeader> getSharedFilesNotInSharedFolders(Long userId) throws ObjectNotFoundException;
276
277         /**
278          * Returns a list of all shared files of a user.
279          *
280          * @param userId the ID of the User
281          * @return the list of shared files
282          * @throws ObjectNotFoundException if the user cannot be found
283          */
284         public List<FileHeader> getSharedFiles(Long userId) throws ObjectNotFoundException;
285
286         /**
287          * Returns a list of all shared folders of a user.
288          *
289          * @param userId the ID of the User
290          * @return the list of shared folders
291          * @throws ObjectNotFoundException if the user cannot be found
292          */
293         public List<Folder> getSharedFolders(Long userId) throws ObjectNotFoundException;
294
295         /**
296          * Returns a list of folders of user with permissions for specified group
297          *
298          * @param userId the ID of the User
299          * @return the list of shared root folders
300          * @param groupId
301          * @throws ObjectNotFoundException if the user cannot be found
302          */
303         public List<Folder> getFoldersPermittedForGroup(Long userId, Long groupId) throws ObjectNotFoundException;
304
305         /**
306          * Returns a list of users sharing files to specified user
307          *
308          * @param userId the ID of the User
309          * @return the list of users sharing files to selected user
310          * @throws ObjectNotFoundException if the user cannot be found
311          */
312         public List<User> getUsersSharingFoldersForUser(Long userId) throws ObjectNotFoundException;
313
314         /**
315          * Returns a list of users sharing files to specified user
316          *
317          * @param userId the ID of the User
318          * @return the list of users sharing files to selected user
319          * @throws ObjectNotFoundException if the user cannot be found
320          */
321         public List<User> getUsersSharingFilesForUser(Long userId) throws ObjectNotFoundException;
322
323         /**
324          * Returns a list of All Shared root folders of a user that calling user has permissions to read them at least.
325          *
326          * @param userId the ID of the User
327          * @param callingUserId
328          * @return the list of shared root folders
329          * @throws ObjectNotFoundException if the user cannot be found
330          */
331         public List<Folder> getSharedRootFolders(Long userId, Long callingUserId) throws ObjectNotFoundException;
332
333         /**
334          * Returns a list of All Shared files of a user not contained in a shared folder that calling user has permissions.
335          *
336          * @param userId the ID of the User
337          * @param callingUserId
338          * @return the list of shared files
339          * @throws ObjectNotFoundException if the user cannot be found
340          */
341         public List<FileHeader> getSharedFiles(Long userId, Long callingUserId) throws ObjectNotFoundException;
342
343         /**
344          * Search Files
345          * @param userId
346          * @param query
347          * @return list of files that match query
348          * @throws ObjectNotFoundException
349          */
350         public List<FileHeader> searchFiles(Long userId, String query) throws ObjectNotFoundException;
351
352         /**
353          * Find the nonce object for the specified encoded nonce, that should be
354          * associated with the specified user.
355          *
356          * @param nonce the issued nonce in Base64 encoding
357          * @param userId the ID of the user for whom this nonce should have been issued
358          * @return the retrieved nonce object
359          * @throws ObjectNotFoundException if the nonce or user were not found
360          */
361         public Nonce getNonce(String nonce, Long userId) throws ObjectNotFoundException;
362
363         /**
364          * Calculates total file size of user.
365          *
366          * @param userId the ID of the user
367          * @return the aggregate size of all the user's files
368          */
369         public Long getFileSize(Long userId);
370
371         /**
372          * Calculates total file count of user.
373          *
374          * @param userId the ID of the user
375          * @return the total number of files in the user's namespace
376          */
377         public Long getFileCount(Long userId);
378
379         /**
380          * This method returns all file ids for rebuilding the search index
381          *
382          * @return a list of Long file ids
383          */
384         public List<Long> getAllFileIds();
385
386         public FileUploadStatus getFileUploadStatus(Long userId, String fileName);
387
388         /**
389          * Fetch the file body with the specified version number.
390          *
391          * @param fileId the ID of the file header
392          * @param version the version number
393          * @return the file body
394          * @throws ObjectNotFoundException if the file body was not found
395          */
396         public FileBody getFileVersion(Long fileId, int version) throws ObjectNotFoundException;
397
398         /**
399          * Update accounting info for given user.
400          * Adds bandwidth used to appropriate time period bucket.
401          * Bucket is created if needed.
402          *
403          * @param user The user to update
404          * @param date Date of transaction
405          * @param bandwidthDiff Bandwidth used; positive for addition,
406          * negative for subtraction (e.g. to rollback)
407          */
408         public void updateAccounting(User user, Date date, long bandwidthDiff);
409
410         /**
411          * Retrieves available user classes.
412          *
413          */
414         public List<UserClass> getUserClasses();
415
416         /**
417          * Find the invite for the specified invitation code.
418          *
419          * @param code the invitation code
420          * @return the Invitation or null if not found
421          */
422         public Invitation findInvite(String code);
423
424         /**
425          * Retrieve the user class for coupon-bearing users.
426          */
427         public UserClass findCouponUserClass();
428
429         /**
430          * Gets the user count.
431          *
432          * @param userClass the user class to use or null to retrieve system statistics
433          * @return the user count
434          */
435         public Long getUserCount(UserClass userClass);
436
437         /**
438          * Gets the file count.
439          *
440          * @param userClass the user class to use or null to retrieve system statistics
441          * @return the file count
442          */
443         public Long getFileCount(UserClass userClass);
444
445         /**
446          * Gets the file size.
447          *
448          * @param userClass the user class to use or null to retrieve system statistics
449          * @return the file size
450          */
451         public Long getFileSize(UserClass userClass);
452
453         public List<User> getUsersByLastLogin(Date lastLoginDate);
454
455         public List<User> getUsersByLastLogin(Date lastLoginDate, int firstResult, int maxResult);
456
457         public Long getCountUsersByLastLogin(Date lastLoginDate);
458
459         public List<User> getInactiveUsers();
460
461         public List<FileHeader> searchFileByFilename(String filename);
462
463         public Long getBandwithUsed(UserClass userClass, Date date);
464
465         public List<AccountingInfo> getAccountingInfo(User user);
466
467         public AccountingInfo getAccountingInfo(User user, Date date);
468
469         /**
470          * Returns a list of files of user with permissions for specified group
471          *
472          * @param userId the ID of the User
473          * @return the list of shared root files
474          * @param groupId
475          * @throws ObjectNotFoundException if the user cannot be found
476          */
477         public List<FileHeader> getFilesPermittedForGroup(Long userId, Long groupId) throws ObjectNotFoundException;
478
479     /**
480      * Gets a file with tags initialized, cause indexing does not always run within a transaction (e.g. during rebuild)
481      * 
482      * @param id
483      * @return
484      * @throws ObjectNotFoundException
485      */
486     public FileHeader getFileForIndexing(Long id) throws ObjectNotFoundException;
487
488         /**
489          * @param userId
490          * @return
491          */
492         List<FileHeader> getSharingFilesForUser(Long userId);
493
494         /**
495          * @param userId
496          * @return
497          */
498         List<Folder> getSharingFoldersForUser(Long userId);
499
500         /**
501          * @param userId
502          * @return
503          */
504         List<Group> getGroupsContainingUser(Long userId);
505
506         /**
507          * @param userId
508          * @return
509          */
510         List<FileUploadStatus> getUploadStatus(Long userId);
511
512         /**
513          * @param userId
514          * @return
515          */
516         int deletePermissionsNotCorrespondingToFilesAndFolders(Long userId);
517         
518         /**
519          * Returns a list of the top two entries related to the date that a user logged in the service. 
520          * The first entry is related to the current session user login 
521          * and the latter is related to the user's last login
522          *  
523          * @param userId
524          * @return a list of last user login and the current session user login
525          */
526         public List<UserLogin> getLoginsForUser (Long userId);
527
528         /**
529          * Returns a list of all entries related to the date that a user logged in the service. 
530          *  
531          * @param userId
532          * @return a list of last user login and the current session user login
533          */
534         public List<UserLogin> getAllLoginsForUser (Long userId);
535
536         /**
537          * @param username
538          * @return
539          */
540         User getUserByUserName(String username);
541
542         /**
543          * @param id
544          * @return
545          */
546         FileLock getLockById(String id);
547
548         /**
549          * @param tokenId
550          * @return
551          */
552         FileLock getLockByToken(String tokenId);
553
554         /**
555          * @param lock
556          */
557         void removeLock(FileLock lock);
558
559         /**
560          * @param lock
561          * @return
562          */
563         FileLock saveOrUpdateLock(FileLock lock);
564
565         /**
566          * @param lock
567          * @return
568          */
569         WebDavNonce saveOrUpdateWebDavNonce(WebDavNonce lock);
570
571         /**
572          * @param lock
573          */
574         void removeWebDavNonce(WebDavNonce lock);
575
576         /**
577          * @param tokenId
578          * @return
579          */
580         WebDavNonce getWebDavNonce(String tokenId);
581
582 }