BrightSide Workbench Full Report + Source Code
CleanupVM.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 org.turro.elephant.util.Messages;
22 import org.turro.elephant.util.Toasts;
23 import org.turro.entities.Entities;
24 import org.turro.entities.IElephantEntity;
25 import org.turro.log.SystemLogger;
26 import org.turro.reflection.stub.Stubs;
27 import org.turro.string.Phrases;
28 import org.turro.string.Strings;
29 import org.zkoss.bind.BindUtils;
30 import org.zkoss.bind.annotation.BindingParam;
31 import org.zkoss.bind.annotation.Command;
32 import org.zkoss.bind.annotation.NotifyChange;
33 
38 public class CleanupVM {
39 
40  private String entityPath;
41  private CleanupConsole console = new CleanupConsole();
42 
43  public CleanupVM() {
44  }
45 
46  @NotifyChange("console")
47  @Command
48  public void entityPath(@BindingParam("entityPath") String entityPath) {
49  this.entityPath = entityPath;
50  }
51 
52  @NotifyChange("console")
53  @Command
54  public void show() {
55  console = new CleanupConsole();
56  IElephantEntity iee = Entities.getController(entityPath);
57  if(iee.isEmpty()) {
58  Toasts.message("Entity cannot be instantiated.").show();
59  return;
60  }
61  Cleanup.getFor(iee.getEntity(), console).forEach(action -> {
62  action.cleanEntity(iee.getEntity(), CleanupMode.ONLY_VERBOSE);
63  BindUtils.postNotifyChange(null, null, CleanupVM.this, "console");
64  });
65  }
66 
67  @NotifyChange("console")
68  @Command
69  public void cleanup() {
70  console = new CleanupConsole();
71  IElephantEntity iee = Entities.getController(entityPath);
72  if(iee.isEmpty()) {
73  Toasts.message("Entity cannot be instantiated.").show();
74  return;
75  }
76  Messages.confirmProcess().add("Cleaup for:").add(iee.getName())
77  .show(() -> {
78  Cleanup.getFor(iee.getEntity(), console).forEach(action -> {
79  String stub = Stubs.json(iee.getEntity());
80  action.cleanEntity(iee.getEntity(), CleanupMode.VERBOSE);
81  SystemLogger.info().entity(iee).comment("cleanup")
82  .data(stub.getBytes()).log();
83  BindUtils.postNotifyChange(null, null, CleanupVM.this, "console");
84  });
85  });
86  }
87 
88  public Phrases getCaution() {
89  return Phrases.start("Proceed with caution!")
90  .paragraph()
91  .add("The cleanup process involves removing the selected entity")
92  .add("along with those depending on.")
93  .paragraph()
94  .add("Try the show option first, in order to see the process")
95  .add("before it starts.");
96  }
97 
98  public String getConsole() {
99  return Strings.isBlank(console.message(), "No process...");
100  }
101 
102 }
void entityPath(@BindingParam("entityPath") String entityPath)
Definition: CleanupVM.java:48
static List< IElephantCleanup > getFor(Object entity, ICleanupConsole console)
Definition: Cleanup.java:34
static Messages confirmProcess()
Definition: Messages.java:95
Messages add(String word)
Definition: Messages.java:50
static Toasts message(String message)
Definition: Toasts.java:114
static IElephantEntity getController(String path)
Definition: Entities.java:78