Skip to content

Commit 2e5ba3e

Browse files
authored
Merge pull request #2356 from ControlSystemStudio/SaveRestoreLogFix
Bug fix save&restore logging
2 parents 2b9ea45 + 7a0db88 commit 2e5ba3e

File tree

10 files changed

+237
-214
lines changed

10 files changed

+237
-214
lines changed

app/logbook/olog/ui/src/main/java/org/phoebus/logbook/olog/ui/write/LogEntryEditorController.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import javafx.scene.layout.VBox;
3636
import javafx.scene.paint.Color;
3737
import org.phoebus.framework.jobs.JobManager;
38+
import org.phoebus.framework.selection.Selection;
39+
import org.phoebus.framework.selection.SelectionService;
3840
import org.phoebus.logbook.*;
3941
import org.phoebus.logbook.olog.ui.HelpViewer;
4042
import org.phoebus.logbook.olog.ui.LogbookUIPreferences;
@@ -376,10 +378,14 @@ public void onChanged(Change<? extends String> change) {
376378
}
377379

378380
/**
379-
* Handler for Cancel button
381+
* Handler for Cancel button. Note that any selections in the {@link SelectionService} are
382+
* cleared to prevent next launch of {@link org.phoebus.logbook.olog.ui.menu.SendToLogBookApp}
383+
* to pick them up.
380384
*/
381385
@FXML
382386
public void cancel() {
387+
// Need to clear selections.
388+
SelectionService.getInstance().clearSelection("");
383389
((LogEntryEditorStage) cancelButton.getScene().getWindow()).handleCloseEditor(isDirty, root);
384390
}
385391

app/save-and-restore/app/src/main/java/org/phoebus/applications/saveandrestore/ui/snapshot/SnapshotController.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -628,15 +628,6 @@ public void restore() {
628628
LOGGER.log(Level.WARNING,
629629
"Not all PVs could be restored for {0}: {1}. The following errors occured:\n{2}",
630630
new Object[] { s.getSnapshot().get().getName(), s.getSnapshot().get(), sb.toString() });
631-
632-
Platform.runLater(() -> {
633-
Alert alert = new Alert(Alert.AlertType.ERROR);
634-
alert.setTitle(Messages.restoreErrorTitle);
635-
alert.setContentText(sb.toString());
636-
alert.setHeaderText(Messages.restoreErrorContent);
637-
DialogHelper.positionDialog(alert, snapshotTab.getTabPane(), -150, -150);
638-
alert.showAndWait();
639-
});
640631
}
641632
logSnapshotRestored(s.getSnapshot().get(), restoreFailed);
642633
}).start();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (C) 2020 European Spallation Source ERIC.
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation; either version 2
7+
* of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*
18+
*/
19+
20+
package org.phoebus.applications.saveandrestore.logging;
21+
22+
import org.phoebus.logbook.LogEntry;
23+
import org.phoebus.logbook.LogEntryImpl.LogEntryBuilder;
24+
import org.phoebus.logbook.PropertyImpl;
25+
26+
import java.text.MessageFormat;
27+
import java.util.Arrays;
28+
import java.util.HashMap;
29+
import java.util.List;
30+
import java.util.Map;
31+
import java.util.Optional;
32+
33+
import static org.phoebus.logbook.LogEntryImpl.LogEntryBuilder.log;
34+
35+
/**
36+
* Adapts save/restore action information to a log entry.
37+
*/
38+
public class RestoreSnapshotActionAdapterFactory extends SaveSnapshotActionAdapterFactory {
39+
40+
@Override
41+
public Class getAdaptableObject() {
42+
return RestoreSnapshotActionInfo.class;
43+
}
44+
45+
@Override
46+
public List<? extends Class> getAdapterList() {
47+
return Arrays.asList(LogEntry.class);
48+
}
49+
50+
@Override
51+
public <T> Optional<T> adapt(Object adaptableObject, Class<T> adapterType) {
52+
Map<String, String> map = new HashMap<>();
53+
map.put("file", "file:/" + ((RestoreSnapshotActionInfo)adaptableObject).getSnapshotUniqueId() + "?app=saveandrestore");
54+
map.put("name", ((RestoreSnapshotActionInfo)adaptableObject).getSnapshotName());
55+
56+
RestoreSnapshotActionInfo restoreSnapshotActionInfo = (RestoreSnapshotActionInfo)adaptableObject;
57+
String title = restoreSnapshotActionInfo.isGolden() ?
58+
MessageFormat.format(Messages.GoldenSnapshotRestored, restoreSnapshotActionInfo.getSnapshotName()) :
59+
MessageFormat.format(Messages.SnapshotRestored, restoreSnapshotActionInfo.getSnapshotName());
60+
LogEntryBuilder log = log()
61+
.title(title)
62+
.appendDescription(getBody(restoreSnapshotActionInfo))
63+
.appendProperty(PropertyImpl.of("resource", map));
64+
return Optional.of(adapterType.cast(log.build()));
65+
}
66+
67+
protected String getBody(RestoreSnapshotActionInfo restoreSnapshotActionInfo){
68+
StringBuilder stringBuilder = new StringBuilder();
69+
stringBuilder.append(Messages.RestoreSnapshotTemplateMessage).append(System.lineSeparator()).append(System.lineSeparator());
70+
getCommonSnapshotInfo(restoreSnapshotActionInfo, stringBuilder);
71+
stringBuilder.append("| Golden | ").append(restoreSnapshotActionInfo.isGolden() ? "yes" : "no").append(" |\n");
72+
stringBuilder.append("| Restored by | ").append(restoreSnapshotActionInfo.getActionPerformedBy()).append(" |\n\n");
73+
74+
if(restoreSnapshotActionInfo.getFailedPVs() != null && !restoreSnapshotActionInfo.getFailedPVs().isEmpty()){
75+
stringBuilder.append("\n").append(Messages.FailedPVs).append("\n\n");
76+
// This is needed!
77+
stringBuilder.append("| |\n");
78+
// This is needed!
79+
stringBuilder.append("|-|\n");
80+
restoreSnapshotActionInfo.getFailedPVs().forEach(p -> stringBuilder.append("| ").append(p).append(" |\n"));
81+
stringBuilder.append("\n");
82+
}
83+
84+
return stringBuilder.toString();
85+
}
86+
}

