001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.model.impl;
042:
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.HashMap;
046: import java.util.Iterator;
047: import java.util.List;
048: import java.util.Map;
049:
050: import org.netbeans.modules.sql.framework.common.utils.TagParserUtility;
051: import org.netbeans.modules.sql.framework.model.GUIInfo;
052: import org.netbeans.modules.sql.framework.model.SQLCondition;
053: import org.netbeans.modules.sql.framework.model.SQLConnectableObject;
054: import org.netbeans.modules.sql.framework.model.SQLConstants;
055: import org.netbeans.modules.sql.framework.model.SQLDBColumn;
056: import org.netbeans.modules.sql.framework.model.SQLInputObject;
057: import org.netbeans.modules.sql.framework.model.SQLJoinOperator;
058: import org.netbeans.modules.sql.framework.model.SQLJoinTable;
059: import org.netbeans.modules.sql.framework.model.SQLJoinView;
060: import org.netbeans.modules.sql.framework.model.SQLObject;
061: import org.netbeans.modules.sql.framework.model.SQLObjectFactory;
062: import org.netbeans.modules.sql.framework.model.SQLPredicate;
063: import org.netbeans.modules.sql.framework.model.SourceTable;
064: import org.netbeans.modules.sql.framework.model.visitors.SQLVisitor;
065: import org.w3c.dom.Element;
066: import org.w3c.dom.Node;
067: import org.w3c.dom.NodeList;
068: import net.java.hulp.i18n.Logger;
069: import com.sun.sql.framework.exception.BaseException;
070: import org.netbeans.modules.etl.logger.Localizer;
071: import org.netbeans.modules.etl.logger.LogUtil;
072: import org.netbeans.modules.sql.framework.model.SQLGroupBy;
073:
074: /**
075: * @author Ritesh Adval
076: */
077: public class SQLJoinViewImpl extends AbstractSQLObject implements
078: SQLJoinView {
079:
080: private static transient final Logger mLogger = LogUtil
081: .getLogger(SQLJoinViewImpl.class.getName());
082: private static transient final Localizer mLoc = Localizer.get();
083:
084: class SecondParseObjectInfo {
085:
086: private Element mElm;
087: private SQLObject mObj;
088:
089: SecondParseObjectInfo(SQLObject obj, Element elm) {
090: this .mObj = obj;
091: this .mElm = elm;
092: }
093:
094: public Element getElement() {
095: return mElm;
096: }
097:
098: public SQLObject getSQLObject() {
099: return mObj;
100: }
101: }
102:
103: public static final String TAG_JOINVIEW = "join-view";
104: public static final String TAG_TABLES = "tables";
105: private static final String ATTR_ALIAS_NAME = "aliasName";
106: private static final String LOG_CATEGORY = SQLJoinViewImpl.class
107: .getName();
108: /* GUI state info */
109: private GUIInfo guiInfo = new GUIInfo();
110: private ArrayList objectList = new ArrayList();
111: private transient List secondPassList = new ArrayList();
112: private SQLGroupBy groupBy;
113:
114: /** Creates a new instance of SQLJoinContainerImpl */
115: public SQLJoinViewImpl() {
116: this .type = SQLConstants.JOIN_VIEW;
117: this .setDisplayName("JoinView");
118: }
119:
120: public SQLJoinViewImpl(SQLJoinView src) throws BaseException {
121: this ();
122:
123: if (src == null) {
124: throw new IllegalArgumentException(
125: "Must supply non-null SQLJoinView instance for src param.");
126: }
127:
128: try {
129: copyFrom(src);
130: } catch (Exception ex) {
131: throw new BaseException(
132: "can not create SQLJoinViewImpl using copy constructor",
133: ex);
134: }
135: }
136:
137: /**
138: * Adds given SQLObject instance to this SQLDefinition.
139: *
140: * @param newObject new instance to add
141: * @throws BaseException if add fails or instance implements an unrecognized object
142: * type.
143: */
144: public void addObject(SQLObject newObject) throws BaseException {
145: if (newObject.getObjectType() == SQLConstants.JOIN_TABLE
146: || newObject.getObjectType() == SQLConstants.JOIN) {
147: add(newObject);
148: } else {
149: throw new BaseException("Addition of sqlobject of type "
150: + TagParserUtility.getStringType(newObject
151: .getObjectType()) + " is not supported.");
152: }
153: }
154:
155: /**
156: * Adds SQLObject to list of object references to be resolved in a second pass.
157: *
158: * @param sqlObj to be added
159: * @param element DOM Element of SQLObject to be resolved later
160: */
161: public void addSecondPassSQLObject(SQLObject sqlObj, Element element) {
162: secondPassList.add(new SecondParseObjectInfo(sqlObj, element));
163: }
164:
165: public Object clone() throws CloneNotSupportedException {
166: SQLJoinViewImpl cond = null;
167: try {
168: cond = new SQLJoinViewImpl(this );
169: } catch (Exception ex) {
170: mLogger.errorNoloc(mLoc.t(
171: "PRSR119: Error while cloniing SQLJoinViewImpl{0}",
172: this .toString()), ex);
173: throw new CloneNotSupportedException(
174: "can not create clone of " + this .toString());
175: }
176: return cond;
177: }
178:
179: /**
180: * all sql objects are cloneable
181: */
182: public Object cloneSQLObject() throws CloneNotSupportedException {
183: return this .clone();
184: }
185:
186: public boolean containsSourceTable(SourceTable table) {
187: Collection tables = this
188: .getObjectsOfType(SQLConstants.JOIN_TABLE);
189: Iterator it = tables.iterator();
190:
191: while (it.hasNext()) {
192: SQLJoinTable jtable = (SQLJoinTable) it.next();
193: SourceTable sTable = jtable.getSourceTable();
194: if (sTable.equals(table)) {
195: return true;
196: }
197: }
198:
199: return false;
200: }
201:
202: /**
203: * Creates a new SQLObject instance of the given type with the given display name -
204: * does not associated the vended SQLObject with this instance. To associate the
205: * returned SQLObject instance with this instance, the calling method should call
206: * addSQLObject(SQLObject) which will ensure the parent-child relationship is
207: * preserved.
208: *
209: * @param objTag objTag of object to create
210: * @return new SQLObject instance
211: * @throws BaseException if error occurs during creation
212: * @see #addObject(SQLObject)
213: */
214: public SQLObject createObject(String objTag) throws BaseException {
215: return SQLObjectFactory.createObjectForTag(objTag);
216: }
217:
218: /**
219: * Creates a new SQLObject instance of the given type with the given display name -
220: * does not associated the vended SQLObject with this instance. To associate the
221: * returned SQLObject instance with this instance, the calling method should call
222: * addSQLObject(SQLObject) which will ensure the parent-child relationship is
223: * preserved.
224: *
225: * @param className className of object to create
226: * @return new SQLObject instance
227: * @throws BaseException if error occurs during creation
228: * @see #addObject(SQLObject)
229: */
230: public SQLObject createSQLObject(String className)
231: throws BaseException {
232: return SQLObjectFactory.createSQLObject(className);
233: }
234:
235: /**
236: * Overrides default implementation to compute hashcode based on any associated
237: * attributes as well as values of non-transient member variables.
238: *
239: * @param o Object to test for equality with this
240: * @return hashcode for this instance
241: */
242: public boolean equals(Object o) {
243: if (o == null) {
244: return false;
245: } else if (o == this ) {
246: return true;
247: } else if (!(o instanceof SQLJoinView)) {
248: return false;
249: }
250:
251: SQLJoinView joinView = (SQLJoinView) o;
252: Collection objList = joinView.getAllObjects();
253:
254: if (this .objectList.size() == objList.size()) {
255: Iterator it = this .objectList.iterator();
256: while (it.hasNext()) {
257: SQLObject sqlObj = (SQLObject) it.next();
258: if (!objList.contains(sqlObj)) {
259: return false;
260: }
261: }
262:
263: return true;
264: }
265: return false;
266: }
267:
268: /**
269: * get the alias name for this join view
270: *
271: * @return alias name
272: */
273: public String getAliasName() {
274: return (String) this .getAttributeObject(ATTR_ALIAS_NAME);
275: }
276:
277: /**
278: * Gets the Collection of active SQLObjects.
279: *
280: * @return Collection of current SQLObjects in this SQLDefinition instance.
281: */
282: public Collection getAllObjects() {
283: return this .objectList;
284: }
285:
286: /**
287: * Gets GUI-related attributes for this instance in the form of a GuiInfo instance.
288: *
289: * @return associated GuiInfo instance
290: * @see GUIInfo
291: */
292: public GUIInfo getGUIInfo() {
293: return guiInfo;
294: }
295:
296: public SQLJoinOperator getJoinofTable(SQLJoinTable jTable) {
297: Collection joins = this .getObjectsOfType(SQLConstants.JOIN);
298:
299: Iterator it = joins.iterator();
300:
301: while (it.hasNext()) {
302: SQLJoinOperator join = (SQLJoinOperator) it.next();
303: SQLInputObject leftIn = join.getInput(SQLJoinOperator.LEFT);
304: SQLInputObject rightIn = join
305: .getInput(SQLJoinOperator.RIGHT);
306:
307: SQLObject leftObj = leftIn.getSQLObject();
308: SQLObject rightObj = rightIn.getSQLObject();
309:
310: if (leftObj != null && leftObj.equals(jTable)) {
311: return join;
312: }
313:
314: if (rightObj != null && rightObj.equals(jTable)) {
315: return join;
316: }
317: }
318:
319: return null;
320: }
321:
322: public SQLJoinTable getJoinTable(SourceTable sTable) {
323: Collection tables = this
324: .getObjectsOfType(SQLConstants.JOIN_TABLE);
325: Iterator it = tables.iterator();
326:
327: while (it.hasNext()) {
328: SQLJoinTable jtable = (SQLJoinTable) it.next();
329: SourceTable table = jtable.getSourceTable();
330: if (sTable.equals(table)) {
331: return jtable;
332: }
333: }
334:
335: return null;
336: }
337:
338: /**
339: * Gets associated SQLObject instance, if any, with the given object ID.
340: *
341: * @param objectId ID of SQLObject instance to be retrieved
342: * @param aType type of object to retrieve
343: * @return associated SQLObject instance, or null if no such instance exists
344: */
345: public SQLObject getObject(String objectId, int aType) {
346: SQLObject sqlObj = null;
347: sqlObj = getSQLObject(objectId);
348:
349: return sqlObj;
350: }
351:
352: /**
353: * Gets a Collection of SQLObjects, if any, with the given type
354: *
355: * @param atype SQLObject type to retrieve
356: * @return Collection (possibly empty) of SQLObjects with the given type
357: */
358: public Collection getObjectsOfType(int atype) {
359: ArrayList list = new ArrayList();
360:
361: Iterator it = objectList.iterator();
362:
363: while (it.hasNext()) {
364: SQLObject sqlObject = (SQLObject) it.next();
365: if (sqlObject.getObjectType() == atype) {
366: list.add(sqlObject);
367: }
368: }
369:
370: return list;
371: }
372:
373: public int getObjectType() {
374: return SQLConstants.JOIN_VIEW;
375: }
376:
377: /**
378: * Gets parent object, if any, that owns this SQLDefinition instance.
379: *
380: * @return parent object
381: */
382: public Object getParent() {
383: return super .getParentObject();
384: }
385:
386: /**
387: * get table qualified name
388: *
389: * @return qualified table name prefixed with alias
390: */
391: public String getQualifiedName() {
392: StringBuilder buf = new StringBuilder(50);
393: String aName = this .getAliasName();
394: if (aName != null && !aName.trim().equals("")) {
395: buf.append("(");
396: buf.append(aName);
397: buf.append(") ");
398: }
399:
400: buf.append(this .getDisplayName());
401:
402: return buf.toString();
403: }
404:
405: /**
406: * get the root join located in this join view
407: *
408: * @return root join
409: */
410: public SQLJoinOperator getRootJoin() {
411: Collection joins = this .getObjectsOfType(SQLConstants.JOIN);
412: Iterator it = joins.iterator();
413:
414: while (it.hasNext()) {
415: SQLJoinOperator join = (SQLJoinOperator) it.next();
416: if (join.isRoot()) {
417: return join;
418: }
419: }
420:
421: return null;
422: }
423:
424: public List getSourceTables() {
425: ArrayList sTables = new ArrayList();
426:
427: Collection tables = this
428: .getObjectsOfType(SQLConstants.JOIN_TABLE);
429: Iterator it = tables.iterator();
430:
431: while (it.hasNext()) {
432: SQLJoinTable jtable = (SQLJoinTable) it.next();
433: SourceTable sTable = jtable.getSourceTable();
434: sTables.add(sTable);
435: }
436:
437: return sTables;
438: }
439:
440: public Collection getSQLJoinTables() {
441: return this .getObjectsOfType(SQLConstants.JOIN_TABLE);
442: }
443:
444: /**
445: * Overrides default implementation to compute hashcode based on any associated
446: * attributes as well as values of non-transient member variables.
447: *
448: * @return hashcode for this instance
449: */
450: public int hashCode() {
451: int hCode = super .hashCode();
452: Iterator it = this .objectList.iterator();
453: while (it.hasNext()) {
454: SQLObject sqlObj = (SQLObject) it.next();
455: hCode += sqlObj.hashCode();
456: }
457:
458: return hCode;
459: }
460:
461: public boolean isSourceColumnVisible(SQLDBColumn column) {
462: Collection tables = this
463: .getObjectsOfType(SQLConstants.JOIN_TABLE);
464: Iterator it = tables.iterator();
465: SourceTable table = (SourceTable) column.getParent();
466:
467: while (it.hasNext()) {
468: SQLJoinTable jtable = (SQLJoinTable) it.next();
469: SourceTable sTable = jtable.getSourceTable();
470: if (sTable.getParent().getFullyQualifiedTableName(sTable)
471: .equals(
472: table.getParent()
473: .getFullyQualifiedTableName(table))) {
474: List columns = sTable.getColumnList();
475: Iterator cIt = columns.iterator();
476: while (cIt.hasNext()) {
477: SQLDBColumn oldColumn = (SQLDBColumn) cIt.next();
478: // check based on name not on object equal
479: if (oldColumn.getName().equals(column.getName())) {
480: return oldColumn.isVisible();
481: }
482: }
483: }
484: }
485: return false;
486: }
487:
488: /**
489: * Parses the XML content, if any, using the given Element as a source for
490: * reconstituting the member variables and collections of this instance.
491: *
492: * @param xmlElement DOM element containing XML marshalled version of a SQLDefinition
493: * instance
494: * @throws BaseException thrown while parsing XML, or if xmlElement is null
495: */
496: public void parseXML(Element xmlElement) throws BaseException {
497: super .parseXML(xmlElement);
498:
499: NodeList list = xmlElement.getChildNodes();
500:
501: parseXML(list);
502:
503: // parse gui info & groupby operator.
504: NodeList guiInfoList = xmlElement.getChildNodes();
505: for (int i = 0; i < guiInfoList.getLength(); i++) {
506: Node gNode = guiInfoList.item(i);
507: if (gNode.getNodeName().equals(GUIInfo.TAG_GUIINFO)) {
508: this .guiInfo = new GUIInfo((Element) gNode);
509: } else if (gNode.getNodeName().equals(
510: SQLGroupByImpl.ELEMENT_TAG)) {
511: groupBy = new SQLGroupByImpl();
512: groupBy.setParentObject(this );
513: groupBy.parseXML((Element) gNode);
514: }
515: }
516: doSecondPassParse();
517: }
518:
519: /**
520: * Remove all objects from this container
521: */
522: public void removeAllObjects() {
523: this .objectList.clear();
524: }
525:
526: /**
527: * Removes the given object from SQLDefinition
528: *
529: * @param sqlObj to be removed
530: * @throws BaseException while removing
531: */
532: public void removeObject(SQLObject sqlObj) throws BaseException {
533: // chec if it is a table object
534: if (sqlObj == null) {
535: throw new BaseException("Can not delete null object");
536:
537: }
538: switch (sqlObj.getObjectType()) {
539:
540: case SQLConstants.JOIN_TABLE:
541: SQLJoinTable jTable = (SQLJoinTable) sqlObj;
542:
543: Collection joins = this .getObjectsOfType(SQLConstants.JOIN);
544: Iterator it = joins.iterator();
545: while (it.hasNext()) {
546: SQLJoinOperator join = (SQLJoinOperator) it.next();
547: SQLInputObject leftInObj = join
548: .getInput(SQLJoinOperator.LEFT);
549: SQLInputObject rightInObj = join
550: .getInput(SQLJoinOperator.RIGHT);
551:
552: if (jTable.equals(leftInObj.getSQLObject())) {
553: leftInObj.setSQLObject(null);
554: this .removeObject(join);
555: } else if (jTable.equals(rightInObj.getSQLObject())) {
556: rightInObj.setSQLObject(null);
557: this .removeObject(join);
558: } else {
559: // see if this join's condition has that table column reference
560: SQLCondition joinCondition = join
561: .getJoinCondition();
562: SourceTable sTable = jTable.getSourceTable();
563: Iterator cIt = sTable.getColumnList().iterator();
564: while (cIt.hasNext()) {
565: SQLDBColumn column = (SQLDBColumn) cIt.next();
566: joinCondition.removeDanglingColumnRef(column);
567: }
568: }
569: }
570: break;
571: case SQLConstants.JOIN:
572: SQLJoinOperator join = (SQLJoinOperator) sqlObj;
573: removeJoinReference(join);
574: break;
575: }
576: objectList.remove(sqlObj);
577: }
578:
579: /**
580: * Removes given SQLObjects from SQLJoinView collection.
581: *
582: * @param sqlObjects to be removed
583: * @throws BaseException while removing
584: */
585: public void removeObjects(Collection sqlObjs) throws BaseException {
586: if (sqlObjs == null) {
587: throw new BaseException("Can not delete null object");
588: }
589:
590: Iterator itr = sqlObjs.iterator();
591: while (itr.hasNext()) {
592: this .removeObject((SQLObject) itr.next());
593: }
594: }
595:
596: public void removeTablesAndJoins(SourceTable sTable)
597: throws BaseException {
598: Collection joins = this .getObjectsOfType(SQLConstants.JOIN);
599: Iterator it = joins.iterator();
600:
601: while (it.hasNext()) {
602: SQLJoinOperator join = (SQLJoinOperator) it.next();
603:
604: SQLInputObject leftInObj = join
605: .getInput(SQLJoinOperator.LEFT);
606: SQLInputObject rightInObj = join
607: .getInput(SQLJoinOperator.RIGHT);
608:
609: SQLObject leftObj = leftInObj.getSQLObject();
610: SQLObject rightObj = rightInObj.getSQLObject();
611:
612: if (sTable.equals(leftObj) || sTable.equals(rightObj)) {
613: this .removeObject(join);
614: }
615: }
616:
617: this .removeObject(sTable);
618:
619: }
620:
621: /**
622: * set the alias name for this join view
623: *
624: * @param aName alias name
625: */
626: public void setAliasName(String aName) {
627: this .setAttribute(ATTR_ALIAS_NAME, aName);
628: }
629:
630: /**
631: * Sets parent object, if any, that owns this SQLDefinition instance.
632: *
633: * @param newParent new parent object
634: * @throws BaseException if error occurs while setting parent object
635: */
636: public void setParent(Object newParent) {
637: try {
638: super .setParentObject(newParent);
639: } catch (BaseException ex) {
640: ex.printStackTrace();
641: }
642: }
643:
644: /**
645: * Returns the XML representation of collabSegment.
646: *
647: * @param prefix the xml.
648: * @return Returns the XML representation of colabSegment.
649: */
650: public String toXMLString(String prefix) throws BaseException {
651: if (prefix == null) {
652: prefix = "";
653: }
654:
655: StringBuilder buffer = new StringBuilder(500);
656: if (prefix == null) {
657: prefix = "";
658: }
659:
660: buffer.append(prefix).append(getHeader());
661: buffer.append(toXMLAttributeTags(prefix));
662:
663: // write out tables
664: Iterator it = this .objectList.iterator();
665: while (it.hasNext()) {
666: SQLObject obj = (SQLObject) it.next();
667: buffer.append(obj.toXMLString(prefix + "\t"));
668: }
669:
670: if (groupBy != null) {
671: buffer.append(groupBy.toXMLString(prefix + "\t"));
672: }
673:
674: buffer.append(this .guiInfo.toXMLString(prefix + "\t"));
675: buffer.append(prefix).append(getFooter());
676:
677: return buffer.toString();
678: }
679:
680: public void visit(SQLVisitor visitor) {
681: visitor.visit(this );
682: }
683:
684: String generateId() {
685: int cnt = 0;
686:
687: String anId = "sqlObject" + "_" + cnt;
688: while (isIdExists(anId)) {
689: cnt++;
690: anId = "sqlObject" + "_" + cnt;
691: }
692:
693: return anId;
694: }
695:
696: SQLObject getSQLObject(String anId) {
697: if (anId == null) {
698: return null;
699: }
700:
701: Iterator it = objectList.iterator();
702: while (it.hasNext()) {
703: SQLObject sqlObj = (SQLObject) it.next();
704: if (anId.equals(sqlObj.getId())) {
705: return sqlObj;
706: }
707: }
708:
709: return null;
710: }
711:
712: boolean isIdExists(String anId) {
713: if (anId == null) {
714: return false;
715: }
716:
717: if (getSQLObject(anId) != null) {
718: return true;
719: }
720:
721: return false;
722: }
723:
724: private void add(SQLObject newObject) throws BaseException {
725: // sql definition make sure an object added has unique id
726: // first check if id exists if yes then generate a unique id
727: // then add the object
728: if (newObject.getId() == null) {
729: newObject.setId(generateId());
730: }
731:
732: newObject.setParentObject(this );
733: objectList.add(newObject);
734:
735: }
736:
737: private void copyFrom(SQLJoinView src) throws BaseException {
738: super .copyFromSource(src);
739:
740: // copy gui info
741: GUIInfo gInfo = src.getGUIInfo();
742: this .guiInfo = gInfo != null ? (GUIInfo) gInfo.clone() : null;
743:
744: // map of original to cloned objects
745: // this is so that we can set links properly
746: HashMap origToCloneMap = new HashMap();
747:
748: // now copy all container object
749: Collection children = src.getAllObjects();
750: Iterator it = children.iterator();
751:
752: while (it.hasNext()) {
753: SQLObject obj = (SQLObject) it.next();
754: try {
755: SQLObject clonedObj = (SQLObject) obj.cloneSQLObject();
756:
757: this .addObject(clonedObj);
758: origToCloneMap.put(obj, clonedObj);
759:
760: } catch (CloneNotSupportedException ex) {
761: throw new BaseException("Failed to clone " + obj, ex);
762: }
763: }
764:
765: SQLGroupBy grpBy = src.getSQLGroupBy();
766: if (grpBy != null) {
767: groupBy = new SQLGroupByImpl(grpBy);
768: }
769:
770: setLinks(origToCloneMap);
771: origToCloneMap.clear();
772: }
773:
774: /**
775: * @see org.netbeans.modules.sql.framework.model.SourceTable#getSQLGroupBy()
776: */
777: public SQLGroupBy getSQLGroupBy() {
778: return groupBy;
779: }
780:
781: private void doSecondPassParse() throws BaseException {
782: Iterator it = secondPassList.iterator();
783: while (it.hasNext()) {
784: SecondParseObjectInfo objInfo = (SecondParseObjectInfo) it
785: .next();
786: objInfo.getSQLObject()
787: .secondPassParse(objInfo.getElement());
788: }
789:
790: secondPassList.clear();
791: }
792:
793: private void parseXML(NodeList list) throws BaseException {
794: for (int i = 0; i < list.getLength(); i++) {
795: Node node = list.item(i);
796: if (!node.getNodeName().equals(SQLObject.TAG_SQLOBJECT)) {
797: continue;
798: }
799:
800: Element opeElem = (Element) node;
801:
802: SQLObject sqlObj = SQLObjectFactory
803: .createSQLObjectForElement(this , opeElem);
804: if (sqlObj != null) {
805: this .addObject(sqlObj);
806: } else {
807: throw new BaseException("Failed to parse " + opeElem);
808: }
809: }
810: }
811:
812: private void removeJoinReference(SQLJoinOperator join)
813: throws BaseException {
814: Collection joins = this .getObjectsOfType(SQLConstants.JOIN);
815: Iterator it = joins.iterator();
816: while (it.hasNext()) {
817: SQLJoinOperator joinEx = (SQLJoinOperator) it.next();
818: SQLInputObject leftInObj = joinEx
819: .getInput(SQLJoinOperator.LEFT);
820: SQLInputObject rightInObj = joinEx
821: .getInput(SQLJoinOperator.RIGHT);
822: if (join.equals(leftInObj.getSQLObject())) {
823: leftInObj.setSQLObject(null);
824: this .removeObject(joinEx);
825: } else if (join.equals(rightInObj.getSQLObject())) {
826: rightInObj.setSQLObject(null);
827: this .removeObject(joinEx);
828: }
829: }
830:
831: // also remove this joins all input so that root join can be set again
832: Map inMap = join.getInputObjectMap();
833: Iterator itIn = inMap.keySet().iterator();
834:
835: while (itIn.hasNext()) {
836: String argName = (String) itIn.next();
837: SQLInputObject inObj = (SQLInputObject) inMap.get(argName);
838: SQLObject obj = inObj.getSQLObject();
839: if (obj != null) {
840: join.removeInputByArgName(argName, obj);
841: }
842: }
843: }
844:
845: private void setLinks(HashMap origToCloneMap) throws BaseException {
846: setLinks(origToCloneMap, origToCloneMap.keySet());
847: }
848:
849: private void setLinks(HashMap origToCloneMap, Collection expObjs)
850: throws BaseException {
851: Iterator it = expObjs.iterator();
852: while (it.hasNext()) {
853: SQLObject origObj = (SQLObject) it.next();
854: if (origObj instanceof SQLConnectableObject) {
855: setLinks(origToCloneMap, (SQLConnectableObject) origObj);
856: }
857: }
858: }
859:
860: private void setLinks(HashMap origToCloneMap,
861: SQLConnectableObject origObj) throws BaseException {
862: SQLConnectableObject clonedObj = (SQLConnectableObject) origToCloneMap
863: .get(origObj);
864:
865: if (origObj instanceof SQLPredicate) {
866: SQLPredicate myRoot = ((SQLPredicate) origObj).getRoot();
867: if (myRoot != null) {
868: ((SQLPredicate) clonedObj)
869: .setRoot((SQLPredicate) origToCloneMap
870: .get(myRoot));
871: }
872: }
873:
874: Map inputObjMap = origObj.getInputObjectMap();
875: Iterator it = inputObjMap.keySet().iterator();
876:
877: while (it.hasNext()) {
878: String name = (String) it.next();
879: SQLInputObject inObj = (SQLInputObject) inputObjMap
880: .get(name);
881:
882: SQLObject sqlObj = inObj.getSQLObject();
883: if (sqlObj != null) {
884: SQLObject clonedSQLObj = (SQLObject) origToCloneMap
885: .get(sqlObj);
886: if (clonedSQLObj != null) {
887: clonedObj.addInput(name, clonedSQLObj);
888: }
889: }
890: }
891:
892: List children = origObj.getChildSQLObjects();
893: setLinks(origToCloneMap, children);
894: }
895:
896: /**
897: * @see org.netbeans.modules.sql.framework.model.SourceTable#setSQLGroupBy(org.netbeans.modules.sql.framework.model.SQLGroupBy)
898: */
899: public void setSQLGroupBy(SQLGroupBy groupBy) {
900: this.groupBy = groupBy;
901: }
902: }
|