001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.sql;
021:
022: /////////////////////////
023: //$Archive: /SOFIA/SourceCode/com/salmonllc/sql/AutoRetrieveCriteria.java $
024: //$Author: Dan $
025: //$Revision: 15 $
026: //$Modtime: 8/20/03 2:14p $
027: /////////////////////////
028:
029: import java.text.SimpleDateFormat;
030: import java.util.*;
031: import java.io.Serializable;
032: import javax.servlet.http.*;
033:
034: /**
035: * This class is used to store information used to decide how to autoretrieve a datastore created inside a JSP page.
036: * The class is populated by datasource selection criteria tags and passed to the datastore providing enough information to build a where clause.
037: * Generally this class is used by the framework and does not need to be created directly in an application.
038: */
039:
040: public class AutoRetrieveCriteria implements Serializable {
041:
042: Vector _items = new Vector();
043: boolean _haveParmsChanged = false;
044:
045: class CriteriaLine implements Serializable {
046: String prefix;
047: String column;
048: String operator;
049: String value;
050: String suffix;
051: String connector;
052: String work;
053: Hashtable oldValues;
054: boolean quoted;
055: }
056:
057: private static String replace(String sString, String sOldString,
058: String sNewString) {
059: StringBuffer sbString = new StringBuffer(sString);
060: while (true) {
061: int iIndex = sbString.toString().indexOf(sOldString);
062: if (iIndex < 0)
063: break;
064: sbString.replace(iIndex, iIndex + sOldString.length(),
065: sNewString);
066: }
067: return sbString.toString();
068: }
069:
070: /**
071: * Creates an new AutoRetrieveCriteria object
072: */
073:
074: public AutoRetrieveCriteria() {
075: super ();
076: }
077:
078: /**
079: * Adds a criteria line to the list of criteria items.
080: */
081: public void addCriteria(String prefix, String column, String op,
082: String value, String suffix, String connector) {
083: CriteriaLine l = new CriteriaLine();
084: l.prefix = prefix;
085: l.column = column;
086: l.operator = op;
087: l.work = value;
088: l.suffix = suffix;
089: l.connector = connector;
090: l.oldValues = new Hashtable();
091:
092: if (value != null) {
093: l.work = value.trim();
094: if (l.work.startsWith("'") && l.work.endsWith("'")) {
095: l.quoted = true;
096: }
097: }
098: _items.addElement(l);
099:
100: }
101:
102: /**
103: * Returns the column for a particular item. The index must be between 0 and getCriteriaCount() - 1;
104: */
105: public String getColumn(int index) {
106: CriteriaLine l = (CriteriaLine) _items.elementAt(index);
107: return l.column;
108: }
109:
110: /**
111: * Sets the column for a particular item. The index must be between 0 and getCriteriaCount() - 1;
112: */
113: public void setColumn(int index, String column) {
114: CriteriaLine l = (CriteriaLine) _items.elementAt(index);
115: l.column = column;
116: }
117:
118: /**
119: * Returns the connector for a particular item. The index must be between 0 and getCriteriaCount() - 1;
120: */
121: public String getConnector(int index) {
122: CriteriaLine l = (CriteriaLine) _items.elementAt(index);
123: return l.connector;
124: }
125:
126: /**
127: * Returns the number of selection criteria items
128: */
129: public int getCriteriaCount() {
130: return _items.size();
131: }
132:
133: /**
134: * Returns the operator for a particular item. The index must be between 0 and getCriteriaCount() - 1;
135: */
136: public String getOperator(int index) {
137: CriteriaLine l = (CriteriaLine) _items.elementAt(index);
138: return l.operator;
139: }
140:
141: /**
142: * Returns the prefix for a particular item. The index must be between 0 and getCriteriaCount() - 1;
143: */
144: public String getPrefix(int index) {
145: CriteriaLine l = (CriteriaLine) _items.elementAt(index);
146: return l.prefix;
147: }
148:
149: /**
150: * Returns the suffix for a particular item. The index must be between 0 and getCriteriaCount() - 1;
151: */
152: public String getSuffix(int index) {
153: CriteriaLine l = (CriteriaLine) _items.elementAt(index);
154: return l.suffix;
155: }
156:
157: /**
158: * Returns the value for a particular item. The index must be between 0 and getCriteriaCount() - 1;
159: */
160: public String getValue(int index) {
161: CriteriaLine l = (CriteriaLine) _items.elementAt(index);
162: return l.value;
163: }
164:
165: /**
166: * Returns true if any of the parameter values have changed in the last call to the setParms method.
167: */
168: public boolean haveParmsChanged() {
169: return _haveParmsChanged;
170: }
171:
172: /**
173: * Sets the values of the parms from the servlet request line and sets the "changed" flag.
174: */
175: public void setParms(HttpServletRequest req) {
176: _haveParmsChanged = false;
177: for (int i = 0; i < _items.size();i++) {
178: CriteriaLine l = (CriteriaLine) _items.elementAt(i);
179: l.value=l.work;
180: Enumeration enum=req.getParameterNames();
181: while (enum.hasMoreElements()) {
182: String sParam=(String)enum.nextElement();
183: if (l.value.indexOf("%"+sParam)>=0) {
184: String val=req.getParameter(sParam);
185: if (val==null)
186: val="";
187: String oldValue=(String)l.oldValues.get(sParam);
188: if (oldValue==null) {
189: l.oldValues.put(sParam,val);
190: _haveParmsChanged=true;
191: }
192: else {
193: if (! oldValue.equals(val)) {
194: l.oldValues.put(sParam,val);
195: _haveParmsChanged = true;
196: }
197: }
198: l.value=replace(l.value,"%"+sParam,val);
199: }
200: }
201: l.value=replace(l.value,"%%","%");
202: if (l.value.indexOf("SYSTEM.$CURRDATE")>=0) {
203: java.util.Date currDate = new java.util.Date(System.currentTimeMillis());
204: SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
205: l.value=replace(l.value,"SYSTEM.$CURRDATE",df.format(currDate));
206: // l.value=replace(l.value,"SYSTEM.$CURRDATE",currDate.toString());
207: }
208: }
209:
210:}
211: }
|