001/** 002 * Copyright (C) 2014 Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/ 003 * 004 * This file is part of Dicoogle/dicoogle. 005 * 006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Dicoogle/dicoogle is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Dicoogle. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package pt.ua.dicoogle.core; 020 021import java.io.FileInputStream; 022import java.io.FileOutputStream; 023import java.io.ObjectInput; 024import java.io.ObjectInputStream; 025import java.io.ObjectOutput; 026import java.io.ObjectOutputStream; 027import java.util.ArrayList; 028import java.util.Iterator; 029import java.util.Observable; 030 031import pt.ua.dicoogle.sdk.Utils.Platform; 032 033/** 034 * 035 * @author Samuel da Costa Campos <samuelcampos@ua.pt> 036 */ 037public class QueryHistorySupport extends Observable { 038 039 //consider replacing this data type "ArrayList<SimpleEntry<String, Boolean>>" 040 private ArrayList<QueryHistoryEntry<String, Boolean>> queryHistory; 041 042 private static String fileName = Platform.homePath() + "QueryHistory.ser"; 043 044 private static QueryHistorySupport instance = null; 045 046 public static synchronized QueryHistorySupport getInstance() 047 { 048 if (instance == null) { 049 instance = new QueryHistorySupport(); 050 } 051 return instance; 052 } 053 054 private QueryHistorySupport(){ 055 if(!loadQueryHistory()) 056 queryHistory = new ArrayList<QueryHistoryEntry<String, Boolean>>(); 057 058 } 059 060 private boolean loadQueryHistory(){ 061 try { 062 ObjectInput in = new ObjectInputStream(new FileInputStream(fileName)); 063 queryHistory = (ArrayList<QueryHistoryEntry<String, Boolean>>) in.readObject(); 064 in.close(); 065 066 return true; 067 } catch (Exception ex) { 068 return false; 069 } 070 } 071 072 public Iterator<QueryHistoryEntry<String, Boolean>> getQueryHistory(){ 073 return queryHistory.iterator(); 074 } 075 076 public boolean saveQueryHistory(){ 077 try { 078 ObjectOutput out = new ObjectOutputStream(new FileOutputStream(fileName)); 079 out.writeObject(queryHistory); 080 out.close(); 081 082 return true; 083 } catch (Exception ex) { 084 return false; 085 } 086 } 087 088 public void addQuery(String query, boolean keywords){ 089 QueryHistoryEntry<String, Boolean> entry = new QueryHistoryEntry<String, Boolean>(query, keywords); 090 091 if(!queryHistory.contains(entry)) 092 queryHistory.add(entry); 093 094 setChanged(); 095 notifyObservers(); 096 } 097 098 public boolean deleteQuery(QueryHistoryEntry<String, Boolean> entry){ 099 boolean result = queryHistory.remove(entry); 100 101 setChanged(); 102 notifyObservers(); 103 104 return result; 105 } 106 107 public void deleteAll(){ 108 queryHistory.clear(); 109 110 setChanged(); 111 notifyObservers(); 112 } 113}