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.bpel.refactoring;
042:
043: import java.io.IOException;
044: import java.util.ArrayList;
045: import java.util.List;
046: import java.util.Set;
047:
048: import org.netbeans.modules.refactoring.api.Problem;
049: import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring;
050: import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
051: import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
052:
053: import org.netbeans.modules.xml.refactoring.ErrorItem;
054: import org.netbeans.modules.xml.refactoring.spi.RefactoringUtil;
055: import org.netbeans.modules.xml.refactoring.XMLRefactoringTransaction;
056:
057: import org.netbeans.modules.xml.xam.Component;
058: import org.netbeans.modules.xml.xam.Model;
059: import org.netbeans.modules.xml.xam.Referenceable;
060:
061: import org.netbeans.modules.xml.wsdl.model.Message;
062: import org.netbeans.modules.xml.wsdl.model.Part;
063: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.CorrelationProperty;
064: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.PropertyAlias;
065: import static org.netbeans.modules.soa.ui.util.UI.*;
066:
067: /**
068: * @author Vladimir Yaroslavskiy
069: * @version 2007.09.17
070: */
071: final class Deleter extends Plugin {
072:
073: Deleter(SafeDeleteRefactoring refactoring) {
074: myRequest = refactoring;
075: }
076:
077: public Problem prepare(RefactoringElementsBag refactoringElements) {
078: //out();
079: //out("PREPARE");
080: //out();
081: Referenceable reference = myRequest.getRefactoringSource()
082: .lookup(Referenceable.class);
083:
084: if (reference == null) {
085: return null;
086: }
087: Set<Component> roots = getRoots(reference);
088: myElements = new ArrayList<Element>();
089:
090: for (Component root : roots) {
091: List<Element> founds = find(reference, root);
092:
093: //out();
094: //out("Founds: " + founds);
095: //out(" root: " + root);
096:
097: if (founds != null && founds.size() > 0) {
098: //out(" size: " + founds.size());
099: //out(" see: " + founds.get(0).getUserObject() + " " + founds.get(0).getText());
100: myElements.addAll(founds);
101: }
102: }
103: if (myElements.size() > 1) {
104: List<Model> models = getModels(myElements);
105: List<ErrorItem> errors = RefactoringUtil
106: .precheckUsageModels(models, true);
107:
108: if (errors == null) {
109: errors = new ArrayList<ErrorItem>();
110: }
111: populateErrors(errors);
112:
113: if (errors.size() > 0) {
114: return processErrors(errors);
115: }
116: }
117: XMLRefactoringTransaction transaction = myRequest.getContext()
118: .lookup(XMLRefactoringTransaction.class);
119: transaction.register(this , myElements);
120: refactoringElements.registerTransaction(transaction);
121: //out();
122: //out("refactoringElements: " + refactoringElements);
123:
124: for (Element element : myElements) {
125: element.setTransactionObject(transaction);
126: refactoringElements.add(myRequest, element);
127: //out(" element: " + element);
128: }
129: return null;
130: }
131:
132: private void populateErrors(List<ErrorItem> errors) {
133: for (Element element : myElements) {
134: Object object = element.getUserObject();
135:
136: if (object instanceof PropertyAlias) {
137: continue;
138: }
139: ErrorItem error = new ErrorItem(object, i18n(Deleter.class,
140: "ERR_Cascade_Delete_For_PropertyAlias_Only"), // NOI18N
141: ErrorItem.Level.FATAL);
142:
143: errors.add(error);
144: break;
145: }
146: }
147:
148: public Problem fastCheckParameters() {
149: return null;
150: }
151:
152: public Problem checkParameters() {
153: return null;
154: }
155:
156: public void doRefactoring(
157: List<RefactoringElementImplementation> elements)
158: throws IOException {
159: //out();
160: //out("DO: " + myRequest.getRefactoringSource());
161: Referenceable referenceable = myRequest.getRefactoringSource()
162: .lookup(Referenceable.class);
163:
164: if (!(referenceable instanceof Component)) {
165: return;
166: }
167: for (Element element : myElements) {
168: Object object = element.getUserObject();
169: //out(" see: " + object);
170:
171: if (!(object instanceof PropertyAlias)) {
172: continue;
173: }
174: delete((PropertyAlias) object, (Component) referenceable);
175: }
176: //out();
177: }
178:
179: private void delete(PropertyAlias alias, Component target) {
180: Model model = alias.getModel();
181: boolean doTransaction = false;
182:
183: if (model != null) {
184: doTransaction = !model.isIntransaction();
185: }
186: try {
187: if (doTransaction && model != null) {
188: model.startTransaction();
189: }
190: if (target instanceof Part) {
191: alias.setPart(null);
192: } else if (target instanceof Message) {
193: alias.setMessageType(null);
194: alias.setPart(null);
195: } else if (target instanceof CorrelationProperty) {
196: alias.setPropertyName(null);
197: }
198: // else {
199: //out("target: " + target.getClass().getName());
200: // }
201: } finally {
202: if (doTransaction && model != null
203: && model.isIntransaction()) {
204: model.endTransaction();
205: }
206: }
207: }
208:
209: private List<Element> myElements;
210: private SafeDeleteRefactoring myRequest;
211: }
|