Search in sources :

Example 6 with PersistenceFieldElement

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

the class DeploymentDescriptorModel method getFields.

/**
 * Returns a list of names of all the declared field elements in the
 * class with the specified name.  If the specified className represents
 * a persistence-capable class, the list of field names from the
 * corresponding ejb is returned (even if there is a Class object
 * available for the persistence-capable).
 * @param className the fully qualified name of the class to be checked
 * @return the names of the field elements for the specified class
 */
public List getFields(final String className) {
    final EjbCMPEntityDescriptor descriptor = getCMPDescriptor(className);
    String testClass = className;
    if (// need to get names of ejb fields
    descriptor != null) {
        Iterator iterator = descriptor.getFieldDescriptors().iterator();
        List returnList = new ArrayList();
        while (iterator.hasNext()) returnList.add(((FieldDescriptor) iterator.next()).getName());
        return returnList;
    } else {
        NameMapper nameMapper = getNameMapper();
        String ejbName = nameMapper.getEjbNameForPersistenceKeyClass(className);
        switch(getPersistenceKeyClassType(className)) {
            // ejb key class
            case NameMapper.USER_DEFINED_KEY_CLASS:
                testClass = nameMapper.getKeyClassForEjbName(ejbName);
                break;
            // find the field name we need in the abstract bean
            case NameMapper.PRIMARY_KEY_FIELD:
                return Arrays.asList(new String[] { getCMPDescriptor(ejbName).getPrimaryKeyFieldDesc().getName() });
            // find the field name we need in the persistence capable
            case NameMapper.UNKNOWN_KEY_CLASS:
                String pcClassName = nameMapper.getPersistenceClassForEjbName(ejbName);
                PersistenceFieldElement[] fields = getPersistenceClass(pcClassName).getFields();
                int i, count = ((fields != null) ? fields.length : 0);
                for (i = 0; i < count; i++) {
                    PersistenceFieldElement pfe = fields[i];
                    if (pfe.isKey())
                        return Arrays.asList(new String[] { pfe.getName() });
                }
                break;
        }
    }
    return super.getFields(testClass);
}
Also used : Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) NameMapper(com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper) ArrayList(java.util.ArrayList) List(java.util.List) PersistenceFieldElement(com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement) EjbCMPEntityDescriptor(org.glassfish.ejb.deployment.descriptor.EjbCMPEntityDescriptor) FieldDescriptor(org.glassfish.ejb.deployment.descriptor.FieldDescriptor)

Example 7 with PersistenceFieldElement

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

the class ClassDesc method initializeFields.

/**
 * This method maps all the visible fields. It has the side-effect of computing
 * the value for maxVisibleFields.
 */
private void initializeFields() {
    ArrayList concurrencyGroups = new ArrayList();
    persistentFields = pcElement.getFields();
    for (int i = 0; i < persistentFields.length; i++) {
        PersistenceFieldElement pcf = persistentFields[i];
        MappingFieldElementImpl mdf = (MappingFieldElementImpl) mdConfig.getField(pcf.getName());
        if (mdf == null) {
            throw new JDOFatalUserException(I18NHelper.getMessage(messages, // NOI18N
            "core.configuration.fieldnotmapped", pcf.getName(), pcElement.getName()));
        }
        FieldDesc f;
        if (!(mdf instanceof MappingRelationshipElement)) {
            f = createLocalField(mdf);
        } else {
            f = createForeignField((RelationshipElement) pcf, (MappingRelationshipElementImpl) mdf);
        }
        initializeFetchAndConcurrencyGroup(f, pcf, mdf, concurrencyGroups);
        if (mdf.isReadOnly()) {
            f.sqlProperties |= FieldDesc.PROP_READ_ONLY;
        }
        try {
            f.setupDesc(pcClass, pcf.getName());
        } catch (JDOException e) {
            throw e;
        } catch (Exception e) {
            throw new JDOFatalInternalException(I18NHelper.getMessage(messages, // NOI18N
            "core.configuration.loadfailed.field", pcf.getName(), pcElement.getName()), e);
        }
        f.absoluteID = pcf.getFieldNumber();
        addField(f);
        if (logger.isLoggable(Logger.FINEST)) {
            Object[] items = new Object[] { f.getName(), new Integer(f.absoluteID) };
            // NOI18N
            logger.finest("sqlstore.model.classdesc.fieldinfo", items);
        }
    }
    this.maxVisibleFields = fields.size();
}
Also used : PersistenceFieldElement(com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement) MappingRelationshipElement(com.sun.jdo.api.persistence.model.mapping.MappingRelationshipElement) RelationshipElement(com.sun.jdo.api.persistence.model.jdo.RelationshipElement) MappingRelationshipElement(com.sun.jdo.api.persistence.model.mapping.MappingRelationshipElement)

Example 8 with PersistenceFieldElement

use of com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement 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 9 with PersistenceFieldElement

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

the class JDOMetaDataModelImpl method isDefaultFetchGroupField.

/**
 * Tests whether a field of a class is known to be part of the
 * Default Fetch Group. Please note that for a relationship field, this
 * method always returns false.
 */
public boolean isDefaultFetchGroupField(String classPath, String fieldName) throws JDOMetaDataUserException, JDOMetaDataFatalError {
    final String className = pathToName(classPath);
    boolean isdfgField = model.isDefaultFetchGroup(className, fieldName);
    if (isdfgField) {
        final PersistenceFieldElement pfe = model.getPersistenceField(className, fieldName);
        if (pfe instanceof RelationshipElement) {
            // This is a relationship field. Flag it as not belonging
            // to dfg.
            // Relationship fields are always flaged as not belonging to dfg
            // This assures that access to a relationship fields is always
            // mediated.
            // Please see call to this method from following for more details.
            // 1. EJBMetaDataModelImpl#getFieldFlags()
            // 2. MethodAnnotater#notePutFieldAnnotation()
            // 3. MethodAnnotater#noteGetFieldAnnotation()
            isdfgField = false;
        }
    }
    return isdfgField;
}
Also used : RelationshipElement(com.sun.jdo.api.persistence.model.jdo.RelationshipElement) PersistenceFieldElement(com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement)

Aggregations

PersistenceFieldElement (com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement)9 RelationshipElement (com.sun.jdo.api.persistence.model.jdo.RelationshipElement)3 PersistenceClassElement (com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement)2 NameMapper (com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MappingClassElement (com.sun.jdo.api.persistence.model.mapping.MappingClassElement)1 MappingFieldElement (com.sun.jdo.api.persistence.model.mapping.MappingFieldElement)1 MappingRelationshipElement (com.sun.jdo.api.persistence.model.mapping.MappingRelationshipElement)1 DatabaseGenerator (com.sun.jdo.spi.persistence.generator.database.DatabaseGenerator)1 Iterator (java.util.Iterator)1 EjbCMPEntityDescriptor (org.glassfish.ejb.deployment.descriptor.EjbCMPEntityDescriptor)1 FieldDescriptor (org.glassfish.ejb.deployment.descriptor.FieldDescriptor)1 SchemaElementImpl (org.netbeans.modules.dbschema.jdbcimpl.SchemaElementImpl)1