Guest User

Untitled

a guest
Jan 12th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.54 KB | None | 0 0
  1. Index: src/main/java/com/xpn/xwiki/objects/classes/ListClass.java
  2. ===================================================================
  3. --- src/main/java/com/xpn/xwiki/objects/classes/ListClass.java (revision 31104)
  4. +++ src/main/java/com/xpn/xwiki/objects/classes/ListClass.java (working copy)
  5. @@ -502,7 +502,7 @@
  6. }
  7. radio.addElement(getDisplayValue(rawvalue, name, map, context));
  8.  
  9. - buffer.append("<label class=\"xwiki-form-listclass\" for=\"xwiki-form-" + name + "-" + count++ + "\">");
  10. + buffer.append("<label class=\"xwiki-form-listclass\" for=\"xwiki-form-" + name + "-" + count + "id=\"xwiki-form-" + name + "-" + count++ + "\">");
  11. buffer.append(radio.toString());
  12. buffer.append("</label>");
  13. }
  14. @@ -600,75 +600,122 @@
  15.  
  16. public abstract List<String> getList(XWikiContext context);
  17.  
  18. + public List<String> getValidatedList(XWikiContext context)
  19. + {
  20. + List<String> result = getList(context);
  21. +
  22. + String regexp = getValidationRegExp();
  23. + if ((regexp == null) || (regexp.trim().equals(""))) {
  24. + return result;
  25. + }
  26. +
  27. + if (isMultiSelect()) {
  28. + for (Iterator<String> it = result.iterator(); it.hasNext();) {
  29. + String rawvalue = it.next();
  30. + if (!context.getUtil().match(regexp, "[" + rawvalue + "]")) {
  31. + it.remove();
  32. + }
  33. + }
  34. + } else {
  35. + for (Iterator<String> it = result.iterator(); it.hasNext();) {
  36. + String rawvalue = it.next();
  37. + if (!context.getUtil().match(regexp, rawvalue)) {
  38. + it.remove();
  39. + }
  40. + }
  41. + }
  42. +
  43. + return result;
  44. + }
  45. +
  46. public abstract Map<String, ListItem> getMap(XWikiContext context);
  47.  
  48. - @Override
  49. public String displaySearch(String name, String prefix, XWikiCriteria criteria, XWikiContext context)
  50. {
  51. + return displaySearch(name, prefix, criteria, true, context);
  52. + }
  53. +
  54. + public String displaySearch(String name, String prefix, XWikiCriteria criteria, boolean multiple,
  55. + XWikiContext context)
  56. + {
  57. if (getDisplayType().equals("input")) {
  58. return super.displaySearch(name, prefix, criteria, context);
  59. - } else if (getDisplayType().equals("radio") || getDisplayType().equals("checkbox")) {
  60. - return displayRadioSearch(name, prefix, criteria, context);
  61. } else {
  62. - return displaySelectSearch(name, prefix, criteria, context);
  63. + List<String> list;
  64. + if (multiple) {
  65. + list = getValidatedList(context);
  66. + } else {
  67. + list = getList(context);
  68. + }
  69. +
  70. + if (getDisplayType().equals("radio") || getDisplayType().equals("checkbox") || list.size() < 6) {
  71. + return displayRadioSearch(name, prefix, criteria, multiple, list, context);
  72. + } else {
  73. + return displaySelectSearch(name, prefix, criteria, multiple, list, context);
  74. + }
  75. }
  76. }
  77.  
  78. - protected String displayRadioSearch(String name, String prefix, XWikiCriteria criteria, XWikiContext context)
  79. + protected String displayRadioSearch(String name, String prefix, XWikiCriteria criteria, boolean multiple,
  80. + List<String> list, XWikiContext context)
  81. {
  82. StringBuffer buffer = new StringBuffer();
  83. - List<String> list = getList(context);
  84. - List<String> selectlist = new ArrayList<String>();
  85.  
  86. - /*
  87. - * BaseProperty prop = (BaseProperty)object.safeget(name); if (prop==null) { selectlist = new ArrayList(); }
  88. - * else if ((prop instanceof ListProperty)||(prop instanceof DBStringListProperty)) { selectlist = (List)
  89. - * prop.getValue(); } else { selectlist = new ArrayList(); selectlist.add(prop.getValue()); }
  90. - */
  91. + Map<String, ListItem> map = getMap(context);
  92.  
  93. + String fieldFullName = getFieldFullName();
  94. + String[] selectArray = ((String[]) criteria.getParameter(fieldFullName));
  95. + List<String> selectlist = (selectArray != null) ? Arrays.asList(selectArray) : new ArrayList<String>();
  96. +
  97. // Add options from Set
  98. - for (Iterator<String> it = list.iterator(); it.hasNext();) {
  99. - String rawvalue = it.next();
  100. + int count = 0;
  101. + for (String rawvalue : list) {
  102. String value = getElementValue(rawvalue);
  103. - String display = getDisplayValue(rawvalue, name, getMap(context), context);
  104. input radio =
  105. - new input(getDisplayType().equals("radio") ? input.radio : input.checkbox, prefix + name, value);
  106. + new input((getDisplayType().equals("radio") && !multiple) ? input.radio : input.checkbox,
  107. + prefix + name, value);
  108.  
  109. if (selectlist.contains(value)) {
  110. radio.setChecked(true);
  111. }
  112. - radio.addElement(display);
  113. + radio.addElement(getDisplayValue(rawvalue, name, map, context));
  114. + buffer.append("<span class=\"xwiki-form-listclass\" id=\"xwiki-form-" + name + "-" + count++ + "\">");
  115. buffer.append(radio.toString());
  116. - if (it.hasNext()) {
  117. - buffer.append("<br/>");
  118. - }
  119. + buffer.append("</span>");
  120. }
  121. +
  122. return buffer.toString();
  123. }
  124.  
  125. - protected String displaySelectSearch(String name, String prefix, XWikiCriteria criteria, XWikiContext context)
  126. + protected String displaySelectSearch(String name, String prefix, XWikiCriteria criteria, boolean multiple,
  127. + List<String> list, XWikiContext context)
  128. {
  129. select select = new select(prefix + name, 1);
  130. - select.setMultiple(true);
  131. - select.setSize(5);
  132. + select.setMultiple(multiple);
  133. + select.setSize((multiple && getSize() == 1) ? 5 : getSize());
  134. select.setName(prefix + name);
  135. select.setID(prefix + name);
  136.  
  137. - List<String> list = getList(context);
  138. + Map<String, ListItem> map = getMap(context);
  139. +
  140. + String sort = getSort();
  141. + if (!"none".equals(sort)) {
  142. + if ("id".equals(sort)) {
  143. + Collections.sort(list);
  144. + }
  145. + if ("value".equals(sort)) {
  146. + Collections.sort(list, new MapComparator(map));
  147. + }
  148. + }
  149. +
  150. String fieldFullName = getFieldFullName();
  151. String[] selectArray = ((String[]) criteria.getParameter(fieldFullName));
  152. List<String> selectlist = (selectArray != null) ? Arrays.asList(selectArray) : new ArrayList<String>();
  153.  
  154. - /*
  155. - * BaseProperty prop = (BaseProperty)object.safeget(name); if (prop==null) { selectlist = new ArrayList(); }
  156. - * else if ((prop instanceof ListProperty)||(prop instanceof DBStringListProperty)) { selectlist = (List)
  157. - * prop.getValue(); } else { selectlist = new ArrayList(); selectlist.add(prop.getValue()); }
  158. - */
  159. -
  160. // Add options from Set
  161. for (String rawvalue : list) {
  162. String value = getElementValue(rawvalue);
  163. - String display = getDisplayValue(rawvalue, name, getMap(context), context);
  164. + String display = getDisplayValue(rawvalue, name, map, context);
  165. option option = new option(display, value);
  166. option.addElement(display);
  167. if (selectlist.contains(value)) {
  168. @@ -684,21 +731,48 @@
  169. public void makeQuery(Map<String, Object> map, String prefix, XWikiCriteria query, List<String> criteriaList)
  170. {
  171. Object values = map.get(prefix);
  172. - if ((values == null) || (values.equals(""))) {
  173. + if ((values != null) && (!values.equals(""))) {
  174. + if (isMultiSelect()) {
  175. + if (values instanceof String) {
  176. + // general comparison 'like' - tests at least one value like
  177. + criteriaList.add("jcr:like(@xp:" + getName() + ",'%" + values.toString() + "%')");
  178. + } else {
  179. + String[] valuesarray = (String[]) values;
  180. + String[] criteriaarray = new String[valuesarray.length];
  181. + for (int i = 0; i < valuesarray.length; i++) {
  182. + criteriaarray[i] = "jcr:like(@xp:" + getName() + ",'%" + valuesarray[i] + "%')";
  183. + }
  184. + criteriaList.add("(" + StringUtils.join(criteriaarray, " or ") + ")");
  185. + }
  186. + } else {
  187. + if (values instanceof String) {
  188. + // general comparison '=' - tests at least one value =
  189. + criteriaList.add("@xp:" + getName() + "='" + values.toString() + "'");
  190. + } else {
  191. + String[] valuesarray = (String[]) values;
  192. + String[] criteriaarray = new String[valuesarray.length];
  193. + for (int i = 0; i < valuesarray.length; i++) {
  194. + criteriaarray[i] = "@xp:" + getName() + "='" + valuesarray[i] + "'";
  195. + }
  196. + criteriaList.add("(" + StringUtils.join(criteriaarray, " or ") + ")");
  197. + }
  198. + }
  199. +
  200. return;
  201. }
  202.  
  203. - if (values instanceof String) {
  204. - // general comparison '=' - tests at least one value =
  205. - criteriaList.add("@xp:" + getName() + "='" + values.toString() + "'");
  206. - } else {
  207. - String[] valuesarray = (String[]) values;
  208. - String[] criteriaarray = new String[valuesarray.length];
  209. - for (int i = 0; i < valuesarray.length; i++) {
  210. - criteriaarray[i] = "@xp:" + getName() + "='" + valuesarray[i] + "'";
  211. - }
  212. - criteriaList.add("(" + StringUtils.join(criteriaarray, " or ") + ")");
  213. + values = map.get(prefix + "lessthan");
  214. + if ((values != null) && (!values.equals("")) && (values instanceof String)) {
  215. + criteriaList.add("@xp:" + getName() + "<='" + values.toString() + "'");
  216. + return;
  217. }
  218. +
  219. + values = map.get(prefix + "morethan");
  220. + if ((values != null) && (!values.equals("")) && (values instanceof String)) {
  221. + criteriaList.add("@xp:" + getName() + ">='" + values.toString() + "'");
  222. + return;
  223. + }
  224. +
  225. return;
  226. }
  227.  
  228. @@ -708,6 +782,19 @@
  229. String[] data = map.get("");
  230. if (data != null) {
  231. query.setParam(getObject().getName() + "_" + getName(), data);
  232. + return;
  233. }
  234. +
  235. + data = map.get("lessthan");
  236. + if (data != null && data.length == 1) {
  237. + query.setParam(getObject().getName() + "_" + getName() + "_lessthan", data[0]);
  238. + return;
  239. + }
  240. +
  241. + data = map.get("morethan");
  242. + if (data != null && data.length == 1) {
  243. + query.setParam(getObject().getName() + "_" + getName() + "_morethan", data[0]);
  244. + return;
  245. + }
  246. }
  247. }
  248. Index: src/main/java/com/xpn/xwiki/objects/classes/BooleanClass.java
  249. ===================================================================
  250. --- src/main/java/com/xpn/xwiki/objects/classes/BooleanClass.java (revision 31104)
  251. +++ src/main/java/com/xpn/xwiki/objects/classes/BooleanClass.java (working copy)
  252. @@ -20,6 +20,7 @@
  253. */
  254. package com.xpn.xwiki.objects.classes;
  255.  
  256. +import java.util.List;
  257. import java.util.Map;
  258.  
  259. import org.apache.commons.lang.StringUtils;
  260. @@ -353,46 +354,88 @@
  261. }
  262. }
  263.  
  264. - @Override
  265. public String displaySearch(String name, String prefix, XWikiCriteria criteria, XWikiContext context)
  266. {
  267. if (getDisplayType().equals("input")) {
  268. return super.displaySearch(name, prefix, criteria, context);
  269. - } else if (getDisplayType().equals("radio")) {
  270. - return displayCheckboxSearch(name, prefix, criteria, context);
  271. } else {
  272. - return displaySelectSearch(name, prefix, criteria, context);
  273. + return displayRadioSearch(name, prefix, criteria, context);
  274. }
  275. }
  276.  
  277. - public String displaySelectSearch(String name, String prefix, XWikiCriteria criteria, XWikiContext context)
  278. + public String displayRadioSearch(String name, String prefix, XWikiCriteria criteria, XWikiContext context)
  279. {
  280. - select select = new select(prefix + name, 1);
  281. - select.setMultiple(true);
  282. - select.setSize(3);
  283. - String String0 = getDisplayValue(context, 0);
  284. - String String1 = getDisplayValue(context, 1);
  285. + StringBuffer buffer = new StringBuffer();
  286. + String StringNone = getDisplayValue(context, 2);
  287. + String StringTrue = getDisplayValue(context, 1);
  288. + String StringFalse = getDisplayValue(context, 0);
  289. + div[] inputs;
  290.  
  291. - option[] options = {new option("---", ""), new option(String1, "1"), new option(String0, "0")};
  292. - options[0].addElement("---");
  293. - options[1].addElement(String1);
  294. - options[2].addElement(String0);
  295. + input radioNone = new input(input.radio, prefix + name, "");
  296. + input radioTrue = new input(input.radio, prefix + name, "1");
  297. + input radioFalse = new input(input.radio, prefix + name, "0");
  298. + label labelNone = new label();
  299. + label labelTrue = new label();
  300. + label labelFalse = new label();
  301. + div divNone = new div();
  302. + div divTrue = new div();
  303. + div divFalse = new div();
  304. + labelNone.addElement(radioNone);
  305. + labelNone.addElement(StringNone);
  306. + divNone.addElement(labelNone);
  307. + labelTrue.addElement(radioTrue);
  308. + labelTrue.addElement(StringTrue);
  309. + divTrue.addElement(labelTrue);
  310. + labelFalse.addElement(radioFalse);
  311. + labelFalse.addElement(StringFalse);
  312. + divFalse.addElement(labelFalse);
  313.  
  314. - /*
  315. - * try { IntegerProperty prop = (IntegerProperty) object.safeget(name); if (prop!=null) { Integer ivalue =
  316. - * (Integer)prop.getValue(); if (ivalue!=null) { int value = ivalue.intValue(); if (value==1)
  317. - * options[1].setSelected(true); else if (value==0) options[2].setSelected(true); } else { int value =
  318. - * getDefaultValue(); if (value==1) options[1].setSelected(true); else if (value==0)
  319. - * options[2].setSelected(true); } } } catch (Exception e) { // This should not happen e.printStackTrace(); }
  320. - */
  321. - select.addElement(options);
  322. - return select.toString();
  323. + radioNone.setID(prefix + name + "_none");
  324. + labelNone.setFor(prefix + name + "_none");
  325. +
  326. + radioTrue.setID(prefix + name);
  327. + labelTrue.setFor(prefix + name);
  328. +
  329. + radioFalse.setID(prefix + name + "_false");
  330. + labelFalse.setFor(prefix + name + "_false");
  331. +
  332. + inputs = new div[] {divNone, divTrue, divFalse};
  333. +
  334. + String fieldFullName = getFieldFullName();
  335. + Integer ivalue = (Integer) criteria.getParameter(fieldFullName);
  336. +
  337. + if (ivalue != null) {
  338. + int value = ivalue.intValue();
  339. + if (value == 1)
  340. + radioTrue.setChecked(true);
  341. + else if (value == 0)
  342. + radioFalse.setChecked(true);
  343. + else
  344. + radioNone.setChecked(true);
  345. + } else {
  346. + radioNone.setChecked(true);
  347. + }
  348. +
  349. + for (int i = 0; i < inputs.length; i++) {
  350. + buffer.append(inputs[i].toString());
  351. + }
  352. + return buffer.toString();
  353. }
  354.  
  355. + @Override
  356. + public void makeQuery(Map<String, Object> map, String prefix, XWikiCriteria query, List<String> criteriaList)
  357. + {
  358. + Integer value = (Integer) map.get(prefix);
  359. + if (value != null) {
  360. + criteriaList.add("@xp:" + getName() + "=" + value.toString());
  361. + }
  362. + }
  363. +
  364. public String displayCheckboxSearch(String name, String prefix, XWikiCriteria criteria, XWikiContext context)
  365. {
  366. StringBuffer buffer = new StringBuffer();
  367. org.apache.ecs.xhtml.input check = new input(input.checkbox, prefix + name, 1);
  368. + check.setID(prefix + name);
  369. org.apache.ecs.xhtml.input checkNo = new input(input.hidden, prefix + name, 0);
  370.  
  371. /*
  372. @@ -411,12 +454,8 @@
  373. public void fromSearchMap(XWikiQuery query, Map<String, String[]> map)
  374. {
  375. String[] data = map.get("");
  376. - if (data != null) {
  377. - Object[] data2 = new Object[data.length];
  378. - for (int i = 0; i < data.length; i++) {
  379. - data2[i] = fromString(data[i]).getValue();
  380. - }
  381. - query.setParam(getObject().getName() + "_" + getName(), data2);
  382. + if (data != null && data.length > 0) {
  383. + query.setParam(getObject().getName() + "_" + getName(), fromString(data[0]).getValue());
  384. }
  385. }
  386. }
  387. Index: src/main/java/com/xpn/xwiki/plugin/query/XWikiCriteria.java
  388. ===================================================================
  389. --- src/main/java/com/xpn/xwiki/plugin/query/XWikiCriteria.java (revision 31104)
  390. +++ src/main/java/com/xpn/xwiki/plugin/query/XWikiCriteria.java (working copy)
  391. @@ -38,6 +38,31 @@
  392. return Util.getSubMap(params, field);
  393. }
  394.  
  395. + public void removeParam(String field)
  396. + {
  397. + params.remove(field);
  398. + }
  399. +
  400. + public void addSuffix(String field, String suffix)
  401. + {
  402. + String sufield = field + "_" + suffix;
  403. + Object value = params.get(field);
  404. + if (value != null) {
  405. + params.remove(field);
  406. + params.put(sufield, value);
  407. + }
  408. + }
  409. +
  410. + public void removeSuffix(String field, String suffix)
  411. + {
  412. + String sufield = field + "_" + suffix;
  413. + Object value = params.get(sufield);
  414. + if (value != null) {
  415. + params.remove(sufield);
  416. + params.put(field, value);
  417. + }
  418. + }
  419. +
  420. public void setParam(String field, Object value)
  421. {
  422. params.put(field, value);
Add Comment
Please, Sign In to add comment