<commit_before>/*******************************************************************************
 * * Copyright 2012 Impetus Infotech.
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 ******************************************************************************/
package com.impetus.kundera.configure;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.EmbeddableType;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.metamodel.SingularAttribute;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.impetus.kundera.PersistenceProperties;
import com.impetus.kundera.client.ClientResolver;
import com.impetus.kundera.configure.schema.CollectionColumnInfo;
import com.impetus.kundera.configure.schema.ColumnInfo;
import com.impetus.kundera.configure.schema.EmbeddedColumnInfo;
import com.impetus.kundera.configure.schema.IndexInfo;
import com.impetus.kundera.configure.schema.SchemaGenerationException;
import com.impetus.kundera.configure.schema.TableInfo;
import com.impetus.kundera.configure.schema.api.SchemaManager;
import com.impetus.kundera.loader.ClientFactory;
import com.impetus.kundera.metadata.KunderaMetadataManager;
import com.impetus.kundera.metadata.MetadataUtils;
import com.impetus.kundera.metadata.model.ApplicationMetadata;
import com.impetus.kundera.metadata.model.EntityMetadata;
import com.impetus.kundera.metadata.model.EntityMetadata.Type;
import com.impetus.kundera.metadata.model.IdDiscriptor;
import com.impetus.kundera.metadata.model.JoinTableMetadata;
import com.impetus.kundera.metadata.model.KunderaMetadata;
import com.impetus.kundera.metadata.model.MetamodelImpl;
import com.impetus.kundera.metadata.model.PersistenceUnitMetadata;
import com.impetus.kundera.metadata.model.PropertyIndex;
import com.impetus.kundera.metadata.model.Relation;
import com.impetus.kundera.metadata.model.Relation.ForeignKey;
import com.impetus.kundera.metadata.model.attributes.AbstractAttribute;
import com.impetus.kundera.metadata.processor.IndexProcessor;
import com.impetus.kundera.metadata.validator.EntityValidator;
import com.impetus.kundera.metadata.validator.EntityValidatorImpl;
import com.impetus.kundera.property.PropertyAccessorHelper;
import com.impetus.kundera.utils.KunderaCoreUtils;

/**
 * Schema configuration implementation to support ddl_schema_creation
 * functionality. e.g. kundera_ddl_auto_prepare
 * (create,create-drop,validate,update)
 * 
 * @author Kuldeep.Kumar
 * 
 */
public class SchemaConfiguration extends AbstractSchemaConfiguration implements Configuration
{
    /** The log. */
    private static Logger log = LoggerFactory.getLogger(SchemaConfiguration.class);

    /**
     * pu to schema metadata map .
     */
    private Map<String, List<TableInfo>> puToSchemaMetadata = new HashMap<String, List<TableInfo>>();

    /**
     * Constructor using persistence units as parameter.
     * 
     * @param persistenceUnits
     *            persistence units.
     */
    public SchemaConfiguration(Map externalProperties, String... persistenceUnits)
    {
        super(persistenceUnits, externalProperties);
    }

