示例#1
0
  /** Generates the navigation menu based on scaffolded entities. */
  protected Resource<?> generateNavigation(final String targetDir) throws IOException {
    WebResourcesFacet web = this.project.getFacet(WebResourcesFacet.class);
    HtmlTag unorderedList = new HtmlTag("ul");

    ResourceFilter filter =
        new ResourceFilter() {
          @Override
          public boolean accept(Resource<?> resource) {
            FileResource<?> file = (FileResource<?>) resource;

            if (!file.isDirectory()
                || file.getName().equals("resources")
                || file.getName().equals("WEB-INF")
                || file.getName().equals("META-INF")) {
              return false;
            }

            return true;
          }
        };

    for (Resource<?> resource : web.getWebResource(targetDir + "/").listResources(filter)) {
      HtmlOutcomeTargetLink outcomeTargetLink = new HtmlOutcomeTargetLink();
      String outcome =
          targetDir.isEmpty() || targetDir.startsWith("/") ? targetDir : "/" + targetDir;
      outcomeTargetLink.putAttribute("outcome", outcome + "/" + resource.getName() + "/search");
      outcomeTargetLink.setValue(StringUtils.uncamelCase(resource.getName()));

      HtmlTag listItem = new HtmlTag("li");
      listItem.getChildren().add(outcomeTargetLink);
      unorderedList.getChildren().add(listItem);
    }

    Writer writer = new IndentedWriter(new StringWriter(), this.navigationTemplateIndent);
    unorderedList.write(writer);

    Map<Object, Object> context = CollectionUtils.newHashMap();
    context.put("appName", StringUtils.uncamelCase(this.project.getRoot().getName()));
    context.put("navigation", writer.toString().trim());
    context.put("targetDir", targetDir);

    if (this.navigationTemplate == null) {
      loadTemplates();
    }

    try {
      return ScaffoldUtil.createOrOverwrite(
          (FileResource<?>) getTemplateStrategy().getDefaultTemplate(),
          FreemarkerTemplateProcessor.processTemplate(context, navigationTemplate));
    } finally {
      writer.close();
    }
  }
示例#2
0
  protected List<Resource<?>> generateTemplates(String targetDir) {
    List<Resource<?>> result = new ArrayList<>();

    try {
      WebResourcesFacet web = this.project.getFacet(WebResourcesFacet.class);

      result.add(
          ScaffoldUtil.createOrOverwrite(
              web.getWebResource(PAGINATOR), getClass().getResourceAsStream(SCAFFOLD_PAGINATOR)));

      result.add(generateNavigation(targetDir));
    } catch (Exception e) {
      throw new RuntimeException("Error generating default templates", e);
    }

    return result;
  }
