AbstractNamespaceImpl.java :  » UML » MetaBoss » com » metaboss » sdlctools » models » impl » metabossmodel » datadictionarymodel » Java Open Source

Java Open Source » UML » MetaBoss 
MetaBoss » com » metaboss » sdlctools » models » impl » metabossmodel » datadictionarymodel » AbstractNamespaceImpl.java
// 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.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.netbeans.mdr.storagemodel.StorableObject;

import com.metaboss.sdlctools.models.impl.metabossmodel.ModelElementImpl;
import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.AbstractNamespace;
import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.DataType;
import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.Namespace;
import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.Structure;
import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.StructureField;
import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.TypeTemplate;
import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Message;

public abstract class AbstractNamespaceImpl extends ModelElementImpl implements AbstractNamespace
{
  // Required constructor
  protected AbstractNamespaceImpl(StorableObject storable)
  {
    super(storable);
  }

  // Returns array of all data types
  public Collection getCombinedDataTypes()
  {
    List lDataTypes = new ArrayList();
    lDataTypes.addAll(getDataTypes());
    Collection lNamespaces = getSubNamespaces();
    if (!lNamespaces.isEmpty())
    {
      for (Iterator lNamespacesIterator = lNamespaces.iterator(); lNamespacesIterator.hasNext();)
      {
        Namespace lNamespace = (Namespace)lNamespacesIterator.next();
        lDataTypes.addAll(lNamespace.getCombinedDataTypes());
      }
    }
    return Collections.unmodifiableCollection(lDataTypes);
  }

  // Returns array of all type templates
  public Collection getCombinedTypeTemplates()
  {
    List lTypeTemplates = new ArrayList();
    lTypeTemplates.addAll(getTypeTemplates());
    Collection lNamespaces = getSubNamespaces();
    if (!lNamespaces.isEmpty())
    {
      for (Iterator lNamespacesIterator = lNamespaces.iterator(); lNamespacesIterator.hasNext();)
      {
        Namespace lNamespace = (Namespace)lNamespacesIterator.next();
        lTypeTemplates.addAll(lNamespace.getCombinedTypeTemplates());
      }
    }
    return Collections.unmodifiableCollection(lTypeTemplates);
  }
  
  // Returns array of all sub namespaces
  public Collection getCombinedSubNamespaces()
  {
    List lSubNamespaces = new ArrayList();
    Collection lNamespaces = getSubNamespaces();
    if (!lNamespaces.isEmpty())
    {
      lSubNamespaces.addAll(lNamespaces);
      for (Iterator lNamespacesIterator = lNamespaces.iterator(); lNamespacesIterator.hasNext();)
      {
        Namespace lNamespace = (Namespace)lNamespacesIterator.next();
        lSubNamespaces.addAll(lNamespace.getCombinedSubNamespaces());
      }
    }
    return Collections.unmodifiableCollection(lSubNamespaces);
  }

  // Returns collection of of all Structures contained within this namespace and it's subnamespaces 
  public Collection getCombinedStructures()
  {
    List lStructures = new ArrayList();
    lStructures.addAll(getStructures());
    Collection lNamespaces = getSubNamespaces();
    if (!lNamespaces.isEmpty())
    {
      for (Iterator lNamespacesIterator = lNamespaces.iterator(); lNamespacesIterator.hasNext();)
      {
        Namespace lNamespace = (Namespace)lNamespacesIterator.next();
        lStructures.addAll(lNamespace.getCombinedStructures());
      }
    }
    return Collections.unmodifiableCollection(lStructures);
  }

  // Returns collection of of all Messages contained within this namespace and it's subnamespaces 
  public Collection getCombinedMessages()
  {
    List lMessages = new ArrayList();
    lMessages.addAll(getMessages());
    Collection lNamespaces = getSubNamespaces();
    if (!lNamespaces.isEmpty())
    {
      for (Iterator lNamespacesIterator = lNamespaces.iterator(); lNamespacesIterator.hasNext();)
      {
        Namespace lNamespace = (Namespace)lNamespacesIterator.next();
        lMessages.addAll(lNamespace.getCombinedMessages());
      }
    }
    return Collections.unmodifiableCollection(lMessages);
  }

