// THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
// OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Copyright 2000-2005 Softaris Pty.Ltd. All Rights Reserved.
package com.metaboss.sdlctools.models.impl.metabossmodel.datadictionarymodel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.netbeans.mdr.storagemodel.StorableObject;
import com.metaboss.sdlctools.models.impl.metabossmodel.ModelElementImpl;
import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.DataType;
import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.Property;
import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.PropertyDescriptor;
public abstract class PropertyImpl extends ModelElementImpl implements Property
{
// Required constructor
protected PropertyImpl(StorableObject storable)
{
super(storable);
}
// Helper. Returns array of all subproperties (not only immedate ones)
public Collection getCombinedProperties()
{
ArrayList lCombinedProperties = new ArrayList();
Collection lSubProperties = getSubProperties();
if (!lSubProperties.isEmpty())
{
Iterator lSubPropertiesIterator = lSubProperties.iterator();
while(lSubPropertiesIterator.hasNext())
{
Property lSubProperty = (Property)lSubPropertiesIterator.next();
lCombinedProperties.add(lSubProperty);
lCombinedProperties.addAll(lSubProperty.getCombinedProperties());
}
}
return Collections.unmodifiableCollection(lCombinedProperties);
}
/** @return property key in format name(optional index[i]).name(optional index[i]).......
* This key is unique and good for use in property files */
public String getKey()
{
StringBuffer lKeyBuffer = new StringBuffer();
Property lParentProperty = getParentProperty();
if (lParentProperty != null)
{
// We are child property - call to get parent's key
lKeyBuffer.append(lParentProperty.getKey());
lKeyBuffer.append(".");
}
lKeyBuffer.append(getName());
// Calculate and append index if necessary
PropertyDescriptor lPropertyDescriptor = getDescriptor();
if (lPropertyDescriptor != null && lPropertyDescriptor.isArray())
{
lKeyBuffer.append("[");
lKeyBuffer.append(getArrayIndex());
lKeyBuffer.append("]");
}
return lKeyBuffer.toString();
}
public Integer getArrayIndex()
{
Property lParentProperty = getParentProperty();
DataType lParentDataType = getDataType();
List lParentsPropertiesList = null;
if (lParentProperty != null)
{
lParentsPropertiesList = lParentProperty.getSubProperties();
}
else
if (lParentDataType != null)
{
lParentsPropertiesList = lParentDataType.getTypetemplateProperties();
}
else
throw new RuntimeException("Unexpected situation. Property must be owned by DataType or another Property.");
// Calculate the number of instances of the property which is governed by the same descriptor
// and which occurs prior to this instance
int lNumberOfInstancesPrior = 0;
PropertyDescriptor lPropertyDescriptor = getDescriptor();
if (lPropertyDescriptor != null && lPropertyDescriptor.isArray())
{
for (Iterator lParentPropertiesIterator = lParentsPropertiesList.iterator(); lParentPropertiesIterator.hasNext();)
{
Property lOtherProperty = (Property)lParentPropertiesIterator.next();
if (lOtherProperty.equals(this))
break; // Hit ourselves - time to get out
if (lOtherProperty.getDescriptor().equals(lPropertyDescriptor))
lNumberOfInstancesPrior++; // Hit our sibling - increase the counter
}
lNumberOfInstancesPrior++; // Array index starts with one. Non array properties have index zero
}
return new Integer(lNumberOfInstancesPrior);
}
/**
* @param pPropertyKey
* @return Property with specified name or throws exception if none found
*/
public Property getPropertyByKey(String pPropertyKey)
{
Property lFoundProperty = findPropertyByKey(pPropertyKey);
// Throw exception if nothing found
if (lFoundProperty == null)
throw new IllegalArgumentException("Unable to locate Property with key '" + pPropertyKey + "' in Property. PropertyRef: " + getRef() + ". PropertyKey: " + getKey());
return lFoundProperty;
}
/** Retrieves the property with the specified key or returns null if none found
* @param pPropertyKey
* @return Property with specified name or null if none found
*/
public Property findPropertyByKey(String pPropertyKey)
{
// Disassemble the property key to its components
String lThisKey = null;
String lSubKey = null;
String lThisName = null;
int lThisArayIndex = 0;
{
int lFirstDotIndex = pPropertyKey.indexOf('.');
if (lFirstDotIndex > 0)
{
lThisKey = pPropertyKey.substring(0, lFirstDotIndex);
lSubKey = pPropertyKey.substring(lFirstDotIndex+1);
}
else
{
lThisKey = pPropertyKey;
lSubKey = null;
}
if (lThisKey.endsWith("]"))
{
int lOpenBracketPos = lThisKey.indexOf('[');
int lCloseBracketPos = lThisKey.length() - 1;
lThisName = lThisKey.substring(0,lOpenBracketPos);
lThisArayIndex = Integer.parseInt(lThisKey.substring(lOpenBracketPos+1,lCloseBracketPos));
}
else
{
lThisName = lThisKey;
lThisArayIndex = 0;
}
}
// Now iterate through properties loking for a match
Collection lProperties = getSubProperties();
for (Iterator lPropertiesIterator = lProperties.iterator(); lPropertiesIterator.hasNext();)
{
Property lProperty = (Property)lPropertiesIterator.next();
if (lProperty.getName().equals(lThisName) == false)
continue; // Name mismatch
if ((lThisArayIndex > 0) && (lProperty.getArrayIndex() == null || lProperty.getArrayIndex().intValue() != lThisArayIndex))
continue; // position mismatch
// Looks like it is ours
if (lSubKey != null)
return lProperty.findPropertyByKey(lSubKey); // Look for the subkey
return lProperty; // Found match
}
return null; // Could not match
}
// Returns all properties described by given descriptor
public Collection getPropertiesByDescriptor(PropertyDescriptor pDescriptor)
{
List lProperties = new ArrayList();
for (Iterator lPropertiesIterator = getCombinedProperties().iterator();lPropertiesIterator.hasNext();)
{
Property lProperty = (Property)lPropertiesIterator.next();
if (lProperty.getDescriptor().equals(pDescriptor))
lProperties.add(lProperty);
}
return Collections.unmodifiableCollection(lProperties);
}
}
|