001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: /**
021: *
022: */package org.netbeans.modules.bpel.model.impl.services;
023:
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: import org.netbeans.modules.bpel.model.api.BpelContainer;
028: import org.netbeans.modules.bpel.model.api.BpelEntity;
029: import org.netbeans.modules.bpel.model.api.CorrelationSet;
030: import org.netbeans.modules.bpel.model.api.Link;
031: import org.netbeans.modules.bpel.model.api.MessageExchange;
032: import org.netbeans.modules.bpel.model.api.NamedElement;
033: import org.netbeans.modules.bpel.model.api.PartnerLink;
034: import org.netbeans.modules.bpel.model.api.Variable;
035: import org.netbeans.modules.bpel.model.api.events.ChangeEvent;
036: import org.netbeans.modules.bpel.model.api.events.PropertyUpdateEvent;
037: import org.netbeans.modules.bpel.model.api.events.VetoException;
038: import org.netbeans.modules.bpel.model.impl.Utils;
039:
040: /**
041: * @author ads
042: * This service checks for unique name definition in the same scope.
043: */
044: public class UniqueNameCheck extends InnerEventDispatcherAdapter {
045:
046: /* (non-Javadoc)
047: * @see org.netbeans.modules.soa.model.bpel20.xam.spi.InnerEventDispatcher#isApplicable(org.netbeans.modules.soa.model.bpel20.api.events.ChangeEvent)
048: */
049: public boolean isApplicable(ChangeEvent event) {
050: if (event instanceof PropertyUpdateEvent) {
051: PropertyUpdateEvent ev = (PropertyUpdateEvent) event;
052: if (event.getParent().getBpelModel().inSync()) {
053: return false;
054: }
055: String attributeName = ev.getName();
056: if (((PropertyUpdateEvent) event).getNewValue() == null) {
057: return false;
058: }
059: return attributeName.equals(NamedElement.NAME)
060: && (event.getParent() instanceof Variable
061: || event.getParent() instanceof CorrelationSet
062: || event.getParent() instanceof PartnerLink
063: || event.getParent() instanceof MessageExchange || event
064: .getParent() instanceof Link);
065: }
066: return false;
067: }
068:
069: /* (non-Javadoc)
070: * @see org.netbeans.modules.soa.model.bpel20.xam.spi.InnerEventDispatcher#preDispatch(org.netbeans.modules.soa.model.bpel20.api.events.ChangeEvent)
071: */
072: public void preDispatch(ChangeEvent event) throws VetoException {
073: BpelContainer parent = event.getParent().getParent();
074: if (parent != null) { // parent can be null for element that is not in tree.
075:
076: for (BpelEntity child : parent.getChildren(event
077: .getParent().getElementType())) {
078: if (!(child instanceof NamedElement)
079: || child.equals(event.getParent())) {
080: continue;
081: }
082: NamedElement kid = (NamedElement) child;
083: if (((PropertyUpdateEvent) event).getNewValue().equals(
084: kid.getName())) {
085: String str = Utils.getResourceString(getError(child
086: .getElementType()), kid.getName());
087: throw new VetoException(str, event);
088: }
089: }
090: }
091: }
092:
093: private String getError(Class<? extends BpelEntity> clazz) {
094: return LazyInit.ERROR_BUILDERS.get(clazz).getError();
095: }
096:
097: private static class LazyInit {
098: private static final Map<Class<? extends BpelEntity>, ErrorMessageBuilder> ERROR_BUILDERS = new HashMap<Class<? extends BpelEntity>, ErrorMessageBuilder>();
099:
100: static {
101: ERROR_BUILDERS.put(Variable.class,
102: new VariableErrorBuilder());
103: ERROR_BUILDERS.put(CorrelationSet.class,
104: new CorrelationSetErrorBuilder());
105: ERROR_BUILDERS.put(PartnerLink.class,
106: new PartnerLinkErrorBuilder());
107: ERROR_BUILDERS.put(MessageExchange.class,
108: new MessageExchangeErrorBuilder());
109: ERROR_BUILDERS.put(Link.class, new LinkErrorBuilder());
110: }
111: }
112: }
113:
114: interface ErrorMessageBuilder {
115: String getError();
116: }
117:
118: class VariableErrorBuilder implements ErrorMessageBuilder {
119:
120: public String getError() {
121: return Utils.BAD_VARIABLE_NAME;
122: }
123: }
124:
125: class CorrelationSetErrorBuilder implements ErrorMessageBuilder {
126:
127: public String getError() {
128: return Utils.BAD_CORRELATION_SET_NAME;
129: }
130: }
131:
132: class PartnerLinkErrorBuilder implements ErrorMessageBuilder {
133:
134: public String getError() {
135: return Utils.BAD_PARTNER_LINK_NAME;
136: }
137: }
138:
139: class MessageExchangeErrorBuilder implements ErrorMessageBuilder {
140:
141: public String getError() {
142: return Utils.BAD_MESSAGE_EXCHANGE_NAME;
143: }
144: }
145:
146: class LinkErrorBuilder implements ErrorMessageBuilder {
147:
148: public String getError() {
149: return Utils.BAD_LINK_NAME;
150: }
151: }
|