Index: src/main/java/com/xpn/xwiki/objects/classes/ListClass.java =================================================================== --- src/main/java/com/xpn/xwiki/objects/classes/ListClass.java (revision 21618) +++ src/main/java/com/xpn/xwiki/objects/classes/ListClass.java (working copy) @@ -499,7 +499,7 @@ } radio.addElement(getDisplayValue(rawvalue, name, map, context)); - buffer.append(""); + buffer.append(""); buffer.append(radio.toString()); buffer.append(""); } @@ -596,75 +596,122 @@ public abstract List getList(XWikiContext context); + public List getValidatedList(XWikiContext context) + { + List result = getList(context); + + String regexp = getValidationRegExp(); + if ((regexp == null) || (regexp.trim().equals(""))) { + return result; + } + + if (isMultiSelect()) { + for (Iterator it = result.iterator(); it.hasNext();) { + String rawvalue = it.next(); + if (!context.getUtil().match(regexp, "[" + rawvalue + "]")) { + it.remove(); + } + } + } else { + for (Iterator it = result.iterator(); it.hasNext();) { + String rawvalue = it.next(); + if (!context.getUtil().match(regexp, rawvalue)) { + it.remove(); + } + } + } + + return result; + } + public abstract Map getMap(XWikiContext context); - @Override public String displaySearch(String name, String prefix, XWikiCriteria criteria, XWikiContext context) { + return displaySearch(name, prefix, criteria, true, context); + } + + public String displaySearch(String name, String prefix, XWikiCriteria criteria, boolean multiple, + XWikiContext context) + { if (getDisplayType().equals("input")) { return super.displaySearch(name, prefix, criteria, context); - } else if (getDisplayType().equals("radio") || getDisplayType().equals("checkbox")) { - return displayRadioSearch(name, prefix, criteria, context); } else { - return displaySelectSearch(name, prefix, criteria, context); + List list; + if (multiple) { + list = getValidatedList(context); + } else { + list = getList(context); + } + + if (getDisplayType().equals("radio") || getDisplayType().equals("checkbox") || list.size() < 6) { + return displayRadioSearch(name, prefix, criteria, multiple, list, context); + } else { + return displaySelectSearch(name, prefix, criteria, multiple, list, context); + } } } - protected String displayRadioSearch(String name, String prefix, XWikiCriteria criteria, XWikiContext context) + protected String displayRadioSearch(String name, String prefix, XWikiCriteria criteria, boolean multiple, + List list, XWikiContext context) { StringBuffer buffer = new StringBuffer(); - List list = getList(context); - List selectlist = new ArrayList(); - /* - * BaseProperty prop = (BaseProperty)object.safeget(name); if (prop==null) { selectlist = new ArrayList(); } - * else if ((prop instanceof ListProperty)||(prop instanceof DBStringListProperty)) { selectlist = (List) - * prop.getValue(); } else { selectlist = new ArrayList(); selectlist.add(prop.getValue()); } - */ + Map map = getMap(context); + String fieldFullName = getFieldFullName(); + String[] selectArray = ((String[]) criteria.getParameter(fieldFullName)); + List selectlist = (selectArray != null) ? Arrays.asList(selectArray) : new ArrayList(); + // Add options from Set - for (Iterator it = list.iterator(); it.hasNext();) { - String rawvalue = it.next(); + int count = 0; + for (String rawvalue : list) { String value = getElementValue(rawvalue); - String display = getDisplayValue(rawvalue, name, getMap(context), context); input radio = - new input(getDisplayType().equals("radio") ? input.radio : input.checkbox, prefix + name, value); + new input((getDisplayType().equals("radio") && !multiple) ? input.radio : input.checkbox, + prefix + name, value); if (selectlist.contains(value)) { radio.setChecked(true); } - radio.addElement(display); + radio.addElement(getDisplayValue(rawvalue, name, map, context)); + buffer.append(""); buffer.append(radio.toString()); - if (it.hasNext()) { - buffer.append("
"); - } + buffer.append("
"); } + return buffer.toString(); } - protected String displaySelectSearch(String name, String prefix, XWikiCriteria criteria, XWikiContext context) + protected String displaySelectSearch(String name, String prefix, XWikiCriteria criteria, boolean multiple, + List list, XWikiContext context) { select select = new select(prefix + name, 1); - select.setMultiple(true); - select.setSize(5); + select.setMultiple(multiple); + select.setSize((multiple && getSize() == 1) ? 5 : getSize()); select.setName(prefix + name); select.setID(prefix + name); - List list = getList(context); + Map map = getMap(context); + + String sort = getSort(); + if (!"none".equals(sort)) { + if ("id".equals(sort)) { + Collections.sort(list); + } + if ("value".equals(sort)) { + Collections.sort(list, new MapComparator(map)); + } + } + String fieldFullName = getFieldFullName(); String[] selectArray = ((String[]) criteria.getParameter(fieldFullName)); List selectlist = (selectArray != null) ? Arrays.asList(selectArray) : new ArrayList(); - /* - * BaseProperty prop = (BaseProperty)object.safeget(name); if (prop==null) { selectlist = new ArrayList(); } - * else if ((prop instanceof ListProperty)||(prop instanceof DBStringListProperty)) { selectlist = (List) - * prop.getValue(); } else { selectlist = new ArrayList(); selectlist.add(prop.getValue()); } - */ - // Add options from Set for (String rawvalue : list) { String value = getElementValue(rawvalue); - String display = getDisplayValue(rawvalue, name, getMap(context), context); + String display = getDisplayValue(rawvalue, name, map, context); option option = new option(display, value); option.addElement(display); if (selectlist.contains(value)) { @@ -680,21 +727,48 @@ public void makeQuery(Map map, String prefix, XWikiCriteria query, List criteriaList) { Object values = map.get(prefix); - if ((values == null) || (values.equals(""))) { + if ((values != null) && (!values.equals(""))) { + if (isMultiSelect()) { + if (values instanceof String) { + // general comparison 'like' - tests at least one value like + criteriaList.add("jcr:like(@xp:" + getName() + ",'%" + values.toString() + "%')"); + } else { + String[] valuesarray = (String[]) values; + String[] criteriaarray = new String[valuesarray.length]; + for (int i = 0; i < valuesarray.length; i++) { + criteriaarray[i] = "jcr:like(@xp:" + getName() + ",'%" + valuesarray[i] + "%')"; + } + criteriaList.add("(" + StringUtils.join(criteriaarray, " or ") + ")"); + } + } else { + if (values instanceof String) { + // general comparison '=' - tests at least one value = + criteriaList.add("@xp:" + getName() + "='" + values.toString() + "'"); + } else { + String[] valuesarray = (String[]) values; + String[] criteriaarray = new String[valuesarray.length]; + for (int i = 0; i < valuesarray.length; i++) { + criteriaarray[i] = "@xp:" + getName() + "='" + valuesarray[i] + "'"; + } + criteriaList.add("(" + StringUtils.join(criteriaarray, " or ") + ")"); + } + } + return; } - if (values instanceof String) { - // general comparison '=' - tests at least one value = - criteriaList.add("@xp:" + getName() + "='" + values.toString() + "'"); - } else { - String[] valuesarray = (String[]) values; - String[] criteriaarray = new String[valuesarray.length]; - for (int i = 0; i < valuesarray.length; i++) { - criteriaarray[i] = "@xp:" + getName() + "='" + valuesarray[i] + "'"; - } - criteriaList.add("(" + StringUtils.join(criteriaarray, " or ") + ")"); + values = map.get(prefix + "lessthan"); + if ((values != null) && (!values.equals("")) && (values instanceof String)) { + criteriaList.add("@xp:" + getName() + "<='" + values.toString() + "'"); + return; } + + values = map.get(prefix + "morethan"); + if ((values != null) && (!values.equals("")) && (values instanceof String)) { + criteriaList.add("@xp:" + getName() + ">='" + values.toString() + "'"); + return; + } + return; } @@ -704,6 +778,19 @@ String[] data = map.get(""); if (data != null) { query.setParam(getObject().getName() + "_" + getName(), data); + return; } + + data = map.get("lessthan"); + if (data != null && data.length == 1) { + query.setParam(getObject().getName() + "_" + getName() + "_lessthan", data[0]); + return; + } + + data = map.get("morethan"); + if (data != null && data.length == 1) { + query.setParam(getObject().getName() + "_" + getName() + "_morethan", data[0]); + return; + } } } Index: src/main/java/com/xpn/xwiki/objects/classes/BooleanClass.java =================================================================== --- src/main/java/com/xpn/xwiki/objects/classes/BooleanClass.java (revision 21618) +++ src/main/java/com/xpn/xwiki/objects/classes/BooleanClass.java (working copy) @@ -35,6 +35,7 @@ import org.apache.ecs.xhtml.option; import org.apache.ecs.xhtml.select; +import java.util.List; import java.util.Map; public class BooleanClass extends PropertyClass @@ -143,6 +144,7 @@ XWikiContext context) { select select = new select(prefix + name, 1); + select.setID(prefix + name); String String0 = getDisplayValue(context, 0); String String1 = getDisplayValue(context, 1); int nb1 = 1; @@ -199,11 +201,17 @@ div[] inputs; input radioNone = new input(input.radio, prefix + name, ""); + radioNone.setID(prefix + name + "2"); input radioTrue = new input(input.radio, prefix + name, "1"); + radioTrue.setID(prefix + name + "1"); input radioFalse = new input(input.radio, prefix + name, "0"); + radioFalse.setID(prefix + name + "0"); label labelNone = new label(); + labelNone.setFor(prefix + name + "2"); label labelTrue = new label(); + labelTrue.setFor(prefix + name + "1"); label labelFalse = new label(); + labelFalse.setFor(prefix + name + "0"); div divNone = new div(); div divTrue = new div(); div divFalse = new div(); @@ -303,46 +311,87 @@ } } - @Override public String displaySearch(String name, String prefix, XWikiCriteria criteria, XWikiContext context) { if (getDisplayType().equals("input")) { return super.displaySearch(name, prefix, criteria, context); - } else if (getDisplayType().equals("radio")) { - return displayCheckboxSearch(name, prefix, criteria, context); } else { - return displaySelectSearch(name, prefix, criteria, context); + return displayRadioSearch(name, prefix, criteria, context); } } - public String displaySelectSearch(String name, String prefix, XWikiCriteria criteria, XWikiContext context) + public String displayRadioSearch(String name, String prefix, XWikiCriteria criteria, XWikiContext context) { - select select = new select(prefix + name, 1); - select.setMultiple(true); - select.setSize(3); - String String0 = getDisplayValue(context, 0); - String String1 = getDisplayValue(context, 1); + StringBuffer buffer = new StringBuffer(); + String StringNone = getDisplayValue(context, 2); + String StringTrue = getDisplayValue(context, 1); + String StringFalse = getDisplayValue(context, 0); + div[] inputs; - option[] options = {new option("---", ""), new option(String1, "1"), new option(String0, "0")}; - options[0].addElement("---"); - options[1].addElement(String1); - options[2].addElement(String0); + input radioNone = new input(input.radio, prefix + name, ""); + radioNone.setID(prefix + name + "2"); + input radioTrue = new input(input.radio, prefix + name, "1"); + radioTrue.setID(prefix + name + "1"); + input radioFalse = new input(input.radio, prefix + name, "0"); + radioFalse.setID(prefix + name + "0"); + label labelNone = new label(); + labelNone.setFor(prefix + name + "2"); + label labelTrue = new label(); + labelTrue.setFor(prefix + name + "1"); + label labelFalse = new label(); + labelFalse.setFor(prefix + name + "0"); + div divNone = new div(); + div divTrue = new div(); + div divFalse = new div(); + labelNone.addElement(radioNone); + labelNone.addElement(StringNone); + divNone.addElement(labelNone); + labelTrue.addElement(radioTrue); + labelTrue.addElement(StringTrue); + divTrue.addElement(labelTrue); + labelFalse.addElement(radioFalse); + labelFalse.addElement(StringFalse); + divFalse.addElement(labelFalse); - /* - * try { IntegerProperty prop = (IntegerProperty) object.safeget(name); if (prop!=null) { Integer ivalue = - * (Integer)prop.getValue(); if (ivalue!=null) { int value = ivalue.intValue(); if (value==1) - * options[1].setSelected(true); else if (value==0) options[2].setSelected(true); } else { int value = - * getDefaultValue(); if (value==1) options[1].setSelected(true); else if (value==0) - * options[2].setSelected(true); } } } catch (Exception e) { // This should not happen e.printStackTrace(); } - */ - select.addElement(options); - return select.toString(); + radioTrue.setID(prefix + name); + + inputs = new div[] {divNone, divTrue, divFalse}; + + String fieldFullName = getFieldFullName(); + Integer ivalue = (Integer) criteria.getParameter(fieldFullName); + + if (ivalue != null) { + int value = ivalue.intValue(); + if (value == 1) + radioTrue.setChecked(true); + else if (value == 0) + radioFalse.setChecked(true); + else + radioNone.setChecked(true); + } else { + radioNone.setChecked(true); + } + + for (int i = 0; i < inputs.length; i++) { + buffer.append(inputs[i].toString()); + } + return buffer.toString(); } + @Override + public void makeQuery(Map map, String prefix, XWikiCriteria query, List criteriaList) + { + Integer value = (Integer) map.get(prefix); + if (value != null) { + criteriaList.add("@xp:" + getName() + "=" + value.toString()); + } + } + public String displayCheckboxSearch(String name, String prefix, XWikiCriteria criteria, XWikiContext context) { StringBuffer buffer = new StringBuffer(); org.apache.ecs.xhtml.input check = new input(input.checkbox, prefix + name, 1); + check.setID(prefix + name); org.apache.ecs.xhtml.input checkNo = new input(input.hidden, prefix + name, 0); /* @@ -361,11 +410,8 @@ public void fromSearchMap(XWikiQuery query, Map map) { String[] data = map.get(""); - if (data != null) { - Object[] data2 = new Object[data.length]; - for (int i = 0; i < data.length; i++) - data2[i] = fromString(data[i]).getValue(); - query.setParam(getObject().getName() + "_" + getName(), data2); + if (data != null && data.length > 0) { + query.setParam(getObject().getName() + "_" + getName(), fromString(data[0]).getValue()); } } } Index: src/main/java/com/xpn/xwiki/plugin/query/XWikiCriteria.java =================================================================== --- src/main/java/com/xpn/xwiki/plugin/query/XWikiCriteria.java (revision 21618) +++ src/main/java/com/xpn/xwiki/plugin/query/XWikiCriteria.java (working copy) @@ -38,6 +38,31 @@ return Util.getSubMap(params, field); } + public void removeParam(String field) + { + params.remove(field); + } + + public void addSuffix(String field, String suffix) + { + String sufield = field + "_" + suffix; + Object value = params.get(field); + if (value != null) { + params.remove(field); + params.put(sufield, value); + } + } + + public void removeSuffix(String field, String suffix) + { + String sufield = field + "_" + suffix; + Object value = params.get(sufield); + if (value != null) { + params.remove(sufield); + params.put(field, value); + } + } + public void setParam(String field, Object value) { params.put(field, value);