Autopsy  3.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 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.border.EmptyBorder;
41 
42 import org.openide.util.NbBundle;
44 
49 class DateSearchFilter extends AbstractFileSearchFilter<DateSearchPanel> {
50 
51  private static final String NONE_SELECTED_MESSAGE = NbBundle.getMessage(DateSearchFilter.class, "DateSearchFilter.noneSelectedMsg.text");
52  private static final DateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy");
53  private static final String SEPARATOR = "SEPARATOR"; //NON-NLS
54 
58  DateSearchFilter() {
59  this(new DateSearchPanel(DATE_FORMAT, DateSearchFilter.createTimeZoneList()));
60  }
61 
62  private DateSearchFilter(DateSearchPanel panel) {
63  super(panel);
64  Case.addPropertyChangeListener(this.new CasePropertyChangeListener());
65  }
66 
67  @Override
68  public boolean isEnabled() {
69  return this.getComponent().getDateCheckBox().isSelected();
70  }
71 
72  @Override
73  public String getPredicate() throws FilterValidationException {
74  String addQuery = "1";
75  DateSearchPanel panel = this.getComponent();
76 
77  // first, get the selected timeZone from the dropdown list
78  String tz = this.getComponent().getTimeZoneComboBox().getSelectedItem().toString();
79  String tzID = tz.substring(tz.indexOf(" ") + 1); // 1 index after the space is the ID
80  TimeZone selectedTZ = TimeZone.getTimeZone(tzID); //
81 
82  // convert the date from the selected timezone to get the GMT
83  long fromDate = 0;
84  String startDateValue = panel.getDateFromTextField().getText();
85  Calendar startDate = null;
86  try {
87  DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
88  sdf.setTimeZone(selectedTZ); // get the time in the selected timezone
89  Date temp = sdf.parse(startDateValue);
90 
91  startDate = Calendar.getInstance(new SimpleTimeZone(0, "GMT")); //NON-NLS
92  startDate.setTime(temp); // convert to GMT
93  } catch (ParseException ex) {
94  // for now, no need to show the error message to the user her
95  }
96  if (!startDateValue.equals("")) {
97  if (startDate != null) {
98  fromDate = startDate.getTimeInMillis() / 1000; // divided by 1000 because we want to get the seconds, not miliseconds
99  }
100  }
101 
102  long toDate = 0;
103  String endDateValue = panel.getDateToTextField().getText();
104  Calendar endDate = null;
105  try {
106  DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
107  sdf.setTimeZone(selectedTZ); // get the time in the selected timezone
108  Date temp2 = sdf.parse(endDateValue);
109 
110  endDate = Calendar.getInstance(new SimpleTimeZone(0, "GMT")); //NON-NLS
111  endDate.setTime(temp2); // convert to GMT
112  endDate.set(Calendar.HOUR, endDate.get(Calendar.HOUR) + 24); // get the next 24 hours
113  } catch (ParseException ex) {
114  // for now, no need to show the error message to the user here
115  }
116  if (!endDateValue.equals("")) {
117  if (endDate != null) {
118  toDate = endDate.getTimeInMillis() / 1000; // divided by 1000 because we want to get the seconds, not miliseconds
119  }
120  }
121 
122  final boolean modifiedChecked = panel.getModifiedCheckBox().isSelected();
123  final boolean changedChecked = panel.getChangedCheckBox().isSelected();
124  final boolean accessedChecked = panel.getAccessedCheckBox().isSelected();
125  final boolean createdChecked = panel.getAccessedCheckBox().isSelected();
126 
127 
128  if (modifiedChecked || changedChecked || accessedChecked || createdChecked) {
129 
130  String subQuery = "0";
131 
132  if (modifiedChecked) {
133  subQuery += " or mtime between " + fromDate + " and " + toDate; //NON-NLS
134  }
135 
136  if (changedChecked) {
137  subQuery += " or ctime between " + fromDate + " and " + toDate; //NON-NLS
138  }
139 
140  if (accessedChecked) {
141  subQuery += " or atime between " + fromDate + " and " + toDate; //NON-NLS
142  }
143 
144  if (createdChecked) {
145  subQuery += " or crtime between " + fromDate + " and " + toDate; //NON-NLS
146  }
147 
148  addQuery += " and (" + subQuery + ")"; //NON-NLS
149  } else {
150  throw new FilterValidationException(NONE_SELECTED_MESSAGE);
151  }
152 
153  return addQuery;
154 
155  }
156 
157  private void updateTimeZoneList() {
158  this.getComponent().setTimeZones(DateSearchFilter.createTimeZoneList());
159  }
160 
161  private static List<String> createTimeZoneList() {
162 
163  List<String> timeZones = new ArrayList<String>();
164 
165  if (Case.existsCurrentCase()) {
166  // get the latest case
167  Case currentCase = Case.getCurrentCase(); // get the most updated case
168 
169  Set<TimeZone> caseTimeZones = currentCase.getTimeZone();
170  Iterator<TimeZone> iterator = caseTimeZones.iterator();
171  while (iterator.hasNext()) {
172  TimeZone zone = iterator.next();
173  int offset = zone.getRawOffset() / 1000;
174  int hour = offset / 3600;
175  int minutes = (offset % 3600) / 60;
176  String item = String.format("(GMT%+d:%02d) %s", hour, minutes, zone.getID()); //NON-NLS
177  timeZones.add(item);
178  }
179 
180  if (caseTimeZones.size() > 0) {
181  timeZones.add(SEPARATOR);
182  }
183 
184  // load and add all timezone
185  String[] ids = SimpleTimeZone.getAvailableIDs();
186  for (String id : ids) {
187  TimeZone zone = TimeZone.getTimeZone(id);
188  int offset = zone.getRawOffset() / 1000;
189  int hour = offset / 3600;
190  int minutes = (offset % 3600) / 60;
191  String item = String.format("(GMT%+d:%02d) %s", hour, minutes, id); //NON-NLS
192  timeZones.add(item);
193  }
194  }
195 
196  return timeZones;
197  }
198 
199  @Override
200  public void addActionListener(ActionListener l) {
201  getComponent().addActionListener(l);
202  }
203 
207  static class ComboBoxRenderer extends JLabel implements ListCellRenderer<String> {
208 
209  JSeparator separator;
210 
211  ComboBoxRenderer() {
212  setOpaque(true);
213  setBorder(new EmptyBorder(1, 1, 1, 1));
214  separator = new JSeparator(JSeparator.HORIZONTAL);
215  }
216 
217  @Override
218  public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus) {
219  String str = (value == null) ? "" : value;
220  if (SEPARATOR.equals(str)) {
221  return separator;
222  }
223  if (isSelected) {
224  setBackground(list.getSelectionBackground());
225  setForeground(list.getSelectionForeground());
226  } else {
227  setBackground(list.getBackground());
228  setForeground(list.getForeground());
229  }
230  setFont(list.getFont());
231  setText(str);
232  return this;
233  }
234  }
235 
236  private class CasePropertyChangeListener implements PropertyChangeListener {
237 
238  @Override
239  public void propertyChange(PropertyChangeEvent evt) {
240  String changed = evt.getPropertyName();
241  Object oldValue = evt.getOldValue();
242  Object newValue = evt.getNewValue();
243 
244  if (changed.equals(Case.Events.CURRENT_CASE.toString().toString())) {
245  // create or open a case
246  if (newValue != null) {
247  DateSearchFilter.this.updateTimeZoneList();
248  }
249  }
250 
251  // if the image is added to the case
252  if (changed.equals(Case.Events.DATA_SOURCE_ADDED.toString())) {
253  DateSearchFilter.this.updateTimeZoneList();
254  }
255 
256  // if the image is removed from the case
257  if (changed.equals(Case.Events.DATA_SOURCE_DELETED.toString())) {
258  DateSearchFilter.this.updateTimeZoneList();
259  }
260  }
261  }
262 }

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