    @Override
    /**
     * configure method responsible for creating pu to schema metadata map for each entity in class path.
     * 
     */
    public void configure()
    {
        ApplicationMetadata appMetadata = KunderaMetadata.INSTANCE.getApplicationMetadata();

        EntityValidator validator = new EntityValidatorImpl(externalPropertyMap);

        // TODO, FIXME: Refactoring is required.
        for (String persistenceUnit : persistenceUnits)
        {
            log.info("Configuring schema export for : " + persistenceUnit);
            List<TableInfo> tableInfos = getSchemaInfo(persistenceUnit);

            Map<String, EntityMetadata> entityMetadataMap = getEntityMetadataCol(appMetadata, persistenceUnit);

            PersistenceUnitMetadata puMetadata = appMetadata.getPersistenceUnitMetadata(persistenceUnit);

            // Iterate each entity metadata.
            for (EntityMetadata entityMetadata : entityMetadataMap.values())
            {
                // get entity metadata(table info as well as columns)
                // if table info exists, get it from map.
                boolean found = false;
                Type type = entityMetadata.getType();
                Class idClassName = entityMetadata.getIdAttribute() != null ? entityMetadata.getIdAttribute()
                        .getJavaType() : null;

                String idName = entityMetadata.getIdAttribute() != null ? ((AbstractAttribute) entityMetadata
                        .getIdAttribute()).getJPAColumnName() : null;

                TableInfo tableInfo = new TableInfo(entityMetadata.getTableName(), type.name(), idClassName, idName);

                // check for tableInfos not empty and contains the present
                // tableInfo.
                if (!tableInfos.isEmpty() && tableInfos.contains(tableInfo))
                {
                    found = true;
                    int idx = tableInfos.indexOf(tableInfo);
                    tableInfo = tableInfos.get(idx);
                    addColumnToTableInfo(entityMetadata, type, tableInfo);
                }
                else
                {
                    addColumnToTableInfo(entityMetadata, type, tableInfo);
                }

                List<Relation> relations = entityMetadata.getRelations();

                parseRelations(persistenceUnit, tableInfos, entityMetadata, tableInfo, relations);

                if (!found)
                {
                    tableInfos.add(tableInfo);
                }
                // Add table for GeneratedValue if opted TableStrategy
                addTableGenerator(appMetadata, persistenceUnit, tableInfos, entityMetadata, idClassName, idName);

                // Validating entity against counter column family.
                validator.validateEntity(entityMetadata.getEntityClazz());
            }

            puToSchemaMetadata.put(persistenceUnit, tableInfos);

        }

        // Need to iterate, as in case of non unary relations
        for (String persistenceUnit : persistenceUnits)
        {
            PersistenceUnitMetadata puMetadata = appMetadata.getPersistenceUnitMetadata(persistenceUnit);

            Map<String, Object> externalPuMap = externalPropertyMap;
            
            //in case of polyglot.
            if(persistenceUnits.length > 1)
            {
                externalPuMap = (Map<String, Object>) externalPropertyMap.get(persistenceUnit);
            }
            if (externalPuMap != null
                    && externalPuMap.get(PersistenceProperties.KUNDERA_DDL_AUTO_PREPARE) != null
                    || puMetadata.getProperty(PersistenceProperties.KUNDERA_DDL_AUTO_PREPARE) != null)
            {
                SchemaManager schemaManager = getSchemaManagerForPu(persistenceUnit);

                if (schemaManager != null)
                {
                    schemaManager.exportSchema(persistenceUnit, puToSchemaMetadata.get(persistenceUnit));
                }
            }
        }
    }
<commit_msg>Tweaked to avoid NPE.  issue#315<commit_after>/*******************************************************************************
 * * Copyright 2012 Impetus Infotech.
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 ******************************************************************************/
package com.impetus.kundera.configure;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.EmbeddableType;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.metamodel.SingularAttribute;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.impetus.kundera.PersistenceProperties;
import com.impetus.kundera.client.ClientResolver;
import com.impetus.kundera.configure.schema.CollectionColumnInfo;
import com.impetus.kundera.configure.schema.ColumnInfo;
import com.impetus.kundera.configure.schema.EmbeddedColumnInfo;
import com.impetus.kundera.configure.schema.IndexInfo;
import com.impetus.kundera.configure.schema.SchemaGenerationException;
import com.impetus.kundera.configure.schema.TableInfo;
import com.impetus.kundera.configure.schema.api.SchemaManager;
import com.impetus.kundera.loader.ClientFactory;
import com.impetus.kundera.metadata.KunderaMetadataManager;
import com.impetus.kundera.metadata.MetadataUtils;
import com.impetus.kundera.metadata.model.ApplicationMetadata;
import com.impetus.kundera.metadata.model.EntityMetadata;
import com.impetus.kundera.metadata.model.EntityMetadata.Type;
import com.impetus.kundera.metadata.model.IdDiscriptor;
import com.impetus.kundera.metadata.model.JoinTableMetadata;
import com.impetus.kundera.metadata.model.KunderaMetadata;
import com.impetus.kundera.metadata.model.MetamodelImpl;
import com.impetus.kundera.metadata.model.PersistenceUnitMetadata;
import com.impetus.kundera.metadata.model.PropertyIndex;
import com.impetus.kundera.metadata.model.Relation;
import com.impetus.kundera.metadata.model.Relation.ForeignKey;
import com.impetus.kundera.metadata.model.attributes.AbstractAttribute;
import com.impetus.kundera.metadata.processor.IndexProcessor;
import com.impetus.kundera.metadata.validator.EntityValidator;
import com.impetus.kundera.metadata.validator.EntityValidatorImpl;
import com.impetus.kundera.property.PropertyAccessorHelper;
import com.impetus.kundera.utils.KunderaCoreUtils;

/**
 * Schema configuration implementation to support ddl_schema_creation
 * functionality. e.g. kundera_ddl_auto_prepare
 * (create,create-drop,validate,update)
 * 
 * @author Kuldeep.Kumar
 * 
 */
public class SchemaConfiguration extends AbstractSchemaConfiguration implements Configuration
{
    /** The log. */
    private static Logger log = LoggerFactory.getLogger(SchemaConfiguration.class);

