Search in sources :

Example 1 with JavaFileWriter

use of com.sun.jdo.spi.persistence.utility.generator.JavaFileWriter 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)

Aggregations

PersistenceClassElement (com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement)1 PersistenceFieldElement (com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement)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