Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
DateSearchFilter.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2017 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.filesearch;
20 
21 import java.awt.Component;
22 import java.awt.event.ActionListener;
23 import java.beans.PropertyChangeEvent;
24 import java.beans.PropertyChangeListener;
25 import java.text.DateFormat;
26 import java.text.ParseException;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Calendar;
30 import java.util.Date;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Set;
34 import java.util.SimpleTimeZone;
35 import java.util.TimeZone;
36 import javax.swing.JLabel;
37 import javax.swing.JList;
38 import javax.swing.JSeparator;
39 import javax.swing.ListCellRenderer;
40 import javax.swing.SwingUtilities;
41 import javax.swing.border.EmptyBorder;
42 import org.openide.util.NbBundle;
44 
50 class DateSearchFilter extends AbstractFileSearchFilter<DateSearchPanel> {
51 
52  private static final String NONE_SELECTED_MESSAGE = NbBundle.getMessage(DateSearchFilter.class, "DateSearchFilter.noneSelectedMsg.text");
53  private static final DateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy");
54  private static final String SEPARATOR = "SEPARATOR"; //NON-NLS
55 
59  DateSearchFilter() {
60  this(new DateSearchPanel(DATE_FORMAT, DateSearchFilter.createTimeZoneList()));
61  }
62 
63  private DateSearchFilter(DateSearchPanel panel) {
64  super(panel);
65  Case.addPropertyChangeListener(this.new CasePropertyChangeListener());
66  }
67 
68  @Override
69  public boolean isEnabled() {
70  return this.getComponent().getDateCheckBox().isSelected();
71  }
72 
73  @Override
74  public String getPredicate() throws FilterValidationException {
75  String query = "NULL";
76  DateSearchPanel panel = this.getComponent();
77 
78  // first, get the selected timeZone from the dropdown list
79  String tz = this.getComponent().getTimeZoneComboBox().getSelectedItem().toString();
80  String tzID = tz.substring(tz.indexOf(" ") + 1); // 1 index after the space is the ID
81  TimeZone selectedTZ = TimeZone.getTimeZone(tzID); //
82 
83  // convert the date from the selected timezone to get the GMT
84  long fromDate = 0;
85  String startDateValue = panel.getDateFromTextField().getText();
86  Calendar startDate = null;
87  try {
88  DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
89  sdf.setTimeZone(selectedTZ); // get the time in the selected timezone
90  Date temp = sdf.parse(startDateValue);
91 
92  startDate = Calendar.getInstance(new SimpleTimeZone(0, "GMT")); //NON-NLS
93  startDate.setTime(temp); // convert to GMT
94  } catch (ParseException ex) {
95  // for now, no need to show the error message to the user here
96  }
97  if (!startDateValue.isEmpty()) {
98  if (startDate != null) {
99  fromDate = startDate.getTimeInMillis() / 1000; // divided by 1000 because we want to get the seconds, not miliseconds
100  }
101  }
102 
103  long toDate = 0;
104  String endDateValue = panel.getDateToTextField().getText();
105  Calendar endDate = null;
106  try {
107  DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
108  sdf.setTimeZone(selectedTZ); // get the time in the selected timezone
109  Date temp2 = sdf.parse(endDateValue);
110 
111  endDate = Calendar.getInstance(new SimpleTimeZone(0, "GMT")); //NON-NLS
112  endDate.setTime(temp2); // convert to GMT
113  endDate.set(Calendar.HOUR, endDate.get(Calendar.HOUR) + 24); // get the next 24 hours
114  } catch (ParseException ex) {
115  // for now, no need to show the error message to the user here
116  }
117  if (!endDateValue.isEmpty()) {
118  if (endDate != null) {
119  toDate = endDate.getTimeInMillis() / 1000; // divided by 1000 because we want to get the seconds, not miliseconds
120  }
121  }
122 
123  // If they put the dates in backwards, help them out.
124  if (fromDate > toDate) {
125  long temp = toDate;
126  toDate = fromDate;
127  fromDate = temp;
128  }
129 
130  final boolean modifiedChecked = panel.getModifiedCheckBox().isSelected();
131  final boolean changedChecked = panel.getChangedCheckBox().isSelected();
132  final boolean accessedChecked = panel.getAccessedCheckBox().isSelected();
133  final boolean createdChecked = panel.getCreatedCheckBox().isSelected();
134 
135  if (modifiedChecked || changedChecked || accessedChecked || createdChecked) {
136 
137  if (modifiedChecked) {
138  query += " OR (mtime BETWEEN " + fromDate + " AND " + toDate + ")"; //NON-NLS
139  }
140 
141  if (changedChecked) {
142  query += " OR (ctime BETWEEN " + fromDate + " AND " + toDate + ")"; //NON-NLS
143  }
144 
145  if (accessedChecked) {
146  query += " OR (atime BETWEEN " + fromDate + " AND " + toDate + ")"; //NON-NLS
147  }
148 
149  if (createdChecked) {
150  query += " OR (crtime BETWEEN " + fromDate + " AND " + toDate + ")"; //NON-NLS
151  }
152 
153  } else {
154  throw new FilterValidationException(NONE_SELECTED_MESSAGE);
155  }
156 
157  return query;
158 
159  }
160 
161  private void updateTimeZoneList() {
162  this.getComponent().setTimeZones(DateSearchFilter.createTimeZoneList());
163  }
164 
165  private static List<String> createTimeZoneList() {
166 
167  List<String> timeZones = new ArrayList<>();
168 
169  try {
170  // get the latest case
171  Case currentCase = Case.getCurrentCase(); // get the most updated case
172 
173  Set<TimeZone> caseTimeZones = currentCase.getTimeZones();
174  Iterator<TimeZone> iterator = caseTimeZones.iterator();
175  while (iterator.hasNext()) {
176  TimeZone zone = iterator.next();
177  int offset = zone.getRawOffset() / 1000;
178  int hour = offset / 3600;
179  int minutes = (offset % 3600) / 60;
180  String item = String.format("(GMT%+d:%02d) %s", hour, minutes, zone.getID()); //NON-NLS
181  timeZones.add(item);
182  }
183 
184  if (caseTimeZones.size() > 0) {
185  timeZones.add(SEPARATOR);
186  }
187 
188  // load and add all timezone
189  String[] ids = SimpleTimeZone.getAvailableIDs();
190  for (String id : ids) {
191  TimeZone zone = TimeZone.getTimeZone(id);
192  int offset = zone.getRawOffset() / 1000;
193  int hour = offset / 3600;
194  int minutes = (offset % 3600) / 60;
195  String item = String.format("(GMT%+d:%02d) %s", hour, minutes, id); //NON-NLS
196  timeZones.add(item);
197  }
198  } catch (IllegalStateException ex) {
199  // No current case.
200  }
201 
202  return timeZones;
203  }
204 
205  @Override
206  public void addActionListener(ActionListener l) {
207  getComponent().addActionListener(l);
208  }
209 
210  @Override
211  public boolean isValid() {
212  return this.getComponent().isValidSearch();
213  }
214 
218  static class ComboBoxRenderer extends JLabel implements ListCellRenderer<String> {
219 
220  JSeparator separator;
221 
222  ComboBoxRenderer() {
223  setOpaque(true);
224  setBorder(new EmptyBorder(1, 1, 1, 1));
225  separator = new JSeparator(JSeparator.HORIZONTAL);
226  }
227 
228  @Override
229  public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus) {
230  String str = (value == null) ? "" : value;
231  if (SEPARATOR.equals(str)) {
232  return separator;
233  }
234  if (isSelected) {
235  setBackground(list.getSelectionBackground());
236  setForeground(list.getSelectionForeground());
237  } else {
238  setBackground(list.getBackground());
239  setForeground(list.getForeground());
240  }
241  setFont(list.getFont());
242  setText(str);
243  return this;
244  }
245  }
246 
247  private class CasePropertyChangeListener implements PropertyChangeListener {
248 
249  @Override
250  public void propertyChange(PropertyChangeEvent evt) {
251  switch (Case.Events.valueOf(evt.getPropertyName())) {
252  case CURRENT_CASE:
253  Object newValue = evt.getNewValue();
254  if (null != newValue) {
258  SwingUtilities.invokeLater(DateSearchFilter.this::updateTimeZoneList);
259  }
260  break;
261  case DATA_SOURCE_ADDED:
262  case DATA_SOURCE_DELETED:
269  try {
271  SwingUtilities.invokeLater(DateSearchFilter.this::updateTimeZoneList);
272  } catch (IllegalStateException notUsed) {
276  }
277  break;
278  }
279  }
280  }
281 }

Copyright © 2012-2016 Basis Technology. Generated on: Mon Apr 24 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.