示例#3
0
  @SuppressWarnings({"unchecked", "rawtypes"})
  private List<Resource<?>> generateFromEntity(
      String targetDir, final Resource<?> template, final JavaClassSource entity) {
    resetMetaWidgets();

    // Track the list of resources generated

    List<Resource<?>> result = new ArrayList<>();
    try {
      JavaSourceFacet java = this.project.getFacet(JavaSourceFacet.class);
      WebResourcesFacet web = this.project.getFacet(WebResourcesFacet.class);
      JPAFacet<PersistenceCommonDescriptor> jpa = this.project.getFacet(JPAFacet.class);

      loadTemplates();
      Map<Object, Object> context = CollectionUtils.newHashMap();
      context.put("entity", entity);
      String ccEntity = StringUtils.decapitalize(entity.getName());
      context.put("ccEntity", ccEntity);
      context.put("rmEntity", ccEntity + "ToDelete");
      setPrimaryKeyMetaData(context, entity);

      // Prepare qbeMetawidget
      this.qbeMetawidget.setPath(entity.getQualifiedName());
      StringWriter stringWriter = new StringWriter();
      this.qbeMetawidget.write(stringWriter, this.backingBeanTemplateQbeMetawidgetIndent);
      context.put("qbeMetawidget", stringWriter.toString().trim());

      // Prepare removeEntityMetawidget
      this.rmEntityMetawidget.setPath(entity.getQualifiedName());
      stringWriter = new StringWriter();
      this.rmEntityMetawidget.write(stringWriter, this.backingBeanTemplateRmEntityMetawidgetIndent);
      context.put("rmEntityMetawidget", stringWriter.toString().trim());

      // Prepare Java imports
      Set<String> qbeMetawidgetImports = this.qbeMetawidget.getImports();
      Set<String> rmEntityMetawidgetImports = this.rmEntityMetawidget.getImports();
      Set<String> metawidgetImports = CollectionUtils.newHashSet();
      metawidgetImports.addAll(qbeMetawidgetImports);
      metawidgetImports.addAll(rmEntityMetawidgetImports);
      metawidgetImports.remove(entity.getQualifiedName());
      context.put(
          "metawidgetImports",
          CollectionUtils.toString(metawidgetImports, ";\r\nimport ", true, false));

      // Prepare JPA Persistence Unit
      context.put("persistenceUnitName", jpa.getConfig().getOrCreatePersistenceUnit().getName());

      // Create the Backing Bean for this entity
      JavaClassSource viewBean =
          Roaster.parse(
              JavaClassSource.class,
              FreemarkerTemplateProcessor.processTemplate(context, this.backingBeanTemplate));
      viewBean.setPackage(java.getBasePackage() + "." + DEFAULT_FACES_PACKAGE);
      result.add(
          ScaffoldUtil.createOrOverwrite(java.getJavaResource(viewBean), viewBean.toString()));

      // Set new context for view generation
      context = getTemplateContext(targetDir, template);
      String beanName = StringUtils.decapitalize(viewBean.getName());
      context.put("beanName", beanName);
      context.put("ccEntity", ccEntity);
      context.put("entityName", StringUtils.uncamelCase(entity.getName()));
      setPrimaryKeyMetaData(context, entity);

      // Prepare entityMetawidget
      this.entityMetawidget.setValue(StaticFacesUtils.wrapExpression(beanName + "." + ccEntity));
      this.entityMetawidget.setPath(entity.getQualifiedName());
      this.entityMetawidget.setReadOnly(false);
      this.entityMetawidget.setStyle(null);

      // Generate create
      writeEntityMetawidget(
          context, this.createTemplateEntityMetawidgetIndent, this.createTemplateNamespaces);

      result.add(
          ScaffoldUtil.createOrOverwrite(
              web.getWebResource(targetDir + "/" + ccEntity + "/create.xhtml"),
              FreemarkerTemplateProcessor.processTemplate(context, this.createTemplate)));

      // Generate view
      this.entityMetawidget.setReadOnly(true);
      writeEntityMetawidget(
          context, this.viewTemplateEntityMetawidgetIndent, this.viewTemplateNamespaces);

      result.add(
          ScaffoldUtil.createOrOverwrite(
              web.getWebResource(targetDir + "/" + ccEntity + "/view.xhtml"),
              FreemarkerTemplateProcessor.processTemplate(context, this.viewTemplate)));

      // Generate search
      this.searchMetawidget.setValue(StaticFacesUtils.wrapExpression(beanName + ".example"));
      this.searchMetawidget.setPath(entity.getQualifiedName());
      this.beanMetawidget.setValue(StaticFacesUtils.wrapExpression(beanName + ".pageItems"));
      this.beanMetawidget.setPath(viewBean.getQualifiedName() + "/pageItems");
      writeSearchAndBeanMetawidget(
          context,
          this.searchTemplateSearchMetawidgetIndent,
          this.searchTemplateBeanMetawidgetIndent,
          this.searchTemplateNamespaces);

      result.add(
          ScaffoldUtil.createOrOverwrite(
              web.getWebResource(targetDir + "/" + ccEntity + "/search.xhtml"),
              FreemarkerTemplateProcessor.processTemplate(context, this.searchTemplate)));

      // Generate navigation
      result.add(generateNavigation(targetDir));

      // Need ViewUtils and forge.taglib.xml for forgeview:asList
      JavaClassSource viewUtils =
          Roaster.parse(
              JavaClassSource.class,
              FreemarkerTemplateProcessor.processTemplate(context, this.viewUtilsTemplate));
      viewUtils.setPackage(viewBean.getPackage());
      result.add(
          ScaffoldUtil.createOrOverwrite(java.getJavaResource(viewUtils), viewUtils.toString()));

      context.put("viewPackage", viewBean.getPackage());
      result.add(
          ScaffoldUtil.createOrOverwrite(
              web.getWebResource("WEB-INF/classes/META-INF/forge.taglib.xml"),
              FreemarkerTemplateProcessor.processTemplate(context, this.taglibTemplate)));

      createInitializers(entity);
    } catch (Exception e) {
      throw new RuntimeException("Error generating default scaffolding: " + e.getMessage(), e);
    }
    return result;
  }
