root / src / gr / ebs / gss / server / ejb / AdminAPIBean.java @ 8dcdfa8d
History | View | Annotate | Download (20.5 kB)
1 |
/*
|
---|---|
2 |
* Copyright 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.common.exceptions.InsufficientPermissionsException; |
22 |
import gr.ebs.gss.common.exceptions.ObjectNotFoundException; |
23 |
import gr.ebs.gss.server.domain.AccountingInfo; |
24 |
import gr.ebs.gss.server.domain.AuditInfo; |
25 |
import gr.ebs.gss.server.domain.FileBody; |
26 |
import gr.ebs.gss.server.domain.FileHeader; |
27 |
import gr.ebs.gss.server.domain.FileUploadStatus; |
28 |
import gr.ebs.gss.server.domain.Folder; |
29 |
import gr.ebs.gss.server.domain.Group; |
30 |
import gr.ebs.gss.server.domain.Permission; |
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.common.dto.FileBodyDTO; |
35 |
import gr.ebs.gss.common.dto.FileHeaderDTO; |
36 |
import gr.ebs.gss.common.dto.FolderDTO; |
37 |
import gr.ebs.gss.common.dto.GroupDTO; |
38 |
import gr.ebs.gss.common.dto.PermissionDTO; |
39 |
import gr.ebs.gss.common.dto.StatsDTO; |
40 |
import gr.ebs.gss.common.dto.SystemStatsDTO; |
41 |
import gr.ebs.gss.common.dto.UserClassDTO; |
42 |
import gr.ebs.gss.common.dto.UserDTO; |
43 |
|
44 |
import java.util.ArrayList; |
45 |
import java.util.Calendar; |
46 |
import java.util.Date; |
47 |
import java.util.Iterator; |
48 |
import java.util.LinkedHashSet; |
49 |
import java.util.LinkedList; |
50 |
import java.util.List; |
51 |
import java.util.Set; |
52 |
import java.util.StringTokenizer; |
53 |
|
54 |
import javax.ejb.EJB; |
55 |
import javax.ejb.Stateless; |
56 |
import javax.jms.Connection; |
57 |
import javax.jms.ConnectionFactory; |
58 |
import javax.jms.JMSException; |
59 |
import javax.jms.MapMessage; |
60 |
import javax.jms.MessageProducer; |
61 |
import javax.jms.Queue; |
62 |
import javax.jms.QueueConnectionFactory; |
63 |
import javax.jms.Session; |
64 |
import javax.naming.Context; |
65 |
import javax.naming.InitialContext; |
66 |
import javax.naming.NamingException; |
67 |
|
68 |
import org.apache.commons.lang.StringUtils; |
69 |
import org.apache.commons.logging.Log; |
70 |
import org.apache.commons.logging.LogFactory; |
71 |
|
72 |
/**
|
73 |
* @author kman
|
74 |
*/
|
75 |
@Stateless
|
76 |
public class AdminAPIBean implements AdminAPI { |
77 |
/**
|
78 |
* Injected reference to the ExternalAPI service.
|
79 |
*/
|
80 |
@EJB
|
81 |
private ExternalAPI api;
|
82 |
|
83 |
/**
|
84 |
* The logger.
|
85 |
*/
|
86 |
private static Log logger = LogFactory.getLog(AdminAPIBean.class); |
87 |
/**
|
88 |
* Injected reference to the GSSDAO data access facade.
|
89 |
*/
|
90 |
@EJB
|
91 |
private GSSDAO dao;
|
92 |
|
93 |
@Override
|
94 |
public FileHeaderDTO getFile(String uri) throws ObjectNotFoundException { |
95 |
if (uri == null) |
96 |
throw new ObjectNotFoundException("No uri specified"); |
97 |
|
98 |
List<String> pathElements = new ArrayList<String>(); |
99 |
StringTokenizer st = new StringTokenizer(uri, "/"); |
100 |
String username = st.nextToken();
|
101 |
st.nextToken(); |
102 |
while (st.hasMoreTokens())
|
103 |
pathElements.add(st.nextToken()); |
104 |
if (pathElements.size() < 1) |
105 |
throw new ObjectNotFoundException("No file found"); |
106 |
User owner = dao.getUser(username); |
107 |
// Store the last element, since it requires special handling.
|
108 |
String lastElement = pathElements.remove(pathElements.size() - 1); |
109 |
FolderDTO cursor = getRootFolder(owner.getId()); |
110 |
// Traverse and verify the specified folder path.
|
111 |
for (String pathElement : pathElements) { |
112 |
cursor = getFolder(cursor.getId(), pathElement); |
113 |
if (cursor.isDeleted())
|
114 |
throw new ObjectNotFoundException("Folder " + cursor.getPath() + " not found"); |
115 |
} |
116 |
FileHeaderDTO file = getFile(cursor.getId(), lastElement); |
117 |
return file;
|
118 |
} |
119 |
|
120 |
@Override
|
121 |
public List<FileHeaderDTO> getFiles(String uri) throws ObjectNotFoundException, InsufficientPermissionsException { |
122 |
if (uri == null) |
123 |
throw new ObjectNotFoundException("No uri specified"); |
124 |
List<FileHeaderDTO> res = new ArrayList<FileHeaderDTO>(); |
125 |
List<String> pathElements = new ArrayList<String>(); |
126 |
StringTokenizer st = new StringTokenizer(uri, "/"); |
127 |
if(st.countTokens()<2) |
128 |
return res;
|
129 |
String username = st.nextToken();
|
130 |
st.nextToken(); |
131 |
User owner = dao.getUser(username); |
132 |
while (st.hasMoreTokens())
|
133 |
pathElements.add(st.nextToken()); |
134 |
if (pathElements.size() < 1){ |
135 |
|
136 |
FolderDTO folder = getRootFolder(owner.getId()); |
137 |
res.addAll(getFiles(folder.getOwner().getId(), folder.getId(), false));
|
138 |
return res;
|
139 |
} |
140 |
|
141 |
// Store the last element, since it requires special handling.
|
142 |
String lastElement = pathElements.remove(pathElements.size() - 1); |
143 |
FolderDTO cursor = getRootFolder(owner.getId()); |
144 |
// Traverse and verify the specified folder path.
|
145 |
for (String pathElement : pathElements) { |
146 |
cursor = getFolder(cursor.getId(), pathElement); |
147 |
if (cursor.isDeleted())
|
148 |
throw new ObjectNotFoundException("Folder " + cursor.getPath() + " not found"); |
149 |
} |
150 |
try {
|
151 |
FileHeaderDTO file = getFile(cursor.getId(), lastElement); |
152 |
res.add(file); |
153 |
} catch (ObjectNotFoundException e) {
|
154 |
// Perhaps the requested resource is not a file, so
|
155 |
// check for folders as well.
|
156 |
FolderDTO folder = getFolder(cursor.getId(), lastElement); |
157 |
res.addAll(getFiles(folder.getOwner().getId(), folder.getId(), false));
|
158 |
|
159 |
} |
160 |
|
161 |
return res;
|
162 |
} |
163 |
|
164 |
private FolderDTO getFolder(Long parentId, String name) throws ObjectNotFoundException { |
165 |
if (parentId == null) |
166 |
throw new ObjectNotFoundException("No parent folder specified"); |
167 |
if (StringUtils.isEmpty(name))
|
168 |
throw new ObjectNotFoundException("No folder specified"); |
169 |
|
170 |
Folder folder = dao.getFolder(parentId, name); |
171 |
return folder.getDTO();
|
172 |
} |
173 |
|
174 |
private FileHeaderDTO getFile(Long folderId, String name) throws ObjectNotFoundException { |
175 |
if (folderId == null) |
176 |
throw new ObjectNotFoundException("No parent folder specified"); |
177 |
if (StringUtils.isEmpty(name))
|
178 |
throw new ObjectNotFoundException("No file specified"); |
179 |
|
180 |
FileHeader file = dao.getFile(folderId, name); |
181 |
FileHeaderDTO dto = file.getDTO(); |
182 |
Set<Permission> perms = file.getPermissions(); |
183 |
Set<PermissionDTO> result = new LinkedHashSet<PermissionDTO>(); |
184 |
for (Permission perm : perms) |
185 |
if (perm.getUser() != null && perm.getUser().getId().equals(file.getOwner().getId())) |
186 |
result.add(perm.getDTO()); |
187 |
for (Permission perm : perms) |
188 |
if (perm.getUser() != null && perm.getUser().getId().equals(file.getOwner().getId())) { |
189 |
} else
|
190 |
result.add(perm.getDTO()); |
191 |
dto.setPermissions(result); |
192 |
return dto;
|
193 |
} |
194 |
@Override
|
195 |
public FileHeaderDTO getFile(Long fileId) throws ObjectNotFoundException { |
196 |
FileHeader file = dao.getEntityById(FileHeader.class, fileId); |
197 |
FileHeaderDTO dto = file.getDTO(); |
198 |
Set<Permission> perms = file.getPermissions(); |
199 |
Set<PermissionDTO> result = new LinkedHashSet<PermissionDTO>(); |
200 |
for (Permission perm : perms) |
201 |
if (perm.getUser() != null && perm.getUser().getId().equals(file.getOwner().getId())) |
202 |
result.add(perm.getDTO()); |
203 |
for (Permission perm : perms) |
204 |
if (perm.getUser() != null && perm.getUser().getId().equals(file.getOwner().getId())) { |
205 |
} else
|
206 |
result.add(perm.getDTO()); |
207 |
dto.setPermissions(result); |
208 |
return dto;
|
209 |
} |
210 |
|
211 |
@Override
|
212 |
public UserDTO getUser(Long userId) throws ObjectNotFoundException { |
213 |
return api.getUserDTO(userId);
|
214 |
} |
215 |
|
216 |
@Override
|
217 |
public StatsDTO getUserStatistics(Long userId) throws ObjectNotFoundException { |
218 |
StatsDTO stats = api.getUserStatistics(userId); |
219 |
User user = dao.getEntityById(User.class, userId); |
220 |
AccountingInfo info = dao.getAccountingInfo(user, new Date()); |
221 |
stats.setBandwithQuotaUsed(info.getBandwidthUsed()); |
222 |
return stats;
|
223 |
} |
224 |
|
225 |
@Override
|
226 |
public List<UserDTO> searchUsers(String query) throws ObjectNotFoundException { |
227 |
List<User> users = dao.getUsersByUserNameOrEmailLike(query);
|
228 |
List<UserDTO> result = new ArrayList<UserDTO>(); |
229 |
for (User u : users){
|
230 |
UserDTO tempDTO = u.getDTO(); |
231 |
try {
|
232 |
List<UserLogin> userLogins = api.getLastUserLogins(u.getId());
|
233 |
tempDTO.setCurrentLoginDate(userLogins.get(0).getLoginDate());
|
234 |
tempDTO.setLastLoginDate(userLogins.get(1).getLoginDate());
|
235 |
} |
236 |
catch (ObjectNotFoundException e) {//Do nothing |
237 |
} |
238 |
result.add(tempDTO); |
239 |
} |
240 |
return result;
|
241 |
} |
242 |
|
243 |
@Override
|
244 |
public UserDTO getUser(String username) throws ObjectNotFoundException{ |
245 |
User u = dao.getUser(username); |
246 |
if(u!=null){ |
247 |
UserDTO tempDTO = u.getDTO(); |
248 |
try {
|
249 |
List<UserLogin> userLogins = api.getLastUserLogins(u.getId());
|
250 |
tempDTO.setCurrentLoginDate(userLogins.get(0).getLoginDate());
|
251 |
tempDTO.setLastLoginDate(userLogins.get(1).getLoginDate());
|
252 |
} |
253 |
catch (ObjectNotFoundException e) {//Do nothing |
254 |
} |
255 |
return tempDTO;
|
256 |
|
257 |
} |
258 |
return null; |
259 |
} |
260 |
@Override
|
261 |
public void toggleActiveUser(Long userId) throws ObjectNotFoundException { |
262 |
User user = dao.getEntityById(User.class, userId); |
263 |
user.setActive(!user.isActive()); |
264 |
dao.update(user); |
265 |
} |
266 |
|
267 |
@Override
|
268 |
public void setFilePermissions(String uri, Set<PermissionDTO> permissions) |
269 |
throws ObjectNotFoundException {
|
270 |
FileHeaderDTO filedto = getFile(uri); |
271 |
FileHeader file = dao.getEntityById(FileHeader.class, filedto.getId()); |
272 |
if (permissions != null && !permissions.isEmpty()) { |
273 |
// Send e-mails to the users that are granted new permissions on the file
|
274 |
// Delete previous entries.
|
275 |
for (Permission perm: file.getPermissions()) |
276 |
dao.delete(perm); |
277 |
file.getPermissions().clear(); |
278 |
for (PermissionDTO dto : permissions) {
|
279 |
//if (dto.getUser()!=null && dto.getUser().getId().equals(file.getOwner().getId()) && (!dto.hasRead() || !dto.hasWrite() || !dto.hasModifyACL()))
|
280 |
//throw new InsufficientPermissionsException("Can't remove permissions from owner");
|
281 |
// Don't include 'empty' permission.
|
282 |
if (!dto.getRead() && !dto.getWrite() && !dto.getModifyACL()) continue; |
283 |
file.addPermission(getPermission(dto)); |
284 |
} |
285 |
dao.flush(); |
286 |
} |
287 |
} |
288 |
|
289 |
private Permission getPermission(PermissionDTO dto) throws ObjectNotFoundException { |
290 |
Permission res = new Permission(); |
291 |
if (dto.getGroup() != null) |
292 |
res.setGroup(dao.getEntityById(Group.class, dto.getGroup().getId()));
|
293 |
else if (dto.getUser() != null) |
294 |
if (dto.getUser().getId() == null) |
295 |
res.setUser(dao.getUser(dto.getUser().getUsername())); |
296 |
else
|
297 |
res.setUser(dao.getEntityById(User.class, dto.getUser().getId())); |
298 |
res.setRead(dto.hasRead()); |
299 |
res.setWrite(dto.hasWrite()); |
300 |
res.setModifyACL(dto.hasModifyACL()); |
301 |
return res;
|
302 |
} |
303 |
|
304 |
@Override
|
305 |
public SystemStatsDTO getSystemStatistics() {
|
306 |
SystemStatsDTO statistics = new SystemStatsDTO();
|
307 |
List<UserClass> uclasses = dao.getUserClasses();
|
308 |
for (UserClass u : uclasses){
|
309 |
UserClassDTO dto = u.getDTOWithoutUsers(); |
310 |
SystemStatsDTO stats = new SystemStatsDTO();
|
311 |
stats.setFileCount(dao.getFileCount(u)); |
312 |
stats.setFileSize(dao.getFileSize(u)); |
313 |
stats.setUserCount(dao.getUserCount(u)); |
314 |
stats.setBandwithUsed(dao.getBandwithUsed(u, new Date())); |
315 |
dto.setStatistics(stats); |
316 |
statistics.getUserClasses().add(dto); |
317 |
|
318 |
} |
319 |
Calendar now = Calendar.getInstance(); |
320 |
now.add(Calendar.DAY_OF_MONTH, -7); |
321 |
Date week = now.getTime();
|
322 |
now=Calendar.getInstance();
|
323 |
now.add(Calendar.MONTH, -1); |
324 |
Date month = now.getTime();
|
325 |
statistics.setLastMonthUsers(dao.getCountUsersByLastLogin(month)); |
326 |
statistics.setLastWeekUsers(dao.getCountUsersByLastLogin(week)); |
327 |
statistics.setFileCount(dao.getFileCount((UserClass)null));
|
328 |
statistics.setFileSize(dao.getFileSize((UserClass)null));
|
329 |
statistics.setUserCount(dao.getUserCount((UserClass)null));
|
330 |
statistics.setBandwithUsed(dao.getBandwithUsed(null, new Date())); |
331 |
return statistics;
|
332 |
} |
333 |
|
334 |
@Override
|
335 |
public List<UserDTO> getLastLoggedInUsers(Date lastLoginDate) { |
336 |
List<User> users = dao.getUsersByLastLogin(lastLoginDate);
|
337 |
List<UserDTO> result = new ArrayList<UserDTO>(); |
338 |
for (User u : users){
|
339 |
UserDTO tempDTO = u.getDTO(); |
340 |
List<UserLogin> userLogins = dao.getLoginsForUser(u.getId());
|
341 |
tempDTO.setCurrentLoginDate(userLogins.get(0).getLoginDate());
|
342 |
tempDTO.setLastLoginDate(userLogins.get(1).getLoginDate());
|
343 |
result.add(tempDTO); |
344 |
} |
345 |
return result;
|
346 |
} |
347 |
|
348 |
@Override
|
349 |
public List<FileBodyDTO> getVersions(Long userId, Long fileId) throws ObjectNotFoundException, InsufficientPermissionsException { |
350 |
if (userId == null) |
351 |
throw new ObjectNotFoundException("No user specified"); |
352 |
if (fileId == null) |
353 |
throw new ObjectNotFoundException("No file specified"); |
354 |
User user = dao.getEntityById(User.class, userId); |
355 |
FileHeader header = dao.getEntityById(FileHeader.class, fileId); |
356 |
List<FileBodyDTO> result = new LinkedList<FileBodyDTO>(); |
357 |
for(int i = header.getBodies().size()-1 ; i>=0; i--) |
358 |
result.add(header.getBodies().get(i).getDTO()); |
359 |
return result;
|
360 |
} |
361 |
@Override
|
362 |
public List<UserDTO> getUsersWaitingActivation(){ |
363 |
List<User> users = dao.getInactiveUsers();
|
364 |
List<UserDTO> result = new ArrayList<UserDTO>(); |
365 |
for(User u :users){
|
366 |
UserDTO tempDTO = u.getDTO(); |
367 |
List<UserLogin> userLogins = dao.getLoginsForUser(u.getId());
|
368 |
tempDTO.setCurrentLoginDate(userLogins.get(0).getLoginDate());
|
369 |
tempDTO.setLastLoginDate(userLogins.get(1).getLoginDate());
|
370 |
result.add(tempDTO); |
371 |
|
372 |
} |
373 |
return result;
|
374 |
} |
375 |
|
376 |
@Override
|
377 |
public void changeUserClass(Long userId, Long userClassId) throws ObjectNotFoundException{ |
378 |
User user = dao.getEntityById(User.class, userId); |
379 |
UserClass userClass = dao.getEntityById(UserClass.class, userClassId); |
380 |
user.setUserClass(userClass); |
381 |
dao.update(user); |
382 |
} |
383 |
|
384 |
@Override
|
385 |
public List<UserClassDTO> getUserClasses(){ |
386 |
List<UserClassDTO> result = new ArrayList<UserClassDTO>(); |
387 |
for(UserClass c : dao.getUserClasses())
|
388 |
result.add(c.getDTOWithoutUsers()); |
389 |
return result;
|
390 |
} |
391 |
|
392 |
@Override
|
393 |
public void saveOrUpdateUserClass(UserClassDTO dto) throws ObjectNotFoundException{ |
394 |
UserClass uclass; |
395 |
if(dto.getId()!=null) |
396 |
uclass = dao.getEntityById(UserClass.class, dto.getId()); |
397 |
else
|
398 |
uclass = new UserClass();
|
399 |
uclass.setName(dto.getName()); |
400 |
uclass.setQuota(dto.getQuota()); |
401 |
if(dto.getId()!=null) |
402 |
dao.update(uclass); |
403 |
else
|
404 |
dao.create(uclass); |
405 |
dao.flush(); |
406 |
|
407 |
} |
408 |
|
409 |
@Override
|
410 |
public void removeUserClass(UserClassDTO dto) throws ObjectNotFoundException{ |
411 |
UserClass uclass = dao.getEntityById(UserClass.class, dto.getId()); |
412 |
if(uclass==null) |
413 |
throw new ObjectNotFoundException("User Class not found"); |
414 |
dao.delete(uclass); |
415 |
} |
416 |
|
417 |
@Override
|
418 |
public List<FileHeaderDTO> searchFileByFilename(String fileName){ |
419 |
List<FileHeader> files = dao.searchFileByFilename(fileName);
|
420 |
List<FileHeaderDTO> result = new ArrayList<FileHeaderDTO>(); |
421 |
for(FileHeader h : files)
|
422 |
result.add(h.getDTO()); |
423 |
return result;
|
424 |
} |
425 |
|
426 |
@Override
|
427 |
public void removeUser(Long userId) throws ObjectNotFoundException, InsufficientPermissionsException{ |
428 |
User user = api.getUser(userId); |
429 |
try{
|
430 |
FolderDTO folder = getRootFolder(userId); |
431 |
deleteFolder(userId, folder.getId()); |
432 |
List<GroupDTO> groups = getGroups(userId);
|
433 |
for(GroupDTO group : groups)
|
434 |
api.deleteGroup(userId, group.getId()); |
435 |
} |
436 |
catch(ObjectNotFoundException e){}
|
437 |
List<Folder> otherFolders = dao.getSharingFoldersForUser(userId);
|
438 |
for(Folder f : otherFolders){
|
439 |
Iterator<Permission> pit = f.getPermissions().iterator(); |
440 |
while(pit.hasNext()){
|
441 |
Permission p = pit.next();
|
442 |
if(p.getUser()!=null&&p.getUser().getId().equals(userId)){ |
443 |
pit.remove(); |
444 |
dao.delete(p); |
445 |
} |
446 |
} |
447 |
dao.update(f); |
448 |
} |
449 |
List<FileHeader> otherFiles = dao.getSharingFilesForUser(userId);
|
450 |
for(FileHeader f : otherFiles){
|
451 |
Iterator<Permission> pit = f.getPermissions().iterator(); |
452 |
while(pit.hasNext()){
|
453 |
Permission p = pit.next();
|
454 |
if(p.getUser()!=null&&p.getUser().getId().equals(userId)){ |
455 |
pit.remove(); |
456 |
dao.delete(p); |
457 |
} |
458 |
} |
459 |
dao.update(f); |
460 |
} |
461 |
List<Group> otherGroups = dao.getGroupsContainingUser(userId); |
462 |
for(Group g : otherGroups){ |
463 |
Iterator<User> uit = g.getMembers().iterator();
|
464 |
while(uit.hasNext()){
|
465 |
User u = uit.next(); |
466 |
if(u.getId().equals(userId)){
|
467 |
uit.remove(); |
468 |
} |
469 |
} |
470 |
dao.update(g); |
471 |
} |
472 |
List<AccountingInfo> infos = dao.getAccountingInfo(user);
|
473 |
Iterator<AccountingInfo> it = infos.iterator();
|
474 |
while(it.hasNext()){
|
475 |
AccountingInfo a = it.next(); |
476 |
dao.delete(a); |
477 |
} |
478 |
List<FileUploadStatus> sts = dao.getUploadStatus(userId);
|
479 |
for(FileUploadStatus s : sts)
|
480 |
dao.delete(s); |
481 |
int deleteCount=dao.deletePermissionsNotCorrespondingToFilesAndFolders(userId);
|
482 |
|
483 |
List<UserLogin> allUserLogins = dao.getAllLoginsForUser(userId);
|
484 |
for(UserLogin ul : allUserLogins)
|
485 |
dao.delete(ul); |
486 |
dao.flush(); |
487 |
dao.delete(user); |
488 |
} |
489 |
|
490 |
/**
|
491 |
* Deletes the given folder and all its subfolders and files
|
492 |
* Only the permissions for top folder are checked
|
493 |
*
|
494 |
* @see gr.ebs.gss.server.ejb.ExternalAPI#deleteFolder(java.lang.Long,
|
495 |
* java.lang.Long)
|
496 |
*/
|
497 |
public void deleteFolder(final Long userId, final Long folderId) throws ObjectNotFoundException { |
498 |
// Validate.
|
499 |
if (userId == null) |
500 |
throw new ObjectNotFoundException("No user specified"); |
501 |
if (folderId == null) |
502 |
throw new ObjectNotFoundException("No folder specified"); |
503 |
|
504 |
// Do the actual work.
|
505 |
final Folder folder = dao.getEntityById(Folder.class, folderId);
|
506 |
final Folder parent = folder.getParent();
|
507 |
final User user = dao.getEntityById(User.class, userId);
|
508 |
|
509 |
removeSubfolderFiles(folder); |
510 |
if(parent!=null) |
511 |
parent.removeSubfolder(folder); |
512 |
dao.delete(folder); |
513 |
if(parent!=null) |
514 |
touchParentFolders(parent, user, new Date()); |
515 |
} |
516 |
|
517 |
private void removeSubfolderFiles(Folder folder) { |
518 |
//remove files for all subfolders
|
519 |
for (Folder subfolder:folder.getSubfolders())
|
520 |
removeSubfolderFiles(subfolder); |
521 |
//remove this folder's file bodies (actual files)
|
522 |
for (FileHeader file:folder.getFiles()) {
|
523 |
for (FileBody body:file.getBodies())
|
524 |
api.deleteActualFile(body.getStoredFilePath()); |
525 |
indexFile(file.getId(), true);
|
526 |
} |
527 |
} |
528 |
|
529 |
private void touchParentFolders(Folder folder, User modifiedBy, Date modificationDate) { |
530 |
Folder f = folder; |
531 |
while (f!=null) { |
532 |
AuditInfo ai = f.getAuditInfo(); |
533 |
ai.setModifiedBy(modifiedBy); |
534 |
ai.setModificationDate(modificationDate); |
535 |
f.setAuditInfo(ai); |
536 |
f = f.getParent(); |
537 |
} |
538 |
} |
539 |
|
540 |
public void indexFile(Long fileId, boolean delete) { |
541 |
Connection qConn = null; |
542 |
Session session = null;
|
543 |
MessageProducer sender = null;
|
544 |
try {
|
545 |
Context jndiCtx = new InitialContext(); |
546 |
ConnectionFactory factory = (QueueConnectionFactory) jndiCtx.lookup("java:/JmsXA");
|
547 |
Queue queue = (Queue) jndiCtx.lookup("queue/gss-indexingQueue"); |
548 |
qConn = factory.createConnection(); |
549 |
session = qConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
550 |
sender = session.createProducer(queue); |
551 |
|
552 |
MapMessage map = session.createMapMessage(); |
553 |
map.setObject("id", fileId);
|
554 |
map.setBoolean("delete", delete);
|
555 |
sender.send(map); |
556 |
} |
557 |
catch (NamingException e) { |
558 |
logger.error("Index was not updated: ", e);
|
559 |
} |
560 |
catch (JMSException e) {
|
561 |
logger.error("Index was not updated: ", e);
|
562 |
} |
563 |
finally {
|
564 |
try {
|
565 |
if (sender != null) |
566 |
sender.close(); |
567 |
if (session != null) |
568 |
session.close(); |
569 |
if (qConn != null) |
570 |
qConn.close(); |
571 |
} |
572 |
catch (JMSException e) {
|
573 |
logger.warn(e); |
574 |
} |
575 |
} |
576 |
} |
577 |
|
578 |
/*** MOVE METHODS WITH DTOS FROM ExternalAPIAbean ***/
|
579 |
private FolderDTO getRootFolder(Long userId) throws ObjectNotFoundException { |
580 |
if (userId == null) |
581 |
throw new ObjectNotFoundException("No user specified"); |
582 |
Folder folder = dao.getRootFolder(userId); |
583 |
return folder.getDTO();
|
584 |
} |
585 |
|
586 |
private List<FileHeaderDTO> getFiles(Long userId, Long folderId, boolean ignoreDeleted) |
587 |
throws ObjectNotFoundException, InsufficientPermissionsException {
|
588 |
// Validate.
|
589 |
if (userId == null) |
590 |
throw new ObjectNotFoundException("No user specified"); |
591 |
if (folderId == null) |
592 |
throw new ObjectNotFoundException("No folder specified"); |
593 |
User user = dao.getEntityById(User.class, userId); |
594 |
Folder folder = dao.getEntityById(Folder.class, folderId); |
595 |
if (!folder.hasReadPermission(user))
|
596 |
throw new InsufficientPermissionsException("You don't have the permissions to read this folder"); |
597 |
// Do the actual work.
|
598 |
List<FileHeaderDTO> result = new ArrayList<FileHeaderDTO>(); |
599 |
List<FileHeader> files = dao.getFiles(folderId, userId, ignoreDeleted);
|
600 |
for (FileHeader f : files)
|
601 |
result.add(f.getDTO()); |
602 |
return result;
|
603 |
} |
604 |
|
605 |
private List<GroupDTO> getGroups(final Long userId) throws ObjectNotFoundException { |
606 |
if (userId == null) |
607 |
throw new ObjectNotFoundException("No user specified"); |
608 |
final List<Group> groups = dao.getGroups(userId); |
609 |
final List<GroupDTO> result = new ArrayList<GroupDTO>(); |
610 |
for (final Group g : groups) |
611 |
result.add(g.getDTO()); |
612 |
return result;
|
613 |
} |
614 |
} |