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.HashMap;
046: import java.util.HashSet;
047: import java.util.List;
048: import java.util.Map;
049: import java.util.Set;
050:
051: import javax.xml.namespace.QName;
052:
053: import org.netbeans.modules.refactoring.api.Problem;
054: import org.netbeans.modules.refactoring.api.RenameRefactoring;
055: import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
056: import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
057:
058: import org.netbeans.modules.xml.refactoring.ErrorItem;
059: import org.netbeans.modules.xml.refactoring.spi.RefactoringUtil;
060: import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
061: import org.netbeans.modules.xml.refactoring.XMLRefactoringTransaction;
062:
063: import org.netbeans.modules.xml.xam.Component;
064: import org.netbeans.modules.xml.xam.Model;
065: import org.netbeans.modules.xml.xam.Nameable;
066: import org.netbeans.modules.xml.xam.Named;
067: import org.netbeans.modules.xml.xam.Referenceable;
068: import org.netbeans.modules.xml.xam.dom.Attribute;
069: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
070:
071: import org.netbeans.modules.xml.schema.model.GlobalElement;
072: import org.netbeans.modules.xml.schema.model.GlobalType;
073:
074: import org.netbeans.modules.xml.wsdl.model.Fault;
075: import org.netbeans.modules.xml.wsdl.model.Message;
076: import org.netbeans.modules.xml.wsdl.model.Operation;
077: import org.netbeans.modules.xml.wsdl.model.Part;
078: import org.netbeans.modules.xml.wsdl.model.PortType;
079: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.CorrelationProperty;
080: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.PartnerLinkType;
081: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.PropertyAlias;
082: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.Query;
083: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.Role;
084:
085: import org.netbeans.modules.bpel.model.api.BpelEntity;
086: import org.netbeans.modules.bpel.model.api.Catch;
087: import org.netbeans.modules.bpel.model.api.ContentElement;
088: import org.netbeans.modules.bpel.model.api.CorrelationSet;
089: import org.netbeans.modules.bpel.model.api.FaultNameReference;
090: import org.netbeans.modules.bpel.model.api.Import;
091: import org.netbeans.modules.bpel.model.api.MessageExchange;
092: import org.netbeans.modules.bpel.model.api.MessageExchangeReference;
093: import org.netbeans.modules.bpel.model.api.OnEvent;
094: import org.netbeans.modules.bpel.model.api.OperationReference;
095: import org.netbeans.modules.bpel.model.api.PartnerLink;
096: import org.netbeans.modules.bpel.model.api.PartReference;
097: import org.netbeans.modules.bpel.model.api.PortTypeReference;
098: import org.netbeans.modules.bpel.model.api.PropertyReference;
099: import org.netbeans.modules.bpel.model.api.Reply;
100: import org.netbeans.modules.bpel.model.api.Throw;
101: import org.netbeans.modules.bpel.model.api.Variable;
102: import org.netbeans.modules.bpel.model.api.events.VetoException;
103: import org.netbeans.modules.bpel.model.api.references.ReferenceCollection;
104: import org.netbeans.modules.bpel.model.api.references.WSDLReference;
105: import static org.netbeans.modules.soa.ui.util.UI.*;
106:
107: /**
108: * @author Vladimir Yaroslavskiy
109: * @version 2007.03.16
110: */
111: final class Renamer extends Plugin {
112:
113: Renamer(RenameRefactoring refactoring) {
114: myRequest = refactoring;
115: }
116:
117: public Problem fastCheckParameters() {
118: Referenceable reference = myRequest.getRefactoringSource()
119: .lookup(Referenceable.class);
120: ErrorItem error = null;
121:
122: if (reference instanceof Model) {
123: error = RefactoringUtil.precheck((Model) reference,
124: myRequest.getNewName());
125: } else if (reference instanceof Nameable) {
126: error = RefactoringUtil.precheck((Nameable) reference,
127: myRequest.getNewName());
128: }
129: if (error != null) {
130: return new Problem(true, error.getMessage());
131: }
132: return null;
133: }
134:
135: public Problem checkParameters() {
136: Referenceable reference = myRequest.getRefactoringSource()
137: .lookup(Referenceable.class);
138:
139: if (reference == null) {
140: return null;
141: }
142: if (!(reference instanceof Model || reference instanceof Nameable)) {
143: return null;
144: }
145: Model model = SharedUtils.getModel(reference);
146: ErrorItem error = RefactoringUtil.precheckTarget(model, true);
147:
148: if (error != null) {
149: return new Problem(isFatal(error), error.getMessage());
150: }
151: if (reference instanceof Model) {
152: error = RefactoringUtil.precheck((Model) model, myRequest
153: .getNewName());
154: } else if (reference instanceof Nameable) {
155: error = RefactoringUtil.precheck((Nameable) reference,
156: myRequest.getNewName());
157: }
158: if (error != null) {
159: return new Problem(isFatal(error), error.getMessage());
160: }
161: return null;
162: }
163:
164: public Problem prepare(RefactoringElementsBag refactoringElements) {
165: Referenceable reference = myRequest.getRefactoringSource()
166: .lookup(Referenceable.class);
167:
168: if (reference == null) {
169: return null;
170: }
171: if (!(reference instanceof Model || reference instanceof Nameable)) {
172: return null;
173: }
174: Set<Component> roots = getRoots(reference);
175: List<Element> elements = new ArrayList<Element>();
176:
177: for (Component root : roots) {
178: List<Element> founds = find(reference, root);
179:
180: if (founds != null) {
181: elements.addAll(founds);
182: }
183: }
184: if (elements.size() > 0) {
185: List<Model> models = getModels(elements);
186: List<ErrorItem> errors = RefactoringUtil
187: .precheckUsageModels(models, true);
188:
189: if (errors != null && errors.size() > 0) {
190: return processErrors(errors);
191: }
192: }
193: XMLRefactoringTransaction transaction = myRequest.getContext()
194: .lookup(XMLRefactoringTransaction.class);
195: transaction.register(this , elements);
196: refactoringElements.registerTransaction(transaction);
197:
198: for (Element element : elements) {
199: element.setTransactionObject(transaction);
200: refactoringElements.add(myRequest, element);
201: }
202: return null;
203: }
204:
205: public void doRefactoring(
206: List<RefactoringElementImplementation> elements)
207: throws IOException {
208: Map<Model, Set<RefactoringElementImplementation>> map = getModelMap(elements);
209: Set<Model> models = map.keySet();
210: Referenceable reference = myRequest.getRefactoringSource()
211: .lookup(Referenceable.class);
212: String oldName = myRequest.getContext().lookup(String.class);
213:
214: for (Model model : models) {
215: if (reference instanceof Nameable) {
216: rename(getComponents(map.get(model)), model,
217: (Named) reference, oldName);
218: } else if (reference instanceof Model) {
219: rename(getComponents(map.get(model)));
220: }
221: }
222: }
223:
224: public String getModelReference(Component component) {
225: if (component instanceof Import) {
226: return ((Import) component).getLocation();
227: }
228: return null;
229: }
230:
231: private Map<Model, Set<RefactoringElementImplementation>> getModelMap(
232: List<RefactoringElementImplementation> elements) {
233: Map<Model, Set<RefactoringElementImplementation>> results = new HashMap<Model, Set<RefactoringElementImplementation>>();
234:
235: for (RefactoringElementImplementation element : elements) {
236: Model model = (element.getLookup().lookup(Component.class))
237: .getModel();
238: Set<RefactoringElementImplementation> components = results
239: .get(model);
240:
241: if (components == null) {
242: components = new HashSet<RefactoringElementImplementation>();
243: components.add(element);
244: results.put(model, components);
245: } else {
246: components.add(element);
247: }
248: }
249: return results;
250: }
251:
252: private List<Component> getComponents(
253: Set<RefactoringElementImplementation> elements) {
254: List<Component> component = new ArrayList<Component>(elements
255: .size());
256:
257: for (RefactoringElementImplementation element : elements) {
258: component.add(element.getLookup().lookup(Component.class));
259: }
260: return component;
261: }
262:
263: private void rename(List<Component> components, Model model,
264: Named target, String oldName) throws IOException {
265: if (target == null || components == null || model == null) {
266: return;
267: }
268: myXPath = new XPath(null, target, oldName);
269: myOldName = oldName;
270: boolean doTransaction = !model.isIntransaction();
271:
272: try {
273: if (doTransaction) {
274: model.startTransaction();
275: }
276: for (Component component : components) {
277: renameComponent(component, target);
278: }
279: } finally {
280: if (doTransaction && model.isIntransaction()) {
281: model.endTransaction();
282: }
283: }
284: }
285:
286: private void rename(List<Component> components) throws IOException {
287: if (components == null) {
288: return;
289: }
290: for (Component component : components) {
291: renameFile(component);
292: }
293: }
294:
295: private void renameFile(Component component) throws IOException {
296: //out();
297: //out("FILE RENAME: " + Util.getName(component));
298: if (!(component instanceof Import)) {
299: return;
300: }
301: try {
302: Import _import = (Import) component;
303: _import.setLocation(SharedUtils.calculateNewLocationString(
304: _import.getLocation(), myRequest));
305: } catch (VetoException e) {
306: throw new IOException(e.getMessage());
307: }
308: }
309:
310: private void renameComponent(Component component, Named target)
311: throws IOException {
312: //out();
313: //out("RENAME: " + Util.getName(target));
314: //out(" in: " + Util.getName(component));
315: //out();
316: if (component instanceof PartReference
317: && ((PartReference) component).getPart() != null
318: && component instanceof ReferenceCollection
319: && target instanceof Part) {
320: rename((PartReference) component, (Part) target);
321: } else if (component instanceof PropertyReference
322: && ((PropertyReference) component).getProperty() != null
323: && component instanceof ReferenceCollection
324: && target instanceof CorrelationProperty) {
325: rename((PropertyReference) component,
326: (CorrelationProperty) target);
327: } else if (component instanceof Variable) {
328: rename((Variable) component, target);
329: } else {
330: renameCamponent(component, target);
331: }
332: }
333:
334: private void renameCamponent(Component component, Named target)
335: throws IOException {
336: if (component instanceof OperationReference
337: && component instanceof ReferenceCollection
338: && target instanceof Operation) {
339: rename((OperationReference) component, (Operation) target);
340: } else if (component instanceof PortTypeReference
341: && component instanceof ReferenceCollection
342: && target instanceof PortType) {
343: rename((PortTypeReference) component, (PortType) target);
344: } else if (component instanceof MessageExchangeReference
345: && component instanceof ReferenceCollection
346: && target instanceof MessageExchange) {
347: rename((MessageExchangeReference) component,
348: (MessageExchange) target);
349: } else {
350: renameCampanent(component, target);
351: }
352: }
353:
354: private void renameCampanent(Component component, Named target)
355: throws IOException {
356: if (component instanceof Role && target instanceof PortType) {
357: rename((Role) component, (PortType) target);
358: } else if (component instanceof Catch) {
359: rename((Catch) component, target);
360: } else if (component instanceof Reply) {
361: rename((Reply) component, target);
362: } else if (component instanceof Throw) {
363: rename((Throw) component, target);
364: } else if (component instanceof OnEvent) {
365: rename((OnEvent) component, target);
366: } else if (component instanceof CorrelationProperty) {
367: rename((CorrelationProperty) component, target);
368: } else if (component instanceof PropertyAlias) {
369: rename((PropertyAlias) component, target);
370: } else if (component instanceof PartnerLink) {
371: rename((PartnerLink) component, target);
372: } else {
373: renameKomponent(component, target);
374: }
375: }
376:
377: private void renameKomponent(Component component, Named target)
378: throws IOException {
379: if (component instanceof CorrelationSet) {
380: rename((CorrelationSet) component, target);
381: } else if (component instanceof ContentElement
382: && component instanceof BpelEntity) {
383: rename((ContentElement) component);
384: } else if (component instanceof Query) {
385: rename((Query) component, target);
386: } else {
387: //out();
388: //out("!!! RENAME IN !!! : " + component.getClass().getName());
389: //out();
390: return;
391: }
392: }
393:
394: private void rename(PartReference reference, Part part) {
395: reference.setPart(((ReferenceCollection) reference)
396: .createWSDLReference(part, Part.class));
397: }
398:
399: private void rename(PropertyReference reference,
400: CorrelationProperty property) {
401: reference.setProperty(((ReferenceCollection) reference)
402: .createWSDLReference(property,
403: CorrelationProperty.class));
404: }
405:
406: private void rename(PortTypeReference reference, PortType portType) {
407: reference.setPortType(((ReferenceCollection) reference)
408: .createWSDLReference(portType, PortType.class));
409: }
410:
411: private void rename(OperationReference reference,
412: Operation operation) {
413: reference.setOperation(((ReferenceCollection) reference)
414: .createWSDLReference(operation, Operation.class));
415: }
416:
417: private void rename(MessageExchangeReference reference,
418: MessageExchange exchange) {
419: reference.setMessageExchange(((ReferenceCollection) reference)
420: .createReference(exchange, MessageExchange.class));
421: }
422:
423: private void rename(Role role, PortType target) throws IOException {
424: role.setPortType(target.createReferenceTo(target,
425: PortType.class));
426: }
427:
428: private void rename(ContentElement element) throws IOException {
429: String content = myXPath.rename(element.getContent(),
430: (BpelEntity) element);
431: //out();
432: //out("New content: " + content);
433: //out();
434: try {
435: element.setContent(content);
436: } catch (VetoException e) {
437: throw new IOException(e.getMessage());
438: }
439: }
440:
441: private void rename(CorrelationProperty property, Named target)
442: throws IOException {
443: NamedComponentReference<GlobalElement> element = property
444: .getElement();
445:
446: if (element != null && target instanceof GlobalElement) {
447: property.setElement(((GlobalElement) target)
448: .createReferenceTo((GlobalElement) target,
449: GlobalElement.class));
450: }
451: NamedComponentReference<GlobalType> type = property.getType();
452:
453: if (type != null && target instanceof GlobalType) {
454: property.setType(((GlobalType) target).createReferenceTo(
455: (GlobalType) target, GlobalType.class));
456: }
457: }
458:
459: private void rename(PropertyAlias alias, Named target)
460: throws IOException {
461: NamedComponentReference<GlobalElement> element = alias
462: .getElement();
463:
464: if (element != null && target instanceof GlobalElement) {
465: alias.setElement(((GlobalElement) target)
466: .createReferenceTo((GlobalElement) target,
467: GlobalElement.class));
468: }
469: NamedComponentReference<GlobalType> type = alias.getType();
470:
471: if (type != null && target instanceof GlobalType) {
472: alias.setType(((GlobalType) target).createReferenceTo(
473: (GlobalType) target, GlobalType.class));
474: }
475: if (target instanceof Message) {
476: alias.setMessageType(((Message) target).createReferenceTo(
477: (Message) target, Message.class));
478: } else if (target instanceof Part) {
479: alias.setPart(((Part) target).getName());
480: } else if (target instanceof CorrelationProperty) {
481: alias.setPropertyName(((CorrelationProperty) target)
482: .createReferenceTo((CorrelationProperty) target,
483: CorrelationProperty.class));
484: }
485: }
486:
487: private void rename(Query query, Named target) throws IOException {
488: int k = Util.checkQuery(query, myOldName);
489: if (k == -1) {
490: return;
491: }
492: String path = query.getContent();
493: query.setContent(path.substring(0, k) + target.getName()
494: + path.substring(k + myOldName.length()));
495: }
496:
497: private void rename(OnEvent event, Named target) throws IOException {
498: if (target instanceof Message) {
499: event.setMessageType(event.createWSDLReference(
500: (Message) target, Message.class));
501: }
502: }
503:
504: private void rename(PartnerLink partnerLink, Named target)
505: throws IOException {
506: if (target instanceof PartnerLinkType) {
507: partnerLink.setPartnerLinkType(partnerLink
508: .createWSDLReference((PartnerLinkType) target,
509: PartnerLinkType.class));
510: } else if (target instanceof Role) {
511: WSDLReference<Role> reference = partnerLink
512: .createWSDLReference((Role) target, Role.class);
513:
514: if (isRenamedRole(partnerLink, PartnerLink.MY_ROLE)) {
515: partnerLink.setMyRole(reference);
516: } else if (isRenamedRole(partnerLink,
517: PartnerLink.PARTNER_ROLE)) {
518: partnerLink.setPartnerRole(reference);
519: }
520: }
521: }
522:
523: private void rename(CorrelationSet correlationSet, Named target)
524: throws IOException {
525: if (target instanceof CorrelationProperty) {
526: //out();
527: //out("RENAME");
528: List<WSDLReference<CorrelationProperty>> references = correlationSet
529: .getProperties();
530:
531: List<WSDLReference<CorrelationProperty>> list = new ArrayList<WSDLReference<CorrelationProperty>>();
532:
533: if (references == null) {
534: return;
535: }
536: for (WSDLReference<CorrelationProperty> reference : references) {
537: if (reference.get() != null) {
538: list.add(reference);
539: //out(" see: " + reference.get());
540: } else {
541: //out(" add");
542: list.add(correlationSet.createWSDLReference(
543: (CorrelationProperty) target,
544: CorrelationProperty.class));
545: }
546: }
547: correlationSet.setProperties(list);
548: }
549: }
550:
551: private boolean isRenamedRole(PartnerLink partnerLink,
552: String attribute) {
553: String roleName = partnerLink.getAttribute(new StringAttribute(
554: attribute));
555: return myOldName.equals(roleName);
556: }
557:
558: private void rename(Variable variable, Named target) {
559: //out();
560: //out("RENAME: " + Util.getName(target));
561: //out(" in: " + Util.getName(variable));
562: //out();
563: if (target instanceof GlobalElement) {
564: variable.setElement(variable.createSchemaReference(
565: (GlobalElement) target, GlobalElement.class));
566: } else if (target instanceof GlobalType) {
567: variable.setType(variable.createSchemaReference(
568: (GlobalType) target, GlobalType.class));
569: } else if (target instanceof Message) {
570: variable.setMessageType(variable.createWSDLReference(
571: (Message) target, Message.class));
572: }
573: }
574:
575: private void rename(Catch _catch, Named target) throws IOException {
576: if (target instanceof Fault) {
577: renameFaultNameReference(_catch, target);
578: } else if (target instanceof GlobalElement) {
579: _catch.setFaultElement(_catch.createSchemaReference(
580: (GlobalElement) target, GlobalElement.class));
581: } else if (target instanceof Message) {
582: _catch.setFaultMessageType(_catch.createWSDLReference(
583: (Message) target, Message.class));
584: }
585: }
586:
587: private void rename(Reply reply, Named target) throws IOException {
588: if (target instanceof Fault) {
589: renameFaultNameReference(reply, target);
590: }
591: }
592:
593: private void rename(Throw _throw, Named target) throws IOException {
594: if (target instanceof Fault) {
595: renameFaultNameReference(_throw, target);
596: }
597: }
598:
599: private void renameFaultNameReference(FaultNameReference reference,
600: Named target) throws IOException {
601: try {
602: reference.setFaultName(getQName(reference.getFaultName(),
603: target));
604: } catch (VetoException e) {
605: throw new IOException(e.getMessage());
606: }
607: }
608:
609: private QName getQName(QName qName, Named target) {
610: if (qName == null) {
611: return null;
612: }
613: return new QName(qName.getNamespaceURI(), target.getName(),
614: qName.getPrefix());
615: }
616:
617: // --------------------------------------------------------------
618: private static final class StringAttribute implements Attribute {
619:
620: public StringAttribute(String name) {
621: myName = name;
622: }
623:
624: public Class getType() {
625: return String.class;
626: }
627:
628: public String getName() {
629: return myName;
630: }
631:
632: public Class getMemberType() {
633: return null;
634: }
635:
636: private String myName;
637: }
638:
639: private XPath myXPath;
640: private String myOldName;
641: private RenameRefactoring myRequest;
642: }
|