    /**
     * pu to schema metadata map .
     */
    private Map<String, List<TableInfo>> puToSchemaMetadata = new HashMap<String, List<TableInfo>>();

    /**
     * Constructor using persistence units as parameter.
     * 
     * @param persistenceUnits
     *            persistence units.
     */
    public SchemaConfiguration(Map externalProperties, String... persistenceUnits)
    {
        super(persistenceUnits, externalProperties);
    }

    @Override
    /**
     * configure method responsible for creating pu to schema metadata map for each entity in class path.
     * 
     */
    public void configure()
    {
        ApplicationMetadata appMetadata = KunderaMetadata.INSTANCE.getApplicationMetadata();

        EntityValidator validator = new EntityValidatorImpl(externalPropertyMap);

        // TODO, FIXME: Refactoring is required.
        for (String persistenceUnit : persistenceUnits)
        {
            log.info("Configuring schema export for : " + persistenceUnit);
            List<TableInfo> tableInfos = getSchemaInfo(persistenceUnit);

            Map<String, EntityMetadata> entityMetadataMap = getEntityMetadataCol(appMetadata, persistenceUnit);

            PersistenceUnitMetadata puMetadata = appMetadata.getPersistenceUnitMetadata(persistenceUnit);

            // Iterate each entity metadata.
            for (EntityMetadata entityMetadata : entityMetadataMap.values())
            {
                // get entity metadata(table info as well as columns)
                // if table info exists, get it from map.
                boolean found = false;
                Type type = entityMetadata.getType();
                Class idClassName = entityMetadata.getIdAttribute() != null ? entityMetadata.getIdAttribute()
                        .getJavaType() : null;

                String idName = entityMetadata.getIdAttribute() != null ? ((AbstractAttribute) entityMetadata
                        .getIdAttribute()).getJPAColumnName() : null;

                TableInfo tableInfo = new TableInfo(entityMetadata.getTableName(), type.name(), idClassName, idName);

                // check for tableInfos not empty and contains the present
                // tableInfo.
                if (!tableInfos.isEmpty() && tableInfos.contains(tableInfo))
                {
                    found = true;
                    int idx = tableInfos.indexOf(tableInfo);
                    tableInfo = tableInfos.get(idx);
                    addColumnToTableInfo(entityMetadata, type, tableInfo);
                }
                else
                {
                    addColumnToTableInfo(entityMetadata, type, tableInfo);
                }

                List<Relation> relations = entityMetadata.getRelations();

                parseRelations(persistenceUnit, tableInfos, entityMetadata, tableInfo, relations);

                if (!found)
                {
                    tableInfos.add(tableInfo);
                }
                // Add table for GeneratedValue if opted TableStrategy
                addTableGenerator(appMetadata, persistenceUnit, tableInfos, entityMetadata, idClassName, idName);

                // Validating entity against counter column family.
                validator.validateEntity(entityMetadata.getEntityClazz());
            }

            puToSchemaMetadata.put(persistenceUnit, tableInfos);

        }

        // Need to iterate, as in case of non unary relations
        for (String persistenceUnit : persistenceUnits)
        {
            PersistenceUnitMetadata puMetadata = appMetadata.getPersistenceUnitMetadata(persistenceUnit);

            Map externalPuMap = externalPropertyMap;
            
            //in case of polyglot.
            if(persistenceUnits.length > 1 && externalPropertyMap != null)
            {
                externalPuMap = (Map) externalPropertyMap.get(persistenceUnit);
            }
            if (externalPuMap != null
                    && externalPuMap.get(PersistenceProperties.KUNDERA_DDL_AUTO_PREPARE) != null
                    || puMetadata.getProperty(PersistenceProperties.KUNDERA_DDL_AUTO_PREPARE) != null)
            {
                SchemaManager schemaManager = getSchemaManagerForPu(persistenceUnit);

                if (schemaManager != null)
                {
                    schemaManager.exportSchema(persistenceUnit, puToSchemaMetadata.get(persistenceUnit));
                }
            }
        }
    }

{'fraction_non_alphanumeric': 0.10265776546164504, 'fraction_numerical': 0.0015745056052399546, 'mean_word_length': 2.913975844219867, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5016233', 'n_tokens_mistral': 4199, 'n_tokens_neox': 4048, 'n_words': 876}