private QName addElement2ComplexType()

in impl/src/main/java/org/apache/tuscany/sdo/helper/SchemaBuilder.java [173:263]


    private QName addElement2ComplexType(String targetNamespace, 
                                            XSDComplexTypeDefinition complexType, 
                                            Property aProperty) 
    {
        String prefix = null;
        QName elementSchemaType = null;
        try 
        {
            elementSchemaType = buildSchema(aProperty.getType());
        }
        catch ( IllegalArgumentException e )
        {
            //schema cannot be generated for this type as there exists an xsd already
            //so include that original XSD
            elementSchemaType = new QName(aProperty.getType().getURI(), 
                                            aProperty.getType().getName(),
                                            generatePrefix());
            if ( aProperty.getType().isDataType() )
            {
                typeTable.addSimpleSchemaType(aProperty.getType().getName(), elementSchemaType);
                
                XSDSimpleTypeDefinition simpleType = xsdFactory.createXSDSimpleTypeDefinition();
                simpleType.setName(aProperty.getType().getName());
                simpleType.setTargetNamespace(aProperty.getType().getURI());
                typeTable.addXSDTypeDef(elementSchemaType.getNamespaceURI(), 
                                        elementSchemaType.getLocalPart(), 
                                        simpleType);
            }
            else
            {
                typeTable.addComplexSchemaType(aProperty.getType().getURI(),
                                                aProperty.getType().getName(),
                                                elementSchemaType);
                XSDComplexTypeDefinition extComplexType = xsdFactory.createXSDComplexTypeDefinition();
                extComplexType.setName(aProperty.getType().getName());
                extComplexType.setTargetNamespace(aProperty.getType().getURI());
                typeTable.addXSDTypeDef(elementSchemaType.getNamespaceURI(), 
                                        elementSchemaType.getLocalPart(), 
                                        extComplexType);
            }
            includeExtXSD(aProperty.getType());
        }
        
        //ensure than an import is done rightaway so that the right prefixes will be used by the 
        //element whose type is set as 'this type'.  Otherwise when setting the type for the element
        //there will be a duplicate prefix (like Q1 or Q2... ) that will be created
        prefix = addImports((XSDSchema)schemaMap.get(targetNamespace), elementSchemaType);
        
        //XmlSchemaElement element = new XmlSchemaElement();
        XSDElementDeclaration element = xsdFactory.createXSDElementDeclaration();
        element.setName(aProperty.getName());
         
        XSDParticle aParticle = xsdFactory.createXSDParticle();
        aParticle.setContent(element);
        
        ((XSDModelGroup)((XSDParticle)complexType.getContent()).getContent()).
        getContents().add(aParticle);
        
        element.updateElement();

        if ( aProperty.isMany() )
        {
            aParticle.setMaxOccurs(-1);
            aParticle.setMinOccurs(0);
            
        }
        
        if ( aProperty.isContainment() )
        {
            element.setTypeDefinition(typeTable.getXSDTypeDef(elementSchemaType.getNamespaceURI(),
                                                                elementSchemaType.getLocalPart()));
        }
        else
        {
            if ( !aProperty.getType().isDataType() )
            {
                QName qName = typeTable.getSimpleSchemaTypeName("URI");
                element.setTypeDefinition(typeTable.getXSDTypeDef(qName.getNamespaceURI(),
                                            qName.getLocalPart())); 
            }
        }
        
        addAnnotations(element, aProperty);
        if ( !aProperty.isContainment() && !aProperty.getType().isDataType() )
        {
            String value = prefix + COLON + elementSchemaType.getLocalPart();
            element.getElement().setAttribute(PROPERTY_TYPE, value);
        }
        return elementSchemaType;
        
    }