private static String getVarName(String identifier) { String[] parts = StringUtilities.tokenizeIdentifierIntoArray(identifier); StringBuilder sb = new StringBuilder(); for (int i = 0; i < parts.length; i++) { if (i != 0) { sb.append("_"); } sb.append(parts[i].toUpperCase()); } return sb.toString(); }
/** * Heuristically checks if the two layout components belong to the same attribute or rel. * * @param lc1 * @param lc2 * @return true if the two layout components belong to the same attribute or rel. */ private static boolean sameAttributeOrRel(LayoutComponent lc1, LayoutComponent lc2) { String lc1Text = lc1.text == null ? "" : lc1.text.replace(" ", "").trim(); String lc2Text = lc2.text == null ? "" : lc2.text.replace(" ", "").trim(); // texts match. if (lc1Text.length() >= 2 && StringUtilities.equals(lc1Text, lc2Text, true)) { return true; } if (StringUtilities.equals(lc1.varName, lc2Text, true) || StringUtilities.equals(lc1Text, lc2.varName, true)) { return true; } // name match. String[] lc1NameParts = StringUtilities.tokenizeIdentifierIntoArray(lc1.varName); String[] lc2NameParts = StringUtilities.tokenizeIdentifierIntoArray(lc2.varName); if (lc1NameParts.length > 1) { if (concatenateStrings(lc1NameParts, 1) .equalsIgnoreCase(concatenateStrings(lc2NameParts, 0))) { return true; } else if (lc2NameParts.length > 1 && concatenateStrings(lc1NameParts, 1) .equalsIgnoreCase(concatenateStrings(lc2NameParts, 1))) { return true; } else if (concatenateStrings(lc1NameParts, 1).equalsIgnoreCase(lc2Text)) { return true; } } else { if (lc2NameParts.length > 1) { if (concatenateStrings(lc1NameParts, 0) .equalsIgnoreCase(concatenateStrings(lc2NameParts, 1))) { return true; } else if (concatenateStrings(lc2NameParts, 1).equalsIgnoreCase(lc1Text)) { return true; } } } return false; }
private static Relationship getRelationship(List<Relationship> rels, LayoutComponent lc) { Relationship rel = getRelationship(rels, lc.varName); if (rel != null) { return rel; } String[] lcNameParts = StringUtilities.tokenizeIdentifierIntoArray(lc.varName); if (lcNameParts.length > 1) { rel = getRelationship(rels, concatenateStrings(lcNameParts, 1)); } if (rel != null) { return rel; } if (lc.text != null) { rel = getRelationship(rels, lc.text.replace(" ", "").trim()); } return rel; }
/** * Try to obtain the attribute using the varName or text. To match an attr/rel named 'itemValue', * the varName could be 'itemVALUE' or the text 'item value'. * * @param attrs * @param lc * @return */ private static Attribute getAttribute(List<Attribute> attrs, LayoutComponent lc) { Attribute attr = getAttribute(attrs, lc.varName); if (attr != null) { return attr; } String[] lcNameParts = StringUtilities.tokenizeIdentifierIntoArray(lc.varName); if (lcNameParts.length > 1) { attr = getAttribute(attrs, concatenateStrings(lcNameParts, 1)); } if (attr != null) { return attr; } if (lc.text != null) { attr = getAttribute(attrs, lc.text.replace(" ", "").trim()); } return attr; }
@Override public void toString(SqlWriter writer, DatabaseType databaseType) { if (context == null) { writer.write(joinType == TYPE_INNER_JOIN ? "INNER JOIN" : "LEFT OUTER JOIN").write(" "); if (fetch) { writer.write("FETCH ").write(schemaName); } else { writer.write(schemaName).write(" ").write(variableName); } } else { // compiled. if (joinType == TYPE_JOIN_TO_OUTER_QUERY) { // no need to join, as join is handled in the where clause. writer .write(relationship.getTargetEntity().getTableName()) .append(' ') .append(variableName); return; } // normal joins - INNER/LEFT OUTER JOINS. writer.write(joinType == TYPE_INNER_JOIN ? "INNER JOIN" : "LEFT OUTER JOIN").write(" "); RelationshipJoinTable joinTable = relationship.getJoinTable(); // retrieve the source table info. String schemaParent = QueryUtil.getPathParent(schemaName); PathInfo sourceTable = context.getPathInfo(schemaParent); if (sourceTable == null) { throw new EJBQLException("Can not find source reference in the context: " + schemaParent); } String sourceTableVarName = sourceTable.varName; if (joinTable == null) { // without join table. // To-one may have filter too boolean hasFilter = /*relationship.isToMany() &&*/ !StringUtilities.isEmpty(relationship.getSqlFilter()); writer.write(relationship.getTargetEntity().getTableName()).write(" ").write(variableName); writer.write(" ON "); if (hasFilter) { writer.append("("); } writer .append(sourceTableVarName) .append('.') .append(relationship.getSourceAttribute().getSystemName()); writer .write(" = ") .append(variableName) .append('.') .append(relationship.getTargetAttribute().getSystemName()); if (hasFilter) { String filterCondition = relationship.getSqlFilter(); filterCondition = filterCondition.replace("{T}", variableName); filterCondition = filterCondition.replace("{S}", sourceTableVarName); writer.append(" AND ").append(filterCondition).append(")"); } // filter org id and status since such filtering can not be done in WHERE otherwise break // LEFT OUTER JOIN (see comments in SelectStatement.toString). // added on Apr 13, 2010 by Jack if (getJoinType() == Join.TYPE_LEFT_OUTER_JOIN) { SelectStatement selectStmt = (SelectStatement) getFirstAncestor(TYPE_SELECT_STATEMENT); if (selectStmt == null) { throw new RuntimeException("Should not reach here 67584"); } boolean multitenancy = !((Entity) relationship.getTargetEntity()).isCoreEntity() && // this line avoid loading metadata being hung. EOThreadLocal.getEOService() != null && EOThreadLocal.getEOService().getMetaDomain() != null && EOThreadLocal.getEOService().getMetaDomain().multitenancy; if (multitenancy && selectStmt.isOrgSpecific(schemaName, relationship.getTargetEntity())) { writer .append(" AND ") .append(variableName) .append('.') .append(EOConstants.COL_ORG_ID) .append(" = ") .append(EOThreadLocal.getOrgId()); } boolean softDeletion = EOThreadLocal.getEOService() != null && EOThreadLocal.getEOService().getMetaDomain() != null && !EOThreadLocal.getEOService().getMetaDomain().hardDeletion; if (softDeletion) { writer .append(" AND ") .append(variableName) .append('.') .append(EOConstants.COL_STATUS) .append(" <> ") .append(EOConstants.STATUS_DELETED); } } } else { // with join table. String joinTableVarName = context.generateUniqueVarName(joinTable.getTableName().substring(0, 1)); // join the join table first. writer.write(joinTable.getTableName()).append(' ').write(joinTableVarName); writer .write(" ON ") .append(sourceTableVarName) .append('.') .append(relationship.getSourceAttribute().getSystemName()); writer .write(" = ") .append(joinTableVarName) .append('.') .append(joinTable.getSourceIdColumnName()); // then the target entity. writer.write(" INNER JOIN "); writer.write(relationship.getTargetEntity().getTableName()).write(" ").write(variableName); writer .write(" ON ") .append(joinTableVarName) .append('.') .append(joinTable.getTargetIdColumnName()); writer .write(" = ") .append(variableName) .append('.') .append(relationship.getTargetAttribute().getSystemName()); } // additional sqlWhere in relationship. // Commented out by Jack on Mar 29, 2009 - sqlFilter has already been handled in above code. // appendAdditionalJoinCondition(writer, databaseType, sourceTableVarName, variableName); } }