Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JakartaEE JSF application generator #4

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/target/
/infra/target/
/jakartaee/target/
/mvc/target/
/rest/target/
/runtime/target/
/jakarta-ee/target/
/tests/target/
/tests/target/
/jsf/target/
19 changes: 19 additions & 0 deletions jsf/nbactions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>run</actionName>
<goals>
<goal>nbm:run-ide</goal>
</goals>
</action>
<action>
<actionName>debug</actionName>
<goals>
<goal>nbm:run-ide</goal>
</goals>
<properties>
<jpda.listen>true</jpda.listen>
<netbeans.run.params.debug>-J-agentlib:jdwp=transport=dt_socket,suspend=n,server=n,address=${jpda.address}</netbeans.run.params.debug>
</properties>
</action>
</actions>
66 changes: 66 additions & 0 deletions jsf/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.jeddict</groupId>
<artifactId>jeddict-extensions</artifactId>
<version>5.4.2</version>
</parent>
<artifactId>jsf</artifactId>
<name>JSF</name>
<packaging>nbm</packaging>

<properties>
<root.dir>${basedir}/../</root.dir>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>nbm-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<moduleDependencies>
<dependency>
<id>org.netbeans.modules:org-netbeans-modules-maven-embedder</id>
<type>impl</type>
</dependency>
</moduleDependencies>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>infra</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jakarta-ee</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-projectapi</artifactId>
<version>${netbeans.version}</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-openide-loaders</artifactId>
<version>${netbeans.version}</version>
</dependency>
<dependency>
<groupId>org.netbeans.modules</groupId>
<artifactId>org-netbeans-modules-maven-embedder</artifactId>
<version>${netbeans.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
MSG_Progress_Now_Generating= Generating CDI repository :
TITLE_Maven_Project_Not_Found= Maven Project
MSG_Maven_Project_Not_Found= Maven(pom.xml) Project not found
JsfControllerPanel.invalidPackage.message= The\u00a0Package\u00a0Name\u00a0is\u00a0not\u00a0valid
JsfControllerPanel.invalidSuffix.message= The\u00a0Suffix\u00a0Name\u00a0is\u00a0not\u00a0valid
JsfControllerPanel.reservedSuffix.message= 'Service' is reserved suffix, please choose another suffix
JsfControllerPanel.invalidPrefix.message= The\u00a0Prefix\u00a0Name\u00a0is\u00a0not\u00a0valid
JsfControllerPanel.invalidAppPackage.message= The\u00a0Application Package\u00a0Name\u00a0is\u00a0not\u00a0valid

JsfControllerPanel.entityLabel.text=<entity>
JsfControllerPanel.prefixField.toolTipText=Prefix
JsfControllerPanel.prefixField.text=
JsfControllerPanel.packageTextField.text=
JsfControllerPanel.packagePrefixLabel.text=<application-package> +
JsfControllerPanel.packageLabel.text=Package :
JsfControllerPanel.warningLabel.text=
JsfControllerPanel.nameLabel.text=File Pattern :
JsfControllerPanel.suffixField.toolTipText=Suffix
JsfControllerPanel.suffixField.text=Repository
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* Copyright 2013-2018 the original author or authors from the Jeddict project (https://jeddict.github.io/).
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package io.github.jeddict.jsf.controller;

import io.github.jeddict.jcode.LayerConfigData;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

/**
*
* @author Shiwani Gupta
*/
public class JsfControllerData extends LayerConfigData {

private String prefixName;
private String suffixName;
private String _package;
private boolean isNamed;
private boolean cdi = true;

/**
* @return the _package
*/
public String getPackage() {
return _package;
}

/**
* @param _package the _package to set
*/
public void setPackage(String _package) {
this._package = _package;
}

/**
* @return the suffixName
*/
public String getSuffixName() {
if(StringUtils.isBlank(suffixName)){
suffixName = "Controller";
}
return suffixName;
}

/**
* @param suffixName the suffixName to set
*/
public void setSuffixName(String suffixName) {
this.suffixName = suffixName;
}

/**
* @return the prefixName
*/
public String getPrefixName() {
return prefixName;
}

/**
* @param prefixName the prefixName to set
*/
public void setPrefixName(String prefixName) {
this.prefixName = prefixName;
}

/**
* @return the isNamed
*/
public boolean isNamed() {
return isNamed;
}

/**
* @param isNamed the isNamed to set
*/
public void setNamed(boolean isNamed) {
this.isNamed = isNamed;
}

/**
* @return the isCDI
*/
public boolean isCDI() {
return cdi;
}

/**
* @param cdi the cdi to set
*/
public void setCDI(boolean cdi) {
this.cdi = cdi;
}

@Override
public List<String> getUsageDetails() {
return Collections.<String>emptyList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/**
* Copyright 2013-2018 the original author or authors from the Jeddict project (https://jeddict.github.io/).
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package io.github.jeddict.jsf.controller;

import io.github.jeddict.jcode.ApplicationConfigData;
import io.github.jeddict.jcode.Generator;
import io.github.jeddict.jcode.annotation.ConfigData;
import io.github.jeddict.jcode.annotation.Technology;
import static io.github.jeddict.jcode.annotation.Technology.Type.CONTROLLER;
import io.github.jeddict.jcode.task.progress.ProgressHandler;
import static io.github.jeddict.jcode.util.Constants.JAVA_EXT;
import static io.github.jeddict.jcode.util.ProjectHelper.getFolderForPackage;
import static io.github.jeddict.jcode.util.StringHelper.firstLower;
import static io.github.jeddict.jcode.util.StringHelper.firstUpper;
import io.github.jeddict.jpa.spec.Entity;
import io.github.jeddict.jpa.spec.EntityMappings;
import io.github.jeddict.repository.RepositoryData;
import io.github.jeddict.repository.RepositoryGenerator;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static java.util.stream.Collectors.toList;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.SourceGroup;
import org.openide.filesystems.FileObject;
import org.openide.util.lookup.ServiceProvider;
import io.github.jeddict.jcode.util.FileUtil;
import io.github.jeddict.jpa.spec.extend.Attribute;
import java.util.List;

/**
* Generates repository for entity classes.
*
* @author Shiwani Gupta
*/
@ServiceProvider(service = Generator.class)
@Technology(type = CONTROLLER,
label = "JSF Controller",
panel = JsfControllerPanel.class,
parents = RepositoryGenerator.class
)
public final class JsfControllerGenerator implements Generator {

private static final String TEMPLATE = "io/github/jeddict/template/";
public static final String REPOSITORY_ABSTRACT = "Abstract";

@ConfigData
private JsfControllerData controllerData;

@ConfigData
private RepositoryData repositoryData;

@ConfigData
private EntityMappings entityMapping;

@ConfigData
private ApplicationConfigData appConfigData;

@ConfigData
private ProgressHandler handler;

private Project targetProject;

private SourceGroup targetSource;

private String targetPackage;

private Project gatewayProject;

private SourceGroup gatewaySource;

@Override
public void execute() throws IOException {
targetProject = appConfigData.getTargetProject();
targetSource = appConfigData.getTargetSourceGroup();
gatewayProject = appConfigData.getGatewayProject();
gatewaySource = appConfigData.getGatewaySourceGroup();
targetPackage = appConfigData.getTargetPackage();
generate();
generatePagination();
generateJsfUtil();

}

private Set<FileObject> generate() throws IOException {

final Set<FileObject> createdFiles = new HashSet<>();
for (Entity entity : entityMapping.getGeneratedEntity().collect(toList())) {
handler.progress(controllerData.getPrefixName() + entity.getClazz() + controllerData.getSuffixName());
createdFiles.add(generateController(entity, true));

}
return createdFiles;
}

private void generatePagination() throws IOException {
String _package = targetPackage + ".util";
FileObject targetFolder = getFolderForPackage(targetSource, _package, true);
String fileName = "PaginationHelper";
Map<String, Object> param = new HashMap<>();
param.put("package", _package);
handler.progress(fileName);
FileUtil.expandTemplate(TEMPLATE + "util/PaginationHelper.java.ftl", targetFolder, fileName + '.' + JAVA_EXT, param);
}

private void generateJsfUtil() throws IOException {
String _package = targetPackage + ".util";
FileObject targetFolder = getFolderForPackage(targetSource, _package, true);
String fileName = "JsfUtil";
Map<String, Object> param = new HashMap<>();
param.put("package", _package);
handler.progress(fileName);
FileUtil.expandTemplate(TEMPLATE + "util/JsfUtil.java.ftl", targetFolder, fileName + '.' + JAVA_EXT, param);
}

private FileObject generateController(final Entity entity, boolean overrideExisting) throws IOException {
FileObject targetFolder = getFolderForPackage(targetSource,
entity.getAbsolutePackage(targetPackage + '.' + controllerData.getPackage()),
true);

final String entitySimpleName = entity.getClazz();
String controllerName = controllerData.getPrefixName() + entitySimpleName + controllerData.getSuffixName();
String entityClass = firstUpper(entitySimpleName);
String entityInstance = firstLower(entitySimpleName);
String entityFQN = entity.getFQN();
String embeddableFQN = entityFQN.substring(0, entityFQN.lastIndexOf("."));

String repositoryName = repositoryData.getRepositoryPrefixName() + entitySimpleName + repositoryData.getRepositorySuffixName();
String repositoryInstance = firstLower(repositoryName);
String repositoryFQN = entity.getAbsolutePackage(repositoryData.getRepositoryPackage()) + '.' + repositoryName;;

List<Attribute> attributes = entity.getAttributes().getAllAttribute();
Map<String, Object> params = new HashMap<>();
params.put("entityInstance", entityInstance);
params.put("Entity", entityClass);
params.put("EntityController", controllerName);
params.put("named", controllerData.isNamed());
params.put("EntityRepository", repositoryName);
params.put("RepositoryInstance", repositoryInstance);
params.put("EntityClass_FQN", entityFQN);
params.put("attributes", attributes);
params.put("embeddableFQN", embeddableFQN);
params.put("RepositoryClass_FQN", targetPackage + '.' + repositoryFQN);
params.put("JsfUtil_FQN", targetPackage + ".util.JsfUtil");
params.put("PaginationHelper_FQN", targetPackage + ".util.PaginationHelper");
params.put("package", targetPackage + '.' + entity.getAbsolutePackage(controllerData.getPackage()));

return FileUtil.expandTemplate(TEMPLATE + "controller/EntityController.java.ftl", targetFolder, controllerName + '.' + JAVA_EXT, params);
}
}
Loading