PageRenderTime 29ms CodeModel.GetById 15ms RepoModel.GetById 2ms 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
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  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. */
  17. package mpv5.db.common;
  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import mpv5.logging.Log;
  22. import mpv5.data.*;
  23. /**
  24. * This class is used to put any kind of data before passing it to the <code>QueryHandler</code> for transmission.
  25. *
  26. */
  27. public class QueryData {
  28. @Override
  29. public String toString() {
  30. String[] a = getKeys();
  31. String c = "";
  32. for (int i = 0; i < a.length; i++) {
  33. c += a[i] + " = " + getValue(a[i]).getWrapper() + getValue(a[i]).toString() + getValue(a[i]).getWrapper() + ", ";
  34. }
  35. if (c.length() > 2) {
  36. c = c.substring(0, c.length() - 2);
  37. }
  38. return c;
  39. }
  40. /**
  41. * Generate a <code>SaveString<code/>
  42. * @param s
  43. * @return
  44. */
  45. public static SaveString getSaveStringFor(String s) {
  46. return new SaveString(s, true);
  47. }
  48. private HashMap<String, SaveString> list = new HashMap<String, SaveString>();
  49. /**
  50. * This is a legacy constructor, to speed up the conversion of old style data into new style data
  51. * what : {set, values}
  52. * @param what
  53. */
  54. public QueryData(String[] what) {
  55. String[] keys = what[0].split(",");
  56. String[] vals = what[1].split(",");
  57. for (int i = 0; i < vals.length; i++) {
  58. add(keys[i], vals[i]);
  59. }
  60. }
  61. public QueryData() {
  62. }
  63. /**
  64. * Adds a key with a value
  65. * @param <T>
  66. * @param key
  67. * @param value
  68. */
  69. public <T extends Number> void add(String key, T value) {
  70. String string = String.valueOf(value);
  71. list.put(key, new SaveString(string, false));
  72. }
  73. /**
  74. *
  75. * @param criteria
  76. */
  77. public void add(QueryCriteria2 criteria){
  78. List<QueryParameter> d = criteria.getFields();
  79. for (int i = 0; i < d.size(); i++) {
  80. QueryParameter o = d.get(i);
  81. add(o.getKey(), o.getValue());
  82. }
  83. }
  84. /**
  85. * Adds a key with a value
  86. * @param key
  87. * @param value
  88. */
  89. public void add(String key, boolean value) {
  90. if (value) {
  91. list.put(key, new SaveString("1", false));
  92. } else {
  93. list.put(key, new SaveString("0", false));
  94. }
  95. }
  96. /**
  97. * Adds a key with a value
  98. * @param key
  99. * @param value
  100. */
  101. public void add(String key, String value) {
  102. list.put(key, new SaveString(value, true));
  103. }
  104. /**
  105. *
  106. * @return True if there is at least one value
  107. */
  108. public boolean hasValues() {
  109. return !list.isEmpty();
  110. }
  111. /**
  112. * Mask backslashes with even more backslashes
  113. * @param string
  114. * @return
  115. */
  116. public String maskBackslashes(String string) {
  117. Log.Debug(QueryData.class, "Masking Backslashes!");
  118. return new SaveString(string.replaceAll("\\\\", "\\\\\\\\"), true).toString();
  119. }
  120. /**
  121. *
  122. * @return An array of keys
  123. */
  124. public String[] getKeys() {
  125. return list.keySet().toArray(new String[]{});
  126. }
  127. /**
  128. * returns the value of the given key
  129. * @param key
  130. * @return
  131. */
  132. public SaveString getValue(String key) {
  133. return list.get(key);
  134. }
  135. /**
  136. * Generates a comma separated String represantion of the current values<br/>
  137. * with String values wrapped in single quotes
  138. * @return
  139. */
  140. public String getValuesString() {
  141. String[] k = getKeys();
  142. String s = "";
  143. for (int i = 0; i < k.length; i++) {
  144. mpv5.db.common.SaveString v = getValue(k[i]);
  145. s += v.getWrapper() + v.toString() + v.getWrapper() + ",";
  146. }
  147. if (s.length() > 1) {
  148. return s.substring(0, s.length() - 1);
  149. } else {
  150. return s;
  151. }
  152. }
  153. /**
  154. *
  155. * @return An array of all values, in getKeys() - order
  156. */
  157. public String[] getValues() {
  158. return getValuesString().split(",");
  159. }
  160. /**
  161. * Generates a comma separated String representation of the current keys
  162. * @return
  163. */
  164. public String getKeysString() {
  165. String[] k = getKeys();
  166. String s = "";
  167. for (int i = 0; i < k.length; i++) {
  168. s += k[i] + ",";
  169. }
  170. if (s.length() > 1) {
  171. return s.substring(0, s.length() - 1);
  172. } else {
  173. return s;
  174. }
  175. }
  176. /**
  177. * This method acts as bridge between {@link PropertyStore} and {@link QueryData}
  178. * All data will be of type <code>String</code>
  179. * @param properties
  180. */
  181. public void parse(PropertyStore properties) {
  182. List<String[]> data = properties.getList();
  183. for (int i = 0; i < data.size(); i++) {
  184. String[] s = data.get(i);
  185. add(s[0], s[1]);
  186. }
  187. }
  188. /**
  189. * Adds a key with a value
  190. * @param columnName
  191. * @param value
  192. */
  193. public void add(String columnName, Object value) {
  194. if (value instanceof Number) {
  195. add(columnName, (Number) value);
  196. } else if (value instanceof Boolean) {
  197. add(columnName, (Boolean) value);
  198. } else {
  199. add(columnName, String.valueOf(value));
  200. }
  201. }
  202. }