@Test public void testGetQueryWithSqlScriptSpecified() { DBTreeListClass dbtlc = new DBTreeListClass(); assertEquals("", dbtlc.getSql()); String sql = "select doc.name, doc.title, doc.creator from XWikiDocument as doc"; dbtlc.setSql(sql); assertEquals(sql, dbtlc.getQuery(this.oldcore.getXWikiContext())); }
@Override public String getQuery(XWikiContext context) { String sql = getSql(); if (StringUtils.isBlank(sql)) { 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()); boolean hasClassname = !StringUtils.isBlank(classname); boolean hasIdField = !StringUtils.isBlank(idField); boolean hasValueField = !StringUtils.isBlank(valueField); boolean hasParentField = !StringUtils.isBlank(parentField); if (!(hasIdField || hasValueField)) { if (hasClassname) { sql = "select distinct doc.fullName, doc.fullName, " + (hasParentField ? parentField : "doc.parent") + " from XWikiDocument as doc, BaseObject as obj" + " where doc.fullName=obj.name and obj.className='" + classname + "'"; } else { sql = DEFAULT_QUERY; } return sql; } if (!hasIdField && hasValueField) { idField = valueField; } else if (hasIdField && !hasValueField) { valueField = idField; } if (!hasParentField) { parentField = "doc.parent"; } boolean usesObj = hasClassname || idField.startsWith("obj.") || valueField.startsWith("obj.") || parentField.startsWith("obj."); boolean usesDoc = idField.startsWith("doc.") || valueField.startsWith("doc.") || parentField.startsWith("doc."); if ((!idField.startsWith("obj.") || !valueField.startsWith("obj.") || !parentField.startsWith("obj.")) && !hasClassname) { usesDoc = true; } StringBuffer select = new StringBuffer("select distinct "); ArrayList<String> fromStatements = new ArrayList<String>(); ArrayList<String> whereStatements = new ArrayList<String>(); if (usesDoc) { fromStatements.add("XWikiDocument as doc"); if (usesObj) { whereStatements.add("doc.fullName=obj.name"); } } if (usesObj) { fromStatements.add("BaseObject as obj"); if (hasClassname) { whereStatements.add("obj.className='" + classname + "'"); } } if (idField.startsWith("doc.") || idField.startsWith("obj.")) { select.append(idField); } else if (!hasClassname) { select.append("doc." + idField); } else { select.append("idprop.value"); fromStatements.add("StringProperty as idprop"); whereStatements.add("obj.id=idprop.id.id and idprop.id.name='" + idField + "'"); } if (valueField.startsWith("doc.") || valueField.startsWith("obj.")) { select.append(", ").append(valueField); } else if (!hasClassname) { select.append(", doc." + valueField); } else { if (valueField.equals(idField)) { select.append(", idprop.value"); } else { select.append(", valueprop.value"); fromStatements.add("StringProperty as valueprop"); whereStatements.add("obj.id=valueprop.id.id and valueprop.id.name='" + valueField + "'"); } } if (parentField.startsWith("doc.") || parentField.startsWith("obj.")) { select.append(", ").append(parentField); } else if (!hasClassname) { select.append(", doc." + parentField); } else { if (parentField.equals(idField)) { select.append(", idprop.value"); } else if (parentField.equals(valueField)) { select.append(", valueprop.value"); } else { select.append(", parentprop.value"); fromStatements.add("StringProperty as parentprop"); whereStatements.add("obj.id=parentprop.id.id and parentprop.id.name='" + parentField + "'"); } } select.append(" from "); select.append(StringUtils.join(fromStatements.iterator(), ", ")); if (whereStatements.size() > 0) { select.append(" where "); select.append(StringUtils.join(whereStatements.iterator(), " and ")); } sql = select.toString(); } else { } } try { sql = context.getWiki().parseContent(sql, context); } catch (Exception e) { LOGGER.error("Failed to parse SQL script [" + sql + "]. Continuing with non-rendered script.", e); } return sql; }
DBTreeListClass extends DBListClass { @Override public String getQuery(XWikiContext context) { String sql = getSql(); if (StringUtils.isBlank(sql)) { 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()); boolean hasClassname = !StringUtils.isBlank(classname); boolean hasIdField = !StringUtils.isBlank(idField); boolean hasValueField = !StringUtils.isBlank(valueField); boolean hasParentField = !StringUtils.isBlank(parentField); if (!(hasIdField || hasValueField)) { if (hasClassname) { sql = "select distinct doc.fullName, doc.fullName, " + (hasParentField ? parentField : "doc.parent") + " from XWikiDocument as doc, BaseObject as obj" + " where doc.fullName=obj.name and obj.className='" + classname + "'"; } else { sql = DEFAULT_QUERY; } return sql; } if (!hasIdField && hasValueField) { idField = valueField; } else if (hasIdField && !hasValueField) { valueField = idField; } if (!hasParentField) { parentField = "doc.parent"; } boolean usesObj = hasClassname || idField.startsWith("obj.") || valueField.startsWith("obj.") || parentField.startsWith("obj."); boolean usesDoc = idField.startsWith("doc.") || valueField.startsWith("doc.") || parentField.startsWith("doc."); if ((!idField.startsWith("obj.") || !valueField.startsWith("obj.") || !parentField.startsWith("obj.")) && !hasClassname) { usesDoc = true; } StringBuffer select = new StringBuffer("select distinct "); ArrayList<String> fromStatements = new ArrayList<String>(); ArrayList<String> whereStatements = new ArrayList<String>(); if (usesDoc) { fromStatements.add("XWikiDocument as doc"); if (usesObj) { whereStatements.add("doc.fullName=obj.name"); } } if (usesObj) { fromStatements.add("BaseObject as obj"); if (hasClassname) { whereStatements.add("obj.className='" + classname + "'"); } } if (idField.startsWith("doc.") || idField.startsWith("obj.")) { select.append(idField); } else if (!hasClassname) { select.append("doc." + idField); } else { select.append("idprop.value"); fromStatements.add("StringProperty as idprop"); whereStatements.add("obj.id=idprop.id.id and idprop.id.name='" + idField + "'"); } if (valueField.startsWith("doc.") || valueField.startsWith("obj.")) { select.append(", ").append(valueField); } else if (!hasClassname) { select.append(", doc." + valueField); } else { if (valueField.equals(idField)) { select.append(", idprop.value"); } else { select.append(", valueprop.value"); fromStatements.add("StringProperty as valueprop"); whereStatements.add("obj.id=valueprop.id.id and valueprop.id.name='" + valueField + "'"); } } if (parentField.startsWith("doc.") || parentField.startsWith("obj.")) { select.append(", ").append(parentField); } else if (!hasClassname) { select.append(", doc." + parentField); } else { if (parentField.equals(idField)) { select.append(", idprop.value"); } else if (parentField.equals(valueField)) { select.append(", valueprop.value"); } else { select.append(", parentprop.value"); fromStatements.add("StringProperty as parentprop"); whereStatements.add("obj.id=parentprop.id.id and parentprop.id.name='" + parentField + "'"); } } select.append(" from "); select.append(StringUtils.join(fromStatements.iterator(), ", ")); if (whereStatements.size() > 0) { select.append(" where "); select.append(StringUtils.join(whereStatements.iterator(), " and ")); } sql = select.toString(); } else { } } try { sql = context.getWiki().parseContent(sql, context); } catch (Exception e) { LOGGER.error("Failed to parse SQL script [" + sql + "]. Continuing with non-rendered script.", e); } return sql; } }
DBTreeListClass extends DBListClass { @Override public String getQuery(XWikiContext context) { String sql = getSql(); if (StringUtils.isBlank(sql)) { 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()); boolean hasClassname = !StringUtils.isBlank(classname); boolean hasIdField = !StringUtils.isBlank(idField); boolean hasValueField = !StringUtils.isBlank(valueField); boolean hasParentField = !StringUtils.isBlank(parentField); if (!(hasIdField || hasValueField)) { if (hasClassname) { sql = "select distinct doc.fullName, doc.fullName, " + (hasParentField ? parentField : "doc.parent") + " from XWikiDocument as doc, BaseObject as obj" + " where doc.fullName=obj.name and obj.className='" + classname + "'"; } else { sql = DEFAULT_QUERY; } return sql; } if (!hasIdField && hasValueField) { idField = valueField; } else if (hasIdField && !hasValueField) { valueField = idField; } if (!hasParentField) { parentField = "doc.parent"; } boolean usesObj = hasClassname || idField.startsWith("obj.") || valueField.startsWith("obj.") || parentField.startsWith("obj."); boolean usesDoc = idField.startsWith("doc.") || valueField.startsWith("doc.") || parentField.startsWith("doc."); if ((!idField.startsWith("obj.") || !valueField.startsWith("obj.") || !parentField.startsWith("obj.")) && !hasClassname) { usesDoc = true; } StringBuffer select = new StringBuffer("select distinct "); ArrayList<String> fromStatements = new ArrayList<String>(); ArrayList<String> whereStatements = new ArrayList<String>(); if (usesDoc) { fromStatements.add("XWikiDocument as doc"); if (usesObj) { whereStatements.add("doc.fullName=obj.name"); } } if (usesObj) { fromStatements.add("BaseObject as obj"); if (hasClassname) { whereStatements.add("obj.className='" + classname + "'"); } } if (idField.startsWith("doc.") || idField.startsWith("obj.")) { select.append(idField); } else if (!hasClassname) { select.append("doc." + idField); } else { select.append("idprop.value"); fromStatements.add("StringProperty as idprop"); whereStatements.add("obj.id=idprop.id.id and idprop.id.name='" + idField + "'"); } if (valueField.startsWith("doc.") || valueField.startsWith("obj.")) { select.append(", ").append(valueField); } else if (!hasClassname) { select.append(", doc." + valueField); } else { if (valueField.equals(idField)) { select.append(", idprop.value"); } else { select.append(", valueprop.value"); fromStatements.add("StringProperty as valueprop"); whereStatements.add("obj.id=valueprop.id.id and valueprop.id.name='" + valueField + "'"); } } if (parentField.startsWith("doc.") || parentField.startsWith("obj.")) { select.append(", ").append(parentField); } else if (!hasClassname) { select.append(", doc." + parentField); } else { if (parentField.equals(idField)) { select.append(", idprop.value"); } else if (parentField.equals(valueField)) { select.append(", valueprop.value"); } else { select.append(", parentprop.value"); fromStatements.add("StringProperty as parentprop"); whereStatements.add("obj.id=parentprop.id.id and parentprop.id.name='" + parentField + "'"); } } select.append(" from "); select.append(StringUtils.join(fromStatements.iterator(), ", ")); if (whereStatements.size() > 0) { select.append(" where "); select.append(StringUtils.join(whereStatements.iterator(), " and ")); } sql = select.toString(); } else { } } try { sql = context.getWiki().parseContent(sql, context); } catch (Exception e) { LOGGER.error("Failed to parse SQL script [" + sql + "]. Continuing with non-rendered script.", e); } return sql; } DBTreeListClass(PropertyMetaClass wclass);  DBTreeListClass(); }
DBTreeListClass extends DBListClass { @Override public String getQuery(XWikiContext context) { String sql = getSql(); if (StringUtils.isBlank(sql)) { 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()); boolean hasClassname = !StringUtils.isBlank(classname); boolean hasIdField = !StringUtils.isBlank(idField); boolean hasValueField = !StringUtils.isBlank(valueField); boolean hasParentField = !StringUtils.isBlank(parentField); if (!(hasIdField || hasValueField)) { if (hasClassname) { sql = "select distinct doc.fullName, doc.fullName, " + (hasParentField ? parentField : "doc.parent") + " from XWikiDocument as doc, BaseObject as obj" + " where doc.fullName=obj.name and obj.className='" + classname + "'"; } else { sql = DEFAULT_QUERY; } return sql; } if (!hasIdField && hasValueField) { idField = valueField; } else if (hasIdField && !hasValueField) { valueField = idField; } if (!hasParentField) { parentField = "doc.parent"; } boolean usesObj = hasClassname || idField.startsWith("obj.") || valueField.startsWith("obj.") || parentField.startsWith("obj."); boolean usesDoc = idField.startsWith("doc.") || valueField.startsWith("doc.") || parentField.startsWith("doc."); if ((!idField.startsWith("obj.") || !valueField.startsWith("obj.") || !parentField.startsWith("obj.")) && !hasClassname) { usesDoc = true; } StringBuffer select = new StringBuffer("select distinct "); ArrayList<String> fromStatements = new ArrayList<String>(); ArrayList<String> whereStatements = new ArrayList<String>(); if (usesDoc) { fromStatements.add("XWikiDocument as doc"); if (usesObj) { whereStatements.add("doc.fullName=obj.name"); } } if (usesObj) { fromStatements.add("BaseObject as obj"); if (hasClassname) { whereStatements.add("obj.className='" + classname + "'"); } } if (idField.startsWith("doc.") || idField.startsWith("obj.")) { select.append(idField); } else if (!hasClassname) { select.append("doc." + idField); } else { select.append("idprop.value"); fromStatements.add("StringProperty as idprop"); whereStatements.add("obj.id=idprop.id.id and idprop.id.name='" + idField + "'"); } if (valueField.startsWith("doc.") || valueField.startsWith("obj.")) { select.append(", ").append(valueField); } else if (!hasClassname) { select.append(", doc." + valueField); } else { if (valueField.equals(idField)) { select.append(", idprop.value"); } else { select.append(", valueprop.value"); fromStatements.add("StringProperty as valueprop"); whereStatements.add("obj.id=valueprop.id.id and valueprop.id.name='" + valueField + "'"); } } if (parentField.startsWith("doc.") || parentField.startsWith("obj.")) { select.append(", ").append(parentField); } else if (!hasClassname) { select.append(", doc." + parentField); } else { if (parentField.equals(idField)) { select.append(", idprop.value"); } else if (parentField.equals(valueField)) { select.append(", valueprop.value"); } else { select.append(", parentprop.value"); fromStatements.add("StringProperty as parentprop"); whereStatements.add("obj.id=parentprop.id.id and parentprop.id.name='" + parentField + "'"); } } select.append(" from "); select.append(StringUtils.join(fromStatements.iterator(), ", ")); if (whereStatements.size() > 0) { select.append(" where "); select.append(StringUtils.join(whereStatements.iterator(), " and ")); } sql = select.toString(); } else { } } try { sql = context.getWiki().parseContent(sql, context); } catch (Exception e) { LOGGER.error("Failed to parse SQL script [" + sql + "]. Continuing with non-rendered script.", e); } return sql; } DBTreeListClass(PropertyMetaClass wclass);  DBTreeListClass(); String getParentField(); void setParentField(String parentField); Map<String, List<ListItem>> getTreeMap(XWikiContext context); @Override void displayView(StringBuffer buffer, String name, String prefix, BaseCollection object,
        XWikiContext context); @Override void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object,
        XWikiContext context); @Override String getQuery(XWikiContext context); }
