Search in sources :

Example 11 with MappingClassElement

use of com.sun.jdo.api.persistence.model.mapping.MappingClassElement in project Payara by payara.

the class JDOCodeGenerator method generatePC.

/**
 * Generate PC class for the ConcreteImpl bean.
 * @see CMPGenerator#generate(IASEjbCMPEntityDescriptor, File, File)
 */
private Collection<File> generatePC(IASEjbCMPEntityDescriptor ejbcmp, File srcout, File classout) throws IOException {
    ArrayList<File> fileList = new ArrayList<File>();
    Main gen = new Main(ejbModel, srcout);
    String className = nameMapper.getPersistenceClassForEjbName(ejbcmp.getName());
    if (className != null) {
        // generate PC class
        // @olsen, 4653156: the enhancer-generator deals with class names
        // in JVM format, i.e., with '/' for '.' as separator
        String jvmClassName = className.replace('.', '/');
        File file = gen.generate(jvmClassName);
        fileList.add(file);
        // write mapping file
        MappingClassElement mappingClass = model.getMappingClass(className);
        BufferedOutputStream mapOut = null;
        try {
            String mapPath = className.replace('.', File.separatorChar);
            String mappingFile = mapPath + MAPPING_EXTENSION;
            mapOut = new BufferedOutputStream(new FileOutputStream(new File(classout, mappingFile)));
            // "touch" need to create the output stream first since the
            // classout directory is not in the classpath and
            // therefore the standard storeMappingClass can't be used
            model.storeMappingClass(mappingClass, mapOut);
        } finally {
            if (mapOut != null) {
                try {
                    mapOut.close();
                } catch (Exception ex) {
                    if (logger.isLoggable(Logger.FINE))
                        logger.fine(ex.getMessage());
                }
            }
        }
    }
    return fileList;
}
Also used : MappingClassElement(com.sun.jdo.api.persistence.model.mapping.MappingClassElement) Main(com.sun.jdo.api.persistence.enhancer.generator.Main) ModelException(com.sun.jdo.api.persistence.model.ModelException) EJBQLException(com.sun.jdo.spi.persistence.support.ejb.ejbqlc.EJBQLException) SQLException(java.sql.SQLException) DBException(org.netbeans.modules.dbschema.DBException) JDOUserException(com.sun.jdo.api.persistence.support.JDOUserException) Schema2BeansException(org.netbeans.modules.schema2beans.Schema2BeansException) GeneratorException(com.sun.jdo.spi.persistence.support.ejb.codegen.GeneratorException) ConversionException(com.sun.jdo.api.persistence.mapping.ejb.ConversionException)

Example 12 with MappingClassElement

use of com.sun.jdo.api.persistence.model.mapping.MappingClassElement in project Payara by payara.

the class MappingGenerator method updateMappingClasses.

/**
 * Puts mapping classes into model's cache
 * @param mappingClasses a collection of mapping classes
 */
private void updateMappingClasses(Collection mappingClasses) {
    Iterator iter = mappingClasses.iterator();
    while (iter.hasNext()) {
        MappingClassElement mapClassElt = (MappingClassElement) iter.next();
        // put it in the models' cache
        model.updateKeyForClass(mapClassElt, null);
        // keep a strong ref
        strongRefs.add(mapClassElt);
    }
}
Also used : MappingClassElement(com.sun.jdo.api.persistence.model.mapping.MappingClassElement)

Example 13 with MappingClassElement

use of com.sun.jdo.api.persistence.model.mapping.MappingClassElement in project Payara by payara.

the class MappingGenerator method generateMappingClasses.

/**
 * Create mapping classes and schema based on database vendor name.
 * @param dbName a string for database vendor name
 * @param uniqueTableNames a Boolean to determin if use unique table names
 * during database generation
 * @param userPolicy a property object holding user overrides
 * @param inputFilesPath a directory where sun-cmp-mappings.xml is located
 * @throws IOException
 * @throws Schema2BeansException
 * @throws ModelException
 * @throws DBException
 * @throws ConversionException
 */
