001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.sdlctools.models.impl.metabossmodel.datadictionarymodel;
016:
017: import java.util.ArrayList;
018: import java.util.Collection;
019: import java.util.Collections;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.netbeans.mdr.storagemodel.StorableObject;
024:
025: import com.metaboss.sdlctools.models.impl.metabossmodel.ModelElementImpl;
026: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.DataType;
027: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.Property;
028: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.PropertyDescriptor;
029:
030: public abstract class PropertyImpl extends ModelElementImpl implements
031: Property {
032: // Required constructor
033: protected PropertyImpl(StorableObject storable) {
034: super (storable);
035: }
036:
037: // Helper. Returns array of all subproperties (not only immedate ones)
038: public Collection getCombinedProperties() {
039: ArrayList lCombinedProperties = new ArrayList();
040: Collection lSubProperties = getSubProperties();
041: if (!lSubProperties.isEmpty()) {
042: Iterator lSubPropertiesIterator = lSubProperties.iterator();
043: while (lSubPropertiesIterator.hasNext()) {
044: Property lSubProperty = (Property) lSubPropertiesIterator
045: .next();
046: lCombinedProperties.add(lSubProperty);
047: lCombinedProperties.addAll(lSubProperty
048: .getCombinedProperties());
049: }
050: }
051: return Collections.unmodifiableCollection(lCombinedProperties);
052: }
053:
054: /** @return property key in format name(optional index[i]).name(optional index[i]).......
055: * This key is unique and good for use in property files */
056: public String getKey() {
057: StringBuffer lKeyBuffer = new StringBuffer();
058: Property lParentProperty = getParentProperty();
059: if (lParentProperty != null) {
060: // We are child property - call to get parent's key
061: lKeyBuffer.append(lParentProperty.getKey());
062: lKeyBuffer.append(".");
063: }
064: lKeyBuffer.append(getName());
065: // Calculate and append index if necessary
066: PropertyDescriptor lPropertyDescriptor = getDescriptor();
067: if (lPropertyDescriptor != null
068: && lPropertyDescriptor.isArray()) {
069: lKeyBuffer.append("[");
070: lKeyBuffer.append(getArrayIndex());
071: lKeyBuffer.append("]");
072: }
073: return lKeyBuffer.toString();
074: }
075:
076: public Integer getArrayIndex() {
077: Property lParentProperty = getParentProperty();
078: DataType lParentDataType = getDataType();
079: List lParentsPropertiesList = null;
080: if (lParentProperty != null) {
081: lParentsPropertiesList = lParentProperty.getSubProperties();
082: } else if (lParentDataType != null) {
083: lParentsPropertiesList = lParentDataType
084: .getTypetemplateProperties();
085: } else
086: throw new RuntimeException(
087: "Unexpected situation. Property must be owned by DataType or another Property.");
088: // Calculate the number of instances of the property which is governed by the same descriptor
089: // and which occurs prior to this instance
090: int lNumberOfInstancesPrior = 0;
091: PropertyDescriptor lPropertyDescriptor = getDescriptor();
092: if (lPropertyDescriptor != null
093: && lPropertyDescriptor.isArray()) {
094: for (Iterator lParentPropertiesIterator = lParentsPropertiesList
095: .iterator(); lParentPropertiesIterator.hasNext();) {
096: Property lOtherProperty = (Property) lParentPropertiesIterator
097: .next();
098: if (lOtherProperty.equals(this ))
099: break; // Hit ourselves - time to get out
100: if (lOtherProperty.getDescriptor().equals(
101: lPropertyDescriptor))
102: lNumberOfInstancesPrior++; // Hit our sibling - increase the counter
103: }
104: lNumberOfInstancesPrior++; // Array index starts with one. Non array properties have index zero
105: }
106: return new Integer(lNumberOfInstancesPrior);
107: }
108:
109: /**
110: * @param pPropertyKey
111: * @return Property with specified name or throws exception if none found
112: */
113: public Property getPropertyByKey(String pPropertyKey) {
114: Property lFoundProperty = findPropertyByKey(pPropertyKey);
115: // Throw exception if nothing found
116: if (lFoundProperty == null)
117: throw new IllegalArgumentException(
118: "Unable to locate Property with key '"
119: + pPropertyKey
120: + "' in Property. PropertyRef: " + getRef()
121: + ". PropertyKey: " + getKey());
122: return lFoundProperty;
123: }
124:
125: /** Retrieves the property with the specified key or returns null if none found
126: * @param pPropertyKey
127: * @return Property with specified name or null if none found
128: */
129: public Property findPropertyByKey(String pPropertyKey) {
130: // Disassemble the property key to its components
131: String lThisKey = null;
132: String lSubKey = null;
133: String lThisName = null;
134: int lThisArayIndex = 0;
135: {
136: int lFirstDotIndex = pPropertyKey.indexOf('.');
137: if (lFirstDotIndex > 0) {
138: lThisKey = pPropertyKey.substring(0, lFirstDotIndex);
139: lSubKey = pPropertyKey.substring(lFirstDotIndex + 1);
140: } else {
141: lThisKey = pPropertyKey;
142: lSubKey = null;
143: }
144: if (lThisKey.endsWith("]")) {
145: int lOpenBracketPos = lThisKey.indexOf('[');
146: int lCloseBracketPos = lThisKey.length() - 1;
147: lThisName = lThisKey.substring(0, lOpenBracketPos);
148: lThisArayIndex = Integer.parseInt(lThisKey.substring(
149: lOpenBracketPos + 1, lCloseBracketPos));
150: } else {
151: lThisName = lThisKey;
152: lThisArayIndex = 0;
153: }
154: }
155: // Now iterate through properties loking for a match
156: Collection lProperties = getSubProperties();
157: for (Iterator lPropertiesIterator = lProperties.iterator(); lPropertiesIterator
158: .hasNext();) {
159: Property lProperty = (Property) lPropertiesIterator.next();
160: if (lProperty.getName().equals(lThisName) == false)
161: continue; // Name mismatch
162: if ((lThisArayIndex > 0)
163: && (lProperty.getArrayIndex() == null || lProperty
164: .getArrayIndex().intValue() != lThisArayIndex))
165: continue; // position mismatch
166: // Looks like it is ours
167: if (lSubKey != null)
168: return lProperty.findPropertyByKey(lSubKey); // Look for the subkey
169: return lProperty; // Found match
170: }
171: return null; // Could not match
172: }
173:
174: // Returns all properties described by given descriptor
175: public Collection getPropertiesByDescriptor(
176: PropertyDescriptor pDescriptor) {
177: List lProperties = new ArrayList();
178: for (Iterator lPropertiesIterator = getCombinedProperties()
179: .iterator(); lPropertiesIterator.hasNext();) {
180: Property lProperty = (Property) lPropertiesIterator.next();
181: if (lProperty.getDescriptor().equals(pDescriptor))
182: lProperties.add(lProperty);
183: }
184: return Collections.unmodifiableCollection(lProperties);
185: }
186: }
|