DBTreeListClass extends DBListClass { @Override public String getQuery(XWikiContext context) { String sql = getSql(); if (StringUtils.isBlank(sql)) { 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()); boolean hasClassname = !StringUtils.isBlank(classname); boolean hasIdField = !StringUtils.isBlank(idField); boolean hasValueField = !StringUtils.isBlank(valueField); boolean hasParentField = !StringUtils.isBlank(parentField); if (!(hasIdField || hasValueField)) { if (hasClassname) { sql = "select distinct doc.fullName, doc.fullName, " + (hasParentField ? parentField : "doc.parent") + " from XWikiDocument as doc, BaseObject as obj" + " where doc.fullName=obj.name and obj.className='" + classname + "'"; } else { sql = DEFAULT_QUERY; } return sql; } if (!hasIdField && hasValueField) { idField = valueField; } else if (hasIdField && !hasValueField) { valueField = idField; } if (!hasParentField) { parentField = "doc.parent"; } boolean usesObj = hasClassname || idField.startsWith("obj.") || valueField.startsWith("obj.") || parentField.startsWith("obj."); boolean usesDoc = idField.startsWith("doc.") || valueField.startsWith("doc.") || parentField.startsWith("doc."); if ((!idField.startsWith("obj.") || !valueField.startsWith("obj.") || !parentField.startsWith("obj.")) && !hasClassname) { usesDoc = true; } StringBuffer select = new StringBuffer("select distinct "); ArrayList<String> fromStatements = new ArrayList<String>(); ArrayList<String> whereStatements = new ArrayList<String>(); if (usesDoc) { fromStatements.add("XWikiDocument as doc"); if (usesObj) { whereStatements.add("doc.fullName=obj.name"); } } if (usesObj) { fromStatements.add("BaseObject as obj"); if (hasClassname) { whereStatements.add("obj.className='" + classname + "'"); } } if (idField.startsWith("doc.") || idField.startsWith("obj.")) { select.append(idField); } else if (!hasClassname) { select.append("doc." + idField); } else { select.append("idprop.value"); fromStatements.add("StringProperty as idprop"); whereStatements.add("obj.id=idprop.id.id and idprop.id.name='" + idField + "'"); } if (valueField.startsWith("doc.") || valueField.startsWith("obj.")) { select.append(", ").append(valueField); } else if (!hasClassname) { select.append(", doc." + valueField); } else { if (valueField.equals(idField)) { select.append(", idprop.value"); } else { select.append(", valueprop.value"); fromStatements.add("StringProperty as valueprop"); whereStatements.add("obj.id=valueprop.id.id and valueprop.id.name='" + valueField + "'"); } } if (parentField.startsWith("doc.") || parentField.startsWith("obj.")) { select.append(", ").append(parentField); } else if (!hasClassname) { select.append(", doc." + parentField); } else { if (parentField.equals(idField)) { select.append(", idprop.value"); } else if (parentField.equals(valueField)) { select.append(", valueprop.value"); } else { select.append(", parentprop.value"); fromStatements.add("StringProperty as parentprop"); whereStatements.add("obj.id=parentprop.id.id and parentprop.id.name='" + parentField + "'"); } } select.append(" from "); select.append(StringUtils.join(fromStatements.iterator(), ", ")); if (whereStatements.size() > 0) { select.append(" where "); select.append(StringUtils.join(whereStatements.iterator(), " and ")); } sql = select.toString(); } else { } } try { sql = context.getWiki().parseContent(sql, context); } catch (Exception e) { LOGGER.error("Failed to parse SQL script [" + sql + "]. Continuing with non-rendered script.", e); } return sql; } DBTreeListClass(PropertyMetaClass wclass);  DBTreeListClass(); String getParentField(); void setParentField(String parentField); Map<String, List<ListItem>> getTreeMap(XWikiContext context); @Override void displayView(StringBuffer buffer, String name, String prefix, BaseCollection object,
        XWikiContext context); @Override void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object,
        XWikiContext context); @Override String getQuery(XWikiContext context);  }