BrightSide Workbench Full Report + Source Code
AbstractCleanup.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.Collections;
22 import java.util.Set;
23 import java.util.stream.Collectors;
24 import java.util.stream.Stream;
25 import org.turro.path.Path;
26 import org.turro.sql.IDao;
27 import org.turro.sql.SqlClause;
28 
33 public abstract class AbstractCleanup implements IElephantCleanup {
34 
35  private ICleanupConsole console;
36 
37  @Override
38  public void setConsole(ICleanupConsole console) {
39  this.console = console;
40  }
41 
43  return console;
44  }
45 
46  @Override
47  public void cleanOrphans() {
48  }
49 
50  @Override
51  public void cleanFromCommons(String entityPath, CleanupMode mode, Set<Commons> commons) {
52  }
53 
54  @Override
55  public Set<String> pathsForRoot(String root, Commons common) {
56  return Collections.EMPTY_SET;
57  }
58 
59  protected Set<String> getOrphans(Set<String> entities, Set<String> mapped) {
60  mapped.removeAll(entities);
61  return mapped;
62  }
63 
64  protected void cleanOrphansFor(String root, Set<String> entities) {
65  if(!entities.isEmpty()) {
66  for(Commons common : Commons.values()) {
67  Set<String> mapped = Cleanup.getEntityPathsFor(root, common);
68  getOrphans(entities, mapped).forEach(entityPath -> {
69 // System.out.print(common);
70 // System.out.println(entityPath);
71  Cleanup.executeForCommons(entityPath, null, CleanupMode.ONLY_EXECUTE, Set.of(common));
72  });
73  }
74  }
75  }
76 
77  protected <T> Set<String> getEntityPaths(IDao dao, Class<T> idClass, String table, String field, String root) {
78  try(Stream<T> stream = SqlClause.select("distinct d." + field).from(table + " d")
79  .dao(dao).stream(idClass)) {
80  return stream.map(id -> Path.of(root, String.valueOf(id)).getPath())
81  .collect(Collectors.toSet());
82  }
83  }
84 
85 }
void cleanOrphansFor(String root, Set< String > entities)
void setConsole(ICleanupConsole console)
Set< String > getOrphans(Set< String > entities, Set< String > mapped)
void cleanFromCommons(String entityPath, CleanupMode mode, Set< Commons > commons)
Set< String > pathsForRoot(String root, Commons common)
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