public DatabaseGenerator.Results generateMappingClasses(String dbName, Boolean uniqueTableNames, Properties userPolicy, String inputFilesPath) throws IOException, Schema2BeansException, ModelException, DBException, ConversionException {
    // generate mapping classes and dbschema in memory
    SunCmpMappings sunCmpMappings = null;
    // sun-cmp-mappings.xml does not exist, use DatabaseGenerator
    // to generate sun-cmp-mappings.xml, *.dbschema
    List pcClasses = new ArrayList();
    sunCmpMappings = getPartialSunCmpMappings(pcClasses, (uniqueTableNames != null) ? uniqueTableNames.booleanValue() : false);
    // load real jdo model and fake mapping model in memory
    ddHelper.setEnsureValidation(false);
    // create fake schema for partial mapping
    SchemaElement fakeSchema = new SchemaElement(new SchemaElementImpl());
    fakeSchema.setName(DBIdentifier.create(FAKE_NAME));
    // add newly created fake schema to SchemaElement cache
    SchemaElement.addToCache(fakeSchema);
    // pass null as class loader in order for MappingFile to load schema
    // from cache not from disk.
    loadMappingClasses(sunCmpMappings, null);
    DatabaseGenerator.Results results = generateSchema(pcClasses, dbName, uniqueTableNames, userPolicy);
    SchemaElement schema = results.getSchema();
    Set mappingClasses = results.getMappingClasses();
    // remove fake schema from cache since the correct schema is generated.
    SchemaElement.removeFromCache(FAKE_NAME);
    // clean up old version of schema in SchemaElement cache
    // if there is one
    SchemaElement.removeFromCache(schema.getName().getName());
    // add newly created schema to SchemaElement cache
    SchemaElement.addToCache(schema);
    // update mapping classes
    updateMappingClasses(mappingClasses);
    // model before returning the result.
    if (skipGeneratedFields) {
        Iterator iter = mappingClasses.iterator();
        while (iter.hasNext()) {
            MappingClassElement mapClassElt = (MappingClassElement) iter.next();
            if (mapClassElt != null) {
                String className = mapClassElt.getName();
                String ejbName = nameMapper.getEjbNameForPersistenceClass(className);
                PersistenceClassElement pce = (PersistenceClassElement) model.getPersistenceClass(className);
                PersistenceFieldElement[] allFields = pce.getFields();
                if (allFields != null) {
                    List generatedFieldList = new ArrayList();
                    // the generated fields from the model.
                    for (int i = 0; i < allFields.length; i++) {
                        PersistenceFieldElement pfe = allFields[i];
                        if (pfe != null) {
                            String pFieldName = pfe.getName();
                            String ejbFieldName = nameMapper.getEjbFieldForPersistenceField(className, pFieldName);
                            if (nameMapper.isGeneratedField(ejbName, ejbFieldName)) {
                                generatedFieldList.add(pfe);
                            }
                        }
                    }
                    // If the field is a version field, don't remove it
                    // from the model even though it is generated because
                    // it is needed to hold the version column information.
                    Iterator iterator = generatedFieldList.iterator();
                    while (iterator.hasNext()) {
                        PersistenceFieldElement pfe = (PersistenceFieldElement) iterator.next();
                        MappingFieldElement mfe = mapClassElt.getField(pfe.getName());
                        if (mfe != null && (!mfe.isVersion())) {
                            model.removeFieldElement(pfe);
                            mapClassElt.removeField(mfe);
                        }
                    }
                }
            }
        }
    }
    return results;
}
Also used : MappingFieldElement(com.sun.jdo.api.persistence.model.mapping.MappingFieldElement) PersistenceFieldElement(com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement) MappingClassElement(com.sun.jdo.api.persistence.model.mapping.MappingClassElement) DatabaseGenerator(com.sun.jdo.spi.persistence.generator.database.DatabaseGenerator) PersistenceClassElement(com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement) SchemaElementImpl(org.netbeans.modules.dbschema.jdbcimpl.SchemaElementImpl)

Example 14 with MappingClassElement

use of com.sun.jdo.api.persistence.model.mapping.MappingClassElement in project Payara by payara.

the class ClassDesc method newInstance.

/**
 * Creates a new instance of <code>ClassDesc</code> for the given
 * <code>pcClass</code>.
 * @param pcClass The persistence capable class.
 * @return A new instance of ClassDesc.
 */
static ClassDesc newInstance(Class pcClass) {
    Model model = Model.RUNTIME;
    String className = pcClass.getName();
    ClassLoader classLoader = pcClass.getClassLoader();
    ClassDesc rc = null;
    try {
        MappingClassElement mdConfig = model.getMappingClass(className, classLoader);
        // Validate the model information for this class.
        validateModel(model, className, classLoader);
        rc = new ClassDesc(mdConfig, pcClass);
    } catch (JDOException e) {
        throw e;
    } catch (IllegalArgumentException e) {
        throw new JDOFatalUserException(I18NHelper.getMessage(messages, "core.configuration.loadfailed.class", className), // NOI18N
        e);
    } catch (Exception e) {
        throw new JDOFatalInternalException(I18NHelper.getMessage(messages, "core.configuration.loadfailed.class", className), // NOI18N
        e);
    }
    return rc;
}
Also used : Model(com.sun.jdo.api.persistence.model.Model) MappingClassElement(com.sun.jdo.api.persistence.model.mapping.MappingClassElement)

Aggregations

MappingClassElement (com.sun.jdo.api.persistence.model.mapping.MappingClassElement)14 PersistenceClassElement (com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement)3 RelationshipElement (com.sun.jdo.api.persistence.model.jdo.RelationshipElement)3 MappingRelationshipElement (com.sun.jdo.api.persistence.model.mapping.MappingRelationshipElement)3 MappingTableElement (com.sun.jdo.api.persistence.model.mapping.MappingTableElement)3 MappingFile (com.sun.jdo.api.persistence.mapping.ejb.MappingFile)2 PersistenceFieldElement (com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement)2 MappingFieldElement (com.sun.jdo.api.persistence.model.mapping.MappingFieldElement)2 DatabaseGenerator (com.sun.jdo.spi.persistence.generator.database.DatabaseGenerator)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 ResourceReferenceDescriptor (com.sun.enterprise.deployment.ResourceReferenceDescriptor)1 Main (com.sun.jdo.api.persistence.enhancer.generator.Main)1 AbstractNameMapper (com.sun.jdo.api.persistence.mapping.ejb.AbstractNameMapper)1 ConversionException (com.sun.jdo.api.persistence.mapping.ejb.ConversionException)1 SunCmpMappings (com.sun.jdo.api.persistence.mapping.ejb.beans.SunCmpMappings)1 Model (com.sun.jdo.api.persistence.model.Model)1 ModelException (com.sun.jdo.api.persistence.model.ModelException)1 MappingClassElementImpl (com.sun.jdo.api.persistence.model.mapping.impl.MappingClassElementImpl)1