1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.geronimo.naming.deployment.jsr88;
19
20 import javax.xml.namespace.QName;
21 import org.apache.geronimo.deployment.plugin.XmlBeanSupport;
22 import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
23 import org.apache.xmlbeans.XmlObject;
24 import org.apache.xmlbeans.impl.values.XmlObjectBase;
25
26 /**
27 * Represents an element in a Geronimo dployment plan that has a child
28 * of type Pattern. This handles patterns that are a member of a choice as
29 * well as singleton patterns.
30 * <p>
31 * Has 1 JavaBean Properties <br />
32 * - pattern (type Pattern) </p>
33 *
34 * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
35 */
36 public class HasPattern extends XmlBeanSupport {
37 public HasPattern() {
38 super(null);
39 }
40
41 public HasPattern(XmlObject xmlObject) {
42 super(xmlObject);
43 }
44
45 /**
46 * JavaBean getter for the Pattern property. Gets a JavaBean of type
47 * Pattern for the pattern child of this element, or null if there is no
48 * pattern child.
49 */
50 public Pattern getPattern() {
51 GerPatternType patternType = findPattern();
52 if(patternType == null) return null;
53 Pattern group = new Pattern();
54 group.setGroupId(patternType.getGroupId());
55 group.setArtifactId(patternType.getArtifactId());
56 group.setVersion(patternType.getVersion());
57 group.setModule(patternType.getModule());
58 group.setName(patternType.getName());
59 return group.empty() ? null : group;
60 }
61
62 /**
63 * JavaBean setter for the Pattern property. Calls the helper
64 * clearNonPatternFromChoice if a non-null Pattern is set.
65 */
66 public void setPattern(Pattern group) {
67 Pattern old = getPattern();
68 if(group != null) {
69 GerPatternType patternType;
70 if(old == null) {
71 patternType = (GerPatternType) ((XmlObjectBase)getXmlObject()).get_store().add_element_user(new QName("http://geronimo.apache.org/xml/ns/naming-1.2", "pattern"));
72 } else {
73 patternType = findPattern();
74 }
75 if(!isEmpty(group.getGroupId())) {
76 patternType.setGroupId(group.getGroupId());
77 } else {
78 if(patternType.isSetGroupId()) patternType.unsetGroupId();
79 }
80 if(!isEmpty(group.getArtifactId())) {
81 patternType.setArtifactId(group.getArtifactId());
82 } else {
83 if(patternType.isSetArtifactId()) patternType.unsetArtifactId();
84 }
85 if(!isEmpty(group.getModule())) {
86 patternType.setModule(group.getModule());
87 } else {
88 if(patternType.isSetModule()) patternType.unsetModule();
89 }
90 patternType.setName(group.getName());
91 if(!isEmpty(group.getVersion())) {
92 patternType.setVersion(group.getVersion());
93 } else {
94 if(patternType.isSetVersion()) patternType.unsetVersion();
95 }
96 clearNonPatternFromChoice();
97 } else {
98 if(old != null) {
99 ((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 }