@Override
 public Result execute(UIExecutionContext context) throws Exception {
   UIContext uiContext = context.getUIContext();
   Project project = (Project) uiContext.getAttributeMap().get(Project.class);
   String coordinate =
       archetypeGroupId.getValue()
           + ":"
           + archetypeArtifactId.getValue()
           + ":"
           + archetypeVersion.getValue();
   DependencyQueryBuilder depQuery = DependencyQueryBuilder.create(coordinate);
   String repository = archetypeRepository.getValue();
   if (repository != null) {
     depQuery.setRepositories(new DependencyRepository("archetype", repository));
   }
   DependencyResolver resolver =
       SimpleContainer.getServices(getClass().getClassLoader(), DependencyResolver.class).get();
   Dependency resolvedArtifact = resolver.resolveArtifact(depQuery);
   FileResource<?> artifact = resolvedArtifact.getArtifact();
   MetadataFacet metadataFacet = project.getFacet(MetadataFacet.class);
   File fileRoot = project.getRoot().reify(DirectoryResource.class).getUnderlyingResourceObject();
   ArchetypeHelper archetypeHelper =
       new ArchetypeHelper(
           artifact.getResourceInputStream(),
           fileRoot,
           metadataFacet.getProjectGroupName(),
           metadataFacet.getProjectName(),
           metadataFacet.getProjectVersion());
   JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class);
   archetypeHelper.setPackageName(facet.getBasePackage());
   archetypeHelper.execute();
   return Results.success();
 }
Пример #2
0
  @Test
  public void testCreateEnum() throws Exception {
    Project project = projectFactory.createTempProject();
    facetFactory.install(project, JavaSourceFacet.class);
    JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class);
    JavaEnumSource javaEnum =
        Roaster.parse(
            JavaEnumSource.class, "package org.jboss.forge.test; public enum CreditCardType{}");
    facet.saveJavaSource(javaEnum);

    JavaResource javaResource = facet.getJavaResource("org.jboss.forge.test.CreditCardType");
    Assert.assertNotNull(javaResource);
    Assert.assertThat(javaResource.getJavaType(), is(instanceOf(JavaEnum.class)));

    CommandController controller =
        testHarness.createCommandController(JavaAddEnumConstantCommand.class, javaResource);
    controller.initialize();
    controller.setValueFor("named", "VISA");
    Assert.assertTrue(controller.isValid());
    Assert.assertTrue(controller.canExecute());
    Result result = controller.execute();
    Assert.assertThat(result, is(not(instanceOf(Failed.class))));

    javaResource = facet.getJavaResource("org.jboss.forge.test.CreditCardType");
    JavaEnumSource enumClass = javaResource.getJavaType();
    List<EnumConstantSource> enumConstants = enumClass.getEnumConstants();
    Assert.assertEquals(1, enumConstants.size());

    Assert.assertEquals("VISA", enumConstants.get(0).getName());
  }
Пример #3
0
 public JavaResource createEmptyEnum(Project project, String enumName) throws IOException {
   JavaSourceFacet javaSourceFacet = project.getFacet(JavaSourceFacet.class);
   JavaEnumSource enumSource = Roaster.create(JavaEnumSource.class).setName(enumName);
   String packageName =
       project.getFacet(JavaSourceFacet.class).getBasePackage() + "." + DEFAULT_ENTITY_PACKAGE;
   enumSource.setPackage(packageName);
   return javaSourceFacet.saveJavaSource(enumSource);
 }
Пример #4
0
 private void setCurrentWorkingResource(UIExecutionContext context, JavaClassSource javaClass)
     throws FileNotFoundException {
   Project selectedProject = getSelectedProject(context);
   if (selectedProject != null) {
     JavaSourceFacet facet = selectedProject.getFacet(JavaSourceFacet.class);
     facet.saveJavaSource(javaClass);
   }
   context.getUIContext().setSelection(javaClass);
 }
