Autopsy  4.15.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ReportWizardAction.java
Go to the documentation of this file.
1 /*
2  *
3  * Autopsy Forensic Browser
4  *
5  * Copyright 2012-2020 Basis Technology Corp.
6  *
7  * Copyright 2012 42six Solutions.
8  * Contact: aebadirad <at> 42six <dot> com
9  * Project Contact/Architect: carrier <at> sleuthkit <dot> org
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 package org.sleuthkit.autopsy.report.infrastructure;
24 
27 import java.awt.Component;
28 import java.awt.Cursor;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31 import java.beans.PropertyChangeEvent;
32 import java.text.MessageFormat;
33 import java.util.EnumSet;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.concurrent.ExecutionException;
37 import java.util.logging.Level;
38 import javax.swing.ImageIcon;
39 import javax.swing.JButton;
40 import javax.swing.SwingWorker;
41 import org.openide.DialogDisplayer;
42 import org.openide.NotifyDescriptor;
43 import org.openide.WizardDescriptor;
44 import org.openide.awt.ActionID;
45 import org.openide.awt.ActionReference;
46 import org.openide.awt.ActionReferences;
47 import org.openide.awt.ActionRegistration;
48 import org.openide.util.HelpCtx;
49 import org.openide.util.NbBundle;
50 import org.openide.util.actions.CallableSystemAction;
51 import org.openide.util.actions.Presenter;
52 import org.openide.windows.WindowManager;
58 
59 @ActionID(category = "Tools", id = "org.sleuthkit.autopsy.report.infrastructure.ReportWizardAction")
60 @ActionRegistration(displayName = "#CTL_ReportWizardAction", lazy = false)
61 @ActionReferences(value = {
62  @ActionReference(path = "Menu/Tools", position = 301, separatorAfter = 399)
63  ,
64  @ActionReference(path = "Toolbars/Case", position = 106)})
65 public final class ReportWizardAction extends CallableSystemAction implements Presenter.Toolbar, ActionListener {
66 
67  private static final Logger logger = Logger.getLogger(ReportWizardAction.class.getName());
68  private static final String REPORTING_CONFIGURATION_NAME = "ReportAction";
69  private static final boolean DISPLAY_CASE_SPECIFIC_DATA = true;
70  private static final boolean RUN_REPORTS = true;
71  private final JButton toolbarButton = new JButton();
72  private static final String ACTION_NAME = NbBundle.getMessage(ReportWizardAction.class, "ReportWizardAction.actionName.text");
73  private static ReportGenerationPanel panel;
74 
86  @SuppressWarnings("unchecked")
87  public static void doReportWizard(String configName, boolean displayCaseSpecificData, boolean runReports) {
88  WizardDescriptor wiz = new WizardDescriptor(new ReportWizardIterator(configName, displayCaseSpecificData));
89  wiz.setTitleFormat(new MessageFormat("{0} {1}"));
90  wiz.setTitle(NbBundle.getMessage(ReportWizardAction.class, "ReportWizardAction.reportWiz.title"));
91  if (DialogDisplayer.getDefault().notify(wiz) == WizardDescriptor.FINISH_OPTION) {
92 
93  // save reporting configuration
94  try {
95  saveReportingConfiguration(configName, wiz);
96  } catch (ReportConfigException ex) {
97  logger.log(Level.SEVERE, "Failed to save reporting configuration " + configName, ex); //NON-NLS
98  NotifyDescriptor descriptor = new NotifyDescriptor.Message(
99  NbBundle.getMessage(ReportWizardAction.class, "ReportWizardAction.unableToSaveConfig.errorLabel.text"),
100  NotifyDescriptor.ERROR_MESSAGE);
101  DialogDisplayer.getDefault().notify(descriptor);
102  }
103 
104  if (runReports) {
105  // generate reports in a separate thread
106  panel = new ReportGenerationPanel();
107  Map<String, ReportModule> modules = (Map<String, ReportModule>) wiz.getProperty("modules");
108  ReportGenerator generator = new ReportGenerator(configName, panel); //NON-NLS
109  ReportWorker worker = new ReportWorker(() -> {
110  generator.generateReports(modules);
111  });
112  worker.execute();
113  generator.displayProgressPanel();
114  }
115  }
116  }
117 
118  @SuppressWarnings(value = "unchecked")
119  private static void saveReportingConfiguration(String configName, WizardDescriptor wiz) throws ReportConfigException {
120 
121  ReportingConfig reportingConfig = new ReportingConfig(configName);
122  List<Long> selectedDataSourceIds = (List<Long>) wiz.getProperty("dataSourceSelections");
123 
124  // Set the selected data source ids.
125  FileReportSettings fileSettings = (FileReportSettings) wiz.getProperty("fileReportSettings");
126  TableReportSettings tableSettings = (TableReportSettings) wiz.getProperty("tableReportSettings");
127  GeneralReportSettings generalSettings = new GeneralReportSettings();
128  if(selectedDataSourceIds != null) {
129  generalSettings.setSelectedDataSources(selectedDataSourceIds);
130  if(fileSettings != null) {
131  fileSettings.setSelectedDataSources(selectedDataSourceIds);
132  }
133  if(tableSettings != null) {
134  tableSettings.setSelectedDataSources(selectedDataSourceIds);
135  }
136  }
137 
138  reportingConfig.setFileReportSettings(fileSettings);
139  reportingConfig.setTableReportSettings(tableSettings);
140  reportingConfig.setGeneralReportSettings(generalSettings);
141 
142  Map<String, ReportModuleConfig> moduleConfigs = (Map<String, ReportModuleConfig>) wiz.getProperty("moduleConfigs");
143 
144  // update portable case settings
145  ReportModuleConfig config = moduleConfigs.get(PortableCaseReportModule.class.getCanonicalName());
146  PortableCaseReportModuleSettings portableCaseReportSettings = (PortableCaseReportModuleSettings) wiz.getProperty("portableCaseReportSettings");
147  if (portableCaseReportSettings != null) {
148  config.setModuleSettings(portableCaseReportSettings);
149  moduleConfigs.put(PortableCaseReportModule.class.getCanonicalName(), config);
150  }
151 
152  // set module configs
153  reportingConfig.setModuleConfigs(moduleConfigs);
154 
155  // save reporting configuration
156  ReportingConfigLoader.saveConfig(reportingConfig);
157  }
158 
160  setEnabled(false);
161  Case.addEventTypeSubscriber(EnumSet.of(Case.Events.CURRENT_CASE), (PropertyChangeEvent evt) -> {
162  if (evt.getPropertyName().equals(Case.Events.CURRENT_CASE.toString())) {
163  Case newCase = (Case) evt.getNewValue();
164  setEnabled(newCase != null && RuntimeProperties.runningWithGUI());
165  }
166  });
167 
168  // Initialize the Generate Report button
169  toolbarButton.addActionListener(ReportWizardAction.this::actionPerformed);
170  }
171 
172  @Override
173  @SuppressWarnings("unchecked")
174  public void actionPerformed(ActionEvent e) {
175  WindowManager.getDefault().getMainWindow().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
176  doReportWizard(REPORTING_CONFIGURATION_NAME, DISPLAY_CASE_SPECIFIC_DATA, RUN_REPORTS);
177  WindowManager.getDefault().getMainWindow().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
178  }
179 
180  @Override
181  public void performAction() {
182  }
183 
184  @Override
185  public String getName() {
186  return ACTION_NAME;
187  }
188 
189  @Override
190  public HelpCtx getHelpCtx() {
191  return HelpCtx.DEFAULT_HELP;
192  }
193 
199  @Override
200  public Component getToolbarPresenter() {
201  ImageIcon icon = new ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/report/images/btn_icon_generate_report.png")); //NON-NLS
202  toolbarButton.setIcon(icon);
203  toolbarButton.setText(NbBundle.getMessage(this.getClass(), "ReportWizardAction.toolBarButton.text"));
204  return toolbarButton;
205  }
206 
212  @Override
213  public void setEnabled(boolean value) {
214  super.setEnabled(value);
215  toolbarButton.setEnabled(value);
216  }
217 
218  private static class ReportWorker extends SwingWorker<Void, Void> {
219 
220  private final Runnable doInBackground;
221 
222  private ReportWorker(Runnable doInBackground) {
223  this.doInBackground = doInBackground;
224  }
225 
226  @Override
227  protected Void doInBackground() throws Exception {
228  doInBackground.run();
229  return null;
230  }
231 
232  @Override
233  protected void done() {
234  try {
235  get();
236  } catch (InterruptedException | ExecutionException ex) {
237  panel.getProgressPanel().updateStatusLabel(NbBundle.getMessage(this.getClass(), "ReportGenerator.errors.reportErrorText") + ex.getLocalizedMessage());
238  logger.log(Level.SEVERE, "failed to generate reports", ex); //NON-NLS
239  } // catch and ignore if we were cancelled
240  catch (java.util.concurrent.CancellationException ex) {
241  }
242  }
243  }
244 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static void addEventTypeSubscriber(Set< Events > eventTypes, PropertyChangeListener subscriber)
Definition: Case.java:491
void setSelectedDataSources(List< Long > selectedDataSources)

Copyright © 2012-2020 Basis Technology. Generated on: Mon Jul 6 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.