  // Returns list of of Structures directly within this namespace in its dependency order. Any external structures are ignored
  public List getStructuresInDependencyOrder()
  {
    Collection lStructures = getStructures();
        Structure[] lReturnArray = new Structure[lStructures.size()];
        HashSet lUnprocessedStructRefs = new HashSet();
        {
          int i = 0;
          for (Iterator lStructsIterator = lStructures.iterator(); lStructsIterator.hasNext();)
          {
              Structure lStruct = (Structure)lStructsIterator.next();
              lReturnArray[i++] = lStruct;
              lUnprocessedStructRefs.add(lStruct.getRef());
          }
        }
        HashSet lProcessedStructRefs = new HashSet();
        ArrayList lProcessedStructs = new ArrayList();
        for (int i = 0; i < lReturnArray.length;)
        {
            Structure lStructure = lReturnArray[i];
            boolean lMustBeMoved = false;
            StructureField[] lFields = (StructureField[])lStructure.getFields().toArray(new StructureField[0]);
            for (int j = 0; j < lFields.length; j++)
            {
        StructureField lField = lFields[j];
                Structure lFieldStructureType = lField.getStructureType();
                if (lFieldStructureType != null && lStructures.contains(lFieldStructureType))
                {
                    if (lFieldStructureType.equals(lStructure))
                        continue; // Structure is referring to itself - already defined
                    if (lProcessedStructRefs.contains(lFieldStructureType.getRef()))
                        continue; // Structure is referring to already defined structure
                    if (!lUnprocessedStructRefs.contains(lFieldStructureType.getRef()))
                        throw new RuntimeException("Unexpected situation. Structure reference not found.");
                    lMustBeMoved = true;
                    break;
                }
            }
            if (!lMustBeMoved)
            {
                lUnprocessedStructRefs.remove(lStructure.getRef());
                lProcessedStructRefs.add(lStructure.getRef());
                i++;
                continue; // Only has already defined fields
            }
            // Shift this structure to the back and do not increment index
            System.arraycopy(lReturnArray, i + 1,lReturnArray, i, lReturnArray.length - i - 1);
            lReturnArray[lReturnArray.length - 1] = lStructure;
        }
        return Arrays.asList(lReturnArray);
  }

  // Returns path to this namespace including itself and including DataDictionary
  public List getPathWithDictionary()
  {
    List lNamespaces = new ArrayList();
    for (AbstractNamespace lNamespace = this; lNamespace != null;)
    {
      lNamespaces.add(0,lNamespace);
      if ((lNamespace instanceof Namespace) == false)
        break; // not a namespace (i.e. DataDictionary) will force break from the loop 
      lNamespace = ((Namespace)lNamespace).getNamespace();
    }
    return Collections.unmodifiableList(lNamespaces);  
  }

  // Returns path to this namespace including itself and including DataDictionary
  public List getPathWithoutDictionary()
  {
    List lNamespaces = new ArrayList();
    for (AbstractNamespace lNamespace = this; lNamespace != null;)
    {
      if ((lNamespace instanceof Namespace) == false)
        break; // not a namespace (i.e. DataDictionary) will force break from the loop 
      lNamespaces.add(0,lNamespace);
      lNamespace = ((Namespace)lNamespace).getNamespace();
    }
    return Collections.unmodifiableList(lNamespaces);  
  }

  // Returns Namespace with specified name or throws exception if none found 
  public Namespace getSubNamespace(String pSubNamespaceName)
  {
    Namespace lFoundSubNamespace = findSubNamespace(pSubNamespaceName);
    // Throw exception if nothing found
    if (lFoundSubNamespace == null)
      throw new IllegalArgumentException("Unable to locate sub Namespace named '" + pSubNamespaceName + "' in Namespace. NamespaceRef: " + getRef());
    return lFoundSubNamespace;
  }

  // Returns Namespace with specified name or null if none found
  public Namespace findSubNamespace(String pSubNamespaceName)
  {
    Collection lSubNamespaces = getSubNamespaces();
    if (!lSubNamespaces.isEmpty())
    {
      for (Iterator lSubNamespacesIterator = lSubNamespaces.iterator(); lSubNamespacesIterator.hasNext();)
      {
        Namespace lSubNamespace = (Namespace)lSubNamespacesIterator.next();
        if (lSubNamespace.getName().equals(pSubNamespaceName))
          return lSubNamespace;
      }
    }
    return null;
  }

  // Returns TypeTemplate with specified name or throws exception if none found
  public TypeTemplate getTypeTemplate(String pTypeTemplateName)
  {
    TypeTemplate lFoundTypeTemplate = findTypeTemplate(pTypeTemplateName);
    // Throw exception if nothing found
    if (lFoundTypeTemplate == null)
      throw new IllegalArgumentException("Unable to locate TypeTemplate named '" + pTypeTemplateName + "' in Namespace. NamespaceRef: " + getRef());
    return lFoundTypeTemplate;
  }

