Search in sources :

Example 1 with PersistenceFieldElement

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

the class ClassType method getKeyFieldNames.

/**
 * Return the list of key field names
 */
public List getKeyFieldNames() {
    if (pce != null) {
        PersistenceFieldElement[] persistentFields = pce.getFields();
        List names = new ArrayList();
        for (int i = 0; i < persistentFields.length; i++) {
            if (persistentFields[i].isKey())
                names.add(persistentFields[i].getName());
        }
        return names;
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) PersistenceFieldElement(com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with PersistenceFieldElement

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

the class JDOConcreteBean11Generator method generateLoadStoreMethods.

/**
 * Adds jdoLoadFields/jdoStoreFields for CMP1.1
 */
void generateLoadStoreMethods(PersistenceFieldElement[] fields) throws IOException {
    int i, count = ((fields != null) ? fields.length : 0);
    StringBuilder lbody = new StringBuilder();
    StringBuilder sbody = new StringBuilder(CMPTemplateFormatter.none_);
    for (i = 0; i < count; i++) {
        PersistenceFieldElement pfe = fields[i];
        if (PersistenceFieldElement.PERSISTENT == pfe.getPersistenceType()) {
            FieldInfo fieldInfo = new FieldInfo(model, nameMapper, pfe, beanName, pcname);
            if (fieldInfo.isGeneratedField) {
                // There are no relationship fields in CMP1.1 beans.
                if (fieldInfo.isKey) {
                    // This is an extra field for the unknown PK class.
                    // PK setter name is used to generate the line for ejbCreate
                    // to set the PK value in _JDOState.
                    setPKField = fieldInfo.setter;
                }
                continue;
            }
            // Add code to load non-DFG field if necessary.
            loadNonDFGField(fieldInfo);
            if (fieldInfo.isByteArray) {
                // A byte[] CMP field should have copy-in, copy-out semantics
                // via System.arraycopy.
                twoParams[0] = fieldInfo.name;
                twoParams[1] = fieldInfo.getter;
                lbody.append(CMP11TemplateFormatter.l11arrayformatter.format(twoParams));
                if (isUpdateable) {
                    threeParams[0] = fieldInfo.getter;
                    threeParams[1] = fieldInfo.name;
                    threeParams[2] = fieldInfo.setter;
                    sbody.append(CMP11TemplateFormatter.s11arrayformatter.format(threeParams));
                }
            } else if (fieldInfo.isSerializable) {
                // A special case for a Serializable CMP field (but not byte[]) -
                // it should be serialized to/from a byte[] in PC instance.
                fourParams[0] = fieldInfo.name;
                fourParams[1] = fieldInfo.getter;
                fourParams[2] = fieldInfo.type;
                fourParams[3] = concreteImplName;
                lbody.append(CMP11TemplateFormatter.l11Serializableformatter.format(fourParams));
                if (isUpdateable) {
                    fourParams[0] = fieldInfo.getter;
                    fourParams[1] = fieldInfo.name;
                    fourParams[2] = fieldInfo.setter;
                    fourParams[3] = concreteImplName;
                    sbody.append(CMP11TemplateFormatter.s11Serializableformatter.format(fourParams));
                }
            } else if (fieldInfo.requireCloneOnGetAndSet) {
                threeParams[0] = fieldInfo.getter;
                threeParams[1] = fieldInfo.type;
                threeParams[2] = fieldInfo.name;
                lbody.append(CMP11TemplateFormatter.l11copyformatter.format(threeParams));
                if (isUpdateable) {
                    fourParams[0] = fieldInfo.getter;
                    fourParams[1] = fieldInfo.name;
                    fourParams[2] = fieldInfo.setter;
                    fourParams[3] = fieldInfo.type;
                    if (!pfe.isKey()) {
                        sbody.append(CMP11TemplateFormatter.s11copyformatter.format(fourParams));
                    } else {
                        twoParams[0] = concreteImplName;
                        twoParams[1] = fieldInfo.name;
                        sbody.append(CMP11TemplateFormatter.assertpks11formatter.format(twoParams)).append(CMP11TemplateFormatter.pkcopy11formatter.format(fourParams));
                    }
                }
            } else {
                twoParams[0] = fieldInfo.name;
                twoParams[1] = fieldInfo.getter;
                lbody.append(CMP11TemplateFormatter.l11formatter.format(twoParams));
                if (isUpdateable) {
                    threeParams[0] = fieldInfo.getter;
                    threeParams[1] = fieldInfo.name;
                    threeParams[2] = fieldInfo.setter;
                    if (!pfe.isKey()) {
                        sbody.append(CMP11TemplateFormatter.s11formatter.format(threeParams));
                    } else {
                        if (!fieldInfo.isPrimitive) {
                            twoParams[0] = concreteImplName;
                            twoParams[1] = fieldInfo.name;
                            sbody.append(CMP11TemplateFormatter.assertpks11formatter.format(twoParams));
                        }
                        sbody.append(requireTrimOnSet(fieldInfo.type) ? CMP11TemplateFormatter.pkstring11formatter.format(threeParams) : CMP11TemplateFormatter.pks11formatter.format(threeParams));
                    }
                }
            }
        }
    }
    // Add jdoLoadFields
    CMPTemplateFormatter.addGenericMethod(CMP11TemplateFormatter.loadFields1_1_, CMP11TemplateFormatter.getBodyAsStrings(lbody.toString()), concreteImplWriter);
    // Add jdoStoreFields
    CMPTemplateFormatter.addGenericMethod(CMP11TemplateFormatter.storeFields1_1_, CMP11TemplateFormatter.getBodyAsStrings(sbody.toString()), concreteImplWriter);
}
Also used : PersistenceFieldElement(com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement)

Example 3 with PersistenceFieldElement

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

the class JDOConcreteBeanGenerator method generate.

/**
 * Generate all required classes for this CMP bean.
 * @param methodHelper the AbstractMethodHelper instance that contains
 * all categorized methods and some other convenience methods for this bean.
 * @param beanName the ejb-name of this bean.
 * @param appName the name of the application that contains this bean.
 * @param srcout that path to the generated source files.
 * @param classout that path to the compiled class files.
 * @return a Collection of generated source files.
 */
Collection<File> generate(AbstractMethodHelper methodHelper, String beanName, String appName, File srcout, File classout) throws IOException {
    Collection<File> files = new ArrayList<File>();
    this.beanName = beanName;
    this.appName = appName;
    this.abstractBean = nameMapper.getAbstractBeanClassForEjbName(beanName);
    String pkgName = CMPTemplateFormatter.getPackageName(abstractBean);
    concreteImplName = nameMapper.getConcreteBeanClassForEjbName(beanName);
    String shortCmpName = CMPTemplateFormatter.getShortClassName(concreteImplName);
    pcname = nameMapper.getPersistenceClassForEjbName(beanName);
    pcnameParam[0] = pcname;
    PersistenceClassElement pcClassElement = model.getPersistenceClass(pcname);
    pkClass = nameMapper.getKeyClassForEjbName(beanName).replace('$', '.');
    pkClassParam[0] = pkClass;
    PersistenceFieldElement[] allFields = pcClassElement.getFields();
    String prefix = srcout.getPath() + File.separator + concreteImplName.replace('.', File.separatorChar);
    String cmp_file_name = prefix + CMPTemplateFormatter.javaExtension_;
    String hlp_file_name = prefix + CMPTemplateFormatter.Helper_ + CMPTemplateFormatter.javaExtension_;
    hasLocalInterface = (nameMapper.getLocalInterfaceForEjbName(beanName) != null);
    hasRemoteInterface = (nameMapper.getRemoteInterfaceForEjbName(beanName) != null);
    if (logger.isLoggable(Logger.FINE)) {
        logger.fine(// NOI18N
        "allFields: " + ((allFields != null) ? allFields.length : 0));
        // NOI18N
        logger.fine("cmp_file_name: " + cmp_file_name);
        // NOI18N
        logger.fine("hlp_file_name: " + hlp_file_name);
        // NOI18N
        logger.fine("cmp_name: " + concreteImplName);
        // NOI18N
        logger.fine("pkClass: " + pkClass);
        // NOI18N
        logger.fine("PCname: " + pcname);
    }
    File cmp_file = new File(cmp_file_name);
    JavaFileWriter concreteImplFileWriter = new IOJavaFileWriter(cmp_file);
    concreteImplWriter = new IOJavaClassWriter();
    File hlp_file = new File(hlp_file_name);
    JavaFileWriter helperFileWriter = new IOJavaFileWriter(hlp_file);
    jdoHelperWriter = new IOJavaClassWriter();
    // Add package statement to both classes.
    if (pkgName != null && pkgName.length() > 0) {
        concreteImplFileWriter.setPackage(pkgName, null);
        helperFileWriter.setPackage(pkgName, null);
    }
    // Add imports statements to both classes.
    addImportStatements(concreteImplFileWriter, helperFileWriter);
    // Generate class name for the concrete impl.
    oneParam[0] = CMPTemplateFormatter.cmpImplCommentsTemplate;
    concreteImplWriter.setClassDeclaration(Modifier.PUBLIC, shortCmpName, oneParam);
    // Add interfaces to the class declarations.
    addInterfaces();
    concreteImplWriter.setSuperclass(abstractBean);
    // Add no-arg constructor.
    concreteImplWriter.addConstructor(shortCmpName, Modifier.PUBLIC, null, null, null, CMPTemplateFormatter.super_, null);
    // Add helper class.
    helperName = shortCmpName + CMPTemplateFormatter.Helper_;
    oneParam[0] = shortCmpName;
    jdoHelperWriter.setClassDeclaration(Modifier.PUBLIC, helperName, CMPTemplateFormatter.getBodyAsStrings(CMPTemplateFormatter.hcomformatter.format(oneParam)));
    setHelperSuperclass();
    // Print internal variables.
    generateFields();
    // Generate type specific methods.
    generateTypeSpecificMethods(allFields, methodHelper);
    // Add finders for all types and selectors for CMP2.0 only.
    generateFinders(methodHelper);
    // Add ejbCreate<XXX> methods.
    generateCreateMethods(methodHelper.getCreateMethods());
    // Add other required methods.
    generateKnownMethods(methodHelper);
    // Add helper methods for the helper class.
    generateHelperClassMethods();
    // Add conversion methods to the helper class.
    generateConversions();
    // Add ObjectId/PrimaryKey conversion methods.
    generatePKObjectIdConversion(getKeyFields(allFields));
    // Print end of classes.
    concreteImplFileWriter.addClass(concreteImplWriter);
    concreteImplFileWriter.save();
    helperFileWriter.addClass(jdoHelperWriter);
    helperFileWriter.save();
    files.add(cmp_file);
    files.add(hlp_file);
    return files;
}
Also used : IOJavaFileWriter(com.sun.jdo.spi.persistence.utility.generator.io.IOJavaFileWriter) JavaFileWriter(com.sun.jdo.spi.persistence.utility.generator.JavaFileWriter) IOJavaFileWriter(com.sun.jdo.spi.persistence.utility.generator.io.IOJavaFileWriter) IOJavaClassWriter(com.sun.jdo.spi.persistence.utility.generator.io.IOJavaClassWriter) PersistenceFieldElement(com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement) PersistenceClassElement(com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement) File(java.io.File)

Example 4 with PersistenceFieldElement

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

the class ModelValidator method getFieldsValidationList.

/**
 * Computes and returns a collection of ValidationComponents
 * representing the field and relationship tests to be performed.
 * @return a collection of ValidationComponents representing the
 * field and relationship tests to be performed.
 */
private Collection getFieldsValidationList() {
    ArrayList list = new ArrayList();
    Model model = getModel();
    String className = getClassName();
    PersistenceClassElement persistenceClass = getPersistenceClass(className);
    if (persistenceClass != null) {
        PersistenceFieldElement[] fields = persistenceClass.getFields();
        int i, count = ((fields != null) ? fields.length : 0);
        Iterator iterator = getMappingClass(className).getFields().iterator();
        for (i = 0; i < count; i++) {
            PersistenceFieldElement field = fields[i];
            list.add(createFieldExistenceComponent(field));
            // only want to add the others if the field exists
            if (model.hasField(className, field.getName())) {
                list.add(createFieldPersistenceComponent(field));
                list.add(createFieldPersistenceTypeComponent(field));
                list.add(createFieldConsistencyComponent(field));
                if (isLegalRelationship(field)) {
                    RelationshipElement rel = (RelationshipElement) field;
                    /* user modifiable collection class not yet supported
						list.add(createCollectionClassComponent(rel));*/
                    list.add(createElementClassComponent(rel));
                    list.add(createRelatedClassMatchesComponent(rel));
                }
            }
        }
        while (iterator.hasNext()) {
            MappingFieldElement field = (MappingFieldElement) iterator.next();
            String fieldName = field.getName();
            // only check this if it is not in the jdo model
            if (persistenceClass.getField(fieldName) == null) {
                list.add(createFieldExistenceComponent(field));
                // only want to add the others if the field exists
                if (model.hasField(className, fieldName))
                    list.add(createFieldConsistencyComponent(field));
            }
            if (!isRelationship(field))
                list.add(createColumnOverlapComponent(field));
            // preliminary fix for CR6239630
            if (// NOI18N
            Boolean.getBoolean("AllowManagedFieldsInDefaultFetchGroup")) {
            // Do nothing - AllowManagedFieldsInDefaultFetchGroup:
            // disabled single model validation test;
            // may use checked read/write access to managed fields
            } else {
                list.add(createFieldDefaultFetchGroupComponent(field));
            }
        }
    }
    return list;
}
Also used : Model(com.sun.jdo.api.persistence.model.Model) RelationshipElement(com.sun.jdo.api.persistence.model.jdo.RelationshipElement) PersistenceFieldElement(com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement) PersistenceClassElement(com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement)

Example 5 with PersistenceFieldElement

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

the class EJBMetaDataModelImpl method isPrimaryKeyField.

public boolean isPrimaryKeyField(String classPath, String fieldName) throws JDOMetaDataUserException, JDOMetaDataFatalError {
    final String className = pathToName(classPath);
    final PersistenceFieldElement pfe = model.getPersistenceField(className, fieldName);
    if (pfe != null) {
        return pfe.isKey();
    } else {
        return false;
    }
}
Also used : PersistenceFieldElement(com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement)

Aggregations

PersistenceFieldElement (com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement)20 PersistenceClassElement (com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement)9 RelationshipElement (com.sun.jdo.api.persistence.model.jdo.RelationshipElement)6 Model (com.sun.jdo.api.persistence.model.Model)2 MappingClassElement (com.sun.jdo.api.persistence.model.mapping.MappingClassElement)2 MappingFieldElement (com.sun.jdo.api.persistence.model.mapping.MappingFieldElement)2 MappingRelationshipElement (com.sun.jdo.api.persistence.model.mapping.MappingRelationshipElement)2 NameMapper (com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MappingTableElement (com.sun.jdo.api.persistence.model.mapping.MappingTableElement)1 DatabaseGenerator (com.sun.jdo.spi.persistence.generator.database.DatabaseGenerator)1 JavaFileWriter (com.sun.jdo.spi.persistence.utility.generator.JavaFileWriter)1 IOJavaClassWriter (com.sun.jdo.spi.persistence.utility.generator.io.IOJavaClassWriter)1 IOJavaFileWriter (com.sun.jdo.spi.persistence.utility.generator.io.IOJavaFileWriter)1 File (java.io.File)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