-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FORGE-1176: Initial endpoint generation
- Loading branch information
Showing
18 changed files
with
1,286 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
javaee/api/src/main/java/org/jboss/forge/addon/javaee/rest/generation/MessageCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.addon.javaee.rest.generation; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Collects validation messages | ||
* | ||
* @author <a href="ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public class MessageCollector | ||
{ | ||
private Map<MessageType, List<String>> messages = new HashMap<MessageType, List<String>>(); | ||
|
||
public void addErrorMessage(String message) | ||
{ | ||
add(MessageType.ERROR, message); | ||
} | ||
|
||
public void addInformationMessage(String message) | ||
{ | ||
add(MessageType.INFO, message); | ||
} | ||
|
||
public void addWarningMessage(String message) | ||
{ | ||
add(MessageType.WARNING, message); | ||
} | ||
|
||
public List<String> getErrorMessages() | ||
{ | ||
return get(MessageType.ERROR); | ||
} | ||
|
||
public List<String> getInformationMessages() | ||
{ | ||
return get(MessageType.INFO); | ||
} | ||
|
||
public List<String> getWarningMessages() | ||
{ | ||
return get(MessageType.WARNING); | ||
} | ||
|
||
private void add(MessageType type, String message) | ||
{ | ||
List<String> list = messages.get(type); | ||
if (list == null) | ||
{ | ||
list = new ArrayList<String>(); | ||
messages.put(type, list); | ||
} | ||
list.add(message); | ||
|
||
} | ||
|
||
private List<String> get(MessageType type) | ||
{ | ||
List<String> list = messages.get(type); | ||
return list == null ? Collections.<String> emptyList() : list; | ||
} | ||
|
||
private static enum MessageType | ||
{ | ||
ERROR, | ||
INFO, | ||
WARNING; | ||
} | ||
|
||
} |
89 changes: 89 additions & 0 deletions
89
...pi/src/main/java/org/jboss/forge/addon/javaee/rest/generation/RestGeneratedResources.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 2012 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.addon.javaee.rest.generation; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import org.jboss.forge.addon.parser.java.resources.JavaResource; | ||
|
||
/** | ||
* @author <a href="mailto:salmon.charles@gmail.com">charless</a> | ||
* | ||
*/ | ||
public class RestGeneratedResources | ||
{ | ||
private final List<JavaResource> endpoints; | ||
private final List<JavaResource> entities; | ||
private final List<JavaResource> others; | ||
|
||
public RestGeneratedResources() | ||
{ | ||
this(new ArrayList<JavaResource>(), new ArrayList<JavaResource>(), new ArrayList<JavaResource>()); | ||
} | ||
|
||
public RestGeneratedResources(List<JavaResource> entities, | ||
List<JavaResource> endpoints) | ||
{ | ||
this(entities, endpoints, new ArrayList<JavaResource>()); | ||
} | ||
|
||
public RestGeneratedResources(List<JavaResource> entities, List<JavaResource> endpoints, List<JavaResource> others) | ||
{ | ||
this.entities = entities; | ||
this.endpoints = endpoints; | ||
this.others = others; | ||
} | ||
|
||
public List<JavaResource> getEndpoints() | ||
{ | ||
return endpoints; | ||
} | ||
|
||
public List<JavaResource> getEntities() | ||
{ | ||
return entities; | ||
} | ||
|
||
public List<JavaResource> getOthers() | ||
{ | ||
return others; | ||
} | ||
|
||
public void addToEndpoints(JavaResource endpoint) | ||
{ | ||
this.endpoints.add(endpoint); | ||
} | ||
|
||
public void addToEndpoints(Collection<JavaResource> endpoints) | ||
{ | ||
this.endpoints.addAll(endpoints); | ||
} | ||
|
||
public void addToEntities(JavaResource entity) | ||
{ | ||
this.entities.add(entity); | ||
} | ||
|
||
public void addToEntities(Collection<JavaResource> entities) | ||
{ | ||
this.entities.addAll(entities); | ||
} | ||
|
||
public void addToOthers(JavaResource other) | ||
{ | ||
this.others.add(other); | ||
} | ||
|
||
public void addToOthers(Collection<JavaResource> others) | ||
{ | ||
this.others.addAll(others); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...api/src/main/java/org/jboss/forge/addon/javaee/rest/generation/RestGenerationContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.addon.javaee.rest.generation; | ||
|
||
import org.jboss.forge.addon.projects.Project; | ||
import org.jboss.forge.parser.java.JavaClass; | ||
|
||
/** | ||
* Parameters for REST resource generation | ||
* | ||
* @author <a href="ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public interface RestGenerationContext | ||
{ | ||
public Project getProject(); | ||
|
||
public JavaClass getEntity(); | ||
|
||
public String getTargetPackageName(); | ||
|
||
public String getContentType(); | ||
|
||
public String getPersistenceUnitName(); | ||
} |
36 changes: 36 additions & 0 deletions
36
...api/src/main/java/org/jboss/forge/addon/javaee/rest/generation/RestResourceGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.addon.javaee.rest.generation; | ||
|
||
import java.util.List; | ||
|
||
import org.jboss.forge.parser.java.JavaClass; | ||
|
||
/** | ||
* Generates a REST resource based on a JPA entity | ||
* | ||
* @author <a href="ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public interface RestResourceGenerator | ||
{ | ||
/** | ||
* A readable description for this strategy | ||
*/ | ||
String getName(); | ||
|
||
/** | ||
* A human-readable description for this strategy | ||
*/ | ||
String getDescription(); | ||
|
||
/** | ||
* Generate a REST endpoint based on a context | ||
*/ | ||
List<JavaClass> generateFrom(RestGenerationContext context) throws Exception; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...c/main/java/org/jboss/forge/addon/javaee/rest/generator/EntityBasedResourceGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.addon.javaee.rest.generator; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.inject.Inject; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
|
||
import org.jboss.forge.addon.javaee.rest.generation.RestGenerationContext; | ||
import org.jboss.forge.addon.javaee.rest.generation.RestResourceGenerator; | ||
import org.jboss.forge.addon.parser.java.facets.JavaSourceFacet; | ||
import org.jboss.forge.addon.projects.Project; | ||
import org.jboss.forge.parser.JavaParser; | ||
import org.jboss.forge.parser.java.JavaClass; | ||
|
||
/** | ||
* A JAX-RS resource generator that uses JPA entities directly in the created REST resources. | ||
*/ | ||
public class EntityBasedResourceGenerator implements RestResourceGenerator | ||
{ | ||
@Inject | ||
FreemarkerTemplateProcessor processor; | ||
|
||
@Override | ||
public List<JavaClass> generateFrom(RestGenerationContext context) throws Exception | ||
{ | ||
JavaClass entity = context.getEntity(); | ||
Project project = context.getProject(); | ||
String contentType = context.getContentType(); | ||
if (!entity.hasAnnotation(XmlRootElement.class)) | ||
{ | ||
entity.addAnnotation(XmlRootElement.class); | ||
project.getFacet(JavaSourceFacet.class).saveJavaSource(entity); | ||
} | ||
String idType = ResourceGeneratorUtil.resolveIdType(entity); | ||
String persistenceUnitName = context.getPersistenceUnitName(); | ||
String idGetterName = ResourceGeneratorUtil.resolveIdGetterName(entity); | ||
String entityTable = ResourceGeneratorUtil.getEntityTable(entity); | ||
String selectExpression = ResourceGeneratorUtil.getSelectExpression(entity, entityTable); | ||
String idClause = ResourceGeneratorUtil.getIdClause(entity, entityTable); | ||
String orderClause = ResourceGeneratorUtil.getOrderClause(entity, | ||
ResourceGeneratorUtil.getJpqlEntityVariable(entityTable)); | ||
String resourcePath = ResourceGeneratorUtil.getResourcePath(context); | ||
|
||
Map<Object, Object> map = new HashMap<Object, Object>(); | ||
map.put("entity", entity); | ||
map.put("idType", idType); | ||
map.put("getIdStatement", idGetterName); | ||
map.put("contentType", contentType); | ||
map.put("persistenceUnitName", persistenceUnitName); | ||
map.put("entityTable", entityTable); | ||
map.put("selectExpression", selectExpression); | ||
map.put("idClause", idClause); | ||
map.put("orderClause", orderClause); | ||
map.put("resourcePath", resourcePath); | ||
|
||
String output = processor.processTemplate(map, "org/jboss/forge/rest/Endpoint.jv"); | ||
JavaClass resource = JavaParser.parse(JavaClass.class, output); | ||
resource.addImport(entity.getQualifiedName()); | ||
resource.setPackage(context.getTargetPackageName()); | ||
return Arrays.asList(resource); | ||
} | ||
|
||
@Override | ||
public String getDescription() | ||
{ | ||
return "Expose JPA entities directly in the REST resources"; | ||
} | ||
|
||
@Override | ||
public String getName() | ||
{ | ||
return "JPA_ENTITY"; | ||
} | ||
} |
Oops, something went wrong.