001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.bookmarks;
024:
025: import java.awt.datatransfer.DataFlavor;
026: import java.awt.datatransfer.UnsupportedFlavorException;
027: import java.io.IOException;
028: import java.util.Date;
029: import java.util.Enumeration;
030: import java.util.Vector;
031:
032: import org.isqlviewer.BindVariable;
033:
034: /**
035: * Bookmark for an SQL command(s).
036: * <p>
037: * Like any kind of bookmark metaphor this allows the creation of an object represent an SQL command or series of
038: * commands. A bookmark can also represent a prepared statement with well formed data objects parameterized and
039: * generated on the fly.
040: *
041: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
042: * @version 1.0
043: */
044: public class Bookmark extends BookmarkReference implements Cloneable {
045:
046: /**
047: * Standard data-transfer flavor for DnD operations.
048: */
049: public static final DataFlavor BOOKMARK_FLAVOR = new DataFlavor(
050: Bookmark.class, "iSQL-Viewer SQL Bookmark");
051:
052: private static final long serialVersionUID = 9004874440081925048L;
053: // raw SQL text for this bookmark.
054: private String commandText = null;
055: // milliseconds for when this bookmark was last used.
056: private Date lastAccess = null;
057: // milliseconds for when this bookmark was created.
058: private Date creationTime = null;
059: // count for number of times this bookmark has been used.
060: private long useCount = 0L;
061: // collection of prepared parameters if any...
062: private Vector<BindVariable> parameters = new Vector<BindVariable>();
063:
064: /**
065: * Creates a new bookmark as child to the given folder.
066: * <p>
067: * Creating a bookmark will implictly add itself to the parentFolder given.
068: *
069: * @param parentFolder that will contain this new bookmark instance.
070: * @throws NullPointerException if the parentFolder null.
071: */
072: public Bookmark() {
073:
074: }
075:
076: @Override
077: public Bookmark clone() {
078:
079: try {
080: Bookmark copy = (Bookmark) super .clone();
081:
082: return copy;
083: } catch (CloneNotSupportedException error) {
084: throw new Error(error);
085: }
086:
087: }
088:
089: @Override
090: public Object getTransferData(DataFlavor flavor)
091: throws UnsupportedFlavorException, IOException {
092:
093: if (BOOKMARK_FLAVOR.equals(flavor)) {
094: return this ;
095: } else if (flavor.isFlavorTextType()) {
096: return getCommandText();
097: }
098: throw new UnsupportedFlavorException(flavor);
099: }
100:
101: @Override
102: public DataFlavor[] getTransferDataFlavors() {
103:
104: return new DataFlavor[] { BOOKMARK_FLAVOR,
105: DataFlavor.stringFlavor };
106: }
107:
108: @Override
109: public boolean isDataFlavorSupported(DataFlavor flavor) {
110:
111: if (flavor != null) {
112: if (BOOKMARK_FLAVOR.equals(flavor)
113: || flavor.isFlavorTextType()) {
114: return true;
115: }
116: }
117: return false;
118: }
119:
120: /**
121: * Date in milliseconds when this bookmark was created.
122: * <p>
123: *
124: * @return timestamp in milliseconds when this bookmark was created.
125: */
126: public Date getCreationTime() {
127:
128: return creationTime;
129: }
130:
131: /**
132: * Sets the creation time of this bookmark.
133: * <p>
134: * Typically this method will only be called when the bookmark is restored from local disk. Explicitly calling this
135: * method otherwise will cause this value to essentially be invaild since it will not be true.
136: *
137: * @param creationTime in milliseconds of the date this bookmark was created.
138: */
139: public void setCreationTime(Date creationTime) {
140:
141: this .creationTime = creationTime;
142: }
143:
144: /**
145: * Gets the last time this bookmark was executed against a JDBC connection.
146: * <p>
147: *
148: * @return timestamp in milliseconds when this bookmark was last executed.
149: */
150: public Date getLastAccess() {
151:
152: return lastAccess;
153: }
154:
155: /**
156: * Sets the last time this bookmark was executed against a JDBC connection.
157: * <p>
158: *
159: * @param lastAccess in milliseconds when this bookmark was last executed.
160: */
161: public void setLastAccess(Date lastUsedTime) {
162:
163: this .lastAccess = lastUsedTime;
164: }
165:
166: /**
167: * Gets the SQL command or commands contained in this bookmark.
168: * <p>
169: * Multiplie SQL commands can be combined within a bookarm using non-quoted semi-colon ';' to seperate the
170: * statements.
171: *
172: * @return the SQL command or commands of this bookmark.
173: */
174: public String getCommandText() {
175:
176: return commandText;
177: }
178:
179: /**
180: * Sets the SQL command or commands for this bookmark.
181: * <p>
182: *
183: * @param commandText command that will be this bookmark.
184: */
185: public void setCommandText(String sql) {
186:
187: this .commandText = sql;
188: }
189:
190: /**
191: * Gets the number of times this bookmark has been executed.
192: * <p>
193: *
194: * @return number times this bookmark has been executed.
195: */
196: public long getUseCount() {
197:
198: return useCount;
199: }
200:
201: /**
202: * Sets the number of times this bookmark has been executed.
203: * <p>
204: * If a negative number is used, then zero will be inferred as the inteded use count.
205: *
206: * @param useCount new number of times this bookmark has been executed.
207: */
208: public void setUseCount(long useCount) {
209:
210: if (useCount < 0) {
211: this .useCount = 0;
212: } else {
213: this .useCount = useCount;
214: }
215: }
216:
217: /**
218: * Adds a bookmark parameter component to this bookmark.
219: * <p>
220: * When a bookmark represents a prepared statement, the bind variables for that statement are defined via the
221: * bookmark parameter data object.
222: * <p>
223: * A prepared statement will only be attempted if and only if there are bookmark parameters defined within the
224: * bookmark.
225: *
226: * @param parameter to add to internal list of parameters.
227: * @return <tt>true</tt> if the bookmark parameter was successfully added to this bookmark.
228: */
229: public boolean add(BindVariable parameter) {
230:
231: return parameters.add(parameter);
232: }
233:
234: /**
235: * Removes all bookmark parameters from this bookmark.
236: * <p>
237: * This call can be used to quickly ensure that this bookmark will not be interpreted as a prepared statement.
238: */
239: public void clearParameters() {
240:
241: parameters.clear();
242: }
243:
244: /**
245: * Gets an enumeration of the bookmark parameter data.
246: * <p>
247: *
248: * @return iterable object of bookmark parameters within this bookmark.
249: */
250: public Enumeration<BindVariable> variables() {
251:
252: return parameters.elements();
253: }
254:
255: /**
256: * Checks to see if this bookmark contains any parameters.
257: * <p>
258: *
259: * @return <tt>true</tt> if there are bookmark parameters contained in this instance.
260: */
261: public boolean containsParameters() {
262:
263: return !parameters.isEmpty();
264: }
265:
266: /**
267: * Removes a bookmark paramter by reference.
268: * <p>
269: *
270: * @param parameter to be removed from this bookmark.
271: * @return <tt>true</tt> if the parameter was successfully removed from this bookmark.
272: */
273: public boolean removeParameter(BindVariable parameter) {
274:
275: return parameters.remove(parameter);
276: }
277:
278: /**
279: * Gets the number of prepared paramters that exist in this bookmark.
280: * <p>
281: *
282: * @return number of parameters that exist in this bookmark.
283: */
284: public int paremeterCount() {
285:
286: return parameters.size();
287: }
288:
289: /**
290: * @return
291: */
292: public BookmarkReference toBookmarkReference() {
293:
294: BookmarkReference reference = new BookmarkReference();
295: reference.setFavorite(isFavorite());
296: reference.setFolder(getFolder());
297: reference.setId(getId());
298: reference.setName(getName());
299: reference.setColorLabel(getColorLabel());
300: return reference;
301: }
302: }
|