0001: /*
0002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003: *
0004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005: *
0006: * The contents of this file are subject to the terms of either the GNU
0007: * General Public License Version 2 only ("GPL") or the Common
0008: * Development and Distribution License("CDDL") (collectively, the
0009: * "License"). You may not use this file except in compliance with the
0010: * License. You can obtain a copy of the License at
0011: * http://www.netbeans.org/cddl-gplv2.html
0012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
0013: * specific language governing permissions and limitations under the
0014: * License. When distributing the software, include this License Header
0015: * Notice in each file and include the License file at
0016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
0017: * particular file as subject to the "Classpath" exception as provided
0018: * by Sun in the GPL Version 2 section of the License file that
0019: * accompanied this code. If applicable, add the following below the
0020: * License Header, with the fields enclosed by brackets [] replaced by
0021: * your own identifying information:
0022: * "Portions Copyrighted [year] [name of copyright owner]"
0023: *
0024: * Contributor(s):
0025: *
0026: * The Original Software is NetBeans. The Initial Developer of the Original
0027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
0028: * Microsystems, Inc. All Rights Reserved.
0029: *
0030: * If you wish your version of this file to be governed by only the CDDL
0031: * or only the GPL Version 2, indicate your decision by adding
0032: * "[Contributor] elects to include this software in this distribution
0033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
0034: * single choice of license, a recipient has the option to distribute
0035: * your version of this file under either the CDDL, the GPL Version 2 or
0036: * to extend the choice of license to its licensees as provided above.
0037: * However, if you add GPL Version 2 code and therefore, elected the GPL
0038: * Version 2 license, then the option applies only if the new code is
0039: * made subject to such option by the copyright holder.
0040: */
0041: package org.netbeans.modules.sql.framework.model.impl;
0042:
0043: import java.util.ArrayList;
0044: import java.util.Collection;
0045: import java.util.HashMap;
0046: import java.util.HashSet;
0047: import java.util.Iterator;
0048: import java.util.List;
0049: import java.util.Map;
0050: import java.util.Set;
0051:
0052: import org.netbeans.modules.sql.framework.common.utils.TagParserUtility;
0053: import org.netbeans.modules.sql.framework.model.ColumnRef;
0054: import org.netbeans.modules.sql.framework.model.GUIInfo;
0055: import org.netbeans.modules.sql.framework.model.SQLCanvasObject;
0056: import org.netbeans.modules.sql.framework.model.SQLCondition;
0057: import org.netbeans.modules.sql.framework.model.SQLConnectableObject;
0058: import org.netbeans.modules.sql.framework.model.SQLConstants;
0059: import org.netbeans.modules.sql.framework.model.SQLDBColumn;
0060: import org.netbeans.modules.sql.framework.model.SQLInputObject;
0061: import org.netbeans.modules.sql.framework.model.SQLModelObjectFactory;
0062: import org.netbeans.modules.sql.framework.model.SQLObject;
0063: import org.netbeans.modules.sql.framework.model.SQLObjectFactory;
0064: import org.netbeans.modules.sql.framework.model.SQLPredicate;
0065: import org.netbeans.modules.sql.framework.model.SourceColumn;
0066: import org.netbeans.modules.sql.framework.model.TargetColumn;
0067: import org.netbeans.modules.sql.framework.model.ValidationInfo;
0068: import org.netbeans.modules.sql.framework.model.VisibleSQLPredicate;
0069: import org.netbeans.modules.sql.framework.model.utils.ConditionUtil;
0070: import org.netbeans.modules.sql.framework.model.utils.SQLObjectUtil;
0071: import org.netbeans.modules.sql.framework.model.visitors.SQLValidationVisitor;
0072: import org.netbeans.modules.sql.framework.model.visitors.SQLVisitor;
0073: import org.netbeans.modules.sql.framework.ui.view.conditionbuilder.ConditionBuilderUtil;
0074: import org.w3c.dom.CDATASection;
0075: import org.w3c.dom.Element;
0076: import org.w3c.dom.Node;
0077: import org.w3c.dom.NodeList;
0078: import org.w3c.dom.Text;
0079: import net.java.hulp.i18n.Logger;
0080: import com.sun.sql.framework.exception.BaseException;
0081: import com.sun.sql.framework.utils.Attribute;
0082: import org.netbeans.modules.etl.logger.Localizer;
0083: import org.netbeans.modules.etl.logger.LogUtil;
0084:
0085: /**
0086: * This class represents the condition set at source table, target table and at each case
0087: * when condition. This holds predicates used in this condition and columnref used in it
0088: *
0089: * @author Ritesh Adval
0090: */
0091: public class SQLConditionImpl implements SQLCondition, Cloneable {
0092:
0093: private static transient final Logger mLogger = LogUtil
0094: .getLogger(SQLConditionImpl.class.getName());
0095: private static transient final Localizer mLoc = Localizer.get();
0096:
0097: class SecondParseObjectInfo {
0098:
0099: private Element mElm;
0100: private SQLObject mObj;
0101:
0102: SecondParseObjectInfo(SQLObject obj, Element elm) {
0103: this .mObj = obj;
0104: this .mElm = elm;
0105: }
0106:
0107: public Element getElement() {
0108: return mElm;
0109: }
0110:
0111: public SQLObject getSQLObject() {
0112: return mObj;
0113: }
0114: }
0115:
0116: private static final String GUIMODE = "guiMode";
0117: private static final String LOG_CATEGORY = SQLConditionImpl.class
0118: .getName();
0119: private static final String PARENTHESIS = "parenthesis";
0120: /**
0121: * Map of attributes; used by concrete implementations to store class-specific fields
0122: * without hardcoding them as member variables
0123: */
0124: private Map attributes = new HashMap();
0125: private String conditionText;
0126: private String dispName;
0127: private GUIInfo guiInfo = new GUIInfo();
0128: private ArrayList objectList;
0129: private Object parent;
0130: private SQLPredicate root;
0131: private transient List secondPassList = new ArrayList();
0132:
0133: public SQLConditionImpl(SQLCondition src) throws BaseException {
0134: this (src.getDisplayName());
0135: if (src == null) {
0136: throw new IllegalArgumentException(
0137: "Must supply non-null SQLCondition instance for src param.");
0138: }
0139:
0140: try {
0141: copyFrom(src);
0142: } catch (Exception ex) {
0143: throw new BaseException(
0144: "can not create SQLConditionImpl using copy constructor",
0145: ex);
0146: }
0147: }
0148:
0149: /** Creates a new instance of SQLCondition */
0150: public SQLConditionImpl(String displayName) {
0151: objectList = new ArrayList();
0152: this .setDisplayName(displayName);
0153: }
0154:
0155: /**
0156: * Adds equality predicate to the condition. If a different predicate already exists
0157: * then it is linked with "AND" predicator. Ex: 1) col = input 2) (T1.EMP_ID =
0158: * S1.EMP_ID) AND (T1.LAST_NAME = S2.LAST_NAME)
0159: *
0160: * @param input
0161: * @param col
0162: */
0163: public void addEqualityPredicate(SQLObject input, SQLDBColumn col)
0164: throws BaseException {
0165: ColumnRefImpl leftCRef = new ColumnRefImpl(col);
0166: ColumnRefImpl rightCRef = new ColumnRefImpl((SQLDBColumn) input);
0167:
0168: VisibleSQLPredicate predicate = SQLModelObjectFactory
0169: .getInstance().createVisibleSQLPredicate();
0170: predicate.setOperatorType(SQLConstants.OPERATOR_STR_EQUAL);
0171: predicate.addInput(SQLPredicate.LEFT, leftCRef);
0172: predicate.addInput(SQLPredicate.RIGHT, rightCRef);
0173:
0174: if (objectList.size() == 0) {
0175: objectList.add(leftCRef);
0176: objectList.add(rightCRef);
0177: objectList.add(predicate);
0178: } else {
0179: SQLObject exPredicate = getMatchingSQLObject(predicate,
0180: PARENTHESIS);
0181: SQLObject exLeftCRef = isObjectExist(leftCRef);
0182: SQLObject exRightCRef = isObjectExist(rightCRef);
0183:
0184: SQLPredicate rootP = getRootPredicate();
0185:
0186: if (exPredicate == null) {
0187: addObject(predicate);
0188:
0189: if (exLeftCRef == null) {
0190: addObject(leftCRef);
0191: } else {
0192: predicate.removeInputByArgName(SQLPredicate.LEFT,
0193: leftCRef);
0194: predicate.addInput(SQLPredicate.LEFT, exLeftCRef);
0195: }
0196:
0197: if (exRightCRef == null) {
0198: addObject(rightCRef);
0199: } else {
0200: predicate.removeInputByArgName(SQLPredicate.RIGHT,
0201: rightCRef);
0202: predicate.addInput(SQLPredicate.RIGHT, exRightCRef);
0203: }
0204:
0205: // By default create an 'and' predicate and add. This newly created
0206: // predicate with existing root predicate in SQLCondition
0207: if (rootP != null) {
0208: VisibleSQLPredicate andP = SQLModelObjectFactory
0209: .getInstance().createVisibleSQLPredicate();
0210: andP.setOperatorType("AND");
0211: andP.addInput(SQLPredicate.LEFT, rootP);
0212: andP.addInput(SQLPredicate.RIGHT, predicate);
0213: addObject(andP);
0214: }
0215:
0216: }
0217: }
0218:
0219: // Now get the condition text and set it
0220: int currentMode = getGuiMode();
0221: setGuiMode(SQLCondition.GUIMODE_GRAPHICAL);
0222: SQLPredicate pred = getRootPredicate();
0223: setGuiMode(currentMode);
0224:
0225: if (pred != null) {
0226: this .setConditionText(pred.toString());
0227: }
0228: }
0229:
0230: /**
0231: * Adds given SQLObject instance to this SQLDefinition.
0232: *
0233: * @param newObject new instance to add
0234: * @throws BaseException if add fails or instance implements an unrecognized object
0235: * type.
0236: */
0237: public void addObject(SQLObject newObject) throws BaseException {
0238: add(newObject);
0239: }
0240:
0241: /**
0242: * Adds SQLObject to list of object references to be resolved in a second pass.
0243: *
0244: * @param sqlObj to be added
0245: * @param element DOM Element of SQLObject to be resolved later
0246: */
0247: public void addSecondPassSQLObject(SQLObject sqlObj, Element element) {
0248: // this is a fix for problem with equal and hashcode method of sql object
0249: // equal method usually use some object which are not yet resolved when
0250: // this method is called during parsing. so different object become
0251: // equal breaking the code because of hash map get and put method
0252: // may return a different object or overwrite existing object
0253: secondPassList.add(new SecondParseObjectInfo(sqlObj, element));
0254: }
0255:
0256: public Object clone() throws CloneNotSupportedException {
0257: SQLCondition cond = null;
0258: try {
0259: cond = new SQLConditionImpl(this );
0260: } catch (Exception ex) {
0261: throw new CloneNotSupportedException(
0262: "can not create clone of " + this .toString());
0263: }
0264:
0265: return cond;
0266: }
0267:
0268: /**
0269: * all sql objects are cloneable
0270: */
0271: public Object cloneSQLObject() throws CloneNotSupportedException {
0272: return this .clone();
0273: }
0274:
0275: /**
0276: * Creates a new SQLObject instance of the given type with the given display name -
0277: * does not associated the vended SQLObject with this instance. To associate the
0278: * returned SQLObject instance with this instance, the calling method should call
0279: * addSQLObject(SQLObject) which will ensure the parent-child relationship is
0280: * preserved.
0281: *
0282: * @param objTag objTag of object to create
0283: * @return new SQLObject instance
0284: * @throws BaseException if error occurs during creation
0285: * @see #addObject(SQLObject)
0286: */
0287: public SQLObject createObject(String objTag) throws BaseException {
0288: return SQLObjectFactory.createObjectForTag(objTag);
0289: }
0290:
0291: /**
0292: * Creates a new SQLObject instance of the given type with the given display name -
0293: * does not associated the vended SQLObject with this instance. To associate the
0294: * returned SQLObject instance with this instance, the calling method should call
0295: * addSQLObject(SQLObject) which will ensure the parent-child relationship is
0296: * preserved.
0297: *
0298: * @param className className of object to create
0299: * @return new SQLObject instance
0300: * @throws BaseException if error occurs during creation
0301: * @see #addObject(SQLObject)
0302: */
0303: public SQLObject createSQLObject(String className)
0304: throws BaseException {
0305: return SQLObjectFactory.createSQLObject(className);
0306: }
0307:
0308: /**
0309: * check if two objects are equal
0310: */
0311: public boolean equals(Object obj) {
0312: if (this == obj) {
0313: return true;
0314: } else if (!(obj instanceof SQLCondition)) {
0315: return false;
0316: } else if (obj == null) {
0317: return false;
0318: }
0319:
0320: SQLCondition cond = (SQLCondition) obj;
0321: // check for display name
0322: boolean matched = (this .getDisplayName() != null) ? this
0323: .getDisplayName().equals(cond.getDisplayName()) : (cond
0324: .getDisplayName() == null);
0325:
0326: // check for condition text
0327: matched &= (this .getConditionText() != null) ? this
0328: .getConditionText().equals(cond.getConditionText())
0329: : (cond.getConditionText() == null);
0330:
0331: // check for object collection
0332: if (matched) {
0333: Collection objList = cond.getAllObjects();
0334: if (this .objectList.size() == objList.size()) {
0335: Iterator it = this .objectList.iterator();
0336: while (it.hasNext()) {
0337: SQLObject sqlObj = (SQLObject) it.next();
0338: if (!objList.contains(sqlObj)) {
0339: matched = false;
0340: break;
0341: }
0342: }
0343: } else {
0344: matched = false;
0345: }
0346: }
0347:
0348: if (matched) {
0349: if (this .getGuiMode() != cond.getGuiMode()) {
0350: matched = false;
0351: }
0352: }
0353:
0354: return matched;
0355: }
0356:
0357: /**
0358: * Gets the Collection of active SQLObjects.
0359: *
0360: * @return Collection of current SQLObjects in this SQLDefinition instance.
0361: */
0362: public Collection getAllObjects() {
0363: return objectList;
0364: }
0365:
0366: /**
0367: * Gets an attribute based on its name
0368: *
0369: * @param attrName attribute Name
0370: * @return Attribute instance associated with attrName, or null if none exists
0371: */
0372: public Attribute getAttribute(String attrName) {
0373: return (Attribute) attributes.get(attrName);
0374: }
0375:
0376: /**
0377: * @see SQLObject#getAttributeNames
0378: */
0379: public Collection getAttributeNames() {
0380: return attributes.keySet();
0381: }
0382:
0383: /**
0384: * @see SQLObject#getAttributeObject
0385: */
0386: public Object getAttributeValue(String attrName) {
0387: Attribute attr = getAttribute(attrName);
0388: return (attr != null) ? attr.getAttributeValue() : null;
0389: }
0390:
0391: public String getConditionText() {
0392: return this .conditionText;
0393: }
0394:
0395: public String getConditionText(boolean constructIfEmpty) {
0396: if (constructIfEmpty) {
0397: if (conditionText == null
0398: || conditionText.trim().equals("")) {
0399: constructSqlText();
0400: }
0401: }
0402: return this .conditionText;
0403: }
0404:
0405: /**
0406: * Gets display name.
0407: *
0408: * @return current display name
0409: */
0410: public String getDisplayName() {
0411: return dispName;
0412: }
0413:
0414: public GUIInfo getGUIInfo() {
0415: return this .guiInfo;
0416: }
0417:
0418: public int getGuiMode() {
0419: Integer mode = (Integer) this .guiInfo
0420: .getAttributeValue(GUIMODE);
0421: if (mode != null) {
0422: return mode.intValue();
0423: }
0424:
0425: return GUIMODE_SQLCODE;
0426: }
0427:
0428: public Collection getInputCanvasObjectsNotIn(List gg) {
0429: Collection inputObjects = getAllInputSQLCanvasObjects(this .root);
0430: inputObjects.removeAll(gg);
0431: return inputObjects;
0432: }
0433:
0434: /**
0435: * Gets associated SQLObject instance, if any, with the given object ID.
0436: *
0437: * @param objectId ID of SQLObject instance to be retrieved
0438: * @param type type of object to retrieve
0439: * @return associated SQLObject instance, or null if no such instance exists
0440: */
0441: public SQLObject getObject(String objectId, int type) {
0442: SQLObject sqlObj = null;
0443: sqlObj = getSQLObject(objectId);
0444:
0445: return sqlObj;
0446: }
0447:
0448: /**
0449: * Gets a Collection of SQLObjects, if any, with the given type
0450: *
0451: * @param type SQLObject type to retrieve
0452: * @return Collection (possibly empty) of SQLObjects with the given type
0453: */
0454: public Collection getObjectsOfType(int type) {
0455: ArrayList list = new ArrayList();
0456:
0457: Iterator it = objectList.iterator();
0458:
0459: while (it.hasNext()) {
0460: SQLObject sqlObject = (SQLObject) it.next();
0461: if (sqlObject.getObjectType() == type) {
0462: list.add(sqlObject);
0463: }
0464: }
0465:
0466: return list;
0467: }
0468:
0469: /**
0470: * Gets parent object, if any, that owns this SQLDefinition instance.
0471: *
0472: * @return parent object
0473: */
0474: public Object getParent() {
0475: return this .parent;
0476: }
0477:
0478: public Object getParentObject() {
0479: return this .parent;
0480: }
0481:
0482: /*
0483: * (non-Javadoc)
0484: *
0485: * @see org.netbeans.modules.sql.framework.model.SQLCondition#getParticipatingColumns()
0486: */
0487: public List getParticipatingColumns() {
0488: List columns = new ArrayList();
0489: SQLConnectableObject expression = this .root;
0490:
0491: if (expression != null) {
0492: columns.addAll(getColumnsInExpression(expression));
0493: }
0494:
0495: return columns;
0496: }
0497:
0498: public SQLPredicate getRootPredicate() {
0499: // return predicate based on where user specified condition
0500: if (this .getGuiMode() == SQLCondition.GUIMODE_GRAPHICAL) {
0501: this .root = findRootPredicate();
0502: // check if there are any dangling objects even though we have a valid root
0503: // predicate
0504: if (this .root != null && isDanglingObjectsExist()) {
0505: this .root = null;
0506: }
0507: } else {
0508: populateObjectsFromConditionText();
0509: }
0510:
0511: return this .root;
0512: }
0513:
0514: /**
0515: * Overrides default implementation to compute hashcode based on any associated
0516: * attributes as well as values of non-transient member variables.
0517: *
0518: * @return hashcode for this instance
0519: */
0520: public int hashCode() {
0521: int hCode = super .hashCode();
0522:
0523: if (this .getDisplayName() != null) {
0524: hCode += this .getDisplayName().hashCode();
0525: }
0526:
0527: if (this .getConditionText() != null) {
0528: hCode += this .getConditionText().hashCode();
0529: }
0530:
0531: Iterator it = this .objectList.iterator();
0532: while (it.hasNext()) {
0533: SQLObject sqlObj = (SQLObject) it.next();
0534: hCode += sqlObj.hashCode();
0535: }
0536:
0537: return hCode;
0538: }
0539:
0540: public boolean isConditionDefined() {
0541: if (this .getAllObjects().size() == 0
0542: && ((this .getConditionText() == null) || (this
0543: .getConditionText() != null && this
0544: .getConditionText().trim().equals("")))) {
0545: return false;
0546: }
0547:
0548: return true;
0549: }
0550:
0551: /**
0552: * Check if a java operator is used in the model.
0553: *
0554: * @return true if a java operator is used.
0555: */
0556: public boolean isContainsJavaOperators() {
0557: Boolean containsJavaOperators = (Boolean) this
0558: .getAttributeValue(ATTR_CONTAINS_JAVA_OPERATORS);
0559: if (containsJavaOperators != null) {
0560: return containsJavaOperators.booleanValue();
0561: }
0562: return false;
0563: }
0564:
0565: /**
0566: * check if the object already exist
0567: *
0568: * @return true if object already exist
0569: */
0570: public SQLObject isObjectExist(SQLObject obj) {
0571: Collection objs = this .getAllObjects();
0572: Iterator it = objs.iterator();
0573:
0574: while (it.hasNext()) {
0575: SQLObject sqlObj = (SQLObject) it.next();
0576: if (sqlObj.equals(obj)) {
0577: return sqlObj;
0578: }
0579: }
0580:
0581: return null;
0582: }
0583:
0584: /**
0585: * is this condition a valid condition
0586: *
0587: * @return true if condition is valid
0588: */
0589: public boolean isValid() {
0590: if (isConditionDefined()) {
0591: if (this .getRootPredicate() != null) {
0592: return true;
0593: }
0594:
0595: return false;
0596: }
0597: // by default condition is valid if condition is not
0598: // yet defined in any way
0599: return true;
0600:
0601: }
0602:
0603: /**
0604: * Parses the XML content, if any, using the given Element as a source for
0605: * reconstituting the member variables and collections of this instance.
0606: *
0607: * @param xmlElement DOM element containing XML marshalled version of a SQLDefinition
0608: * instance
0609: * @exception BaseException thrown while parsing XML, or if xmlElement is null
0610: */
0611: public void parseXML(Element xmlElement) throws BaseException {
0612: NodeList list = null;
0613:
0614: if (xmlElement == null) {
0615: throw new BaseException("xmlElement is null");
0616: }
0617:
0618: // parse sql code
0619: list = xmlElement.getElementsByTagName(TAG_SQLCODE);
0620: if (list != null) {
0621: // we will have first child as TEXT_NODE where we have
0622: // sql text.
0623: if (list.getLength() > 0) {
0624: Node sqlCodeNode = list.item(0);
0625: Node textNode = sqlCodeNode.getFirstChild();
0626: if (textNode != null) {
0627: final short nodeType = textNode.getNodeType();
0628: if (nodeType == Node.CDATA_SECTION_NODE) {
0629: String sql = ((CDATASection) textNode)
0630: .getData();
0631: this .setConditionText(sql);
0632: } else if (nodeType == Node.TEXT_NODE) {
0633: String sql = ((Text) textNode).getData();
0634: this .setConditionText(sql);
0635: }
0636: }
0637: }
0638: }
0639:
0640: // parse the attributes
0641: list = xmlElement.getChildNodes();
0642: TagParserUtility.parseAttributeList(this .attributes, list);
0643:
0644: list = xmlElement.getChildNodes();
0645: parseXML(list);
0646:
0647: // parse gui info
0648: NodeList guiInfoList = xmlElement.getChildNodes();
0649: for (int i = 0; i < guiInfoList.getLength(); i++) {
0650: Node gNode = guiInfoList.item(i);
0651: if (gNode.getNodeName().equals(GUIInfo.TAG_GUIINFO)) {
0652: this .guiInfo = new GUIInfo((Element) gNode);
0653: }
0654: }
0655:
0656: doSecondPassParse();
0657:
0658: }
0659:
0660: /**
0661: * Remove all objects from this container
0662: */
0663: public void removeAllObjects() {
0664: this .objectList.clear();
0665: }
0666:
0667: public void removeDanglingColumnRef(ColumnRefImpl col) {
0668: Collection allObj = this .getAllObjects();
0669: Iterator it = allObj.iterator();
0670:
0671: while (it.hasNext()) {
0672: Object obj = it.next();
0673: if (obj instanceof SQLConnectableObject) {
0674: SQLConnectableObject expObj = (SQLConnectableObject) obj;
0675:
0676: Map inputObjMap = expObj.getInputObjectMap();
0677: Iterator itIn = inputObjMap.keySet().iterator();
0678:
0679: while (itIn.hasNext()) {
0680: String name = (String) itIn.next();
0681: SQLInputObject inputObj = (SQLInputObject) inputObjMap
0682: .get(name);
0683: SQLObject sqlObj = inputObj.getSQLObject();
0684: // null out the dangling condition column ref
0685: if (sqlObj != null && sqlObj.equals(col)) {
0686: inputObj.setSQLObject(null);
0687: }
0688: }
0689: }
0690: }
0691: }
0692:
0693: /**
0694: * when a table is removed whose column are refered in this condition rhen we need to
0695: * remove the column references
0696: */
0697: public void removeDanglingColumnRef(SQLObject column)
0698: throws BaseException {
0699: // get all ColumnRefImpl and check if the ref to given passed column
0700: // if so then remove the reference to this ConditionCoulmnImpl from
0701: // other expression objects within this condition and remove ColumnRefImpl
0702: // also
0703: Collection columnRefs = this
0704: .getObjectsOfType(SQLConstants.COLUMN_REF);
0705: Iterator it = columnRefs.iterator();
0706:
0707: while (it.hasNext()) {
0708: ColumnRefImpl colRef = (ColumnRefImpl) it.next();
0709: SQLObject columnRef = colRef.getColumn();
0710: // check if column contained in the ColumnRefImpl ref to the passed
0711: // coulmn
0712: if (columnRef != null && columnRef.equals(column)) {
0713: removeDanglingColumnRef(colRef);
0714: this .removeObject(colRef);
0715: }
0716: }
0717: }
0718:
0719: /**
0720: * Removes equality operator "col = value" from the condition and any reference to it
0721: * using "AND" operator.
0722: *
0723: * @param col
0724: * @param value
0725: * @throws BaseException
0726: */
0727: public void removeEqualsPredicate(SQLDBColumn col, SQLObject value)
0728: throws BaseException {
0729: ColumnRefImpl leftCRef = new ColumnRefImpl(col);
0730: ColumnRefImpl rightCRef = new ColumnRefImpl((SQLDBColumn) value);
0731:
0732: VisibleSQLPredicate predicate = SQLModelObjectFactory
0733: .getInstance().createVisibleSQLPredicate();
0734: predicate.setOperatorType(SQLConstants.OPERATOR_STR_EQUAL);
0735: predicate.addInput(SQLPredicate.LEFT, leftCRef);
0736: predicate.addInput(SQLPredicate.RIGHT, rightCRef);
0737:
0738: SQLObject exPredicate = getMatchingSQLObject(predicate,
0739: PARENTHESIS);
0740: SQLObject exLeftCRef = isObjectExist(leftCRef);
0741: SQLObject exRightCRef = isObjectExist(rightCRef);
0742: SQLPredicate rootP = getRootPredicate();
0743:
0744: boolean foundExistingAnd = false;
0745: VisibleSQLPredicate exAndP = null;
0746:
0747: if ((exPredicate != null) && (rootP != null)) {
0748: VisibleSQLPredicate andP = SQLModelObjectFactory
0749: .getInstance().createVisibleSQLPredicate();
0750: andP.setOperatorType("AND");
0751:
0752: Collection objColl = getAllObjects();
0753: if (objColl != null) {
0754: Iterator itr = objColl.iterator();
0755: SQLObject sqlObject = null;
0756: SQLObject leftOperand = null;
0757: SQLObject rightOperand = null;
0758: SQLInputObject input = null;
0759: while (itr.hasNext()) {
0760: sqlObject = (SQLObject) itr.next();
0761: if ((sqlObject != null)
0762: && (sqlObject.getObjectType() == andP
0763: .getObjectType())) {
0764: exAndP = (VisibleSQLPredicate) sqlObject;
0765: input = exAndP.getInput(SQLPredicate.LEFT);
0766: leftOperand = input.getSQLObject();
0767:
0768: if (predicate.equals(leftOperand)) {
0769: foundExistingAnd = true;
0770: break;
0771: }
0772:
0773: input = exAndP.getInput(SQLPredicate.RIGHT);
0774: if (input != null) {
0775: rightOperand = input.getSQLObject();
0776: if (predicate.equals(rightOperand)) {
0777: foundExistingAnd = true;
0778: break;
0779: }
0780: }
0781: }
0782: }
0783: }
0784: }
0785:
0786: if (foundExistingAnd) {
0787: if (exAndP != null) {
0788: removeObject(exAndP);
0789: Map inputMap = exAndP.getInputObjectMap();
0790: if (inputMap != null) {
0791: Collection inputs = inputMap.values();
0792: Iterator itr = inputs.iterator();
0793: VisibleSQLPredicate inputPredicate = null;
0794: SQLInputObject inputObject = null;
0795: while (itr.hasNext()) {
0796: inputObject = (SQLInputObject) itr.next();
0797: if (inputObject.getSQLObject() instanceof VisibleSQLPredicate) {
0798: inputPredicate = (VisibleSQLPredicate) inputObject
0799: .getSQLObject();
0800: inputPredicate.setRoot(exAndP.getRoot());
0801: }
0802: }
0803: }
0804: }
0805: }
0806:
0807: if (exPredicate != null) {
0808: removeObject(exPredicate);
0809:
0810: if (exLeftCRef != null) {
0811: removeObject(exLeftCRef);
0812: }
0813:
0814: if (exRightCRef != null) {
0815: removeObject(exRightCRef);
0816: }
0817: }
0818: }
0819:
0820: /**
0821: * Removes the given object from SQLDefinition
0822: *
0823: * @param sqlObj to be removed
0824: * @throws BaseException while removing
0825: */
0826: public void removeObject(SQLObject sqlObj) throws BaseException {
0827: // chec if it is a table object
0828: if (sqlObj == null) {
0829: throw new BaseException("Can not delete null object");
0830: }
0831:
0832: objectList.remove(sqlObj);
0833: }
0834:
0835: /**
0836: * Removes the given list of objects from SQLCondition collection.
0837: *
0838: * @param sqlObjs collection of SQLObjects to be removed
0839: * @throws BaseException while removing
0840: */
0841: public void removeObjects(Collection sqlObjs) throws BaseException {
0842: if (sqlObjs == null) {
0843: throw new BaseException("Can not delete null object");
0844: }
0845: objectList.removeAll(sqlObjs);
0846: }
0847:
0848: /**
0849: * Removes the "TargetColumn IS NULL" expression from the target condition. Used when
0850: * target table is outer joined with source/table view and SQL being generated for
0851: * ANSI satndard FROM clause.
0852: *
0853: * @param cond
0854: * @throws BaseException
0855: */
0856: public void replaceTargetColumnIsNullPredicate()
0857: throws BaseException {
0858: if (objectList != null) {
0859: Iterator itr = objectList.iterator();
0860:
0861: SQLObject condObject = null;
0862: SQLObject operand = null;
0863: SQLObject targetColumnInput = null;
0864: SQLInputObject input = null;
0865: ColumnRef colref = null;
0866: TargetColumn tc = null;
0867: VisibleSQLPredicate isNullPredicate = null;
0868: SQLPredicate isNullParent = null;
0869: boolean resetText = false;
0870: Collection objectsTobeRemoved = new ArrayList();
0871: Collection objectsTobeAdded = new ArrayList();
0872:
0873: while (itr.hasNext()) {
0874: condObject = (SQLObject) itr.next();
0875: if (condObject instanceof VisibleSQLPredicate) {
0876: isNullPredicate = (VisibleSQLPredicate) condObject;
0877: if ("IS".equalsIgnoreCase(isNullPredicate
0878: .getOperatorType())) {
0879: input = isNullPredicate
0880: .getInput(SQLPredicate.LEFT);
0881: colref = (ColumnRef) input.getSQLObject();
0882: operand = colref.getColumn();
0883:
0884: if (operand instanceof TargetColumn) {
0885: objectsTobeRemoved.add(isNullPredicate);
0886:
0887: isNullParent = isNullPredicate.getRoot();
0888:
0889: tc = (TargetColumn) operand;
0890: targetColumnInput = tc.getValue();
0891: // If source column is mapped to a target column,
0892: // replacewith tcol = sCol.
0893: if (targetColumnInput instanceof SourceColumn) {
0894: ColumnRef rightCRef = new ColumnRefImpl(
0895: (SQLDBColumn) targetColumnInput);
0896: VisibleSQLPredicate eqPredicate = SQLModelObjectFactory
0897: .getInstance()
0898: .createVisibleSQLPredicate();
0899: eqPredicate
0900: .setOperatorType(SQLConstants.OPERATOR_STR_EQUAL);
0901: eqPredicate.addInput(SQLPredicate.LEFT,
0902: colref);
0903: eqPredicate.addInput(
0904: SQLPredicate.RIGHT, rightCRef);
0905: eqPredicate.setRoot(isNullParent);
0906: objectsTobeAdded.add(eqPredicate);
0907: objectsTobeAdded.add(rightCRef);
0908:
0909: if (isNullParent != null) {
0910: if (isNullPredicate
0911: .equals(isNullParent
0912: .getInput(
0913: SQLPredicate.LEFT)
0914: .getSQLObject())) {
0915: isNullParent.addInput(
0916: SQLPredicate.LEFT,
0917: eqPredicate);
0918: } else {
0919: isNullParent.addInput(
0920: SQLPredicate.RIGHT,
0921: eqPredicate);
0922: }
0923: }
0924: } else {
0925: objectsTobeRemoved.add(colref);
0926: if (isNullParent != null) {
0927: preparePredicateRemoval(isNullParent);
0928: objectsTobeRemoved
0929: .add(isNullParent);
0930: }
0931: }
0932: }
0933: }
0934: }
0935: }
0936:
0937: if (objectsTobeRemoved.size() > 0) {
0938: removeObjects(objectsTobeRemoved);
0939: resetText = true;
0940: }
0941:
0942: if (objectsTobeAdded.size() > 0) {
0943: itr = objectsTobeAdded.iterator();
0944: while (itr.hasNext()) {
0945: addObject((SQLObject) itr.next());
0946: }
0947: resetText = true;
0948: }
0949:
0950: // Now get the condition text and set it
0951: int currentMode = getGuiMode();
0952: // As SQLCondition mode may be text or graphical depending on whether this
0953: // method is invoked from main eTL canvas or Condition editor canvas.
0954: setGuiMode(SQLCondition.GUIMODE_GRAPHICAL);
0955: SQLPredicate pred = getRootPredicate();
0956: setGuiMode(currentMode);
0957:
0958: if ((pred != null) && (resetText)) {
0959: setConditionText(pred.toString());
0960: }
0961: }
0962: }
0963:
0964: /**
0965: * @see SQLObject#setAttribute
0966: */
0967: public void setAttribute(String attrName, Object val) {
0968: Attribute attr = getAttribute(attrName);
0969: if (attr != null) {
0970: attr.setAttributeValue(val);
0971: } else {
0972: attr = new Attribute(attrName, val);
0973: attributes.put(attrName, attr);
0974: }
0975: }
0976:
0977: public void setConditionText(String text) {
0978: this .conditionText = text;
0979: }
0980:
0981: /**
0982: * set it to true if a java operator is used in the model
0983: *
0984: * @param javaOp true if there is a java operator
0985: */
0986: public void setContainsJavaOperators(boolean javaOp) {
0987: this .setAttribute(ATTR_CONTAINS_JAVA_OPERATORS, new Boolean(
0988: javaOp));
0989: }
0990:
0991: /**
0992: * Sets display name to given value.
0993: *
0994: * @param newName new display name
0995: */
0996: public void setDisplayName(String newName) {
0997: this .dispName = newName;
0998: }
0999:
1000: public void setGuiMode(int mode) {
1001: this .guiInfo.setAttribute(GUIMODE, new Integer(mode));
1002: }
1003:
1004: /**
1005: * Sets parent object, if any, that owns this SQLDefinition instance.
1006: *
1007: * @param newParent new parent object
1008: */
1009: public void setParent(Object newParent) {
1010: this .parent = newParent;
1011: }
1012:
1013: public void setParentObject(Object myParent) {
1014: this .parent = myParent;
1015: }
1016:
1017: /**
1018: * Returns the XML representation of collabSegment.
1019: *
1020: * @param prefix the xml.
1021: * @return Returns the XML representation of collabSegment.
1022: */
1023: public String toXMLString(String prefix) throws BaseException {
1024: if (prefix == null) {
1025: prefix = "";
1026: }
1027:
1028: StringBuilder xml = new StringBuilder(500);
1029:
1030: xml.append(prefix + "<" + TAG_CONDITION);
1031:
1032: xml.append(" " + DISPLAY_NAME + "=\"");
1033: if (this .getDisplayName() != null) {
1034: xml.append(this .getDisplayName().trim());
1035: }
1036: xml.append("\">\n");
1037:
1038: // write out attributes
1039: xml.append(TagParserUtility.toXMLAttributeTags(this .attributes,
1040: prefix));
1041:
1042: String nestedPrefix = prefix + "\t";
1043:
1044: if (getConditionText() != null) {
1045: xml.append(nestedPrefix + "<" + TAG_SQLCODE + ">");
1046: xml.append("<![CDATA[").append(getConditionText()).append(
1047: "]]>");
1048: xml.append("</" + TAG_SQLCODE + ">\n");
1049: }
1050:
1051: xml.append(toXMLString(nestedPrefix, objectList));
1052:
1053: // write out gui info
1054: xml.append(this .guiInfo.toXMLString(nestedPrefix));
1055:
1056: xml.append(prefix).append("</").append(TAG_CONDITION).append(
1057: ">\n");
1058:
1059: return xml.toString();
1060: }
1061:
1062: public List validate() {
1063: SQLValidationVisitor vVisitor = new SQLValidationVisitor();
1064: vVisitor.visit(this );
1065: return vVisitor.getValidationInfoList();
1066: }
1067:
1068: public void visit(SQLVisitor visitor) {
1069: visitor.visit(this );
1070: }
1071:
1072: String generateId() {
1073: int cnt = 0;
1074:
1075: String id = "sqlObject" + "_" + cnt;
1076: while (isIdExists(id)) {
1077: cnt++;
1078: id = "sqlObject" + "_" + cnt;
1079: }
1080:
1081: return id;
1082: }
1083:
1084: SQLObject getSQLObject(String id) {
1085: if (id == null) {
1086: return null;
1087: }
1088:
1089: Iterator it = objectList.iterator();
1090: while (it.hasNext()) {
1091: SQLObject sqlObj = (SQLObject) it.next();
1092: if (id.equals(sqlObj.getId())) {
1093: return sqlObj;
1094: }
1095: }
1096:
1097: return null;
1098: }
1099:
1100: boolean isIdExists(String id) {
1101: if (id == null) {
1102: return false;
1103: }
1104:
1105: if (getSQLObject(id) != null) {
1106: return true;
1107: }
1108:
1109: return false;
1110: }
1111:
1112: private void add(SQLObject newObject) throws BaseException {
1113: // sql definition make sure an object added has unique id
1114: // first check if id exists if yes then generate a unique id
1115: // then add the object
1116: if (newObject.getId() == null) {
1117: newObject.setId(generateId());
1118: }
1119:
1120: newObject.setParentObject(this );
1121: objectList.add(newObject);
1122: }
1123:
1124: private void constructSqlText() {
1125: SQLPredicate theroot = this .getRootPredicate();
1126: if (theroot != null) {
1127: this .setConditionText(theroot.toString());
1128: }
1129: }
1130:
1131: private void copyFrom(SQLCondition src) throws BaseException {
1132: // we need to set the parent otherwise this clone
1133: // condition which is used in condition builder will throw
1134: // null pointer exception as there is no parent set
1135: this .setParent(src.getParent());
1136: this .setDisplayName(src.getDisplayName());
1137: this .setConditionText(src.getConditionText());
1138:
1139: // clone attributes
1140: Collection attrNames = src.getAttributeNames();
1141: Iterator it = attrNames.iterator();
1142:
1143: while (it.hasNext()) {
1144: String name = (String) it.next();
1145: Attribute attr = src.getAttribute(name);
1146: if (attr != null) {
1147: try {
1148: Attribute copiedAttr = (Attribute) attr.clone();
1149: this .attributes.put(name, copiedAttr);
1150: } catch (CloneNotSupportedException ex) {
1151: mLogger
1152: .errorNoloc(
1153: mLoc
1154: .t(
1155: "PRSR108: Failed to copy source objects attributes{0}",
1156: LOG_CATEGORY), ex);
1157: }
1158: }
1159: }
1160:
1161: // copy gui info
1162: GUIInfo gInfo = src.getGUIInfo();
1163: this .guiInfo = gInfo != null ? (GUIInfo) gInfo.clone() : null;
1164:
1165: // map of original to cloned objects
1166: // this is so that we can set links properly
1167: HashMap origToCloneMap = new HashMap();
1168:
1169: // now copy all container object
1170: Collection children = src.getAllObjects();
1171: it = children.iterator();
1172:
1173: while (it.hasNext()) {
1174: SQLObject obj = (SQLObject) it.next();
1175: try {
1176: SQLObject clonedObj = (SQLObject) obj.cloneSQLObject();
1177: this .addObject(clonedObj);
1178: origToCloneMap.put(obj, clonedObj);
1179: } catch (CloneNotSupportedException ex) {
1180: mLogger
1181: .errorNoloc(
1182: mLoc
1183: .t(
1184: "PRSR109: Failed to copy source objects attributes{0}",
1185: LOG_CATEGORY), ex);
1186: }
1187: }
1188:
1189: setLinks(origToCloneMap);
1190: origToCloneMap.clear();
1191: }
1192:
1193: private void doSecondPassParse() throws BaseException {
1194: Iterator it = secondPassList.iterator();
1195: while (it.hasNext()) {
1196: SecondParseObjectInfo objInfo = (SecondParseObjectInfo) it
1197: .next();
1198: objInfo.getSQLObject()
1199: .secondPassParse(objInfo.getElement());
1200: }
1201:
1202: secondPassList.clear();
1203: }
1204:
1205: private SQLPredicate findRootPredicate() {
1206: ArrayList rootPredicateList = new ArrayList();
1207: ArrayList oneTopRootPredicateList = new ArrayList();
1208:
1209: Collection objects = this .objectList;
1210: Iterator it = objects.iterator();
1211:
1212: while (it.hasNext()) {
1213: SQLObject obj = (SQLObject) it.next();
1214: if (obj instanceof VisibleSQLPredicate) {
1215: SQLPredicate predicate = (SQLPredicate) obj;
1216: SQLValidationVisitor vVisitor = new SQLValidationVisitor();
1217: predicate.visit(vVisitor);
1218: List vInfos = vVisitor.getValidationInfoList();
1219: vInfos = ConditionBuilderUtil.filterValidations(vInfos);
1220: if ((vInfos.size() != 0) && hasError(vInfos)) {
1221: return null;
1222: }
1223:
1224: SQLPredicate rootP = predicate.getRoot();
1225:
1226: if (rootP != null) {
1227: // add to root predicate list
1228: rootPredicateList.add(predicate);
1229: } else {
1230: oneTopRootPredicateList.add(predicate);
1231: }
1232: }
1233: }
1234:
1235: // now there should be only one predicate which will have root predicate
1236: // is null and all other predicate should have root predicate
1237: // if this is the case we have one and only one root predicate so we return that'
1238: if (oneTopRootPredicateList.size() == 1) {
1239: return (SQLPredicate) oneTopRootPredicateList.get(0);
1240: }
1241:
1242: return null;
1243: }
1244:
1245: private Collection getAllInputSQLCanvasObjects(
1246: SQLConnectableObject expObj) {
1247: Set inputObjs = new HashSet();
1248: inputObjs.add(expObj);
1249:
1250: Collection inputs = expObj.getInputObjectMap().values();
1251: Iterator it = inputs.iterator();
1252:
1253: while (it.hasNext()) {
1254: SQLInputObject inObj = (SQLInputObject) it.next();
1255: SQLObject sqlObj = inObj.getSQLObject();
1256: // if sqlObj is a SQLCanvasObject then only we need to check.
1257: // This sqlObj may not be a SQLCanvasObject if it is
1258: // a part of object like SQLLiteral for literal values
1259: if (sqlObj != null && sqlObj instanceof SQLCanvasObject) {
1260: if (sqlObj instanceof SQLConnectableObject) {
1261: inputObjs
1262: .addAll(getAllInputSQLCanvasObjects((SQLConnectableObject) sqlObj));
1263: } else {
1264: inputObjs.add(sqlObj);
1265: }
1266: }
1267: }
1268: return inputObjs;
1269: }
1270:
1271: /**
1272: * @param expression
1273: * @return List of source or target table columns participating in the expression.
1274: */
1275: private List getColumnsInExpression(SQLConnectableObject expression) {
1276: List columns = new ArrayList();
1277: Collection inputs = expression.getInputObjectMap().values();
1278: Iterator itr = inputs.iterator();
1279:
1280: while (itr.hasNext()) {
1281: SQLInputObject inObj = (SQLInputObject) itr.next();
1282: SQLObject sqlObj = inObj.getSQLObject();
1283: if (sqlObj != null) {
1284: if (sqlObj instanceof SQLConnectableObject) {
1285: columns
1286: .addAll(getColumnsInExpression((SQLConnectableObject) sqlObj));
1287: } else {
1288: if (sqlObj instanceof ColumnRef) {
1289: SQLObject sObj = ((ColumnRef) sqlObj)
1290: .getColumn();
1291: if ((sObj instanceof SourceColumn)
1292: || (sObj instanceof TargetColumn)) {
1293: columns.add(sObj);
1294: }
1295: }
1296: }
1297: }
1298: }
1299: return columns;
1300: }
1301:
1302: /**
1303: * Gets SQLObject participating in the condition, if one exists. While checking for
1304: * equality ingnores the attribute passed.
1305: *
1306: * @param sqlObject
1307: * @param ignoreAttributeName
1308: * @return matching SQLObject else null
1309: */
1310: private SQLObject getMatchingSQLObject(SQLObject sqlObject,
1311: String ignoreAttributeName) {
1312: SQLObject matchingObject = null;
1313: matchingObject = isObjectExist(sqlObject);
1314:
1315: if (matchingObject == null) {
1316: sqlObject.setAttribute(ignoreAttributeName, Boolean.FALSE);
1317: matchingObject = isObjectExist(sqlObject);
1318: }
1319:
1320: if (matchingObject == null) {
1321: sqlObject.setAttribute(ignoreAttributeName, Boolean.TRUE);
1322: matchingObject = isObjectExist(sqlObject);
1323: }
1324:
1325: return matchingObject;
1326: }
1327:
1328: /**
1329: * @param valInfoList
1330: * @return
1331: */
1332: private boolean hasError(List valInfoList) {
1333: boolean ret = false;
1334:
1335: if (valInfoList != null) {
1336: Iterator itr = valInfoList.iterator();
1337: ValidationInfo vInfo = null;
1338:
1339: while (itr.hasNext()) {
1340: vInfo = (ValidationInfo) itr.next();
1341: if (vInfo.getValidationType() == ValidationInfo.VALIDATION_ERROR) {
1342: ret = true;
1343: break;
1344: }
1345: }
1346: }
1347: return ret;
1348: }
1349:
1350: // check for dangling condition columns in graph mode
1351: // there may be a valid predicate but if there are other dangling
1352: // objects then we warn user. The dangling objects does not harm
1353: // since we still get root predicate , but still we need to check for it to be
1354: // consistent
1355: private boolean isDanglingObjectsExist() {
1356: boolean response = false;
1357: // so we have a valid root predicate, this can be used for condition
1358: // but now check for dangling condition columns by going through all inputs of
1359: // root in a recursive fashion
1360: if (this .root != null) {
1361: Collection usedObjects = getAllInputSQLCanvasObjects(this .root);
1362: if (usedObjects.size() != this .objectList.size()) {
1363: response = true;
1364: }
1365: }
1366:
1367: return response;
1368: }
1369:
1370: private void parseXML(NodeList list) throws BaseException {
1371: for (int i = 0; i < list.getLength(); i++) {
1372: Node node = list.item(i);
1373: if (!node.getNodeName().equals(SQLObject.TAG_SQLOBJECT)) {
1374: continue;
1375: }
1376: Element opeElem = (Element) node;
1377:
1378: SQLObject sqlObj = SQLObjectFactory
1379: .createSQLObjectForElement(this , opeElem);
1380: if (sqlObj != null) {
1381: this .addObject(sqlObj);
1382: } else {
1383: throw new BaseException("Failed to parse " + opeElem);
1384: }
1385: }
1386: }
1387:
1388: private void populateObjectsFromConditionText() {
1389: try {
1390: this .root = null;
1391: SQLObject obj = ConditionUtil.parseCondition(
1392: this .conditionText, SQLObjectUtil
1393: .getAncestralSQLDefinition((SQLObject) this
1394: .getParent()));
1395: this .removeAllObjects();
1396: ConditionUtil.populateCondition(this , obj);
1397: this .root = findRootPredicate();
1398: } catch (Exception ex) {
1399: mLogger
1400: .errorNoloc(
1401: mLoc
1402: .t(
1403: "PRSR110: Error finding root predicate from text condition{0}in {1}",
1404: this .conditionText,
1405: LOG_CATEGORY), ex);
1406: }
1407: }
1408:
1409: /**
1410: * Assign current predicate's parent as its childs parent.
1411: *
1412: * @param cond
1413: * @param predicate
1414: * @throws BaseException
1415: */
1416: private void preparePredicateRemoval(SQLPredicate predicate) {
1417: Map inputMap = predicate.getInputObjectMap();
1418: if (inputMap != null) {
1419: Collection inputs = inputMap.values();
1420: Iterator itr = inputs.iterator();
1421: VisibleSQLPredicate inputPredicate = null;
1422: SQLInputObject inputObject = null;
1423: while (itr.hasNext()) {
1424: inputObject = (SQLInputObject) itr.next();
1425: if (inputObject.getSQLObject() instanceof VisibleSQLPredicate) {
1426: inputPredicate = (VisibleSQLPredicate) inputObject
1427: .getSQLObject();
1428: inputPredicate.setRoot(predicate.getRoot());
1429: }
1430: }
1431: }
1432: }
1433:
1434: private void setLinks(HashMap origToCloneMap) throws BaseException {
1435: setLinks(origToCloneMap, origToCloneMap.keySet());
1436: }
1437:
1438: private void setLinks(HashMap origToCloneMap, Collection expObjs)
1439: throws BaseException {
1440: Iterator it = expObjs.iterator();
1441: while (it.hasNext()) {
1442: SQLObject origObj = (SQLObject) it.next();
1443: if (origObj instanceof SQLConnectableObject) {
1444: setLinks(origToCloneMap, (SQLConnectableObject) origObj);
1445: }
1446: }
1447: }
1448:
1449: private void setLinks(HashMap origToCloneMap,
1450: SQLConnectableObject origObj) throws BaseException {
1451: SQLConnectableObject clonedObj = (SQLConnectableObject) origToCloneMap
1452: .get(origObj);
1453:
1454: if (origObj instanceof SQLPredicate) {
1455: SQLPredicate myRoot = ((SQLPredicate) origObj).getRoot();
1456: if (myRoot != null) {
1457: ((SQLPredicate) clonedObj)
1458: .setRoot((SQLPredicate) origToCloneMap
1459: .get(myRoot));
1460: }
1461: }
1462:
1463: Map inputObjMap = origObj.getInputObjectMap();
1464: Iterator it = inputObjMap.keySet().iterator();
1465:
1466: while (it.hasNext()) {
1467: String name = (String) it.next();
1468: SQLInputObject inObj = (SQLInputObject) inputObjMap
1469: .get(name);
1470:
1471: SQLObject sqlObj = inObj.getSQLObject();
1472: if (sqlObj != null) {
1473: SQLObject clonedSQLObj = (SQLObject) origToCloneMap
1474: .get(sqlObj);
1475: if (clonedSQLObj != null) {
1476: clonedObj.addInput(name, clonedSQLObj);
1477: }
1478: }
1479: }
1480:
1481: List children = origObj.getChildSQLObjects();
1482: setLinks(origToCloneMap, children);
1483: }
1484:
1485: private String toXMLString(String prefix, Collection sqlObjects)
1486: throws BaseException {
1487: Iterator it = sqlObjects.iterator();
1488: StringBuilder xml = new StringBuilder(" ");
1489:
1490: int i = 0;
1491: while (it.hasNext()) {
1492: SQLObject obj = (SQLObject) it.next();
1493: if (obj != null) {
1494: // Add newline between each element, starting after first one.
1495: if (i++ != 0) {
1496: xml.append("\n");
1497: }
1498: xml.append(obj.toXMLString(prefix));
1499: }
1500: }
1501:
1502: return xml.toString();
1503: }
1504: }
|