}
// SELECT
stmt.append("SELECT ");
StringListBuilder selectList = new StringListBuilder(",", stmt);
if (isNullOrEmpty(selectPropertyIds)) {
// select all properties
for (String alias : types.keySet()) {
selectList.add(alias + ".*");
}
} else {
// select provided properties
for (String propertyId : selectPropertyIds) {
propertyId = propertyId.trim();
if (propertyId.equals("*")) {
// found property "*" -> select all properties
for (String alias : types.keySet()) {
selectList.add(alias + ".*");
}
continue;
}
if (propertyId.endsWith(".*")) {
// found property "x.*"
// -> select all properties of the type with alias "x"
String starAlias = propertyId.substring(0, propertyId.length() - 2);
if (types.containsKey(starAlias)) {
selectList.add(starAlias + ".*");
continue;
} else {
throw new IllegalArgumentException("Alias '" + starAlias + "' is not defined!");
}
}
PropertyDefinition<?> propertyDef = null;
String alias = null;
for (Map.Entry<String, ObjectType> te : types.entrySet()) {
propertyDef = te.getValue().getPropertyDefinitions().get(propertyId);
if (propertyDef != null) {
alias = te.getKey();
break;
}
}
if (propertyDef == null) {
throw new IllegalArgumentException("Property '" + propertyId
+ "' is not defined in the provided object types!");
}
if (propertyDef.getQueryName() == null) {
throw new IllegalArgumentException("Property '" + propertyId + "' has no query name!");
}
selectList.add(alias + "." + propertyDef.getQueryName());
}
}
// FROM
stmt.append(" FROM ");
stmt.append(primaryType.getQueryName());
stmt.append(" AS ");
stmt.append(primaryAlias);
for (Map.Entry<String, ObjectType> te : types.entrySet()) {
if (te.getKey().equals(primaryAlias)) {
continue;
}
stmt.append(" JOIN ");
stmt.append(te.getValue().getQueryName());
stmt.append(" AS ");
stmt.append(te.getKey());
stmt.append(" ON ");
stmt.append(primaryAlias);
stmt.append(".cmis:objectId=");
stmt.append(te.getKey());
stmt.append(".cmis:objectId");
}
// WHERE
if (whereClause != null && whereClause.trim().length() > 0) {
stmt.append(" WHERE ");
stmt.append(whereClause.trim());
}
// ORDER BY
if (isNotEmpty(orderByPropertyIds)) {
stmt.append(" ORDER BY ");
StringListBuilder orderByList = new StringListBuilder(",", stmt);
for (String propertyId : orderByPropertyIds) {
String realPropertyId = propertyId.trim();
String realPropertyIdLower = realPropertyId.toLowerCase(Locale.ENGLISH);
boolean desc = false;
if (realPropertyIdLower.endsWith(" asc")) {
// property ends with " asc" -> remove it
realPropertyId = realPropertyId.substring(0, realPropertyId.length() - 4);
}
if (realPropertyIdLower.endsWith(" desc")) {
// property ends with " desc" -> remove it and mark it as
// descending
realPropertyId = realPropertyId.substring(0, realPropertyId.length() - 5);
desc = true;
}
PropertyDefinition<?> propertyDef = null;
String alias = null;
for (Map.Entry<String, ObjectType> te : types.entrySet()) {
propertyDef = te.getValue().getPropertyDefinitions().get(realPropertyId);
if (propertyDef != null) {
alias = te.getKey();
break;
}
}
if (propertyDef == null) {
throw new IllegalArgumentException("Property '" + realPropertyId
+ "' is not defined in the provided object types!");
}
if (propertyDef.getQueryName() == null) {
throw new IllegalArgumentException("Property '" + realPropertyId + "' has no query name!");
}
if (Boolean.FALSE.equals(propertyDef.isOrderable())) {
throw new IllegalArgumentException("Property '" + realPropertyId + "' is not orderable!");
}
orderByList.add(alias + "." + propertyDef.getQueryName() + (desc ? " DESC" : ""));
}
}
this.statement = stmt.toString();
}