Skip to content

Commit

Permalink
FORGE-1176: Initial endpoint generation
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Sep 24, 2013
1 parent acd347e commit 042da1a
Show file tree
Hide file tree
Showing 18 changed files with 1,286 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@

package org.jboss.forge.addon.javaee.ejb;

import org.jboss.forge.addon.facets.constraints.FacetConstraint;
import org.jboss.forge.addon.javaee.jta.JTAFacet_1_1;

/**
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
@FacetConstraint(JTAFacet_1_1.class)
public interface EJBFacet_3_1 extends EJBFacet
{

Expand Down
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;
}

}
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);
}

}
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();
}
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;

}
8 changes: 8 additions & 0 deletions javaee/impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,13 @@
<classifier>forge-addon</classifier>
<scope>provided</scope>
</dependency>

<!-- Freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>

</dependencies>
</project>
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";
}
}

0 comments on commit 042da1a

Please sign in to comment.