示例#4
0
  @SuppressWarnings("rawtypes")
  protected List<Resource<?>> generateIndex(String targetDir, final Resource<?> template) {
    List<Resource<?>> result = new ArrayList<>();
    WebResourcesFacet web = this.project.getFacet(WebResourcesFacet.class);

    ServletFacet servlet = this.project.getFacet(ServletFacet.class);
    if (servlet instanceof ServletFacet_3_0) {
      WebAppDescriptor servletConfig = (WebAppDescriptor) servlet.getConfig();
      servletConfig.getOrCreateWelcomeFileList().welcomeFile(INDEX_HTML);
    } else if (servlet instanceof ServletFacet_3_1) {
      org.jboss.shrinkwrap.descriptor.api.webapp31.WebAppDescriptor servletConfig =
          (org.jboss.shrinkwrap.descriptor.api.webapp31.WebAppDescriptor) servlet.getConfig();
      servletConfig.getOrCreateWelcomeFileList().welcomeFile(INDEX_HTML);
    }
    loadTemplates();

    generateTemplates(targetDir);
    HashMap<Object, Object> context = getTemplateContext(targetDir, template);

    // Basic pages

    result.add(
        ScaffoldUtil.createOrOverwrite(
            web.getWebResource(targetDir + INDEX_HTML),
            FreemarkerTemplateProcessor.processTemplate(context, indexWelcomeTemplate)));

    result.add(
        ScaffoldUtil.createOrOverwrite(
            web.getWebResource(targetDir + INDEX_XHTML),
            FreemarkerTemplateProcessor.processTemplate(context, indexTemplate)));

    result.add(
        ScaffoldUtil.createOrOverwrite(
            web.getWebResource(ERROR_XHTML),
            FreemarkerTemplateProcessor.processTemplate(context, errorTemplate)));

    // Static resources

    result.add(
        ScaffoldUtil.createOrOverwrite(
            web.getWebResource(ADD_PNG), getClass().getResourceAsStream(SCAFFOLD_ADD_PNG)));

    result.add(
        ScaffoldUtil.createOrOverwrite(
            web.getWebResource(BOOTSTRAP_CSS),
            getClass().getResourceAsStream(SCAFFOLD_BOOTSTRAP_CSS)));

    result.add(
        ScaffoldUtil.createOrOverwrite(
            web.getWebResource(FALSE_PNG), getClass().getResourceAsStream(SCAFFOLD_FALSE_PNG)));

    result.add(
        ScaffoldUtil.createOrOverwrite(
            web.getWebResource(FAVICON_ICO), getClass().getResourceAsStream(SCAFFOLD_FAVICON_ICO)));

    result.add(
        ScaffoldUtil.createOrOverwrite(
            web.getWebResource(FORGE_LOGO_PNG),
            getClass().getResourceAsStream(SCAFFOLD_FORGE_LOGO_PNG)));

    result.add(
        ScaffoldUtil.createOrOverwrite(
            web.getWebResource(FORGE_STYLE_CSS),
            getClass().getResourceAsStream(SCAFFOLD_FORGE_STYLE_CSS)));

    result.add(
        ScaffoldUtil.createOrOverwrite(
            web.getWebResource(REMOVE_PNG), getClass().getResourceAsStream(SCAFFOLD_REMOVE_PNG)));

    result.add(
        ScaffoldUtil.createOrOverwrite(
            web.getWebResource(SEARCH_PNG), getClass().getResourceAsStream(SCAFFOLD_SEARCH_PNG)));

    result.add(
        ScaffoldUtil.createOrOverwrite(
            web.getWebResource(TRUE_PNG), getClass().getResourceAsStream(SCAFFOLD_TRUE_PNG)));

    result.add(
        ScaffoldUtil.createOrOverwrite(
            web.getWebResource(JBOSS_COMMUNITY_PNG),
            getClass().getResourceAsStream(SCAFFOLD_JBOSS_COMMUNITY_PNG)));

    return result;
  }