|
| 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/restore action information to a log entry. |
| 38 | + */ |
| 39 | +public class SaveAndRestoreActionAdapterFactory 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 | + if(adaptableObject instanceof RestoreSnapshotActionInfo){ |
| 57 | + RestoreSnapshotActionInfo restoreSnapshotActionInfo = (RestoreSnapshotActionInfo)adaptableObject; |
| 58 | + String title = restoreSnapshotActionInfo.isGolden() ? |
| 59 | + MessageFormat.format(Messages.GoldenSnapshotRestored, restoreSnapshotActionInfo.getSnapshotName()) : |
| 60 | + MessageFormat.format(Messages.SnapshotRestored, restoreSnapshotActionInfo.getSnapshotName()); |
| 61 | + LogEntryBuilder log = log() |
| 62 | + .title(title) |
| 63 | + .appendDescription(getBody(restoreSnapshotActionInfo)) |
| 64 | + .appendProperty(PropertyImpl.of("resource", map)); |
| 65 | + return Optional.of(adapterType.cast(log.build())); |
| 66 | + } |
| 67 | + else { |
| 68 | + SaveSnapshotActionInfo saveSnapshotActionInfo = (SaveSnapshotActionInfo)adaptableObject; |
| 69 | + LogEntryBuilder log = log() |
| 70 | + .title(MessageFormat.format(Messages.SnapshotCreated, saveSnapshotActionInfo.getSnapshotName())) |
| 71 | + .appendDescription(getBody(saveSnapshotActionInfo)) |
| 72 | + .appendProperty(PropertyImpl.of("resource", map)); |
| 73 | + return Optional.of(adapterType.cast(log.build())); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private String getBody(SaveSnapshotActionInfo restoreSnapshotActionInfo){ |
| 78 | + StringBuilder stringBuilder = new StringBuilder(); |
| 79 | + stringBuilder.append(Messages.RestoreSnapshotTemplateMessage).append(System.lineSeparator()).append(System.lineSeparator()); |
| 80 | + getCommonSnapshotInfo(restoreSnapshotActionInfo, stringBuilder); |
| 81 | + stringBuilder.append("| Saved by | ").append(restoreSnapshotActionInfo.getActionPerformedBy()).append(" |\n\n"); |
| 82 | + |
| 83 | + return stringBuilder.toString(); |
| 84 | + } |
| 85 | + |
| 86 | + private String getBody(RestoreSnapshotActionInfo restoreSnapshotActionInfo){ |
| 87 | + StringBuilder stringBuilder = new StringBuilder(); |
| 88 | + stringBuilder.append(Messages.SaveSnapshotTemplateMessage).append(System.lineSeparator()).append(System.lineSeparator()); |
| 89 | + getCommonSnapshotInfo(restoreSnapshotActionInfo, stringBuilder); |
| 90 | + stringBuilder.append("| Golden | ").append(restoreSnapshotActionInfo.isGolden() ? "yes" : "no").append(" |\n"); |
| 91 | + stringBuilder.append("| Restored by | ").append(restoreSnapshotActionInfo.getActionPerformedBy()).append(" |\n\n"); |
| 92 | + |
| 93 | + if(restoreSnapshotActionInfo.getFailedPVs() != null && !restoreSnapshotActionInfo.getFailedPVs().isEmpty()){ |
| 94 | + stringBuilder.append(Messages.FailedPVs).append("\n\n"); |
| 95 | + // This is needed! |
| 96 | + stringBuilder.append("| |\n"); |
| 97 | + // This is needed! |
| 98 | + stringBuilder.append("|-|\n"); |
| 99 | + restoreSnapshotActionInfo.getFailedPVs().forEach(p -> stringBuilder.append("| ").append(p).append(" |\n\n")); |
| 100 | + } |
| 101 | + |
| 102 | + return stringBuilder.toString(); |
| 103 | + } |
| 104 | + |
| 105 | + private void getCommonSnapshotInfo(SaveSnapshotActionInfo saveSnapshotActionInfo, StringBuilder stringBuilder){ |
| 106 | + // This is needed! |
| 107 | + stringBuilder.append("| | |\n"); |
| 108 | + // This is needed! |
| 109 | + stringBuilder.append("|-|-|\n"); |
| 110 | + stringBuilder.append("| Snapshot name | ").append(saveSnapshotActionInfo.getSnapshotName()).append(" |\n"); |
| 111 | + stringBuilder.append("| Comment | ").append(saveSnapshotActionInfo.getComment()).append(" |\n"); |
| 112 | + stringBuilder.append("| Created | ").append(saveSnapshotActionInfo.getSnapshotCreatedDate()).append(" |\n"); |
| 113 | + } |
| 114 | +} |
0 commit comments