BrightSide Workbench Full Report + Source Code
Cleanup.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.List;
22 import java.util.Set;
23 import java.util.stream.Collectors;
24 import org.turro.log.WebLoggers;
25 import org.turro.path.Path;
26 import org.turro.reflection.Instances;
27 
32 public class Cleanup {
33 
34  public static List<IElephantCleanup> getFor(Object entity, ICleanupConsole console) {
35  return Instances.fresh().byAnnotation(ElephantCleanup.class, IElephantCleanup.class)
36  .stream().filter(cleanup -> cleanup.isMine(entity))
37  .peek(cleanup -> cleanup.setConsole(console)).toList();
38  }
39 
40  public static void executeForEntity(Object entity, ICleanupConsole console, CleanupMode mode) {
41  Instances.fresh().byAnnotation(ElephantCleanup.class, IElephantCleanup.class)
42  .stream().filter(cleanup -> cleanup.isMine(entity))
43  .forEach(cleanup -> {
44  cleanup.setConsole(console);
45  cleanup.cleanEntity(entity, mode);
46  });
47  }
48 
49  public static void executeForCommons(String entityPath, ICleanupConsole console, CleanupMode mode, Set<IElephantCleanup.Commons> commons) {
50  if(Path.pathFrom(entityPath).getSize() != 2) return; // Not an entityPath
51  Instances.fresh().byAnnotation(ElephantCleanup.class, IElephantCleanup.class)
52  .stream()
53  .forEach(cleanup -> {
54  WebLoggers.info(cleanup).message("From commons started...").log();
55  cleanup.setConsole(console);
56  cleanup.cleanFromCommons(entityPath, mode, commons);
57  WebLoggers.info(cleanup).message("From commons ended...").log();
58  });
59  }
60 
61  public static void orphans() {
62  Instances.fresh().byAnnotation(ElephantCleanup.class, IElephantCleanup.class)
63  .stream().forEach(cleanup -> {
64  WebLoggers.info(cleanup).message("Orphans started...").log();
65  cleanup.cleanOrphans();
66  WebLoggers.info(cleanup).message("Orphans ended...").log();
67  });
68  }
69 
70  public static Set<String> getEntityPathsFor(String root, IElephantCleanup.Commons common) {
71  return Instances.fresh().byAnnotation(ElephantCleanup.class, IElephantCleanup.class)
72  .stream().flatMap(cleanup -> cleanup.pathsForRoot(root, common).stream())
73  .collect(Collectors.toSet());
74  }
75 
76  private Cleanup() {}
77 
78 }
static List< IElephantCleanup > getFor(Object entity, ICleanupConsole console)
Definition: Cleanup.java:34
static void executeForEntity(Object entity, ICleanupConsole console, CleanupMode mode)
Definition: Cleanup.java:40
static void orphans()
Definition: Cleanup.java:61
static void executeForCommons(String entityPath, ICleanupConsole console, CleanupMode mode, Set< IElephantCleanup.Commons > commons)
Definition: Cleanup.java:49
static Set< String > getEntityPathsFor(String root, IElephantCleanup.Commons common)
Definition: Cleanup.java:70
static WebLoggers info(Object entity)
Definition: WebLoggers.java:43