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