Package org.jpox.metadata

Examples of org.jpox.metadata.ColumnMetaData


            }
            // New column
            else if (localName.equals("column"))
            {
                MetaData md = getStack();
                ColumnMetaData clnmd = new ColumnMetaData(md,
                                getAttr(attrs,"name"),
                                getAttr(attrs,"target"),
                                getAttr(attrs,"target-field"),
                                getAttr(attrs,"jdbc-type"),
                                getAttr(attrs,"sql-type"),
View Full Code Here


            // Create physical datastore columns where we require a FK link to the related table.
            if (createDatastoreMappings)
            {
                // Find the Column MetaData that maps to the referenced datastore field
                ColumnMetaData colmd = correspondentColumnsMapping.getColumnMetaDataByIdentifier(
                    refDatastoreMapping.getDatastoreField().getIdentifier());
                if (colmd == null)
                {
                    throw new JPOXUserException(LOCALISER.msg("041038",
                        refDatastoreMapping.getDatastoreField().getIdentifier(), toString())).setFatal();
View Full Code Here

        setIdentifier(identifier);
        if (colmd == null)
        {
            // Create a default ColumnMetaData since none provided
            columnMetaData = new ColumnMetaData(null, (String) null);
        }
        else
        {
            // TODO Consider making a copy here
            columnMetaData = colmd;
View Full Code Here

        {
            // Nothing to do since no definition of requirements
            return;
        }

        ColumnMetaData colmd = (ColumnMetaData)md;
        if (colmd.getJdbcType() != null)
        {
            columnMetaData.setJdbcType(colmd.getJdbcType());
        }
        if (colmd.getSqlType() != null)
        {
            columnMetaData.setSqlType(colmd.getSqlType());
        }
        if (colmd.getName() != null)
        {
            columnMetaData.setName(colmd.getName());
        }
        if (colmd.isAllowsNullSet())
        {
            columnMetaData.setAllowsNull(new Boolean(colmd.isAllowsNull()));
        }
        if (colmd.getLength() != null)
        {
            columnMetaData.setLength(colmd.getLength());
        }
        if (colmd.getScale() != null)
        {
            columnMetaData.setScale(colmd.getScale());
        }

        if (colmd.isAllowsNullSet() && colmd.isAllowsNull())
        {
            // MetaData requires it to be nullable
            setNullable();
        }
        if (colmd.getUnique())
        {
            // MetaData requires it to be unique
            setUnique();
        }
    }
View Full Code Here

        AbstractMemberMetaData fmd = mapping.getFieldMetaData();
        int roleForField = mapping.getRoleForField();
        DatastoreContainerObject datastoreContainer = mapping.getDatastoreContainer();

        // Take the column MetaData from the component that this mappings role relates to
        ColumnMetaData colmd = null;
        ColumnMetaDataContainer columnContainer = fmd;
        if (roleForField == JavaTypeMapping.MAPPING_COLLECTION_ELEMENT ||
            roleForField == JavaTypeMapping.MAPPING_ARRAY_ELEMENT)
        {
            columnContainer = fmd.getElementMetaData();
        }
        else if (roleForField == JavaTypeMapping.MAPPING_MAP_KEY)
        {
            columnContainer = fmd.getKeyMetaData();
        }
        else if (roleForField == JavaTypeMapping.MAPPING_MAP_VALUE)
        {
            columnContainer= fmd.getValueMetaData();
        }

        Column col;
        ColumnMetaData[] colmds;
        if (columnContainer != null && columnContainer.getColumnMetaData().length > datastoreFieldIndex)
        {
            colmd = columnContainer.getColumnMetaData()[datastoreFieldIndex];
            colmds = columnContainer.getColumnMetaData();
        }
        else
        {
            // If column specified add one (use any column name specified on field element)
            colmd = new ColumnMetaData(fmd, fmd.getColumn());
            if (columnContainer != null)
            {
                columnContainer.addColumn(colmd);
                colmds = columnContainer.getColumnMetaData();
            }
            else
            {
                colmds = new ColumnMetaData[1];
                colmds[0] = colmd;
            }
        }

        // Generate the column identifier
        IdentifierFactory idFactory = datastoreContainer.getStoreManager().getIdentifierFactory();
        DatastoreIdentifier identifier = null;
        if (colmd.getName() == null)
        {
            // No name specified, so generate the identifier from the field name
            if (roleForField == JavaTypeMapping.MAPPING_FIELD)
            {
                identifier = idFactory.newIdentifier(IdentifierFactory.COLUMN, fmd.getName());
                int i=0;
                while (datastoreContainer.hasDatastoreField(identifier))
                {
                    identifier = idFactory.newIdentifier(IdentifierFactory.COLUMN, fmd.getName() + "_" + i);
                    i++;
                }
            }
            else if (roleForField == JavaTypeMapping.MAPPING_COLLECTION_ELEMENT)
            {
                // Join table collection element
                identifier = ((RDBMSIdentifierFactory)idFactory).newJoinTableFieldIdentifier(fmd, null, null, true, FieldRole.ROLE_COLLECTION_ELEMENT);
            }
            else if (roleForField == JavaTypeMapping.MAPPING_ARRAY_ELEMENT)
            {
                // Join table array element
                identifier = ((RDBMSIdentifierFactory)idFactory).newJoinTableFieldIdentifier(fmd, null, null, true, FieldRole.ROLE_ARRAY_ELEMENT);
            }
            else if (roleForField == JavaTypeMapping.MAPPING_MAP_KEY)
            {
                // Join table map key
                identifier = ((RDBMSIdentifierFactory)idFactory).newJoinTableFieldIdentifier(fmd, null, null, true, FieldRole.ROLE_MAP_KEY);
            }
            else if (roleForField == JavaTypeMapping.MAPPING_MAP_VALUE)
            {
                // Join table map value
                identifier = ((RDBMSIdentifierFactory)idFactory).newJoinTableFieldIdentifier(fmd, null, null, true, FieldRole.ROLE_MAP_VALUE);
            }

            colmd.setName(identifier.getIdentifier());
        }
        else
        {
            // User has specified a name, so try to keep this unmodified
            identifier = idFactory.newDatastoreFieldIdentifier(colmds[datastoreFieldIndex].getName(),
                datastoreContainer.getStoreManager().getOMFContext().getTypeManager().isDefaultEmbeddedType(fmd.getType()),
                FieldRole.ROLE_CUSTOM);
        }

        // Create the column
        col = (Column) datastoreContainer.addDatastoreField(javaType, identifier, mapping, colmd);

        if (fmd.isPrimaryKey())
        {
            col.setAsPrimaryKey();
        }

        if (datastoreContainer.getStoreManager().isStrategyDatastoreAttributed(fmd.getValueStrategy(), false))
        {
            if (datastoreContainer instanceof DatastoreClass)
            {
                if ((fmd.isPrimaryKey() && ((DatastoreClass)datastoreContainer).isBaseDatastoreClass()) || !fmd.isPrimaryKey())
                {
                    // Increment any PK field if we are in base class, and increment any other field
                    col.setAutoIncrement(true);
                }
            }
        }

        if (fmd.getValueForExtension("select-function") != null)
        {
            col.setWrapperFunction(fmd.getValueForExtension("select-function"),Column.WRAPPER_FUNCTION_SELECT);
        }
        if (fmd.getValueForExtension("insert-function") != null)
        {
            col.setWrapperFunction(fmd.getValueForExtension("insert-function"),Column.WRAPPER_FUNCTION_INSERT);
        }
        if (fmd.getValueForExtension("update-function") != null)
        {
            col.setWrapperFunction(fmd.getValueForExtension("update-function"),Column.WRAPPER_FUNCTION_UPDATE);
        }

        setDatastoreFieldNullability(fmd, colmd, col);
        if (fmd.getNullValue() == NullValue.DEFAULT)
        {
            // Users default should be applied if a null is to be inserted
            col.setDefaultable();
            if (colmd.getDefaultValue() != null)
            {
                col.setDefaultValue(colmd.getDefaultValue());
            }
        }

        return col;
    }
View Full Code Here

        Column col;
        if (colmd == null)
        {
            // If column specified add one (use any column name specified on field element)
            colmd = new ColumnMetaData(fmd, fmd.getColumn());
            fmd.addColumn(colmd);
        }

        IdentifierFactory idFactory = datastoreContainer.getStoreManager().getIdentifierFactory();
        if (colmd.getName() == null)
View Full Code Here

                {
                    discriminatorMapping = new DiscriminatorMapping(dba, this, dba.getMapping(String.class, storeMgr));
                }
                else if (dismd.getStrategy() == DiscriminatorStrategy.VALUE_MAP)
                {
                    ColumnMetaData disColmd = dismd.getColumnMetaData();
                    if (disColmd != null && disColmd.getJdbcType() != null)
                    {
                        if (disColmd.getJdbcType().equalsIgnoreCase("INTEGER") ||
                                disColmd.getJdbcType().equalsIgnoreCase("BIGINT") ||
                                disColmd.getJdbcType().equalsIgnoreCase("NUMERIC"))
                        {
                            discriminatorMapping = new DiscriminatorMapping(dba, this, dba.getMapping(Long.class, storeMgr));
                        }
                        else
                        {
                            discriminatorMapping = new DiscriminatorMapping(dba, this, dba.getMapping(String.class, storeMgr));
                        }
                    }
                    else
                    {
                        discriminatorMapping = new DiscriminatorMapping(dba, this, dba.getMapping(String.class, storeMgr));
                    }
                }
            }
            else
            {
                // Create discriminator column only in top most table that needs it
                ClassTable tableWithDiscrim = getTableWithDiscriminator();
                if (tableWithDiscrim == this)
                {
                    // No superclass with a discriminator so add it in this table
                    if (dismd.getStrategy() == DiscriminatorStrategy.CLASS_NAME)
                    {
                        discriminatorMapping = new DiscriminatorMapping(dba, this,
                            dba.getMapping(String.class, storeMgr));
                    }
                    else if (dismd.getStrategy() == DiscriminatorStrategy.VALUE_MAP)
                    {
                        ColumnMetaData disColmd = dismd.getColumnMetaData();
                        if (disColmd != null && disColmd.getJdbcType() != null)
                        {
                            if (disColmd.getJdbcType().equalsIgnoreCase("INTEGER") ||
                                    disColmd.getJdbcType().equalsIgnoreCase("BIGINT") ||
                                    disColmd.getJdbcType().equalsIgnoreCase("NUMERIC"))
                            {
                                discriminatorMapping = new DiscriminatorMapping(dba, this,
                                    dba.getMapping(Long.class, storeMgr));
                            }
                            else
View Full Code Here

        if (cols != null && cols.size() > 0)
        {
            Iterator colsIter = cols.iterator();
            while (colsIter.hasNext())
            {
                ColumnMetaData colmd = (ColumnMetaData)colsIter.next();

                // Create a column with the specified name and jdbc-type
                if (colmd.getJdbcType().equals("VARCHAR") && colmd.getLength() == null)
                {
                    colmd.setLength(getStoreManager().getOMFContext().getPersistenceConfiguration().getIntProperty("org.jpox.rdbms.stringDefaultLength"));
                }
                IdentifierFactory idFactory = getStoreManager().getIdentifierFactory();
                DatastoreIdentifier colIdentifier = idFactory.newIdentifier(IdentifierFactory.COLUMN, colmd.getName());
                Column col = (Column)addDatastoreField(null, colIdentifier, null, colmd);
                TypeInfo typeInfo = dba.getTypeInfo(JDBCUtils.getJDBCTypeForName(colmd.getJdbcType()));
                col.setTypeInfo(typeInfo);

                if (unmappedColumns == null)
                {
                    unmappedColumns = new HashSet();
                }

                if (JPOXLogger.DATASTORE.isDebugEnabled())
                {
                    JPOXLogger.DATASTORE.debug(LOCALISER.msg("057011",
                        col.toString(), colmd.getJdbcType()));
                }
                unmappedColumns.add(col);
            }
        }
    }
View Full Code Here

                            }

                            if (!duplicate)
                            {
                                // Create the relation discriminator column since we dont have this discriminator
                                ColumnMetaData colmd = new ColumnMetaData(null, colName);
                                colmd.setAllowsNull(Boolean.TRUE); // Allow for elements not in any discriminated collection
                                fkDiscrimMapping = dba.getMapping(String.class, storeMgr); // Only support String discriminators currently
                                ColumnCreator.createIndexColumn(fkDiscrimMapping, storeMgr, clr, this, colmd, false);
                            }

                            if (fkDiscrimMapping != null)
                            {
                                getExternalFkDiscriminatorMappings().put(ownerFmd, fkDiscrimMapping);
                            }
                        }

                        // Add the order mapping as necessary
                        addOrderMapping(ownerFmd, orderMapping, clr);
                    }
                    else
                    {
                        // Unidirectional (element knows nothing about the owner)
                        String ownerClassName = ownerFmd.getAbstractClassMetaData().getFullClassName();
                        JavaTypeMapping fkMapping = new PersistenceCapableMapping();
                        fkMapping.initialize(dba, ownerClassName);
                        JavaTypeMapping fkDiscrimMapping = null;
                        JavaTypeMapping orderMapping = null;
                        boolean duplicate = false;

                        try
                        {
                            // Get the owner id mapping of the "1" end
                            JavaTypeMapping ownerIdMapping = storeMgr.getDatastoreClass(ownerClassName, clr).getIDMapping();
                            ColumnMetaDataContainer colmdContainer = null;
                            if (ownerFmd.hasCollection() || ownerFmd.hasArray())
                            {
                                // 1-N Collection/array
                                colmdContainer = ownerFmd.getElementMetaData();
                            }
                            else if (ownerFmd.hasMap() && ownerFmd.getKeyMetaData() != null && ownerFmd.getKeyMetaData().getMappedBy() != null)
                            {
                                // 1-N Map with key stored in the value
                                colmdContainer = ownerFmd.getValueMetaData();
                            }
                            else if (ownerFmd.hasMap() && ownerFmd.getValueMetaData() != null && ownerFmd.getValueMetaData().getMappedBy() != null)
                            {
                                // 1-N Map with value stored in the key
                                colmdContainer = ownerFmd.getKeyMetaData();
                            }
                            CorrespondentColumnsMapper correspondentColumnsMapping =
                                new CorrespondentColumnsMapper(colmdContainer, ownerIdMapping, true);
                            int countIdFields = ownerIdMapping.getNumberOfDatastoreFields();
                            for (int i=0; i<countIdFields; i++)
                            {
                                DatastoreMapping refDatastoreMapping = ownerIdMapping.getDataStoreMapping(i);
                                JavaTypeMapping mapping = dba.getMapping(refDatastoreMapping.getJavaTypeMapping().getJavaType(), storeMgr);
                                ColumnMetaData colmd = correspondentColumnsMapping.getColumnMetaDataByIdentifier(((Column)refDatastoreMapping.getDatastoreField()).getIdentifier());
                                if (colmd == null)
                                {
                                    throw new JPOXUserException(LOCALISER.msg("057035",
                                        ((Column)refDatastoreMapping.getDatastoreField()).getIdentifier(), toString())).setFatal();
                                }

                                DatastoreIdentifier identifier = null;
                                IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
                                if (colmd.getName() == null || colmd.getName().length() < 1)
                                {
                                    // No user provided name so generate one
                                    identifier = ((RDBMSIdentifierFactory)idFactory).newForeignKeyFieldIdentifier(ownerFmd,
                                        null, refDatastoreMapping.getDatastoreField().getIdentifier(),
                                        storeMgr.getOMFContext().getTypeManager().isDefaultEmbeddedType(mapping.getJavaType()),
                                        FieldRole.ROLE_OWNER);
                                }
                                else
                                {
                                    // User-defined name
                                    identifier = idFactory.newDatastoreFieldIdentifier(colmd.getName());
                                }
                                DatastoreField refColumn = addDatastoreField(mapping.getJavaType().getName(), identifier, mapping, colmd);
                                ((Column)refDatastoreMapping.getDatastoreField()).copyConfigurationTo(refColumn);

                                if (colmd == null || (colmd != null && !colmd.isAllowsNullSet()) ||
                                    (colmd != null && colmd.isAllowsNullSet() && colmd.isAllowsNull()))
                                {
                                    // User either wants it nullable, or havent specified anything, so make it nullable
                                    refColumn.setNullable();
                                }

                                fkMapping.addDataStoreMapping(getStoreManager().getMappingManager().createDatastoreMapping(mapping, storeMgr, refColumn, refDatastoreMapping.getJavaTypeMapping().getJavaType().getName()));
                                ((PersistenceCapableMapping)fkMapping).addJavaTypeMapping(mapping);
                            }
                        }
                        catch (DuplicateColumnNameException dcne)
                        {
                            // If the user hasnt specified "relation-discriminator-column" here we dont allow the sharing of columns
                            if (!ownerFmd.hasExtension("relation-discriminator-column"))
                            {
                                throw dcne;
                            }

                            // Find the FK using this column and use it instead of creating a new one since we're sharing
                            Iterator fkIter = getExternalFkMappings().entrySet().iterator();
                            fkMapping = null;
                            while (fkIter.hasNext())
                            {
                                Map.Entry entry = (Map.Entry)fkIter.next();
                                JavaTypeMapping existingFkMapping = (JavaTypeMapping)entry.getValue();
                                for (int j=0;j<existingFkMapping.getNumberOfDatastoreFields();j++)
                                {
                                    if (existingFkMapping.getDataStoreMapping(j).getDatastoreField().getIdentifier().toString().equals(dcne.getConflictingColumn().getIdentifier().toString()))
                                    {
                                        // The FK is shared (and so if it is a List we also share the index)
                                        fkMapping = existingFkMapping;
                                        fkDiscrimMapping = (JavaTypeMapping)externalFkDiscriminatorMappings.get(entry.getKey());
                                        orderMapping = (JavaTypeMapping)getExternalOrderMappings().get(entry.getKey());
                                        break;
                                    }
                                }
                            }
                            if (fkMapping == null)
                            {
                                // Should never happen since we know there is a col duplicating ours
                                throw dcne;
                            }
                            duplicate = true;
                        }

                        if (!duplicate && ownerFmd.hasExtension("relation-discriminator-column"))
                        {
                            // Create the relation discriminator column
                            String colName = ownerFmd.getValueForExtension("relation-discriminator-column");
                            if (colName == null)
                            {
                                // No column defined so use a fallback name
                                colName = "RELATION_DISCRIM";
                            }
                            ColumnMetaData colmd = new ColumnMetaData(null, colName);
                            colmd.setAllowsNull(Boolean.TRUE); // Allow for elements not in any discriminated collection
                            fkDiscrimMapping = dba.getMapping(String.class, storeMgr); // Only support String discriminators currently
                            ColumnCreator.createIndexColumn(fkDiscrimMapping, storeMgr, clr, this, colmd, false);
                        }

                        // Save the external FK
View Full Code Here

        Class indexType = Integer.class;
        JavaTypeMapping indexMapping = new IndexMapping();
        indexMapping.initialize(dba, indexType.getName());
        IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
        DatastoreIdentifier indexColumnName = null;
        ColumnMetaData colmd = null;

        // Allow for any user definition in OrderMetaData
        OrderMetaData omd = fmd.getOrderMetaData();
        if (omd != null)
        {
            colmd = (omd.getColumnMetaData() != null && omd.getColumnMetaData().length > 0 ? omd.getColumnMetaData()[0] : null);
            if (omd.getMappedBy() != null)
            {
                // User has defined ordering using the column(s) of an existing field.
                state = TABLE_STATE_INITIALIZED; // Not adding anything so just set table back to "initialised"
                JavaTypeMapping orderMapping = getFieldMapping(omd.getMappedBy());
                if (orderMapping == null)
                {
                    throw new JPOXUserException(LOCALISER.msg("057021",
                        fmd.getFullFieldName(), omd.getMappedBy()));
                }
                if (!(orderMapping instanceof IntegerMapping) && !(orderMapping instanceof LongMapping))
                {
                    throw new JPOXUserException(LOCALISER.msg("057022",
                        fmd.getFullFieldName(), omd.getMappedBy()));
                }
                return orderMapping;
            }

            String colName = null;
            if (omd.getColumnMetaData() != null && omd.getColumnMetaData().length > 0 && omd.getColumnMetaData()[0].getName() != null)
            {
                // User-defined name so create an identifier using it
                colName = omd.getColumnMetaData()[0].getName();
                indexColumnName = idFactory.newDatastoreFieldIdentifier(colName);
            }
        }
        if (indexColumnName == null)
        {
            // No name defined so generate one
            indexColumnName = ((RDBMSIdentifierFactory)idFactory).newForeignKeyFieldIdentifier(fmd, null, null,
                storeMgr.getOMFContext().getTypeManager().isDefaultEmbeddedType(indexType), FieldRole.ROLE_INDEX);
        }

        DatastoreField column = addDatastoreField(indexType.getName(), indexColumnName, indexMapping, colmd);
        if (colmd == null || (colmd != null && !colmd.isAllowsNullSet()) ||
            (colmd != null && colmd.isAllowsNullSet() && colmd.isAllowsNull()))
        {
            // User either wants it nullable, or havent specified anything, so make it nullable
            column.setNullable();
        }
View Full Code Here

TOP

Related Classes of org.jpox.metadata.ColumnMetaData

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.