BrightSide Workbench Full Report + Source Code
DossierCleanup.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.cleanup;
20 
21 import java.util.Set;
22 import java.util.stream.Stream;
23 import org.turro.dossier.db.DossierPU;
24 import org.turro.dossier.dw.DatawarehouseUtil;
25 import org.turro.dossier.entity.Dossier;
26 import org.turro.dossier.entity.Issue;
27 import org.turro.jpa.Dao;
28 import org.turro.sql.SqlClause;
29 
34 @ElephantCleanup
35 public class DossierCleanup extends AbstractCleanup {
36 
37  @Override
38  public boolean isMine(Object entity) {
39  return entity instanceof Dossier;
40  }
41 
42  @Override
43  public void cleanEntity(Object entity, CleanupMode mode) {
44  if(entity instanceof Dossier dossier) {
45  String entityPath = DossierPU.getObjectPath(dossier);
46  Dao dao = new DossierPU();
47  cleanupIssues(dao, dossier, mode);
48  cleanupRequests(dao, dossier, mode);
49  getConsole().addMessage(dossier.getDescription());
50  Cleanup.executeForCommons(entityPath, getConsole(), mode, Set.of(Commons.values()));
51  CleanupAction.from("Removing DW...",
52  () -> DatawarehouseUtil.remove(dao, dossier))
53  .execute(getConsole(), mode);
54  CleanupAction.from("Deleting entity...",
55  () -> dao.deleteObject(dossier))
56  .execute(getConsole(), mode);
57  }
58  }
59 
60  private void cleanupIssues(Dao dao, Dossier dossier, CleanupMode mode) {
61  try(Stream<Issue> stream = SqlClause.select("i").from("Issue i")
62  .where().equal("i.dossier", dossier)
63  .dao(dao)
64  .stream(Issue.class)) {
65  stream.forEach(issue -> {
66  Cleanup.executeForEntity(issue, getConsole(), mode);
67  });
68  }
69  }
70 
71  private void cleanupRequests(Dao dao, Dossier dossier, CleanupMode mode) {
72  CleanupAction.from("Removing requests...", () -> {
73  SqlClause.delete("ParticipantRequest")
74  .where().equal("dossier", dossier)
75  .dao(dao).execute();
76  }).execute(getConsole(), mode);
77  }
78 
79  @Override
80  public void cleanOrphans() {
81  Dao dao = new DossierPU();
82  SqlClause.delete("Participant")
83  .where().isNull("dossier")
84  .dao(dao)
85  .execute();
86  SqlClause.delete("ParticipantRequest")
87  .where().isNull("dossier")
88  .dao(dao)
89  .execute();
90  SqlClause.delete("CategoryRequest")
91  .where().isNull("category")
92  .dao(dao)
93  .execute();
94  cleanOrphansFor("dossier", getEntityPaths(dao, Long.class, "Dossier", "id", "dossier"));
95  }
96 
97 }
void cleanOrphansFor(String root, Set< String > entities)
static CleanupAction from(String message, Runnable action)
void execute(ICleanupConsole console, CleanupMode mode)
static void executeForEntity(Object entity, ICleanupConsole console, CleanupMode mode)
Definition: Cleanup.java:40
static void executeForCommons(String entityPath, ICleanupConsole console, CleanupMode mode, Set< IElephantCleanup.Commons > commons)
Definition: Cleanup.java:49
boolean isMine(Object entity)
void cleanEntity(Object entity, CleanupMode mode)
static String getObjectPath(Object object)
Definition: DossierPU.java:66
static void remove(Dao dao, Dossier dossier)
void deleteObject(Object obj)
Definition: Dao.java:162
void addMessage(String message)