/** * @since 1.8M1 */ public boolean addDBTreeListField(String fieldName, String fieldPrettyName, int size, boolean multiSelect, boolean relationalStorage, String sql) { if (get(fieldName) == null) { DBTreeListClass list_class = new DBTreeListClass(); list_class.setName(fieldName); list_class.setPrettyName(fieldPrettyName); list_class.setSize(size); list_class.setMultiSelect(multiSelect); list_class.setRelationalStorage(relationalStorage); list_class.setSql(sql); list_class.setObject(this); put(fieldName, list_class); return true; } return false; }
public Map<String, List<ListItem>> getTreeMap(XWikiContext context) { List<ListItem> list = getDBList(context); Map<String, List<ListItem>> map = new HashMap<String, List<ListItem>>(); if ((list == null) || (list.size() == 0)) { return map; } // The root of the tree is considered to be the empty string. // Make sure that entries with invalid parents end up in the tree. // TODO: Detect cycles, as these also don't appear in the tree. List<String> validParents = this.getList(context); for (ListItem item : list) { if (validParents.contains(item.getParent())) { addToList(map, item.getParent(), item); } else { addToList(map, "", item); } } return map; }
/** * Gets an ordered list of items in the tree. This is necessary to make sure children are coming right after their * parents. * * @param treemap the unordered list of tree nodes * @param map the mapping between a node name and its corresponding tree node * @param context the current request context * @return ordered list of {@code ListItem} tree nodes */ protected List<ListItem> getTreeList(Map<String, List<ListItem>> treemap, Map<String, ListItem> map, XWikiContext context) { List<ListItem> list = getCachedDBTreeList(context); if (list == null) { list = new ArrayList<ListItem>(); addToTreeList(list, treemap, map, "", context); setCachedDBTreeList(list, context); } return list; }
protected void displayTreeSelectEdit(StringBuffer buffer, String name, String prefix, BaseCollection object, XWikiContext context) { select select = new select(prefix + name, 1); select.setMultiple(isMultiSelect()); select.setSize(getSize()); select.setName(prefix + name); select.setID(prefix + name); select.setDisabled(isDisabled()); Map<String, ListItem> map = getMap(context); Map<String, List<ListItem>> treemap = getTreeMap(context); List<String> selectlist; BaseProperty prop = (BaseProperty) object.safeget(name); if (prop == null) { selectlist = new ArrayList<String>(); } else if (prop instanceof ListProperty) { selectlist = ((ListProperty) prop).getList(); } else { selectlist = new ArrayList<String>(); selectlist.add(String.valueOf(prop.getValue())); } // Add options from Set addToSelect(select, selectlist, map, treemap, "", "", context); buffer.append(select.toString()); }
String sql = getSql(); if (context.getWiki().getHibernateStore() != null) { String classname = StringUtils.defaultString(getClassname()); String idField = StringUtils.defaultString(getIdField()); String valueField = StringUtils.defaultString(getValueField()); String parentField = StringUtils.defaultString(getParentField());
@Override public void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object, XWikiContext context) { List<String> selectlist; BaseProperty prop = (BaseProperty) object.safeget(name); if (prop == null) { selectlist = new ArrayList<String>(); } else if (prop instanceof ListProperty) { selectlist = ((ListProperty) prop).getList(); } else { selectlist = new ArrayList<String>(); selectlist.add(String.valueOf(prop.getValue())); } if (isPicker()) { String result = displayTree(name, prefix, selectlist, "edit", context); if (result.equals("")) { displayTreeSelectEdit(buffer, name, prefix, object, context); } else { displayHidden(buffer, name, prefix, object, context); buffer.append(result); } } else { displayTreeSelectEdit(buffer, name, prefix, object, context); } }
protected void addToTreeList(List<ListItem> treelist, Map<String, List<ListItem>> treemap, Map<String, ListItem> map, String parent, XWikiContext context) { List<ListItem> list = treemap.get(parent); if (list != null) { for (ListItem item : list) { ListItem item2 = new ListItem(item.getId(), getDisplayValue(item.getId(), "", map, context), item.getParent()); treelist.add(item2); addToTreeList(treelist, treemap, map, item.getId(), context); } } }
protected void addToSelect(select select, List<String> selectlist, Map<String, ListItem> map, Map<String, List<ListItem>> treemap, String parent, String level, XWikiContext context) { List<ListItem> list = treemap.get(parent); if (list != null) { for (ListItem item : list) { String display = level + getDisplayValue(item.getId(), "", map, context); option option = new option(display, item.getId()); option.addElement(display); if (selectlist.contains(item.getId())) { option.setSelected(true); } select.addElement(option); addToSelect(select, selectlist, map, treemap, item.getId(), level + " ", context); } } }
public BaseCollection newObject(XWikiContext context) { return new DBTreeListClass(); } }
@Override public void displayView(StringBuffer buffer, String name, String prefix, BaseCollection object, XWikiContext context) { List<String> selectlist; BaseProperty prop = (BaseProperty) object.safeget(name); if (prop == null) { selectlist = new ArrayList<String>(); } else if (prop instanceof ListProperty) { selectlist = ((ListProperty) prop).getList(); } else { selectlist = new ArrayList<String>(); selectlist.add(String.valueOf(prop.getValue())); } String result = displayFlatView(selectlist, context); if (result.equals("")) { super.displayView(buffer, name, prefix, object, context); } else { buffer.append(result); } }