Пример #5
0
 @Override
 public Result execute(UIExecutionContext context) throws Exception {
   String packageName = named.getValue();
   JavaSourceFacet facet = getSelectedProject(context).getFacet(JavaSourceFacet.class);
   DirectoryResource newPackage;
   if (testFolder.getValue()) {
     newPackage = facet.saveTestPackage(packageName, createPackageInfo.getValue());
   } else {
     newPackage = facet.savePackage(packageName, createPackageInfo.getValue());
   }
   context.getUIContext().setSelection(newPackage);
   return Results.success(String.format("Package '%s' created succesfully.", packageName));
 }
  @Test
  public void testCreateNewObserverMethodWithQualifiers() throws Exception {
    try (CommandController controller =
        uiTestHarness.createCommandController(CDINewBeanCommand.class, project.getRoot())) {
      controller.initialize();
      controller.setValueFor("named", "MyBean");
      controller.setValueFor("targetPackage", "org.jboss.forge.test.bean");
      assertTrue(controller.isValid());
      assertTrue(controller.canExecute());
      Result result = controller.execute();
      Assert.assertThat(result, is(not(instanceOf(Failed.class))));
    }

    try (CommandController controller =
        uiTestHarness.createCommandController(
            CDIAddObserverMethodCommand.class, project.getRoot())) {
      controller.initialize();
      controller.setValueFor("named", "observe");
      controller.setValueFor("targetClass", "org.jboss.forge.test.bean.MyBean");
      controller.setValueFor("eventType", "java.lang.String");
      controller.setValueFor(
          "qualifiers",
          Arrays.asList("javax.enterprise.inject.Default", "javax.enterprise.inject.Any"));
      assertTrue(controller.isValid());
      assertTrue(controller.canExecute());
      Result result = controller.execute();
      Assert.assertThat(result, is(not(instanceOf(Failed.class))));
    }

    JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class);
    JavaResource javaResource = facet.getJavaResource("org.jboss.forge.test.bean.MyBean");
    Assert.assertNotNull(javaResource);
    Assert.assertThat(javaResource.getJavaType(), is(instanceOf(JavaClass.class)));
    JavaClass<?> myBean = javaResource.getJavaType();
    Assert.assertEquals(1, myBean.getMethods().size());
    Assert.assertEquals(0, myBean.getInterfaces().size());
    Method<?, ?> method = myBean.getMethods().get(0);
    Assert.assertEquals("observe", method.getName());
    Assert.assertEquals(1, method.getParameters().size());
    Parameter<?> parameter = method.getParameters().get(0);
    Assert.assertTrue(parameter.hasAnnotation(Observes.class));
    Assert.assertEquals("java.lang.String", parameter.getType().getQualifiedName());
    Assert.assertEquals("event", parameter.getName());
    Assert.assertEquals(3, parameter.getAnnotations().size());
    Assert.assertTrue(parameter.hasAnnotation(Observes.class));
    Assert.assertTrue(parameter.hasAnnotation(Default.class));
    Assert.assertTrue(parameter.hasAnnotation(Any.class));
  }
  @Override
  public void initializeUI(UIBuilder builder) throws Exception {
    super.initializeUI(builder);

    UIContext context = builder.getUIContext();
    Project project = getSelectedProject(context);
    JavaSourceFacet javaSourceFacet = project.getFacet(JavaSourceFacet.class);

    final List<JavaResource> entities = persistenceOperations.getProjectEntities(project);
    forEntity.setValueChoices(entities);
    setDefaultTargetEntity(context.<FileResource<?>>getInitialSelection(), entities);

    getTargetPackage().setDescription("Repository package name");
    getTargetPackage()
        .setDefaultValue(javaSourceFacet.getBasePackage() + DEFAULT_REPOSITORY_PACKAGE);

    repositoryType.setDefaultValue(SpringBootRepositoryType.PAGING_AND_SORTING);

    builder.add(forEntity);
    builder.add(repositoryType);
  }
Пример #8
0
  @Override
  public Result execute(UIExecutionContext context) throws Exception {
    // TODO: Super implementation should have an "overwrite" flag for existing files?
    Result result = super.execute(context);
    if (!(result instanceof Failed)) {
      JavaSourceFacet javaSourceFacet = getSelectedProject(context).getFacet(JavaSourceFacet.class);
      JavaResource javaResource = context.getUIContext().getSelection();
      JavaSource<?> scope = javaResource.getJavaSource();
      if (pseudo.getValue()) {
        scope.addAnnotation(Scope.class);
      } else {
        Annotation<?> normalScope = scope.addAnnotation(NormalScope.class);
        if (passivating.getValue()) {
          normalScope.setLiteralValue("passivating", Boolean.toString(true));
        }
      }
      scope.addAnnotation(Retention.class).setEnumValue(RUNTIME);
      scope.addAnnotation(Target.class).setEnumValue(TYPE, METHOD, FIELD);
      scope.addAnnotation(Documented.class);

      javaSourceFacet.saveJavaSource(scope);
    }
    return result;
  }
Пример #9
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;
  }