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.ui.model.impl;
042:
043: import java.awt.Point;
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.Iterator;
047: import java.util.List;
048: import java.util.Map;
049:
050: import javax.swing.SwingUtilities;
051: import javax.swing.undo.UndoManager;
052: import javax.swing.undo.UndoableEditSupport;
053:
054: import org.netbeans.modules.sql.framework.model.SQLCanvasObject;
055: import org.netbeans.modules.sql.framework.model.SQLConnectableObject;
056: import org.netbeans.modules.sql.framework.model.SQLInputObject;
057: import org.netbeans.modules.sql.framework.model.SQLObject;
058: import org.netbeans.modules.sql.framework.model.SQLObjectEvent;
059: import org.netbeans.modules.sql.framework.model.SQLObjectListener;
060: import org.netbeans.modules.sql.framework.model.SQLOperator;
061: import org.netbeans.modules.sql.framework.model.VisibleSQLPredicate;
062: import org.netbeans.modules.sql.framework.model.utils.SQLObjectUtil;
063: import org.netbeans.modules.sql.framework.ui.event.SQLDataEvent;
064: import org.netbeans.modules.sql.framework.ui.event.SQLDataListener;
065: import org.netbeans.modules.sql.framework.ui.event.SQLLinkEvent;
066: import org.netbeans.modules.sql.framework.ui.model.SQLUIModel;
067: import org.netbeans.modules.sql.framework.ui.undo.AddLink;
068: import org.netbeans.modules.sql.framework.ui.undo.AddNode;
069: import org.netbeans.modules.sql.framework.ui.undo.RemoveLink;
070: import org.netbeans.modules.sql.framework.ui.undo.RemoveNode;
071: import org.netbeans.modules.sql.framework.ui.undo.SQLUndoManager;
072: import net.java.hulp.i18n.Logger;
073: import com.sun.sql.framework.exception.BaseException;
074: import org.netbeans.modules.etl.logger.Localizer;
075: import org.netbeans.modules.etl.logger.LogUtil;
076: import org.netbeans.modules.sql.framework.model.DBTable;
077:
078: /**
079: * @author Ritesh Adval
080: */
081: public abstract class AbstractSQLModel implements SQLObjectListener,
082: SQLUIModel {
083:
084: private static final String LOG_CATEGORY = AbstractSQLModel.class
085: .getName();
086: private static transient final Logger mLogger = LogUtil
087: .getLogger(AbstractSQLModel.class.getName());
088: private static transient final Localizer mLoc = Localizer.get();
089: protected UndoableEditSupport editSupport = new UndoableEditSupport();
090: protected boolean isDirty = false;
091: protected List<SQLDataListener> listeners = new ArrayList<SQLDataListener>();
092: protected SQLUndoManager undoManager = new SQLUndoManager();
093: private List<SQLOperator> javaOperatorList = new ArrayList<SQLOperator>();
094:
095: protected AbstractSQLModel() {
096: UndoableEditSupport editSupt = this .getUndoEditSupport();
097: editSupt.addUndoableEditListener(this .getUndoManager());
098: }
099:
100: public void addJavaOperator(SQLOperator javaOp) {
101: this .javaOperatorList.add(javaOp);
102: setContainsJavaOperators(true);
103: }
104:
105: public void addObject(SQLObject sqlObject) throws BaseException {
106: editSupport.postEdit(new AddNode(this , sqlObject));
107: }
108:
109: public abstract void addObjectIgnoreUndo(SQLObject sqlObject)
110: throws BaseException;
111:
112: /**
113: * Adds a SourceTableImpl instance using the given DBTable instance as a template, if
114: * it does not already exist.
115: *
116: * @param srcTable DBTable to serve as template for the new SourceTableImpl instance.
117: * @return new SourceTableImpl instance
118: * @throws BaseException if error occurs during creation
119: */
120: public SQLObject addSourceTable(DBTable srcTable, Point loc)
121: throws BaseException {
122: return null;
123: }
124:
125: public synchronized void addSQLDataListener(SQLDataListener l) {
126: listeners.add(l);
127: }
128:
129: public abstract void addSQLObject(SQLObject sqlObject)
130: throws BaseException;
131:
132: /**
133: * Adds a TargetTableImpl instance using the given DBTable instance as a template, if
134: * it does not already exist.
135: *
136: * @param targetTable DBTable to serve as template for the new TargetTableImpl
137: * instance.
138: * @return SourceTableImpl representing the contents of the given template object; may
139: * be a pre-existing object.
140: * @throws BaseException if error occurs during creation
141: */
142: public SQLObject addTargetTable(DBTable targetTable, Point loc)
143: throws BaseException {
144: return null;
145: }
146:
147: public void clearJavaOperators() {
148: this .javaOperatorList.clear();
149: setContainsJavaOperators(false);
150: }
151:
152: public void clearListener() {
153: this .listeners.clear();
154: }
155:
156: /**
157: * Called when a link is created in collaboration view
158: *
159: * @param srcObject object which is source of new link
160: * @param srcFieldName -
161: * @param destObject object which is destination of new link
162: * @param destFieldName -
163: * @throws BaseException if error occurs during linking
164: */
165: public void createLink(SQLCanvasObject srcObject,
166: String srcFieldName, SQLConnectableObject destObject,
167: String destFieldName) throws BaseException {
168:
169: SQLObject fieldObj = srcObject.getOutput(srcFieldName);
170: destObject.addInput(destFieldName, fieldObj);
171:
172: SQLLinkEvent evt = new SQLLinkEvent(this , srcObject,
173: destObject, srcFieldName, destFieldName);
174: fireSQLLinkCreationEvent(evt);
175: isDirty = true;
176: editSupport.postEdit(new AddLink(this , srcObject, srcFieldName,
177: destObject, destFieldName));
178: }
179:
180: /**
181: * Called when a link is created in collaboration view
182: *
183: * @param srcObject object which is source of new link
184: * @param srcFieldName -
185: * @param destObject object which is destination of new link
186: * @param destFieldName -
187: * @throws BaseException if error occurs during linking
188: */
189: public void createLinkIgnoreUndo(SQLCanvasObject srcObject,
190: String srcFieldName, SQLConnectableObject destObject,
191: String destFieldName) throws BaseException {
192:
193: SQLObject fieldObj = srcObject.getOutput(srcFieldName);
194: destObject.addInput(destFieldName, fieldObj);
195:
196: SQLLinkEvent evt = new SQLLinkEvent(this , srcObject,
197: destObject, srcFieldName, destFieldName);
198: fireSQLLinkCreationEvent(evt);
199: }
200:
201: public abstract SQLCanvasObject createObject(String className)
202: throws BaseException;
203:
204: public void createVisiblePredicateRefObj(
205: VisibleSQLPredicate predicate) {
206: String newOperator = predicate.getOperatorType();
207: if (newOperator == null) {
208: return;
209: }
210: }
211:
212: public synchronized void fireChildObjectCreatedEvent(
213: final SQLDataEvent evt) {
214: Iterator it = listeners.iterator();
215: while (it.hasNext()) {
216: final SQLDataListener l = (SQLDataListener) it.next();
217: Runnable run = new Runnable() {
218:
219: public void run() {
220: l.childObjectCreated(evt);
221: }
222: };
223:
224: SwingUtilities.invokeLater(run);
225: }
226: }
227:
228: public synchronized void fireChildObjectDeletedEvent(
229: final SQLDataEvent evt) {
230: Iterator it = listeners.iterator();
231: while (it.hasNext()) {
232: final SQLDataListener l = (SQLDataListener) it.next();
233: Runnable run = new Runnable() {
234:
235: public void run() {
236: l.childObjectDeleted(evt);
237: }
238: };
239:
240: SwingUtilities.invokeLater(run);
241: }
242: }
243:
244: public synchronized void fireSQLDataCreationEvent(
245: final SQLDataEvent evt) throws BaseException {
246: Iterator it = listeners.iterator();
247: while (it.hasNext()) {
248: final SQLDataListener l = (SQLDataListener) it.next();
249: l.objectCreated(evt);
250: }
251:
252: }
253:
254: public synchronized void fireSQLDataDeletionEvent(
255: final SQLDataEvent evt) {
256: Iterator it = listeners.iterator();
257: while (it.hasNext()) {
258: final SQLDataListener l = (SQLDataListener) it.next();
259: Runnable run = new Runnable() {
260:
261: public void run() {
262: l.objectDeleted(evt);
263: }
264: };
265:
266: SwingUtilities.invokeLater(run);
267: }
268: }
269:
270: public synchronized void fireSQLDataUpdatedEvent(
271: final SQLDataEvent evt) {
272: Iterator it = listeners.iterator();
273: while (it.hasNext()) {
274: final SQLDataListener l = (SQLDataListener) it.next();
275: Runnable run = new Runnable() {
276:
277: public void run() {
278: l.objectUpdated(evt);
279: }
280: };
281:
282: SwingUtilities.invokeLater(run);
283: }
284: }
285:
286: public synchronized void fireSQLLinkCreationEvent(
287: final SQLLinkEvent evt) {
288: Iterator it = listeners.iterator();
289: while (it.hasNext()) {
290: final SQLDataListener l = (SQLDataListener) it.next();
291: Runnable run = new Runnable() {
292:
293: public void run() {
294: l.linkCreated(evt);
295: }
296: };
297:
298: SwingUtilities.invokeLater(run);
299: }
300: }
301:
302: public synchronized void fireSQLLinkDeletionEvent(
303: final SQLLinkEvent evt) {
304: Iterator it = listeners.iterator();
305: while (it.hasNext()) {
306: final SQLDataListener l = (SQLDataListener) it.next();
307: Runnable run = new Runnable() {
308:
309: public void run() {
310: l.linkDeleted(evt);
311: }
312: };
313:
314: SwingUtilities.invokeLater(run);
315: }
316: }
317:
318: public UndoableEditSupport getUndoEditSupport() {
319: return editSupport;
320: }
321:
322: public UndoManager getUndoManager() {
323: return undoManager;
324: }
325:
326: /**
327: * Check if a java operator is used in the model.
328: *
329: * @return true if a java operator is used.
330: */
331: public boolean isContainsJavaOperators() {
332: return this .javaOperatorList.size() != 0;
333: }
334:
335: public boolean isDirty() {
336: return this .isDirty;
337: }
338:
339: /**
340: * called when an sql object is added
341: *
342: * @param evt event object
343: */
344: public void objectAdded(SQLObjectEvent evt) {
345: this .setDirty(true);
346: }
347:
348: /**
349: * called when an sql object is removed
350: *
351: * @param evt event object
352: */
353: public void objectRemoved(SQLObjectEvent evt) {
354: this .setDirty(true);
355: }
356:
357: public void removeJavaOperator(SQLOperator javaOp) {
358: this .javaOperatorList.remove(javaOp);
359:
360: // if we removed the last java operator from the
361: // graph then reset the flag
362: if (!isContainsJavaOperators()) {
363: setContainsJavaOperators(false);
364: }
365: }
366:
367: /**
368: * Removes a link from backend model object.
369: *
370: * @param srcObject object which is source of link removed
371: * @param srcFieldName -
372: * @param destObject object which is destination of link removed
373: * @param destFieldName -
374: * @throws BaseException if error occurs during unlinking
375: */
376: public void removeLink(SQLCanvasObject srcObject,
377: String srcFieldName, SQLConnectableObject destObject,
378: String destFieldName) throws BaseException {
379: SQLObject fieldObj = srcObject.getOutput(srcFieldName);
380: destObject.removeInputByArgName(destFieldName, fieldObj);
381:
382: SQLLinkEvent evt = new SQLLinkEvent(this , srcObject,
383: destObject, srcFieldName, destFieldName);
384:
385: fireSQLLinkDeletionEvent(evt);
386: isDirty = true;
387: editSupport.postEdit(new RemoveLink(this , srcObject,
388: srcFieldName, destObject, destFieldName));
389: }
390:
391: /**
392: * Removes a link from backend model object.
393: *
394: * @param srcObject object which is source of link removed
395: * @param srcFieldName -
396: * @param destObject object which is destination of link removed
397: * @param destFieldName -
398: * @throws BaseException if error occurs during unlinking
399: */
400: public void removeLinkIgnoreUndo(SQLCanvasObject srcObject,
401: String srcFieldName, SQLConnectableObject destObject,
402: String destFieldName) throws BaseException {
403:
404: SQLObject fieldObj = srcObject.getOutput(srcFieldName);
405: destObject.removeInputByArgName(destFieldName, fieldObj);
406:
407: SQLLinkEvent evt = new SQLLinkEvent(this , srcObject,
408: destObject, srcFieldName, destFieldName);
409:
410: fireSQLLinkDeletionEvent(evt);
411: }
412:
413: public void removeObject(SQLObject sqlObject) throws BaseException {
414: editSupport.postEdit(new RemoveNode(this , sqlObject));
415: }
416:
417: public abstract void removeObjectIgnoreUndo(SQLObject sqlObject)
418: throws BaseException;
419:
420: public synchronized void removeSQLDataListener(SQLDataListener l) {
421: listeners.remove(l);
422: }
423:
424: public void setDirty(boolean dirty) {
425: this .isDirty = dirty;
426: }
427:
428: abstract void setContainsJavaOperators(boolean containsJavaOp);
429:
430: // this is temp till I refactor join
431: protected abstract void addObjectInGraph(SQLObject obj,
432: boolean handleAutojoin) throws BaseException;
433:
434: protected void restoreLinks(Collection objectC) {
435:
436: // now link other objects
437: Iterator it = objectC.iterator();
438: while (it.hasNext()) {
439: SQLObject sqlObj = (SQLObject) it.next();
440: if (sqlObj instanceof SQLConnectableObject) {
441: restoreLinks((SQLConnectableObject) sqlObj);
442: }
443: }
444:
445: isDirty = false;
446: }
447:
448: protected void restoreLinks(SQLConnectableObject sqlExObj) {
449:
450: Map inputMap = sqlExObj.getInputObjectMap();
451: Iterator it = inputMap.keySet().iterator();
452:
453: while (it.hasNext()) {
454: String argName = (String) it.next();
455: SQLInputObject inputObj = (SQLInputObject) inputMap
456: .get(argName);
457: SQLObject srcObj = inputObj.getSQLObject();
458: String srcFieldName = null;
459:
460: if (srcObj == null) {
461: continue;
462: }
463:
464: srcFieldName = srcObj.getDisplayName();
465:
466: srcObj = SQLObjectUtil.getTopSQLCanvasObject(srcObj);
467:
468: if (srcObj instanceof SQLCanvasObject) {
469:
470: SQLLinkEvent evt = new SQLLinkEvent(this ,
471: (SQLCanvasObject) srcObj, sqlExObj,
472: srcFieldName, argName);
473: fireSQLLinkCreationEvent(evt);
474:
475: if (srcObj instanceof SQLConnectableObject) {
476: restoreLinks((SQLConnectableObject) srcObj);
477: }
478: }
479: }
480:
481: // now restore child sql object links
482: List children = sqlExObj.getChildSQLObjects();
483: it = children.iterator();
484: while (it.hasNext()) {
485: SQLObject childObj = (SQLObject) it.next();
486: if (childObj instanceof SQLConnectableObject) {
487: restoreLinks((SQLConnectableObject) childObj);
488: }
489: }
490: }
491:
492: protected void restoreObjects(Collection col) throws BaseException {
493: Iterator it = col.iterator();
494:
495: while (it.hasNext()) {
496: SQLObject sqlObj = (SQLObject) it.next();
497: try {
498: // reload time we do not want to handle auto join
499: addObjectInGraph(sqlObj, false);
500: } catch (BaseException e) {
501: mLogger
502: .errorNoloc(
503: mLoc
504: .t(
505: "PRSR141: Error caught while restoring object ({0})",
506: sqlObj.getDisplayName()),
507: e);
508: throw e;
509: }
510: }
511:
512: isDirty = false;
513: }
514: }
|