root / src / gr / ebs / gss / server / ejb / AdminAPIBean.java @ 99d0d40c
History | View | Annotate | Download (16.1 kB)
1 | 023f6f1e | Panagiotis Astithas | /*
|
---|---|---|---|
2 | 023f6f1e | Panagiotis Astithas | * Copyright 2010 Electronic Business Systems Ltd.
|
3 | 023f6f1e | Panagiotis Astithas | *
|
4 | 023f6f1e | Panagiotis Astithas | * This file is part of GSS.
|
5 | 023f6f1e | Panagiotis Astithas | *
|
6 | 023f6f1e | Panagiotis Astithas | * GSS is free software: you can redistribute it and/or modify
|
7 | 023f6f1e | Panagiotis Astithas | * it under the terms of the GNU General Public License as published by
|
8 | 023f6f1e | Panagiotis Astithas | * the Free Software Foundation, either version 3 of the License, or
|
9 | 023f6f1e | Panagiotis Astithas | * (at your option) any later version.
|
10 | 023f6f1e | Panagiotis Astithas | *
|
11 | 023f6f1e | Panagiotis Astithas | * GSS is distributed in the hope that it will be useful,
|
12 | 023f6f1e | Panagiotis Astithas | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 023f6f1e | Panagiotis Astithas | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | 023f6f1e | Panagiotis Astithas | * GNU General Public License for more details.
|
15 | 023f6f1e | Panagiotis Astithas | *
|
16 | 023f6f1e | Panagiotis Astithas | * You should have received a copy of the GNU General Public License
|
17 | 023f6f1e | Panagiotis Astithas | * along with GSS. If not, see <http://www.gnu.org/licenses/>.
|
18 | 023f6f1e | Panagiotis Astithas | */
|
19 | 023f6f1e | Panagiotis Astithas | package gr.ebs.gss.server.ejb; |
20 | 023f6f1e | Panagiotis Astithas | |
21 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.client.exceptions.InsufficientPermissionsException; |
22 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.client.exceptions.ObjectNotFoundException; |
23 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.AccountingInfo; |
24 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.AuditInfo; |
25 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.FileBody; |
26 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.FileHeader; |
27 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.Folder; |
28 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.Group; |
29 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.Permission; |
30 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.User; |
31 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.UserClass; |
32 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.dto.FileBodyDTO; |
33 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.dto.FileHeaderDTO; |
34 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.dto.FolderDTO; |
35 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.dto.GroupDTO; |
36 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.dto.PermissionDTO; |
37 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.dto.StatsDTO; |
38 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.dto.SystemStatsDTO; |
39 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.dto.UserClassDTO; |
40 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.server.domain.dto.UserDTO; |
41 | 023f6f1e | Panagiotis Astithas | |
42 | 023f6f1e | Panagiotis Astithas | import java.util.ArrayList; |
43 | 023f6f1e | Panagiotis Astithas | import java.util.Calendar; |
44 | 023f6f1e | Panagiotis Astithas | import java.util.Date; |
45 | 023f6f1e | Panagiotis Astithas | import java.util.Iterator; |
46 | 023f6f1e | Panagiotis Astithas | import java.util.LinkedHashSet; |
47 | 023f6f1e | Panagiotis Astithas | import java.util.List; |
48 | 023f6f1e | Panagiotis Astithas | import java.util.Set; |
49 | 023f6f1e | Panagiotis Astithas | import java.util.StringTokenizer; |
50 | 023f6f1e | Panagiotis Astithas | |
51 | 023f6f1e | Panagiotis Astithas | import javax.ejb.EJB; |
52 | 023f6f1e | Panagiotis Astithas | import javax.ejb.Stateless; |
53 | 023f6f1e | Panagiotis Astithas | import javax.jms.Connection; |
54 | 023f6f1e | Panagiotis Astithas | import javax.jms.ConnectionFactory; |
55 | 023f6f1e | Panagiotis Astithas | import javax.jms.JMSException; |
56 | 023f6f1e | Panagiotis Astithas | import javax.jms.MapMessage; |
57 | 023f6f1e | Panagiotis Astithas | import javax.jms.MessageProducer; |
58 | 023f6f1e | Panagiotis Astithas | import javax.jms.Queue; |
59 | 023f6f1e | Panagiotis Astithas | import javax.jms.QueueConnectionFactory; |
60 | 023f6f1e | Panagiotis Astithas | import javax.jms.Session; |
61 | 023f6f1e | Panagiotis Astithas | import javax.naming.Context; |
62 | 023f6f1e | Panagiotis Astithas | import javax.naming.InitialContext; |
63 | 023f6f1e | Panagiotis Astithas | import javax.naming.NamingException; |
64 | 023f6f1e | Panagiotis Astithas | |
65 | 023f6f1e | Panagiotis Astithas | import org.apache.commons.lang.StringUtils; |
66 | 023f6f1e | Panagiotis Astithas | import org.apache.commons.logging.Log; |
67 | 023f6f1e | Panagiotis Astithas | import org.apache.commons.logging.LogFactory; |
68 | 023f6f1e | Panagiotis Astithas | |
69 | 023f6f1e | Panagiotis Astithas | /**
|
70 | 023f6f1e | Panagiotis Astithas | * @author kman
|
71 | 023f6f1e | Panagiotis Astithas | */
|
72 | 023f6f1e | Panagiotis Astithas | @Stateless
|
73 | 023f6f1e | Panagiotis Astithas | public class AdminAPIBean implements AdminAPI { |
74 | 023f6f1e | Panagiotis Astithas | /**
|
75 | 023f6f1e | Panagiotis Astithas | * Injected reference to the ExternalAPI service.
|
76 | 023f6f1e | Panagiotis Astithas | */
|
77 | 023f6f1e | Panagiotis Astithas | @EJB
|
78 | 023f6f1e | Panagiotis Astithas | private ExternalAPI api;
|
79 | 023f6f1e | Panagiotis Astithas | |
80 | 023f6f1e | Panagiotis Astithas | /**
|
81 | 023f6f1e | Panagiotis Astithas | * The logger.
|
82 | 023f6f1e | Panagiotis Astithas | */
|
83 | 023f6f1e | Panagiotis Astithas | private static Log logger = LogFactory.getLog(AdminAPIBean.class); |
84 | 023f6f1e | Panagiotis Astithas | /**
|
85 | 023f6f1e | Panagiotis Astithas | * Injected reference to the GSSDAO data access facade.
|
86 | 023f6f1e | Panagiotis Astithas | */
|
87 | 023f6f1e | Panagiotis Astithas | @EJB
|
88 | 023f6f1e | Panagiotis Astithas | private GSSDAO dao;
|
89 | 023f6f1e | Panagiotis Astithas | |
90 | 023f6f1e | Panagiotis Astithas | @Override
|
91 | 023f6f1e | Panagiotis Astithas | public FileHeaderDTO getFile(String uri) throws ObjectNotFoundException { |
92 | 023f6f1e | Panagiotis Astithas | if (uri == null) |
93 | 023f6f1e | Panagiotis Astithas | throw new ObjectNotFoundException("No uri specified"); |
94 | 023f6f1e | Panagiotis Astithas | |
95 | 023f6f1e | Panagiotis Astithas | List<String> pathElements = new ArrayList<String>(); |
96 | 023f6f1e | Panagiotis Astithas | StringTokenizer st = new StringTokenizer(uri, "/"); |
97 | 023f6f1e | Panagiotis Astithas | String username = st.nextToken();
|
98 | 023f6f1e | Panagiotis Astithas | st.nextToken(); |
99 | 023f6f1e | Panagiotis Astithas | while (st.hasMoreTokens())
|
100 | 023f6f1e | Panagiotis Astithas | pathElements.add(st.nextToken()); |
101 | 023f6f1e | Panagiotis Astithas | if (pathElements.size() < 1) |
102 | 023f6f1e | Panagiotis Astithas | throw new ObjectNotFoundException("No file found"); |
103 | 023f6f1e | Panagiotis Astithas | User owner = dao.getUser(username); |
104 | 023f6f1e | Panagiotis Astithas | // Store the last element, since it requires special handling.
|
105 | 023f6f1e | Panagiotis Astithas | String lastElement = pathElements.remove(pathElements.size() - 1); |
106 | 023f6f1e | Panagiotis Astithas | FolderDTO cursor = api.getRootFolder(owner.getId()); |
107 | 023f6f1e | Panagiotis Astithas | // Traverse and verify the specified folder path.
|
108 | 023f6f1e | Panagiotis Astithas | for (String pathElement : pathElements) { |
109 | 023f6f1e | Panagiotis Astithas | cursor = getFolder(cursor.getId(), pathElement); |
110 | 023f6f1e | Panagiotis Astithas | if (cursor.isDeleted())
|
111 | 023f6f1e | Panagiotis Astithas | throw new ObjectNotFoundException("Folder " + cursor.getPath() + " not found"); |
112 | 023f6f1e | Panagiotis Astithas | } |
113 | 023f6f1e | Panagiotis Astithas | FileHeaderDTO file = getFile(cursor.getId(), lastElement); |
114 | 023f6f1e | Panagiotis Astithas | return file;
|
115 | 023f6f1e | Panagiotis Astithas | } |
116 | 023f6f1e | Panagiotis Astithas | |
117 | 023f6f1e | Panagiotis Astithas | @Override
|
118 | 023f6f1e | Panagiotis Astithas | public List<FileHeaderDTO> getFiles(String uri) throws ObjectNotFoundException, InsufficientPermissionsException { |
119 | 023f6f1e | Panagiotis Astithas | if (uri == null) |
120 | 023f6f1e | Panagiotis Astithas | throw new ObjectNotFoundException("No uri specified"); |
121 | 023f6f1e | Panagiotis Astithas | List<FileHeaderDTO> res = new ArrayList<FileHeaderDTO>(); |
122 | 023f6f1e | Panagiotis Astithas | List<String> pathElements = new ArrayList<String>(); |
123 | 023f6f1e | Panagiotis Astithas | StringTokenizer st = new StringTokenizer(uri, "/"); |
124 | 023f6f1e | Panagiotis Astithas | if(st.countTokens()<2) |
125 | 023f6f1e | Panagiotis Astithas | return res;
|
126 | 023f6f1e | Panagiotis Astithas | String username = st.nextToken();
|
127 | 023f6f1e | Panagiotis Astithas | st.nextToken(); |
128 | 023f6f1e | Panagiotis Astithas | User owner = dao.getUser(username); |
129 | 023f6f1e | Panagiotis Astithas | while (st.hasMoreTokens())
|
130 | 023f6f1e | Panagiotis Astithas | pathElements.add(st.nextToken()); |
131 | 023f6f1e | Panagiotis Astithas | if (pathElements.size() < 1){ |
132 | 023f6f1e | Panagiotis Astithas | |
133 | 023f6f1e | Panagiotis Astithas | FolderDTO folder = api.getRootFolder(owner.getId()); |
134 | 023f6f1e | Panagiotis Astithas | res.addAll(api.getFiles(folder.getOwner().getId(), folder.getId(), false));
|
135 | 023f6f1e | Panagiotis Astithas | return res;
|
136 | 023f6f1e | Panagiotis Astithas | } |
137 | 023f6f1e | Panagiotis Astithas | |
138 | 023f6f1e | Panagiotis Astithas | // Store the last element, since it requires special handling.
|
139 | 023f6f1e | Panagiotis Astithas | String lastElement = pathElements.remove(pathElements.size() - 1); |
140 | 023f6f1e | Panagiotis Astithas | FolderDTO cursor = api.getRootFolder(owner.getId()); |
141 | 023f6f1e | Panagiotis Astithas | // Traverse and verify the specified folder path.
|
142 | 023f6f1e | Panagiotis Astithas | for (String pathElement : pathElements) { |
143 | 023f6f1e | Panagiotis Astithas | cursor = getFolder(cursor.getId(), pathElement); |
144 | 023f6f1e | Panagiotis Astithas | if (cursor.isDeleted())
|
145 | 023f6f1e | Panagiotis Astithas | throw new ObjectNotFoundException("Folder " + cursor.getPath() + " not found"); |
146 | 023f6f1e | Panagiotis Astithas | } |
147 | 023f6f1e | Panagiotis Astithas | try {
|
148 | 023f6f1e | Panagiotis Astithas | FileHeaderDTO file = getFile(cursor.getId(), lastElement); |
149 | 023f6f1e | Panagiotis Astithas | res.add(file); |
150 | 023f6f1e | Panagiotis Astithas | } catch (ObjectNotFoundException e) {
|
151 | 023f6f1e | Panagiotis Astithas | // Perhaps the requested resource is not a file, so
|
152 | 023f6f1e | Panagiotis Astithas | // check for folders as well.
|
153 | 023f6f1e | Panagiotis Astithas | FolderDTO folder = getFolder(cursor.getId(), lastElement); |
154 | 023f6f1e | Panagiotis Astithas | res.addAll(api.getFiles(folder.getOwner().getId(), folder.getId(), false));
|
155 | 023f6f1e | Panagiotis Astithas | |
156 | 023f6f1e | Panagiotis Astithas | } |
157 | 023f6f1e | Panagiotis Astithas | |
158 | 023f6f1e | Panagiotis Astithas | return res;
|
159 | 023f6f1e | Panagiotis Astithas | } |
160 | 023f6f1e | Panagiotis Astithas | |
161 | 023f6f1e | Panagiotis Astithas | private FolderDTO getFolder(Long parentId, String name) throws ObjectNotFoundException { |
162 | 023f6f1e | Panagiotis Astithas | if (parentId == null) |
163 | 023f6f1e | Panagiotis Astithas | throw new ObjectNotFoundException("No parent folder specified"); |
164 | 023f6f1e | Panagiotis Astithas | if (StringUtils.isEmpty(name))
|
165 | 023f6f1e | Panagiotis Astithas | throw new ObjectNotFoundException("No folder specified"); |
166 | 023f6f1e | Panagiotis Astithas | |
167 | 023f6f1e | Panagiotis Astithas | Folder folder = dao.getFolder(parentId, name); |
168 | 023f6f1e | Panagiotis Astithas | return folder.getDTO();
|
169 | 023f6f1e | Panagiotis Astithas | } |
170 | 023f6f1e | Panagiotis Astithas | |
171 | 023f6f1e | Panagiotis Astithas | private FileHeaderDTO getFile(Long folderId, String name) throws ObjectNotFoundException { |
172 | 023f6f1e | Panagiotis Astithas | if (folderId == null) |
173 | 023f6f1e | Panagiotis Astithas | throw new ObjectNotFoundException("No parent folder specified"); |
174 | 023f6f1e | Panagiotis Astithas | if (StringUtils.isEmpty(name))
|
175 | 023f6f1e | Panagiotis Astithas | throw new ObjectNotFoundException("No file specified"); |
176 | 023f6f1e | Panagiotis Astithas | |
177 | 023f6f1e | Panagiotis Astithas | FileHeader file = dao.getFile(folderId, name); |
178 | 023f6f1e | Panagiotis Astithas | FileHeaderDTO dto = file.getDTO(); |
179 | 023f6f1e | Panagiotis Astithas | Set<Permission> perms = file.getPermissions(); |
180 | 023f6f1e | Panagiotis Astithas | Set<PermissionDTO> result = new LinkedHashSet<PermissionDTO>(); |
181 | 023f6f1e | Panagiotis Astithas | for (Permission perm : perms) |
182 | 023f6f1e | Panagiotis Astithas | if (perm.getUser() != null && perm.getUser().getId().equals(file.getOwner().getId())) |
183 | 023f6f1e | Panagiotis Astithas | result.add(perm.getDTO()); |
184 | 023f6f1e | Panagiotis Astithas | for (Permission perm : perms) |
185 | 023f6f1e | Panagiotis Astithas | if (perm.getUser() != null && perm.getUser().getId().equals(file.getOwner().getId())) { |
186 | 023f6f1e | Panagiotis Astithas | } else
|
187 | 023f6f1e | Panagiotis Astithas | result.add(perm.getDTO()); |
188 | 023f6f1e | Panagiotis Astithas | dto.setPermissions(result); |
189 | 023f6f1e | Panagiotis Astithas | return dto;
|
190 | 023f6f1e | Panagiotis Astithas | } |
191 | 023f6f1e | Panagiotis Astithas | @Override
|
192 | 023f6f1e | Panagiotis Astithas | public FileHeaderDTO getFile(Long fileId) throws ObjectNotFoundException { |
193 | 023f6f1e | Panagiotis Astithas | FileHeader file = dao.getEntityById(FileHeader.class, fileId); |
194 | 023f6f1e | Panagiotis Astithas | FileHeaderDTO dto = file.getDTO(); |
195 | 023f6f1e | Panagiotis Astithas | Set<Permission> perms = file.getPermissions(); |
196 | 023f6f1e | Panagiotis Astithas | Set<PermissionDTO> result = new LinkedHashSet<PermissionDTO>(); |
197 | 023f6f1e | Panagiotis Astithas | for (Permission perm : perms) |
198 | 023f6f1e | Panagiotis Astithas | if (perm.getUser() != null && perm.getUser().getId().equals(file.getOwner().getId())) |
199 | 023f6f1e | Panagiotis Astithas | result.add(perm.getDTO()); |
200 | 023f6f1e | Panagiotis Astithas | for (Permission perm : perms) |
201 | 023f6f1e | Panagiotis Astithas | if (perm.getUser() != null && perm.getUser().getId().equals(file.getOwner().getId())) { |
202 | 023f6f1e | Panagiotis Astithas | } else
|
203 | 023f6f1e | Panagiotis Astithas | result.add(perm.getDTO()); |
204 | 023f6f1e | Panagiotis Astithas | dto.setPermissions(result); |
205 | 023f6f1e | Panagiotis Astithas | return dto;
|
206 | 023f6f1e | Panagiotis Astithas | } |
207 | 023f6f1e | Panagiotis Astithas | |
208 | 023f6f1e | Panagiotis Astithas | @Override
|
209 | 023f6f1e | Panagiotis Astithas | public UserDTO getUser(Long userId) throws ObjectNotFoundException { |
210 | 023f6f1e | Panagiotis Astithas | return api.getUserDTO(userId);
|
211 | 023f6f1e | Panagiotis Astithas | } |
212 | 023f6f1e | Panagiotis Astithas | |
213 | 023f6f1e | Panagiotis Astithas | @Override
|
214 | 023f6f1e | Panagiotis Astithas | public StatsDTO getUserStatistics(Long userId) throws ObjectNotFoundException { |
215 | 023f6f1e | Panagiotis Astithas | StatsDTO stats = api.getUserStatistics(userId); |
216 | 023f6f1e | Panagiotis Astithas | User user = dao.getEntityById(User.class, userId); |
217 | 023f6f1e | Panagiotis Astithas | AccountingInfo info = dao.getAccountingInfo(user, new Date()); |
218 | 023f6f1e | Panagiotis Astithas | stats.setBandwithQuotaUsed(info.getBandwidthUsed()); |
219 | 023f6f1e | Panagiotis Astithas | return stats;
|
220 | 023f6f1e | Panagiotis Astithas | } |
221 | 023f6f1e | Panagiotis Astithas | |
222 | 023f6f1e | Panagiotis Astithas | @Override
|
223 | 023f6f1e | Panagiotis Astithas | public List<UserDTO> searchUsers(String query) { |
224 | 023f6f1e | Panagiotis Astithas | List<User> users = dao.getUsersByUserNameOrEmailLike(query);
|
225 | 023f6f1e | Panagiotis Astithas | List<UserDTO> result = new ArrayList<UserDTO>(); |
226 | 023f6f1e | Panagiotis Astithas | for (User u : users)
|
227 | 023f6f1e | Panagiotis Astithas | result.add(u.getDTO()); |
228 | 023f6f1e | Panagiotis Astithas | return result;
|
229 | 023f6f1e | Panagiotis Astithas | } |
230 | 023f6f1e | Panagiotis Astithas | |
231 | 023f6f1e | Panagiotis Astithas | @Override
|
232 | 023f6f1e | Panagiotis Astithas | public UserDTO getUser(String username) throws ObjectNotFoundException{ |
233 | 023f6f1e | Panagiotis Astithas | User u = dao.getUser(username); |
234 | 023f6f1e | Panagiotis Astithas | if(u!=null) |
235 | 023f6f1e | Panagiotis Astithas | return u.getDTO();
|
236 | 023f6f1e | Panagiotis Astithas | return null; |
237 | 023f6f1e | Panagiotis Astithas | } |
238 | 023f6f1e | Panagiotis Astithas | @Override
|
239 | 023f6f1e | Panagiotis Astithas | public void toggleActiveUser(Long userId) throws ObjectNotFoundException { |
240 | 023f6f1e | Panagiotis Astithas | User user = dao.getEntityById(User.class, userId); |
241 | 023f6f1e | Panagiotis Astithas | user.setActive(!user.isActive()); |
242 | 023f6f1e | Panagiotis Astithas | dao.update(user); |
243 | 023f6f1e | Panagiotis Astithas | } |
244 | 023f6f1e | Panagiotis Astithas | |
245 | 023f6f1e | Panagiotis Astithas | @Override
|
246 | 023f6f1e | Panagiotis Astithas | public void setFilePermissions(String uri, Set<PermissionDTO> permissions) |
247 | 023f6f1e | Panagiotis Astithas | throws ObjectNotFoundException {
|
248 | 023f6f1e | Panagiotis Astithas | FileHeaderDTO filedto = getFile(uri); |
249 | 023f6f1e | Panagiotis Astithas | FileHeader file = dao.getEntityById(FileHeader.class, filedto.getId()); |
250 | 023f6f1e | Panagiotis Astithas | if (permissions != null && !permissions.isEmpty()) { |
251 | 023f6f1e | Panagiotis Astithas | // Send e-mails to the users that are granted new permissions on the file
|
252 | 023f6f1e | Panagiotis Astithas | // Delete previous entries.
|
253 | 023f6f1e | Panagiotis Astithas | for (Permission perm: file.getPermissions()) |
254 | 023f6f1e | Panagiotis Astithas | dao.delete(perm); |
255 | 023f6f1e | Panagiotis Astithas | file.getPermissions().clear(); |
256 | 023f6f1e | Panagiotis Astithas | for (PermissionDTO dto : permissions) {
|
257 | 023f6f1e | Panagiotis Astithas | //if (dto.getUser()!=null && dto.getUser().getId().equals(file.getOwner().getId()) && (!dto.hasRead() || !dto.hasWrite() || !dto.hasModifyACL()))
|
258 | 023f6f1e | Panagiotis Astithas | //throw new InsufficientPermissionsException("Can't remove permissions from owner");
|
259 | 023f6f1e | Panagiotis Astithas | // Don't include 'empty' permission.
|
260 | 023f6f1e | Panagiotis Astithas | if (!dto.getRead() && !dto.getWrite() && !dto.getModifyACL()) continue; |
261 | 023f6f1e | Panagiotis Astithas | file.addPermission(getPermission(dto)); |
262 | 023f6f1e | Panagiotis Astithas | } |
263 | 023f6f1e | Panagiotis Astithas | dao.flush(); |
264 | 023f6f1e | Panagiotis Astithas | } |
265 | 023f6f1e | Panagiotis Astithas | } |
266 | 023f6f1e | Panagiotis Astithas | |
267 | 023f6f1e | Panagiotis Astithas | private Permission getPermission(PermissionDTO dto) throws ObjectNotFoundException { |
268 | 023f6f1e | Panagiotis Astithas | Permission res = new Permission(); |
269 | 023f6f1e | Panagiotis Astithas | if (dto.getGroup() != null) |
270 | 023f6f1e | Panagiotis Astithas | res.setGroup(dao.getEntityById(Group.class, dto.getGroup().getId()));
|
271 | 023f6f1e | Panagiotis Astithas | else if (dto.getUser() != null) |
272 | 023f6f1e | Panagiotis Astithas | if (dto.getUser().getId() == null) |
273 | 023f6f1e | Panagiotis Astithas | res.setUser(dao.getUser(dto.getUser().getUsername())); |
274 | 023f6f1e | Panagiotis Astithas | else
|
275 | 023f6f1e | Panagiotis Astithas | res.setUser(dao.getEntityById(User.class, dto.getUser().getId())); |
276 | 023f6f1e | Panagiotis Astithas | res.setRead(dto.hasRead()); |
277 | 023f6f1e | Panagiotis Astithas | res.setWrite(dto.hasWrite()); |
278 | 023f6f1e | Panagiotis Astithas | res.setModifyACL(dto.hasModifyACL()); |
279 | 023f6f1e | Panagiotis Astithas | return res;
|
280 | 023f6f1e | Panagiotis Astithas | } |
281 | 023f6f1e | Panagiotis Astithas | |
282 | 023f6f1e | Panagiotis Astithas | @Override
|
283 | 023f6f1e | Panagiotis Astithas | public SystemStatsDTO getSystemStatistics() {
|
284 | 023f6f1e | Panagiotis Astithas | SystemStatsDTO statistics = new SystemStatsDTO();
|
285 | 023f6f1e | Panagiotis Astithas | List<UserClass> uclasses = dao.getUserClasses();
|
286 | 023f6f1e | Panagiotis Astithas | for (UserClass u : uclasses){
|
287 | 023f6f1e | Panagiotis Astithas | UserClassDTO dto = u.getDTOWithoutUsers(); |
288 | 023f6f1e | Panagiotis Astithas | SystemStatsDTO stats = new SystemStatsDTO();
|
289 | 023f6f1e | Panagiotis Astithas | stats.setFileCount(dao.getFileCount(u)); |
290 | 023f6f1e | Panagiotis Astithas | stats.setFileSize(dao.getFileSize(u)); |
291 | 023f6f1e | Panagiotis Astithas | stats.setUserCount(dao.getUserCount(u)); |
292 | 023f6f1e | Panagiotis Astithas | stats.setBandwithUsed(dao.getBandwithUsed(u, new Date())); |
293 | 023f6f1e | Panagiotis Astithas | dto.setStatistics(stats); |
294 | 023f6f1e | Panagiotis Astithas | statistics.getUserClasses().add(dto); |
295 | 023f6f1e | Panagiotis Astithas | |
296 | 023f6f1e | Panagiotis Astithas | } |
297 | 023f6f1e | Panagiotis Astithas | Calendar now = Calendar.getInstance(); |
298 | 023f6f1e | Panagiotis Astithas | now.add(Calendar.DAY_OF_MONTH, -7); |
299 | 023f6f1e | Panagiotis Astithas | Date week = now.getTime();
|
300 | 023f6f1e | Panagiotis Astithas | now=Calendar.getInstance();
|
301 | 023f6f1e | Panagiotis Astithas | now.add(Calendar.MONTH, -1); |
302 | 023f6f1e | Panagiotis Astithas | Date month = now.getTime();
|
303 | 023f6f1e | Panagiotis Astithas | statistics.setLastMonthUsers(dao.getCountUsersByLastLogin(month)); |
304 | 023f6f1e | Panagiotis Astithas | statistics.setLastWeekUsers(dao.getCountUsersByLastLogin(week)); |
305 | 023f6f1e | Panagiotis Astithas | statistics.setFileCount(dao.getFileCount((UserClass)null));
|
306 | 023f6f1e | Panagiotis Astithas | statistics.setFileSize(dao.getFileSize((UserClass)null));
|
307 | 023f6f1e | Panagiotis Astithas | statistics.setUserCount(dao.getUserCount((UserClass)null));
|
308 | 023f6f1e | Panagiotis Astithas | statistics.setBandwithUsed(dao.getBandwithUsed(null, new Date())); |
309 | 023f6f1e | Panagiotis Astithas | return statistics;
|
310 | 023f6f1e | Panagiotis Astithas | } |
311 | 023f6f1e | Panagiotis Astithas | |
312 | 023f6f1e | Panagiotis Astithas | @Override
|
313 | 023f6f1e | Panagiotis Astithas | public List<UserDTO> getLastLoggedInUsers(Date lastLoginDate) { |
314 | 023f6f1e | Panagiotis Astithas | List<User> users = dao.getUsersByLastLogin(lastLoginDate);
|
315 | 023f6f1e | Panagiotis Astithas | List<UserDTO> result = new ArrayList<UserDTO>(); |
316 | 023f6f1e | Panagiotis Astithas | for (User u : users)
|
317 | 023f6f1e | Panagiotis Astithas | result.add(u.getDTO()); |
318 | 023f6f1e | Panagiotis Astithas | return result;
|
319 | 023f6f1e | Panagiotis Astithas | } |
320 | 023f6f1e | Panagiotis Astithas | |
321 | 023f6f1e | Panagiotis Astithas | @Override
|
322 | 023f6f1e | Panagiotis Astithas | public List<FileBodyDTO> getVersions(Long userId, Long fileId) throws ObjectNotFoundException, InsufficientPermissionsException { |
323 | 023f6f1e | Panagiotis Astithas | return api.getVersions(userId, fileId);
|
324 | 023f6f1e | Panagiotis Astithas | } |
325 | 023f6f1e | Panagiotis Astithas | @Override
|
326 | 023f6f1e | Panagiotis Astithas | public List<UserDTO> getUsersWaitingActivation(){ |
327 | 023f6f1e | Panagiotis Astithas | List<User> users = dao.getInactiveUsers();
|
328 | 023f6f1e | Panagiotis Astithas | List<UserDTO> result = new ArrayList<UserDTO>(); |
329 | 023f6f1e | Panagiotis Astithas | for(User u :users)
|
330 | 023f6f1e | Panagiotis Astithas | result.add(u.getDTO()); |
331 | 023f6f1e | Panagiotis Astithas | return result;
|
332 | 023f6f1e | Panagiotis Astithas | } |
333 | 023f6f1e | Panagiotis Astithas | |
334 | 023f6f1e | Panagiotis Astithas | @Override
|
335 | 023f6f1e | Panagiotis Astithas | public void changeUserClass(Long userId, Long userClassId) throws ObjectNotFoundException{ |
336 | 023f6f1e | Panagiotis Astithas | User user = dao.getEntityById(User.class, userId); |
337 | 023f6f1e | Panagiotis Astithas | UserClass userClass = dao.getEntityById(UserClass.class, userClassId); |
338 | 023f6f1e | Panagiotis Astithas | user.setUserClass(userClass); |
339 | 023f6f1e | Panagiotis Astithas | dao.update(user); |
340 | 023f6f1e | Panagiotis Astithas | } |
341 | 023f6f1e | Panagiotis Astithas | |
342 | 023f6f1e | Panagiotis Astithas | @Override
|
343 | 023f6f1e | Panagiotis Astithas | public List<UserClassDTO> getUserClasses(){ |
344 | 023f6f1e | Panagiotis Astithas | List<UserClassDTO> result = new ArrayList<UserClassDTO>(); |
345 | 023f6f1e | Panagiotis Astithas | for(UserClass c : dao.getUserClasses())
|
346 | 023f6f1e | Panagiotis Astithas | result.add(c.getDTOWithoutUsers()); |
347 | 023f6f1e | Panagiotis Astithas | return result;
|
348 | 023f6f1e | Panagiotis Astithas | } |
349 | 023f6f1e | Panagiotis Astithas | |
350 | 023f6f1e | Panagiotis Astithas | @Override
|
351 | 023f6f1e | Panagiotis Astithas | public void saveOrUpdateUserClass(UserClassDTO dto) throws ObjectNotFoundException{ |
352 | 023f6f1e | Panagiotis Astithas | UserClass uclass; |
353 | 023f6f1e | Panagiotis Astithas | if(dto.getId()!=null) |
354 | 023f6f1e | Panagiotis Astithas | uclass = dao.getEntityById(UserClass.class, dto.getId()); |
355 | 023f6f1e | Panagiotis Astithas | else
|
356 | 023f6f1e | Panagiotis Astithas | uclass = new UserClass();
|
357 | 023f6f1e | Panagiotis Astithas | uclass.setName(dto.getName()); |
358 | 023f6f1e | Panagiotis Astithas | uclass.setQuota(dto.getQuota()); |
359 | 023f6f1e | Panagiotis Astithas | if(dto.getId()!=null) |
360 | 023f6f1e | Panagiotis Astithas | dao.update(uclass); |
361 | 023f6f1e | Panagiotis Astithas | else
|
362 | 023f6f1e | Panagiotis Astithas | dao.create(uclass); |
363 | 023f6f1e | Panagiotis Astithas | dao.flush(); |
364 | 023f6f1e | Panagiotis Astithas | |
365 | 023f6f1e | Panagiotis Astithas | } |
366 | 023f6f1e | Panagiotis Astithas | |
367 | 023f6f1e | Panagiotis Astithas | @Override
|
368 | 023f6f1e | Panagiotis Astithas | public void removeUserClass(UserClassDTO dto) throws ObjectNotFoundException{ |
369 | 023f6f1e | Panagiotis Astithas | UserClass uclass = dao.getEntityById(UserClass.class, dto.getId()); |
370 | 023f6f1e | Panagiotis Astithas | if(uclass==null) |
371 | 023f6f1e | Panagiotis Astithas | throw new ObjectNotFoundException("User Class not found"); |
372 | 023f6f1e | Panagiotis Astithas | dao.delete(uclass); |
373 | 023f6f1e | Panagiotis Astithas | } |
374 | 023f6f1e | Panagiotis Astithas | |
375 | 023f6f1e | Panagiotis Astithas | @Override
|
376 | 023f6f1e | Panagiotis Astithas | public List<FileHeaderDTO> searchFileByFilename(String fileName){ |
377 | 023f6f1e | Panagiotis Astithas | List<FileHeader> files = dao.searchFileByFilename(fileName);
|
378 | 023f6f1e | Panagiotis Astithas | List<FileHeaderDTO> result = new ArrayList<FileHeaderDTO>(); |
379 | 023f6f1e | Panagiotis Astithas | for(FileHeader h : files)
|
380 | 023f6f1e | Panagiotis Astithas | result.add(h.getDTO()); |
381 | 023f6f1e | Panagiotis Astithas | return result;
|
382 | 023f6f1e | Panagiotis Astithas | } |
383 | 023f6f1e | Panagiotis Astithas | |
384 | 023f6f1e | Panagiotis Astithas | @Override
|
385 | 023f6f1e | Panagiotis Astithas | public void removeUser(Long userId) throws ObjectNotFoundException, InsufficientPermissionsException{ |
386 | 023f6f1e | Panagiotis Astithas | User user = api.getUser(userId); |
387 | 023f6f1e | Panagiotis Astithas | try{
|
388 | 023f6f1e | Panagiotis Astithas | FolderDTO folder = api.getRootFolder(userId); |
389 | 023f6f1e | Panagiotis Astithas | deleteFolder(userId, folder.getId()); |
390 | 023f6f1e | Panagiotis Astithas | List<GroupDTO> groups = api.getGroups(userId);
|
391 | 023f6f1e | Panagiotis Astithas | for(GroupDTO group : groups)
|
392 | 023f6f1e | Panagiotis Astithas | api.deleteGroup(userId, group.getId()); |
393 | 023f6f1e | Panagiotis Astithas | } |
394 | 023f6f1e | Panagiotis Astithas | catch(ObjectNotFoundException e){}
|
395 | 023f6f1e | Panagiotis Astithas | List<AccountingInfo> infos = dao.getAccountingInfo(user);
|
396 | 023f6f1e | Panagiotis Astithas | Iterator<AccountingInfo> it = infos.iterator();
|
397 | 023f6f1e | Panagiotis Astithas | while(it.hasNext()){
|
398 | 023f6f1e | Panagiotis Astithas | AccountingInfo a = it.next(); |
399 | 023f6f1e | Panagiotis Astithas | dao.delete(a); |
400 | 023f6f1e | Panagiotis Astithas | } |
401 | 023f6f1e | Panagiotis Astithas | dao.flush(); |
402 | 023f6f1e | Panagiotis Astithas | dao.delete(user); |
403 | 023f6f1e | Panagiotis Astithas | } |
404 | 023f6f1e | Panagiotis Astithas | |
405 | 023f6f1e | Panagiotis Astithas | /**
|
406 | 023f6f1e | Panagiotis Astithas | * Deletes the given folder and all its subfolders and files
|
407 | 023f6f1e | Panagiotis Astithas | * Only the permissions for top folder are checked
|
408 | 023f6f1e | Panagiotis Astithas | *
|
409 | 023f6f1e | Panagiotis Astithas | * @see gr.ebs.gss.server.ejb.ExternalAPI#deleteFolder(java.lang.Long,
|
410 | 023f6f1e | Panagiotis Astithas | * java.lang.Long)
|
411 | 023f6f1e | Panagiotis Astithas | */
|
412 | 023f6f1e | Panagiotis Astithas | public void deleteFolder(final Long userId, final Long folderId) throws ObjectNotFoundException { |
413 | 023f6f1e | Panagiotis Astithas | // Validate.
|
414 | 023f6f1e | Panagiotis Astithas | if (userId == null) |
415 | 023f6f1e | Panagiotis Astithas | throw new ObjectNotFoundException("No user specified"); |
416 | 023f6f1e | Panagiotis Astithas | if (folderId == null) |
417 | 023f6f1e | Panagiotis Astithas | throw new ObjectNotFoundException("No folder specified"); |
418 | 023f6f1e | Panagiotis Astithas | |
419 | 023f6f1e | Panagiotis Astithas | // Do the actual work.
|
420 | 023f6f1e | Panagiotis Astithas | final Folder folder = dao.getEntityById(Folder.class, folderId);
|
421 | 023f6f1e | Panagiotis Astithas | final Folder parent = folder.getParent();
|
422 | 023f6f1e | Panagiotis Astithas | final User user = dao.getEntityById(User.class, userId);
|
423 | 023f6f1e | Panagiotis Astithas | |
424 | 023f6f1e | Panagiotis Astithas | removeSubfolderFiles(folder); |
425 | 023f6f1e | Panagiotis Astithas | if(parent!=null) |
426 | 023f6f1e | Panagiotis Astithas | parent.removeSubfolder(folder); |
427 | 023f6f1e | Panagiotis Astithas | dao.delete(folder); |
428 | 023f6f1e | Panagiotis Astithas | if(parent!=null) |
429 | 023f6f1e | Panagiotis Astithas | touchParentFolders(parent, user, new Date()); |
430 | 023f6f1e | Panagiotis Astithas | } |
431 | 023f6f1e | Panagiotis Astithas | |
432 | 023f6f1e | Panagiotis Astithas | private void removeSubfolderFiles(Folder folder) { |
433 | 023f6f1e | Panagiotis Astithas | //remove files for all subfolders
|
434 | 023f6f1e | Panagiotis Astithas | for (Folder subfolder:folder.getSubfolders())
|
435 | 023f6f1e | Panagiotis Astithas | removeSubfolderFiles(subfolder); |
436 | 023f6f1e | Panagiotis Astithas | //remove this folder's file bodies (actual files)
|
437 | 023f6f1e | Panagiotis Astithas | for (FileHeader file:folder.getFiles()) {
|
438 | 023f6f1e | Panagiotis Astithas | for (FileBody body:file.getBodies())
|
439 | 023f6f1e | Panagiotis Astithas | api.deleteActualFile(body.getStoredFilePath()); |
440 | 023f6f1e | Panagiotis Astithas | indexFile(file.getId(), true);
|
441 | 023f6f1e | Panagiotis Astithas | } |
442 | 023f6f1e | Panagiotis Astithas | } |
443 | 023f6f1e | Panagiotis Astithas | |
444 | 023f6f1e | Panagiotis Astithas | private void touchParentFolders(Folder folder, User modifiedBy, Date modificationDate) { |
445 | 023f6f1e | Panagiotis Astithas | Folder f = folder; |
446 | 023f6f1e | Panagiotis Astithas | while (f!=null) { |
447 | 023f6f1e | Panagiotis Astithas | AuditInfo ai = f.getAuditInfo(); |
448 | 023f6f1e | Panagiotis Astithas | ai.setModifiedBy(modifiedBy); |
449 | 023f6f1e | Panagiotis Astithas | ai.setModificationDate(modificationDate); |
450 | 023f6f1e | Panagiotis Astithas | f.setAuditInfo(ai); |
451 | 023f6f1e | Panagiotis Astithas | f = f.getParent(); |
452 | 023f6f1e | Panagiotis Astithas | } |
453 | 023f6f1e | Panagiotis Astithas | } |
454 | 023f6f1e | Panagiotis Astithas | |
455 | 023f6f1e | Panagiotis Astithas | public void indexFile(Long fileId, boolean delete) { |
456 | 023f6f1e | Panagiotis Astithas | Connection qConn = null; |
457 | 023f6f1e | Panagiotis Astithas | Session session = null;
|
458 | 023f6f1e | Panagiotis Astithas | MessageProducer sender = null;
|
459 | 023f6f1e | Panagiotis Astithas | try {
|
460 | 023f6f1e | Panagiotis Astithas | Context jndiCtx = new InitialContext(); |
461 | 023f6f1e | Panagiotis Astithas | ConnectionFactory factory = (QueueConnectionFactory) jndiCtx.lookup("java:/JmsXA");
|
462 | 023f6f1e | Panagiotis Astithas | Queue queue = (Queue) jndiCtx.lookup("queue/gss-indexingQueue"); |
463 | 023f6f1e | Panagiotis Astithas | qConn = factory.createConnection(); |
464 | 023f6f1e | Panagiotis Astithas | session = qConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
465 | 023f6f1e | Panagiotis Astithas | sender = session.createProducer(queue); |
466 | 023f6f1e | Panagiotis Astithas | |
467 | 023f6f1e | Panagiotis Astithas | MapMessage map = session.createMapMessage(); |
468 | 023f6f1e | Panagiotis Astithas | map.setObject("id", fileId);
|
469 | 023f6f1e | Panagiotis Astithas | map.setBoolean("delete", delete);
|
470 | 023f6f1e | Panagiotis Astithas | sender.send(map); |
471 | 023f6f1e | Panagiotis Astithas | } |
472 | 023f6f1e | Panagiotis Astithas | catch (NamingException e) { |
473 | 023f6f1e | Panagiotis Astithas | logger.error("Index was not updated: ", e);
|
474 | 023f6f1e | Panagiotis Astithas | } |
475 | 023f6f1e | Panagiotis Astithas | catch (JMSException e) {
|
476 | 023f6f1e | Panagiotis Astithas | logger.error("Index was not updated: ", e);
|
477 | 023f6f1e | Panagiotis Astithas | } |
478 | 023f6f1e | Panagiotis Astithas | finally {
|
479 | 023f6f1e | Panagiotis Astithas | try {
|
480 | 023f6f1e | Panagiotis Astithas | if (sender != null) |
481 | 023f6f1e | Panagiotis Astithas | sender.close(); |
482 | 023f6f1e | Panagiotis Astithas | if (session != null) |
483 | 023f6f1e | Panagiotis Astithas | session.close(); |
484 | 023f6f1e | Panagiotis Astithas | if (qConn != null) |
485 | 023f6f1e | Panagiotis Astithas | qConn.close(); |
486 | 023f6f1e | Panagiotis Astithas | } |
487 | 023f6f1e | Panagiotis Astithas | catch (JMSException e) {
|
488 | 023f6f1e | Panagiotis Astithas | logger.warn(e); |
489 | 023f6f1e | Panagiotis Astithas | } |
490 | 023f6f1e | Panagiotis Astithas | } |
491 | 023f6f1e | Panagiotis Astithas | } |
492 | 023f6f1e | Panagiotis Astithas | } |