private static Relationship getRelationship(List<Relationship> rels, String attempName) { for (Iterator<Relationship> iterator = rels.iterator(); iterator.hasNext(); ) { Relationship rel = iterator.next(); if (rel.getSystemName().equalsIgnoreCase(attempName)) { return rel; } } return null; }
/** Appends additional sqlWhere in relationship if any to the end of the join. */ private void appendAdditionalJoinCondition( SqlWriter writer, DatabaseType databaseType, final String sourceVarName, final String targetVarName) { if (relationship.getSqlFilter() == null || relationship.getSqlFilter().length() == 0) { return; } Node node = NodeFactory.parseExpression(relationship.getSqlFilter()); NodeVisitor visit = new AbstractNodeVisitor() { @Override public boolean visitIdentifier(Identifier identifier) { String path = identifier.getName(); int dotPos = path.indexOf('.'); if (dotPos < 0) { throw new EJBQLException( "Invalid sqlWhere in relationship: " + relationship + " - " + relationship.getSqlFilter()); } String entityName = path.substring(0, dotPos); if (entityName.equals(relationship.getSourceEntity().getSystemName()) || entityName.equals(relationship.getSourceEntity().getTableName())) { identifier.setName(sourceVarName + path.substring(dotPos)); } else if (entityName.equals(relationship.getTargetEntity().getSystemName()) || entityName.equals(relationship.getTargetEntity().getTableName())) { identifier.setName(targetVarName + path.substring(dotPos)); } return true; } }; node.accept(visit); writer.write(" AND "); node.toString(writer, databaseType); }
@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); } }
/** * Translates the given JFormDesigner generated Java GridBagLayout source code to ActionScript. * * @param entity the target entity * @param containerVarName If not null, it is the container var name in ActionScript; otherwise * the container var name is used from Java. * @param sourceJava the editor generated Java source code * @return the translated ActionScript source code */ public static String translateGridBagLayoutSourceToFlex( Entity entity, String containerVarName, String sourceJava) { StringBuilder sb = new StringBuilder(); Pattern pattern = Pattern.compile(REGEX_COMPONENT_HEADER); Matcher matcher = pattern.matcher(sourceJava); List<LayoutComponent> lcs = new ArrayList<LayoutComponent>(); int lastEndPos = -1; String lastVarName = null; while (matcher.find()) { if (lastEndPos != -1) { lcs.addAll(parseComponent(lastVarName, sourceJava.substring(lastEndPos, matcher.start()))); } lastEndPos = matcher.end(); lastVarName = matcher.group(1); } if (lastEndPos != -1) { lcs.addAll(parseComponent(lastVarName, sourceJava.substring(lastEndPos))); } List<Attribute> attrs = entity == null ? null : entity.getAttributes(); List<Relationship> rels = entity == null ? null : entity.getRelationships(); for (int i = 0; i < lcs.size(); i++) { LayoutComponent lc = lcs.get(i); LayoutComponent lcPair = null; for (int k = i + 1; entity != null && k < lcs.size(); k++) { if (sameAttributeOrRel(lc, lcs.get(k))) { lcPair = lcs.get(k); lcs.remove(k); // remove it. break; } } Attribute attr = entity == null ? null : getAttribute(attrs, lc); Relationship rel = entity == null ? null : getRelationship(rels, lc); if (rel != null && !rel.isToOne()) { // not to-one; unable to handle. rel = null; } if (lcPair != null) { // same one. if (attr == null && rel == null) { sb.append("\t\t") .append(getVarNameWithDot(containerVarName, lcPair.containerVarName)) .append("addComponent(") .append(lc.varName) .append("/*") .append(lc.varName); if (lc.text != null && lc.text.trim().length() > 0) { sb.append("-'").append(lc.text).append("'"); } sb.append("*/, ").append(lc.gridBagLayoutConstrains).append("); \n"); // pair. sb.append("\t\t") .append(getVarNameWithDot(containerVarName, lcPair.containerVarName)) .append("addComponent(") .append(lcPair.varName) .append("/*") .append(lcPair.varName); if (lcPair.text != null && lcPair.text.trim().length() > 0) { sb.append("-'").append(lcPair.text).append("'"); } sb.append("*/, ").append(lcPair.gridBagLayoutConstrains).append("); \n"); } else { if (attr == null) { // use rel sb.append("\t\t_editorGroup.createAndAddRelToOneLookupToContainer(") .append(entity.getSystemName()) .append("_EO.REL_") .append(rel == null ? "!" + lc.varName : rel.getSystemName()); } else { // use attr sb.append("\t\t_editorGroup.createAndAddAttrEditorToContainer(") .append(entity.getSystemName()) .append("_EO.ATTR_") .append(attr == null ? "!" + lc.varName : attr.getSystemName()); } sb.append(", ") .append( containerVarName == null || containerVarName.trim().length() == 0 ? lc.containerVarName : containerVarName) .append(", true, ") .append(lc.gridBagLayoutConstrains) .append(", \n\t\t\t") .append(lcPair.gridBagLayoutConstrains) .append("); \n"); } } else { // lonely, assuming no label. if (attr == null && rel == null) { sb.append("\t\t") .append(getVarNameWithDot(containerVarName, lc.containerVarName)) .append("addComponent(") .append(lc.varName) .append("/*") .append(lc.varName); if (lc.text != null && lc.text.trim().length() > 0) { sb.append("-'").append(lc.text).append("'"); } sb.append("*/, ").append(lc.gridBagLayoutConstrains).append("); \n"); } else { if (attr == null) { // use rel sb.append("\t\t_editorGroup.createAndAddRelToOneLookupToContainer(") .append(entity.getSystemName()) .append("_EO.REL_") .append(rel == null ? "!" + lc.varName : rel.getSystemName()); } else { // use attr sb.append("\t\t_editorGroup.createAndAddAttrEditorToContainer(") .append(entity.getSystemName()) .append("_EO.ATTR_") .append(attr == null ? "!" + lc.varName : attr.getSystemName()); } sb.append(", ") .append( containerVarName == null || containerVarName.trim().length() == 0 ? lc.containerVarName : containerVarName) .append(", false, ") .append("null") .append(", ") .append(lc.gridBagLayoutConstrains) .append(");\n"); } } } return sb.toString(); }