BrightSide Workbench Full Report + Source Code
IssueCleanup.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 org.turro.dossier.db.DossierPU;
23 import org.turro.dossier.dw.DatawarehouseUtil;
24 import org.turro.dossier.entity.Issue;
25 import org.turro.jpa.Dao;
26 import org.turro.sql.SqlClause;
27 
32 @ElephantCleanup
33 public class IssueCleanup extends AbstractCleanup {
34 
35  @Override
36  public boolean isMine(Object entity) {
37  return entity instanceof Issue;
38  }
39 
40  @Override
41  public void cleanEntity(Object entity, CleanupMode mode) {
42  if(entity instanceof Issue issue) {
43  String entityPath = DossierPU.getObjectPath(issue);
44  Dao dao = new DossierPU();
45  getConsole().addMessage(issue.getDescription());
46  Cleanup.executeForCommons(entityPath, getConsole(), mode, Set.of(Commons.values()));
47  CleanupAction.from("Cleaning duplicates...",
48  () -> cleanupDuplicates(dao, issue))
49  .execute(getConsole(), mode);
50  CleanupAction.from("Removing DW...",
51  () -> DatawarehouseUtil.remove(dao, issue))
52  .execute(getConsole(), mode);
53  CleanupAction.from("Deleting entity...",
54  () -> dao.deleteObject(issue))
55  .execute(getConsole(), mode);
56  }
57  }
58 
59  @Override
60  public void cleanOrphans() {
61  Dao dao = new DossierPU();
62  SqlClause.delete("IssueParticipant")
63  .where().append("issue").isNull()
64  .dao(dao)
65  .execute();
66  cleanOrphansFor("issue", getEntityPaths(dao, Long.class, "Issue", "id", "issue"));
67  }
68 
69  private void cleanupDuplicates(Dao dao, Issue issue) {
70  SqlClause.update("Issue")
71  .set("duplicated", null)
72  .where().equal("duplicated", issue)
73  .dao(dao)
74  .execute();
75  }
76 
77 }
void cleanOrphansFor(String root, Set< String > entities)
static CleanupAction from(String message, Runnable action)
void execute(ICleanupConsole console, CleanupMode mode)
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)