|
6 | 6 | import org.apache.struts.action.ActionForm; |
7 | 7 | import org.apache.struts.action.ActionForward; |
8 | 8 | import org.apache.struts.action.ActionMapping; |
| 9 | + import org.apache.struts.action.ActionMessage; |
| 10 | + import org.apache.struts.action.ActionMessages; |
9 | 11 | import org.digijava.module.aim.dbentity.AmpOrganisation; |
| 12 | + import org.digijava.module.aim.dbentity.AmpTeam; |
10 | 13 | import org.digijava.module.aim.form.OrgManagerForm; |
11 | 14 | import org.digijava.module.aim.helper.TeamMember; |
| 15 | + import org.digijava.module.aim.util.ActivityUtil; |
12 | 16 | import org.digijava.module.aim.util.DbUtil; |
| 17 | + import org.digijava.module.aim.util.TeamUtil; |
13 | 18 | import org.digijava.module.calendar.util.AmpUtil; |
| 19 | + import org.hibernate.JDBCException; |
14 | 20 |
|
| 21 | + import javax.servlet.http.HttpServletRequest; |
15 | 22 | import javax.servlet.http.HttpSession; |
16 | 23 | import java.util.*; |
17 | 24 |
|
@@ -50,6 +57,9 @@ public ActionForward execute(ActionMapping mapping, ActionForm form, |
50 | 57 |
|
51 | 58 | OrgManagerForm eaForm = (OrgManagerForm) form; |
52 | 59 | eaForm.setAdminSide(isAdmin); |
| 60 | + if (isAdmin && "true".equals(request.getParameter("deleteSelectedOrgs"))) { |
| 61 | + deleteSelectedOrganisations(eaForm, request); |
| 62 | + } |
53 | 63 | if (request.getParameter("orgSelReset") != null |
54 | 64 | && request.getParameter("orgSelReset").equals("false")) { |
55 | 65 | eaForm.setOrgSelReset(false); |
@@ -229,6 +239,84 @@ else if (StringUtils.isNotBlank(eaForm.getKeyword())) { |
229 | 239 |
|
230 | 240 | } |
231 | 241 |
|
| 242 | + /** |
| 243 | + * Deletes the organizations checked in the "Select" column, applying the same |
| 244 | + * referential checks as the single-organization delete on the edit page. |
| 245 | + */ |
| 246 | + private void deleteSelectedOrganisations(OrgManagerForm eaForm, HttpServletRequest request) { |
| 247 | + Long[] ids = eaForm.getSelectedOrgIds(); |
| 248 | + if (ids == null || ids.length == 0) { |
| 249 | + return; |
| 250 | + } |
| 251 | + |
| 252 | + ActionMessages messages = new ActionMessages(); |
| 253 | + int deletedCount = 0; |
| 254 | + for (Long orgId : ids) { |
| 255 | + AmpOrganisation org = DbUtil.getOrganisation(orgId); |
| 256 | + if (org == null) { |
| 257 | + continue; |
| 258 | + } |
| 259 | + |
| 260 | + boolean blocked = false; |
| 261 | + |
| 262 | + Set<String> ampIds = new TreeSet<>(); |
| 263 | + addAllIfNotNull(ampIds, DbUtil.getAmpIdsByOrg(orgId)); |
| 264 | + addAllIfNotNull(ampIds, ActivityUtil.getAmpIdsByFundingOrg(orgId)); |
| 265 | + addAllIfNotNull(ampIds, DbUtil.getAmpIdsByInternalIdOrg(orgId)); |
| 266 | + if (!ampIds.isEmpty()) { |
| 267 | + messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage( |
| 268 | + "error.aim.organizationManager.deleteOrgActErrorBulk", org.getName(), String.join(", ", ampIds))); |
| 269 | + blocked = true; |
| 270 | + } |
| 271 | + |
| 272 | + if (org.getCalendar() != null && !org.getCalendar().isEmpty()) { |
| 273 | + messages.add(ActionMessages.GLOBAL_MESSAGE, |
| 274 | + new ActionMessage("error.aim.organizationManager.deleteOrgEventErrorBulk", org.getName())); |
| 275 | + blocked = true; |
| 276 | + } |
| 277 | + |
| 278 | + List<AmpTeam> relatedTeams = TeamUtil.getTeamByOrg(orgId); |
| 279 | + if (relatedTeams != null && !relatedTeams.isEmpty()) { |
| 280 | + messages.add(ActionMessages.GLOBAL_MESSAGE, |
| 281 | + new ActionMessage("error.aim.organizationManager.deleteOrgTeamErrorBulk", org.getName())); |
| 282 | + blocked = true; |
| 283 | + } |
| 284 | + |
| 285 | + if (org.getUsers() != null && !org.getUsers().isEmpty()) { |
| 286 | + messages.add(ActionMessages.GLOBAL_MESSAGE, |
| 287 | + new ActionMessage("error.aim.organizationManager.deleteOrgVerifiedOrgErrorBulk", org.getName())); |
| 288 | + blocked = true; |
| 289 | + } |
| 290 | + |
| 291 | + if (blocked) { |
| 292 | + continue; |
| 293 | + } |
| 294 | + |
| 295 | + try { |
| 296 | + DbUtil.deleteOrg(org); |
| 297 | + deletedCount++; |
| 298 | + } catch (JDBCException e) { |
| 299 | + messages.add(ActionMessages.GLOBAL_MESSAGE, |
| 300 | + new ActionMessage("error.aim.organizationManager.deleteOrgJdbcErrorBulk", org.getName())); |
| 301 | + } |
| 302 | + } |
| 303 | + |
| 304 | + if (deletedCount > 0) { |
| 305 | + messages.add(ActionMessages.GLOBAL_MESSAGE, |
| 306 | + new ActionMessage("error.aim.organizationManager.deleteOrgSuccessBulk", String.valueOf(deletedCount))); |
| 307 | + } |
| 308 | + if (!messages.isEmpty()) { |
| 309 | + saveErrors(request, messages); |
| 310 | + } |
| 311 | + eaForm.setSelectedOrgIds(null); |
| 312 | + } |
| 313 | + |
| 314 | + private void addAllIfNotNull(Set<String> set, Collection<String> toAdd) { |
| 315 | + if (toAdd != null && !toAdd.isEmpty()) { |
| 316 | + set.addAll(toAdd); |
| 317 | + } |
| 318 | + } |
| 319 | + |
232 | 320 | private void collectAlphaArray(OrgManagerForm eaForm, Collection<AmpOrganisation> col) { |
233 | 321 | SortedSet<String> chars = new TreeSet<String>(AmpUtil.CharUnicodeComparator); |
234 | 322 | SortedSet<String> digits = new TreeSet<String>(AmpUtil.CharUnicodeComparator); |
|
0 commit comments