001: package net.sourceforge.squirrel_sql.plugins.favs;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021: import java.io.File;
022: import java.io.FileNotFoundException;
023: import java.io.IOException;
024: import java.util.Iterator;
025:
026: import net.sourceforge.squirrel_sql.client.IApplication;
027: import net.sourceforge.squirrel_sql.fw.util.DuplicateObjectException;
028: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
029: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
030: import net.sourceforge.squirrel_sql.fw.xml.XMLException;
031: import net.sourceforge.squirrel_sql.fw.xml.XMLObjectCache;
032:
033: /**
034: * XML cache of <CODE>Folder</CODE> objects.
035: */
036: public final class FoldersCache {
037: /** Logger for this class. */
038: private static final ILogger s_log = LoggerController
039: .createLogger(FoldersCache.class);
040:
041: /** Application API. */
042: @SuppressWarnings("unused")
043: private IApplication _app;
044:
045: /** Root folder. */
046: private Folder _rootFolder = null;
047:
048: private String _queriesFileName;
049:
050: public FoldersCache(IApplication app, File userSettingsFolder) {
051: super ();
052: _app = app;
053: _queriesFileName = userSettingsFolder.getAbsolutePath()
054: + File.separator + "queries.xml";
055: }
056:
057: public Folder getRootFolder() {
058: return _rootFolder;
059: }
060:
061: public void setRootFolder(Folder folder) {
062: _rootFolder = folder;
063: }
064:
065: void load() {
066: try {
067: if (new File(_queriesFileName).exists()) {
068: XMLObjectCache<Folder> cache = new XMLObjectCache<Folder>();
069: cache.load(_queriesFileName, getClass()
070: .getClassLoader());
071: Iterator<Folder> it = cache
072: .getAllForClass(Folder.class);
073: if (it.hasNext()) {
074: _rootFolder = it.next();
075: }
076: }
077: } catch (FileNotFoundException ignore) {
078: // first time user has run pgm.
079: } catch (XMLException ex) {
080: s_log.error("Error loading queries file: "
081: + _queriesFileName, ex);
082: } catch (DuplicateObjectException ex) {
083: s_log.error("Error loading queries file: "
084: + _queriesFileName, ex);
085: }
086: }
087:
088: /**
089: * Save cached objects.
090: */
091: void save() {
092: try {
093: XMLObjectCache<Folder> cache = new XMLObjectCache<Folder>();
094: try {
095: if (_rootFolder != null) {
096: cache.add(_rootFolder);
097: }
098: } catch (DuplicateObjectException ignore) {
099: }
100: cache.save(_queriesFileName);
101: } catch (IOException ex) {
102: s_log.error("Error occured saving queries to "
103: + _queriesFileName, ex);
104: } catch (XMLException ex) {
105: s_log.error("Error occured saving queries to "
106: + _queriesFileName, ex);
107: }
108: }
109: }
|