  // Returns TypeTemplate with specified name or null if none found
  public TypeTemplate findTypeTemplate(String pTypeTemplateName)
  {
    Collection lTypeTemplates = getTypeTemplates();
    if (!lTypeTemplates.isEmpty())
    {
      for (Iterator lTypeTemplatesIterator = lTypeTemplates.iterator(); lTypeTemplatesIterator.hasNext();)
      {
        TypeTemplate lTypeTemplate = (TypeTemplate)lTypeTemplatesIterator.next();
        if (lTypeTemplate.getName().equals(pTypeTemplateName))
          return lTypeTemplate;
      }
    }
    return null;
  }

  // Return DataType with specified name or throws exception if none found
  public DataType getDataType(String pDataTypeName)
  {
    DataType lFoundDataType = findDataType(pDataTypeName);
    // Throw exception if nothing found
    if (lFoundDataType == null)
      throw new IllegalArgumentException("Unable to locate DataType named '" + pDataTypeName + "' in Namespace. NamespaceRef: " + getRef());
    return lFoundDataType;
  }

  // Return DataType with specified name or null if none found
  public DataType findDataType(String pDataTypeName)
  {
    Collection lDataTypes = getDataTypes();
    if (!lDataTypes.isEmpty())
    {
      for (Iterator lDataTypesIterator = lDataTypes.iterator(); lDataTypesIterator.hasNext();)
      {
        DataType lDataType = (DataType)lDataTypesIterator.next();
        if (lDataType.getName().equals(pDataTypeName))
          return lDataType;
      }
    }
    return null;
  }

  // Return Structure with specified name or throws exception if none found
  public Structure getStructure(String pStructureName)
  {
    Structure lFoundStructure = findStructure(pStructureName);
    // Throw exception if nothing found
    if (lFoundStructure == null)
      throw new IllegalArgumentException("Unable to locate Structure named '" + pStructureName + "' in Namespace. NamespaceRef: " + getRef());
    return lFoundStructure;
  }

  // Return Structure with specified name or null if none found
  public Structure findStructure(String pStructureName)
  {
    Collection lStructures = getStructures();
    if (!lStructures.isEmpty())
    {
      for (Iterator lStructuresIterator = lStructures.iterator(); lStructuresIterator.hasNext();)
      {
        Structure lStructure = (Structure)lStructuresIterator.next();
        if (lStructure.getName().equals(pStructureName))
          return lStructure;
      }
    }
    return null;
  }
  
  // Return Message with specified name or throws exception if none found
  public Message getMessage(String pMessageName)
  {
    Message lFoundMessage = findMessage(pMessageName);
    // Throw exception if nothing found
    if (lFoundMessage == null)
      throw new IllegalArgumentException("Unable to locate Message named '" + pMessageName + "' in Namespace. NamespaceRef: " + getRef());
    return lFoundMessage;
  }

  // Return Message with specified name or null if none found
  public Message findMessage(String pMessageName)
  {
    Collection lMessages = getMessages();
    if (!lMessages.isEmpty())
    {
      for (Iterator lMessagesIterator = lMessages.iterator(); lMessagesIterator.hasNext();)
      {
        Message lMessage = (Message)lMessagesIterator.next();
        if (lMessage.getName().equals(pMessageName))
          return lMessage;
      }
    }
    return null;
  }

  // Returns list of DataTypes, Structures and Messages used in the namespace. This includes owned and referenced elements
  public Collection getCombinedTypes()
  {
    Set lCombinedTypes = new HashSet();
    // Add all datatypes from this namespace
    lCombinedTypes.addAll(getDataTypes());
    // Add all structures and datatypes used in them
    for (Iterator lStructuresIterator = getStructures().iterator();lStructuresIterator.hasNext();)
    {
      Structure lStructure = (Structure)lStructuresIterator.next();
      lCombinedTypes.add(lStructure);
      lCombinedTypes.addAll(lStructure.getCombinedTypes());
    }
    // Add all messages and datatypes used in them
    for (Iterator lMessagesIterator = getMessages().iterator();lMessagesIterator.hasNext();)
    {
      Message lMessage = (Message)lMessagesIterator.next();
      lCombinedTypes.add(lMessage);
      lCombinedTypes.addAll(lMessage.getCombinedTypes());
    }
        // Call subnamespaces
    for (Iterator lSubNamespacesIterator = getSubNamespaces().iterator(); lSubNamespacesIterator.hasNext();)
    {
      Namespace lSubNamespace = (Namespace)lSubNamespacesIterator.next();
      lCombinedTypes.addAll(lSubNamespace.getCombinedTypes());
    }
    return java.util.Collections.unmodifiableCollection(lCombinedTypes);
  }
  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.