Index: src/main/java/com/xpn/xwiki/objects/meta/StringMetaClass.java =================================================================== --- src/main/java/com/xpn/xwiki/objects/meta/StringMetaClass.java (revision 4234) +++ src/main/java/com/xpn/xwiki/objects/meta/StringMetaClass.java (working copy) @@ -1,51 +0,0 @@ -/* - * Copyright 2006-2007, XpertNet SARL, and individual contributors as indicated - * by the contributors.txt. - * - * This is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This software is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this software; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA, or see the FSF site: http://www.fsf.org. - * - */ - -package com.xpn.xwiki.objects.meta; - -import com.xpn.xwiki.XWikiContext; -import com.xpn.xwiki.objects.BaseCollection; -import com.xpn.xwiki.objects.classes.NumberClass; -import com.xpn.xwiki.objects.classes.StringClass; - -public class StringMetaClass extends PropertyMetaClass -{ - - public StringMetaClass() - { - super(); - // setType("stringmetaclass"); - setPrettyName("String Class"); - setName(StringClass.class.getName()); - - NumberClass size_class = new NumberClass(this); - size_class.setName("size"); - size_class.setPrettyName("Size"); - size_class.setSize(5); - size_class.setNumberType("integer"); - safeput("size", size_class); - } - - public BaseCollection newObject(XWikiContext context) - { - return new StringClass(); - } -} Index: src/main/java/com/xpn/xwiki/objects/meta/ListMetaClass.java =================================================================== --- src/main/java/com/xpn/xwiki/objects/meta/ListMetaClass.java (revision 4234) +++ src/main/java/com/xpn/xwiki/objects/meta/ListMetaClass.java (working copy) @@ -47,6 +47,12 @@ multi_class.setUnmodifiable(true); safeput("multiSelect", multi_class); + StringClass separators_class = new StringClass(this); + separators_class.setName("separators"); + separators_class.setPrettyName("Separators"); + separators_class.setSize(5); + safeput("separators", separators_class); + BooleanClass relational_class = new BooleanClass(this); relational_class.setName("relationalStorage"); relational_class.setPrettyName("Relational Storage"); Index: src/main/java/com/xpn/xwiki/objects/meta/DBListMetaClass.java =================================================================== --- src/main/java/com/xpn/xwiki/objects/meta/DBListMetaClass.java (revision 4234) +++ src/main/java/com/xpn/xwiki/objects/meta/DBListMetaClass.java (working copy) @@ -25,39 +25,50 @@ import com.xpn.xwiki.objects.BaseCollection; import com.xpn.xwiki.objects.classes.DBListClass; import com.xpn.xwiki.objects.classes.TextAreaClass; +import com.xpn.xwiki.objects.classes.BooleanClass; +import com.xpn.xwiki.objects.classes.ListClass; +import com.xpn.xwiki.objects.classes.NumberClass; +import com.xpn.xwiki.objects.classes.StaticListClass; import com.xpn.xwiki.objects.classes.StringClass; public class DBListMetaClass extends ListMetaClass { public DBListMetaClass() { - super(); - setPrettyName("Database List Class"); - setName(DBListClass.class.getName()); + super(); + setPrettyName("Database List Class"); + setName(DBListClass.class.getName()); - TextAreaClass sql_class = new TextAreaClass(this); - sql_class.setName("sql"); - sql_class.setPrettyName("Hibernate Query"); - sql_class.setSize(80); - sql_class.setRows(5); - safeput("sql", sql_class); + TextAreaClass sql_class = new TextAreaClass(this); + sql_class.setName("sql"); + sql_class.setPrettyName("Hibernate Query"); + sql_class.setSize(80); + sql_class.setRows(5); + safeput("sql", sql_class); - StringClass classname_class = new StringClass(this); - classname_class.setName("classname"); - classname_class.setPrettyName("XWiki Class Name"); - classname_class.setSize(20); - safeput("classname", classname_class); + StringClass classname_class = new StringClass(this); + classname_class.setName("classname"); + classname_class.setPrettyName("XWiki Class Name"); + classname_class.setSize(20); + safeput("classname", classname_class); - StringClass idfield_class = new StringClass(this); - idfield_class.setName("idField"); - idfield_class.setPrettyName("Id Field Name"); - idfield_class.setSize(20); - safeput("idField", idfield_class); + StringClass idfield_class = new StringClass(this); + idfield_class.setName("idField"); + idfield_class.setPrettyName("Id Field Name"); + idfield_class.setSize(20); + safeput("idField", idfield_class); - StringClass valuefield_class = new StringClass(this); - valuefield_class.setName("valueField"); - valuefield_class.setPrettyName("Value Field Name"); - valuefield_class.setSize(20); - safeput("valueField", valuefield_class); + StringClass valuefield_class = new StringClass(this); + valuefield_class.setName("valueField"); + valuefield_class.setPrettyName("Value Field Name"); + valuefield_class.setSize(20); + safeput("valueField", valuefield_class); + + BooleanClass picker_class = new BooleanClass(this); + picker_class.setName("picker"); + picker_class.setPrettyName("Use Suggest"); + picker_class.setDisplayType("yesno"); + picker_class.setUnmodifiable(true); + safeput("picker", picker_class); } public BaseCollection newObject(XWikiContext context) { Index: src/main/java/com/xpn/xwiki/objects/meta/StaticListMetaClass.java =================================================================== --- src/main/java/com/xpn/xwiki/objects/meta/StaticListMetaClass.java (revision 4234) +++ src/main/java/com/xpn/xwiki/objects/meta/StaticListMetaClass.java (working copy) @@ -24,13 +24,16 @@ import com.xpn.xwiki.XWikiContext; import com.xpn.xwiki.objects.BaseCollection; import com.xpn.xwiki.objects.classes.StaticListClass; +import com.xpn.xwiki.objects.classes.BooleanClass; +import com.xpn.xwiki.objects.classes.ListClass; +import com.xpn.xwiki.objects.classes.NumberClass; import com.xpn.xwiki.objects.classes.StringClass; public class StaticListMetaClass extends ListMetaClass { public StaticListMetaClass() { - super(); + super(); setPrettyName("Static List Class"); setName(StaticListClass.class.getName()); @@ -38,13 +41,15 @@ values_class.setName("values"); values_class.setPrettyName("Values"); values_class.setSize(40); - safeput("values", values_class); - - StringClass separators_class = new StringClass(this); - separators_class.setName("separators"); - separators_class.setPrettyName("Separators"); - separators_class.setSize(5); - safeput("separators", separators_class); + safeput("values", values_class); + + BooleanClass picker_class = new BooleanClass(this); + picker_class.setName("picker"); + picker_class.setPrettyName("Use Suggest"); + picker_class.setDisplayType("yesno"); + picker_class.setUnmodifiable(true); + safeput("picker", picker_class); + } public BaseCollection newObject(XWikiContext context) { Index: src/main/java/com/xpn/xwiki/objects/classes/ListClass.java =================================================================== --- src/main/java/com/xpn/xwiki/objects/classes/ListClass.java (revision 4234) +++ src/main/java/com/xpn/xwiki/objects/classes/ListClass.java (working copy) @@ -47,10 +47,9 @@ import com.xpn.xwiki.plugin.query.XWikiCriteria; import com.xpn.xwiki.plugin.query.XWikiQuery; + public abstract class ListClass extends PropertyClass { - - public ListClass(String name, String prettyname, PropertyMetaClass wclass) { super(name, prettyname, wclass); @@ -72,11 +71,14 @@ this(null); } - public String getSeparators() - { - return null; + public String getSeparators() { + return getStringValue("separators"); } + public void setSeparators(String separators) { + setStringValue("separators", separators); + } + public String getDisplayType() { return getStringValue("displayType"); @@ -429,7 +431,9 @@ input.setSize(getSize()); input.setName(prefix + name); input.setID(prefix + name); + buffer.append(input.toString()); + } else if (getDisplayType().equals("radio") || getDisplayType().equals("checkbox")) { displayRadioEdit(buffer, name, prefix, object, context); } else { Index: src/main/java/com/xpn/xwiki/objects/classes/DBListClass.java =================================================================== --- src/main/java/com/xpn/xwiki/objects/classes/DBListClass.java (revision 4234) +++ src/main/java/com/xpn/xwiki/objects/classes/DBListClass.java (working copy) @@ -25,12 +25,25 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Iterator; import com.xpn.xwiki.XWiki; import com.xpn.xwiki.XWikiContext; import com.xpn.xwiki.objects.meta.PropertyMetaClass; import com.xpn.xwiki.plugin.query.QueryPlugin; +import java.text.CharacterIterator; +import java.text.StringCharacterIterator; +import java.util.StringTokenizer; +import org.apache.commons.lang.StringUtils; +import org.apache.ecs.xhtml.input; +import com.xpn.xwiki.objects.BaseCollection; +import com.xpn.xwiki.objects.BaseProperty; +import com.xpn.xwiki.objects.DBStringListProperty; +import com.xpn.xwiki.objects.ListProperty; +import com.xpn.xwiki.XWikiException; + + public class DBListClass extends ListClass { private List cachedDBList; @@ -232,4 +245,206 @@ public void flushCache() { setCachedDBList(null); } + + // return first or second col from user query + public String returnCol(String hibquery, boolean first) { + String firstCol = "-", secondCol = "-"; + + int fromIndx = hibquery.indexOf("from"); + + if(fromIndx > 0) { + String firstPart = hibquery.substring(0, fromIndx); + firstPart.replaceAll("\\s+", " "); + int comIndx = hibquery.indexOf(","); + + //there are more than one columns to select- take the second one (the value) + if(comIndx > 0 && comIndx < fromIndx) { + + StringTokenizer st = new StringTokenizer(firstPart, " ,()", true); + ArrayList words = new ArrayList(); + + while(st.hasMoreTokens()) + words.add(st.nextToken().toLowerCase()); + + int comma = words.indexOf(",") - 1; + while(words.get(comma).toString().compareTo(" ") == 0) { + comma--; + } + firstCol = words.get(comma).toString().trim(); + + comma = words.indexOf(",") + 1; + while(words.get(comma).toString().compareTo(" ") == 0) { + comma++; + } + + if(words.get(comma).toString().compareTo("(") == 0) { + int i = comma+1; + while(words.get(i).toString().compareTo(")") != 0) { + secondCol += words.get(i).toString(); + i++; + } + secondCol += ")"; + } + else secondCol = words.get(comma).toString().trim(); + } + //has only one column + else { + int i = fromIndx - 1; + while(firstPart.charAt(i) == ' ') + i--; + String col = " "; + while(firstPart.charAt(i) != ' ') { + col += firstPart.charAt(i); + i--; + } + String reverse = " "; + for(i = (col.length()-1); i >= 0; i--) + reverse += col.charAt(i); + firstCol = reverse.trim(); + } + } + if(first == true) return firstCol; + else return secondCol; + } + + //the result of the second query, to retrieve the value + public String getValue(String val, String sql, XWikiContext context) { + String firstCol = returnCol(sql, true); + String secondCol = returnCol(sql, false); + + String newsql = sql.substring(0, sql.indexOf(firstCol)); + newsql += secondCol + " "; + newsql += sql.substring(sql.indexOf("from")); + newsql += "and " + firstCol + "='" + val + "'"; + + Object[] list = null; + XWiki xwiki = context.getWiki(); + String res = ""; + try { + list = xwiki.search(newsql, context).toArray(); + if(list.length > 0) res = list[0].toString(); + }catch(Exception e) { + e.printStackTrace(); + } + return res; + } + + + //override the method from parent ListClass + public void displayEdit(StringBuffer buffer, String name, String prefix, + BaseCollection object, XWikiContext context) + { + //input display + if (getDisplayType().equals("input")) { + input input = new input(); + input.setType("text"); + input.setSize(getSize()); + boolean changeInputName = false; + boolean setInpVal = true; + + BaseProperty prop = (BaseProperty) object.safeget(name); + String val = ""; + if (prop != null) val = prop.toFormString(); + + if(isPicker()) { + input.addAttribute("autocomplete", "off"); + String path = ""; + try { + XWiki xwiki = context.getWiki(); + path = xwiki.getURL("Main.WebHome", "view", context); + } catch(XWikiException e) { + e.printStackTrace(); + } + String classname = this.getObject().getName(); + String fieldname = this.getName(); + String hibquery = this.getSql(); + String secondCol = "-", firstCol = "-"; + + if(hibquery != null && !hibquery.equals("")) { + firstCol = returnCol(hibquery, true); + secondCol = returnCol(hibquery, false); + + if(secondCol.compareTo("-") != 0) { + changeInputName = true; + input hidden = new input(); + hidden.setID(prefix + name); + hidden.setName(prefix + name); + hidden.setType("hidden"); + if(val != null && !val.equals("")) hidden.setValue(val); + buffer.append(hidden.toString()); + + input.setValue(getValue(val, hibquery, context)); + setInpVal = false; + } + } + + String script = "\""+path+"?xpage=suggest&classname="+classname+"&fieldname="+fieldname+"&firCol="+firstCol+"&secCol="+secondCol+"&\""; + String varname = "\"input\""; + String seps = "\""+this.getSeparators()+"\""; + if(isMultiSelect()) + input.setOnFocus("new ajaxSuggest(this, {script:"+script+", varname:"+varname+", seps:"+seps+"} )"); + else + input.setOnFocus("new ajaxSuggest(this, {script:"+script+", varname:"+varname+"} )"); + + + + } + + if(changeInputName == true) { + input.setName(prefix + name + "_suggest"); + input.setID(prefix + name + "_suggest"); + } + else { + input.setName(prefix + name); + input.setID(prefix + name); + } + if(setInpVal == true) input.setValue(val); + + buffer.append(input.toString()); + + } else if (getDisplayType().equals("radio") || getDisplayType().equals("checkbox")) { + displayRadioEdit(buffer, name, prefix, object, context); + } else { + displaySelectEdit(buffer, name, prefix, object, context); + } + + if (!getDisplayType().equals("input")) { + org.apache.ecs.xhtml.input hidden = new input(input.hidden, prefix + name, ""); + buffer.append(hidden); + } + } + + public void displayView(StringBuffer buffer, String name, String prefix, + BaseCollection object, XWikiContext context) + { + if(isPicker() && getSql().compareTo("") != 0) { + BaseProperty prop = (BaseProperty) object.safeget(name); + String val = ""; + if(prop != null) val = prop.toFormString(); + Map map = getMap(context); + + String secondCol = returnCol(getSql(), false); + if(secondCol.compareTo("-") != 0) { + String res = getValue(val, getSql(), context); + buffer.append(getDisplayValue(res, name, map, context)); + } + else buffer.append(getDisplayValue(val, name, map, context)); + } + else { + List selectlist; + String separator = getSeparator(); + BaseProperty prop = (BaseProperty) object.safeget(name); + Map map = getMap(context); + if ((prop instanceof ListProperty) || (prop instanceof DBStringListProperty)) { + selectlist = (List) prop.getValue(); + List newlist = new ArrayList(); + for (Iterator it = selectlist.iterator(); it.hasNext();) { + newlist.add(getDisplayValue(it.next(), name, map, context)); + } + buffer.append(StringUtils.join(newlist.toArray(), separator)); + } else { + buffer.append(getDisplayValue(prop.getValue(), name, map, context)); + } + } + } } Index: src/main/java/com/xpn/xwiki/objects/classes/StaticListClass.java =================================================================== --- src/main/java/com/xpn/xwiki/objects/classes/StaticListClass.java (revision 4234) +++ src/main/java/com/xpn/xwiki/objects/classes/StaticListClass.java (working copy) @@ -23,10 +23,17 @@ import com.xpn.xwiki.XWikiContext; import com.xpn.xwiki.objects.meta.PropertyMetaClass; +import com.xpn.xwiki.XWiki; import java.util.List; import java.util.Map; +import com.xpn.xwiki.XWiki; +import com.xpn.xwiki.XWikiException; +import com.xpn.xwiki.objects.BaseCollection; +import com.xpn.xwiki.objects.BaseProperty; +import org.apache.ecs.xhtml.input; + public class StaticListClass extends ListClass { public StaticListClass(PropertyMetaClass wclass) { @@ -46,14 +53,6 @@ setStringValue("values", values); } - public String getSeparators() { - return getStringValue("separators"); - } - - public void setSeparators(String separators) { - setStringValue("separators", separators); - } - public List getList(XWikiContext context) { String values = getValues(); return getListFromString(values); @@ -63,4 +62,55 @@ String values = getValues(); return getMapFromString(values); } + + public void displayEdit(StringBuffer buffer, String name, String prefix, + BaseCollection object, XWikiContext context) + { + if (getDisplayType().equals("input")) { + input input = new input(); + BaseProperty prop = (BaseProperty) object.safeget(name); + if (prop != null) { + input.setValue(prop.toFormString()); + } + input.setType("text"); + input.setSize(getSize()); + input.setName(prefix + name); + input.setID(prefix + name); + + if(isPicker()) { + input.addAttribute("autocomplete", "off"); + String path = ""; + try { + XWiki xwiki = context.getWiki(); + path = xwiki.getURL("Main.WebHome", "view", context); + } catch(XWikiException e) { + e.printStackTrace(); + } + + String classname = this.getObject().getName(); + String fieldname = this.getName(); + String secondCol = "-", firstCol = "-"; + + String script = "\""+path+"?xpage=suggest&classname="+classname+"&fieldname="+fieldname+"&firCol="+firstCol+"&secCol="+secondCol+"&\""; + String varname = "\"input\""; + String seps = "\""+this.getSeparators()+"\""; + if(isMultiSelect()) + input.setOnFocus("new ajaxSuggest(this, {script:"+script+", varname:"+varname+", seps:"+seps+"} )"); + else + input.setOnFocus("new ajaxSuggest(this, {script:"+script+", varname:"+varname+"} )"); + } + + buffer.append(input.toString()); + + } else if (getDisplayType().equals("radio") || getDisplayType().equals("checkbox")) { + displayRadioEdit(buffer, name, prefix, object, context); + } else { + displaySelectEdit(buffer, name, prefix, object, context); + } + + if (!getDisplayType().equals("input")) { + org.apache.ecs.xhtml.input hidden = new input(input.hidden, prefix + name, ""); + buffer.append(hidden); + } + } } Index: src/main/java/com/xpn/xwiki/objects/classes/StringClass.java =================================================================== --- src/main/java/com/xpn/xwiki/objects/classes/StringClass.java (revision 4234) +++ src/main/java/com/xpn/xwiki/objects/classes/StringClass.java (working copy) @@ -26,6 +26,7 @@ import org.apache.ecs.xhtml.input; +import com.xpn.xwiki.XWiki; import com.xpn.xwiki.XWikiContext; import com.xpn.xwiki.objects.BaseCollection; import com.xpn.xwiki.objects.BaseProperty; @@ -33,6 +34,7 @@ import com.xpn.xwiki.objects.meta.PropertyMetaClass; import com.xpn.xwiki.plugin.query.XWikiCriteria; import com.xpn.xwiki.plugin.query.XWikiQuery; +import com.xpn.xwiki.XWikiException; public class StringClass extends PropertyClass { @@ -62,7 +64,17 @@ { setIntValue("size", size); } + + public boolean isPicker() + { + return (getIntValue("picker") == 1); + } + public void setPicker(boolean picker) + { + setIntValue("picker", picker ? 1 : 0); + } + public BaseProperty fromString(String value) { BaseProperty property = newProperty(); @@ -90,6 +102,26 @@ input.setName(prefix + name); input.setID(prefix + name); input.setSize(getSize()); + + if(isPicker()) { + input.addAttribute("autocomplete", "off"); + String path = ""; + try { + XWiki xwiki = context.getWiki(); + path = xwiki.getURL("Main.WebHome", "view", context); + } catch(XWikiException e) { + e.printStackTrace(); + } + + String classname = this.getObject().getName(); + String fieldname = this.getName(); + String secondCol = "-", firstCol = "-"; + + String script = "\""+path+"?xpage=suggest&classname="+classname+"&fieldname="+fieldname+"&firCol="+firstCol+"&secCol="+secondCol+"&\""; + String varname = "\"input\""; + input.setOnFocus("new ajaxSuggest(this, {script:"+script+", varname:"+varname+"} )"); + } + buffer.append(input.toString()); } Index: standard/src/main/webapp/skins/albatross/tagedit.vm =================================================================== --- standard/src/main/webapp/skins/albatross/tagedit.vm (revision 4234) +++ standard/src/main/webapp/skins/albatross/tagedit.vm (working copy) @@ -88,9 +88,13 @@ #end #else + #set($path = $xwiki.getURL("Main.WebHome", "view")) + #set($script = $path+"?xpage=suggest&classname=XWiki.TagClass&fieldname=tags&firCol=-&secCol=-&") + #set($seps = $xwiki.getDocument("XWiki.TagClass").xWikiClass.tags.getProperty('separators').value) +