PageRenderTime 313ms CodeModel.GetById 91ms app.highlight 138ms RepoModel.GetById 74ms app.codeStats 0ms

/src/mpv5/db/common/QueryData.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 226 lines | 111 code | 24 blank | 91 comment | 17 complexity | 495d0d8b6ff8d5cec1208c99e342c125 MD5 | raw file
  1/*
  2 *  This file is part of YaBS.
  3 *
  4 *  YaBS is free software: you can redistribute it and/or modify
  5 *  it under the terms of the GNU General Public License as published by
  6 *  the Free Software Foundation, either version 3 of the License, or
  7 *  (at your option) any later version.
  8 *
  9 *  YaBS is distributed in the hope that it will be useful,
 10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 *  GNU General Public License for more details.
 13 *
 14 *  You should have received a copy of the GNU General Public License
 15 *  along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 16 */
 17package mpv5.db.common;
 18
 19import java.util.ArrayList;
 20import java.util.HashMap;
 21import java.util.List;
 22import mpv5.logging.Log;
 23import mpv5.data.*;
 24
 25/**
 26 * This class is used to put any kind of data before passing it to the <code>QueryHandler</code> for transmission.
 27 * 
 28 */
 29public class QueryData {
 30
 31    @Override
 32    public String toString() {
 33        String[] a = getKeys();
 34        String c = "";
 35
 36        for (int i = 0; i < a.length; i++) {
 37            c += a[i] + " = " + getValue(a[i]).getWrapper() + getValue(a[i]).toString() + getValue(a[i]).getWrapper() + ", ";
 38        }
 39
 40        if (c.length() > 2) {
 41            c = c.substring(0, c.length() - 2);
 42        }
 43        return c;
 44    }
 45
 46    /**
 47     * Generate a <code>SaveString<code/>
 48     * @param s
 49     * @return
 50     */
 51    public static SaveString getSaveStringFor(String s) {
 52        return new SaveString(s, true);
 53    }
 54    private HashMap<String, SaveString> list = new HashMap<String, SaveString>();
 55
 56    /**
 57     * This is a legacy constructor, to speed up the conversion of old style data into new style data
 58     * what  : {set, values}
 59     * @param what
 60     */
 61    public QueryData(String[] what) {
 62        String[] keys = what[0].split(",");
 63        String[] vals = what[1].split(",");
 64
 65        for (int i = 0; i < vals.length; i++) {
 66            add(keys[i], vals[i]);
 67        }
 68    }
 69
 70    public QueryData() {
 71    }
 72
 73    /**
 74     * Adds a key with a value
 75     * @param <T> 
 76     * @param key
 77     * @param value
 78     */
 79    public <T extends Number> void add(String key, T value) {
 80        String string = String.valueOf(value);
 81        list.put(key, new SaveString(string, false));
 82    }
 83    
 84    /**
 85     * 
 86     * @param criteria 
 87     */
 88    public void add(QueryCriteria2 criteria){
 89        List<QueryParameter> d = criteria.getFields();
 90        for (int i = 0; i < d.size(); i++) {
 91            QueryParameter o = d.get(i);
 92            add(o.getKey(), o.getValue());
 93        }
 94    }
 95
 96    /**
 97     * Adds a key with a value
 98     * @param key
 99     * @param value
100     */
101    public void add(String key, boolean value) {
102        if (value) {
103            list.put(key, new SaveString("1", false));
104        } else {
105            list.put(key, new SaveString("0", false));
106        }
107    }
108
109    /**
110     * Adds a key with a value
111     * @param key
112     * @param value
113     */
114    public void add(String key, String value) {
115        list.put(key, new SaveString(value, true));
116    }
117
118    /**
119     *
120     * @return True if there is at least one value
121     */
122    public boolean hasValues() {
123        return !list.isEmpty();
124    }
125
126    /**
127     * Mask backslashes with even more backslashes
128     * @param string
129     * @return
130     */
131    public String maskBackslashes(String string) {
132        Log.Debug(QueryData.class, "Masking Backslashes!");
133        return new SaveString(string.replaceAll("\\\\", "\\\\\\\\"), true).toString();
134    }
135
136    /**
137     *
138     * @return An array of keys
139     */
140    public String[] getKeys() {
141        return list.keySet().toArray(new String[]{});
142    }
143
144    /**
145     * returns the value of the given key
146     * @param key
147     * @return
148     */
149    public SaveString getValue(String key) {
150        return list.get(key);
151    }
152
153    /**
154     * Generates a comma separated String represantion of the current values<br/>
155     * with String values wrapped in single quotes
156     * @return
157     */
158    public String getValuesString() {
159        String[] k = getKeys();
160        String s = "";
161
162        for (int i = 0; i < k.length; i++) {
163            mpv5.db.common.SaveString v = getValue(k[i]);
164            s += v.getWrapper() + v.toString() + v.getWrapper() + ",";
165        }
166        if (s.length() > 1) {
167            return s.substring(0, s.length() - 1);
168        } else {
169            return s;
170        }
171    }
172
173    /**
174     * 
175     * @return An array of all values, in getKeys() - order
176     */
177    public String[] getValues() {
178        return getValuesString().split(",");
179    }
180
181    /**
182     * Generates a comma separated String representation of the current keys
183     * @return
184     */
185    public String getKeysString() {
186        String[] k = getKeys();
187        String s = "";
188
189        for (int i = 0; i < k.length; i++) {
190            s += k[i] + ",";
191        }
192        if (s.length() > 1) {
193            return s.substring(0, s.length() - 1);
194        } else {
195            return s;
196        }
197    }
198
199    /**
200     * This method acts as bridge between  {@link PropertyStore} and {@link QueryData}
201     * All data will be of type <code>String</code>
202     * @param properties
203     */
204    public void parse(PropertyStore properties) {
205        List<String[]> data = properties.getList();
206        for (int i = 0; i < data.size(); i++) {
207            String[] s = data.get(i);
208            add(s[0], s[1]);
209        }
210    }
211
212    /**
213     * Adds a key with a value
214     * @param columnName
215     * @param value
216     */
217    public void add(String columnName, Object value) {
218        if (value instanceof Number) {
219            add(columnName, (Number) value);
220        } else if (value instanceof Boolean) {
221            add(columnName, (Boolean) value);
222        } else {
223            add(columnName, String.valueOf(value));
224        }
225    }
226}