001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    package org.apache.geronimo.naming.deployment.jsr88;
019    
020    import javax.xml.namespace.QName;
021    import org.apache.geronimo.deployment.plugin.XmlBeanSupport;
022    import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
023    import org.apache.xmlbeans.XmlObject;
024    import org.apache.xmlbeans.impl.values.XmlObjectBase;
025    
026    /**
027     * Represents an element in a Geronimo dployment plan that has a child
028     * of type Pattern.  This handles patterns that are a member of a choice as
029     * well as singleton patterns.
030     *                                     <p>
031     * Has 1 JavaBean Properties           <br />
032     *  - pattern (type Pattern)           </p>
033     *
034     * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
035     */
036    public class HasPattern extends XmlBeanSupport {
037        public HasPattern() {
038            super(null);
039        }
040    
041        public HasPattern(XmlObject xmlObject) {
042            super(xmlObject);
043        }
044    
045        /**
046         * JavaBean getter for the Pattern property.  Gets a JavaBean of type
047         * Pattern for the pattern child of this element, or null if there is no
048         * pattern child.
049         */
050        public Pattern getPattern() {
051            GerPatternType patternType = findPattern();
052            if(patternType == null) return null;
053            Pattern group = new Pattern();
054            group.setGroupId(patternType.getGroupId());
055            group.setArtifactId(patternType.getArtifactId());
056            group.setVersion(patternType.getVersion());
057            group.setModule(patternType.getModule());
058            group.setName(patternType.getName());
059            return group.empty() ? null : group;
060        }
061    
062        /**
063         * JavaBean setter for the Pattern property.  Calls the helper
064         * clearNonPatternFromChoice if a non-null Pattern is set.
065         */
066        public void setPattern(Pattern group) {
067            Pattern old = getPattern();
068            if(group != null) {
069                GerPatternType patternType;
070                if(old == null) {
071                    patternType = (GerPatternType) ((XmlObjectBase)getXmlObject()).get_store().add_element_user(new QName("http://geronimo.apache.org/xml/ns/naming-1.2", "pattern"));
072                } else {
073                    patternType = findPattern();
074                }
075                if(!isEmpty(group.getGroupId())) {
076                    patternType.setGroupId(group.getGroupId());
077                } else {
078                    if(patternType.isSetGroupId()) patternType.unsetGroupId();
079                }
080                if(!isEmpty(group.getArtifactId())) {
081                    patternType.setArtifactId(group.getArtifactId());
082                } else {
083                    if(patternType.isSetArtifactId()) patternType.unsetArtifactId();
084                }
085                if(!isEmpty(group.getModule())) {
086                    patternType.setModule(group.getModule());
087                } else {
088                    if(patternType.isSetModule()) patternType.unsetModule();
089                }
090                patternType.setName(group.getName());
091                if(!isEmpty(group.getVersion())) {
092                    patternType.setVersion(group.getVersion());
093                } else {
094                    if(patternType.isSetVersion()) patternType.unsetVersion();
095                }
096                clearNonPatternFromChoice();
097            } else {
098                if(old != null) {
099                    ((XmlObjectBase)getXmlObject()).get_store().remove_element(new QName("http://geronimo.apache.org/xml/ns/naming-1.2", "pattern"), 0);
100                }
101            }
102            pcs.firePropertyChange("objectNameComponents", old, group);
103        }
104    
105        /**
106         * Should be overridden to remove any non-pattern elements if this
107         * element has a pattern that's part of a choice.  If this is called, it
108         * means a non-null Pattern is in the process of being set.  This method
109         * should fire property change events on any elements it removes.
110         */
111        protected void clearNonPatternFromChoice() {}
112    
113        /**
114         * Should be called to remove any pattern child element if the pattern is
115         * part of a choice and some other element in the choice was set to a
116         * non-null value.  This will clear the pattern and send a property change
117         * event on the "pattern" property if the pattern was set.
118         */
119        protected void clearPatternFromChoice() {
120            Pattern pattern = getPattern();
121            if(pattern != null) {
122                ((XmlObjectBase)getXmlObject()).get_store().remove_element(new QName("http://geronimo.apache.org/xml/ns/naming-1.2", "pattern"), 0);
123                pcs.firePropertyChange("pattern", pattern, null);
124            }
125        }
126    
127        /**
128         * Gets the pattern child of this element, or null if there is none.
129         */
130        protected GerPatternType findPattern() {
131            XmlObject[] patterns = getXmlObject().selectChildren(new QName(GerPatternType.type.getName().getNamespaceURI(), "pattern"));
132            if(patterns.length == 0) {
133                return null;
134            }
135            return (GerPatternType)patterns[0];
136        }
137    }