app/save-and-restore/logging/src/main/java/org/phoebus/applications/saveandrestore/logging/SaveAndRestoreActionAdapterFactory.java

Lines changed: 0 additions & 114 deletions
This file was deleted.

app/save-and-restore/logging/src/main/java/org/phoebus/applications/saveandrestore/logging/SaveAndRestoreEventLogger.java

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -90,31 +90,4 @@ public void snapshotRestored(Node node, List<String> failedPVs, Consumer<String>
9090
SelectionService.getInstance().setSelection("SaveAndRestoreLogging", List.of(restoreSnapshotActionInfo));
9191
Platform.runLater(() -> ApplicationService.createInstance("logbook"));
9292
}
93-
94-
protected String getSnapshotInfoTable(Node node) {
95-
StringBuilder stringBuilder = new StringBuilder();
96-
// This is needed!
97-
stringBuilder.append("| | |\n");
98-
// This is needed!
99-
stringBuilder.append("|-|-|\n");
100-
stringBuilder.append("| Snapshot name | ").append(node.getName()).append(" |\n");
101-
stringBuilder.append("| Comment | ").append(node.getProperty("comment")).append(" |\n");
102-
stringBuilder.append("| Created | ").append(node.getCreated()).append(" |\n");
103-
String isGolden = node.getProperty("golden");
104-
stringBuilder.append("| Golden | ").append("true".equals(isGolden) ? "yes" : "no").append(" |\n");
105-
stringBuilder.append("| User id | ").append(node.getUserName()).append(" |\n\n");
106-
107-
return stringBuilder.toString();
108-
}
109-
110-
protected String getFailedPVsTable(List<String> pvs) {
111-
StringBuilder stringBuilder = new StringBuilder();
112-
// This is needed!
113-
stringBuilder.append("| |\n");
114-
// This is needed!
115-
stringBuilder.append("|-|\n");
116-
pvs.forEach(p -> stringBuilder.append("| ").append(p).append(" |\n"));
117-
118-
return stringBuilder.toString();
119-
}
12093
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (C) 2020 European Spallation Source ERIC.
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation; either version 2
7+
* of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*
18+
*/
19+
20+
package org.phoebus.applications.saveandrestore.logging;
21+
22+
import org.phoebus.framework.adapter.AdapterFactory;
23+
import org.phoebus.logbook.LogEntry;
24+
import org.phoebus.logbook.LogEntryImpl.LogEntryBuilder;
25+
import org.phoebus.logbook.PropertyImpl;
26+
27+
import java.text.MessageFormat;
28+
import java.util.Arrays;
29+
import java.util.HashMap;
30+
import java.util.List;
31+
import java.util.Map;
32+
import java.util.Optional;
33+
34+
import static org.phoebus.logbook.LogEntryImpl.LogEntryBuilder.log;
35+
36+
/**
37+
* Adapts save snapshot action information to a log entry.
38+
*/
39+
public class SaveSnapshotActionAdapterFactory implements AdapterFactory {
40+
41+
@Override
42+
public Class getAdaptableObject() {
43+
return SaveSnapshotActionInfo.class;
44+
}
45+
46+
@Override
47+
public List<? extends Class> getAdapterList() {
48+
return Arrays.asList(LogEntry.class);
49+
}
50+
51+
@Override
52+
public <T> Optional<T> adapt(Object adaptableObject, Class<T> adapterType) {
53+
Map<String, String> map = new HashMap<>();
54+
map.put("file", "file:/" + ((SaveSnapshotActionInfo) adaptableObject).getSnapshotUniqueId() + "?app=saveandrestore");
55+
map.put("name", ((SaveSnapshotActionInfo) adaptableObject).getSnapshotName());
56+
57+
SaveSnapshotActionInfo saveSnapshotActionInfo = (SaveSnapshotActionInfo) adaptableObject;
58+
LogEntryBuilder log = log()
59+
.title(MessageFormat.format(Messages.SnapshotCreated, saveSnapshotActionInfo.getSnapshotName()))
60+
.appendDescription(getBody(saveSnapshotActionInfo))
61+
.appendProperty(PropertyImpl.of("resource", map));
62+
return Optional.of(adapterType.cast(log.build()));
63+
}
64+
65+
private String getBody(SaveSnapshotActionInfo saveSnapshotActionInfo) {
66+
StringBuilder stringBuilder = new StringBuilder();
67+
stringBuilder.append(Messages.SaveSnapshotTemplateMessage).append(System.lineSeparator()).append(System.lineSeparator());
68+
getCommonSnapshotInfo(saveSnapshotActionInfo, stringBuilder);
69+
stringBuilder.append("| Saved by | ").append(saveSnapshotActionInfo.getActionPerformedBy()).append(" |\n\n");
70+
71+
return stringBuilder.toString();
72+
}
73+
74+
protected void getCommonSnapshotInfo(SaveSnapshotActionInfo saveSnapshotActionInfo, StringBuilder stringBuilder) {
75+
// This is needed!
76+
stringBuilder.append("| | |\n");
77+
// This is needed!
78+
stringBuilder.append("|-|-|\n");
79+
stringBuilder.append("| Snapshot name | ").append(saveSnapshotActionInfo.getSnapshotName()).append(" |\n");
80+
stringBuilder.append("| Comment | ").append(saveSnapshotActionInfo.getComment()).append(" |\n");
81+
stringBuilder.append("| Created | ").append(saveSnapshotActionInfo.getSnapshotCreatedDate()).append(" |\n");
82+
}
83+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
org.phoebus.applications.saveandrestore.logging.SaveAndRestoreActionAdapterFactory
1+
org.phoebus.applications.saveandrestore.logging.SaveSnapshotActionAdapterFactory
2+
org.phoebus.applications.saveandrestore.logging.RestoreSnapshotActionAdapterFactory

0 commit comments

Comments
 (0)