Search in sources :

Example 1 with PersistenceClassElement

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

the class JDOMetaDataModelImpl method getPersistentFields.

/**
 * Returns an array of field names of all declared, persistent fields
 * of a class.
 */
private final String[] getPersistentFields(String classPath) throws JDOMetaDataUserException, JDOMetaDataFatalError {
    final String className = pathToName(classPath);
    final PersistenceClassElement pce = model.getPersistenceClass(className);
    if (pce == null)
        return new String[] {};
    // exctract field names into result array
    final PersistenceFieldElement[] pfes = pce.getFields();
    final int nofFields = (pfes != null ? pfes.length : 0);
    final String[] names = new String[nofFields];
    for (int i = 0; i < nofFields; i++) {
        final PersistenceFieldElement pfe = pfes[i];
        names[i] = pfe.getName();
        // @olsen: debugging check
        if (false) {
            if (pfe.getPersistenceType() != PersistenceFieldElement.PERSISTENT) {
                final String msg = (// NOI18N
                "Getting persistent field names: " + // NOI18N
                "Encountered non-persistent field '" + names[i] + "' for class " + // NOI18N
                classPath);
                throw new JDOMetaDataFatalError(msg);
            // out.println(msg);
            // names[i] = null;
            }
        }
    }
    return names;
}
Also used : PersistenceFieldElement(com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement) PersistenceClassElement(com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement)

Example 2 with PersistenceClassElement

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

the class PersistenceMemberElementImpl method firePropertyChange.

/**
 * Fires property change event.  This method overrides that of
 * PersistenceElementImpl to update the PersistenceClassElementImpl's
 * modified status.
 * @param name property name
 * @param o old value
 * @param n new value
 */
protected final void firePropertyChange(String name, Object o, Object n) {
    // even though o == null and n == null will signify a change, that
    // is consistent with PropertyChangeSupport's behavior and is
    // necessary for this to work
    boolean noChange = ((o != null) && (n != null) && o.equals(n));
    PersistenceClassElement classElement = ((PersistenceMemberElement) _element).getDeclaringClass();
    super.firePropertyChange(name, o, n);
    if ((classElement != null) && !noChange)
        classElement.setModified(true);
}
Also used : PersistenceMemberElement(com.sun.jdo.api.persistence.model.jdo.PersistenceMemberElement) PersistenceClassElement(com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement)

Example 3 with PersistenceClassElement

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

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

the class PersistenceMemberElementImpl method fireVetoableChange.

/**
 * Fires vetoable change event.  This method overrides that of
 * PersistenceElementImpl to give listeners a chance to block
 * changes on the persistence class element modified status.
 * @param name property name
 * @param o old value
 * @param n new value
 * @exception PropertyVetoException when the change is vetoed by a listener
 */
protected final void fireVetoableChange(String name, Object o, Object n) throws PropertyVetoException {
    // even though o == null and n == null will signify a change, that
    // is consistent with PropertyChangeSupport's behavior and is
    // necessary for this to work
    boolean noChange = ((o != null) && (n != null) && o.equals(n));
    PersistenceClassElement classElement = ((PersistenceMemberElement) _element).getDeclaringClass();
    super.fireVetoableChange(name, o, n);
    if ((classElement != null) && !noChange) {
        ((PersistenceElementImpl) classElement.getImpl()).fireVetoableChange(PROP_MODIFIED, Boolean.FALSE, Boolean.TRUE);
    }
}
Also used : PersistenceMemberElement(com.sun.jdo.api.persistence.model.jdo.PersistenceMemberElement) PersistenceClassElement(com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement)

Aggregations

PersistenceClassElement (com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement)4 PersistenceFieldElement (com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement)2 PersistenceMemberElement (com.sun.jdo.api.persistence.model.jdo.PersistenceMemberElement)2 MappingClassElement (com.sun.jdo.api.persistence.model.mapping.MappingClassElement)1 MappingFieldElement (com.sun.jdo.api.persistence.model.mapping.MappingFieldElement)1 DatabaseGenerator (com.sun.jdo.spi.persistence.generator.database.DatabaseGenerator)1 SchemaElementImpl (org.netbeans.modules.dbschema.jdbcimpl.SchemaElementImpl)1