Skip to content

Commit

Permalink
Merge pull request #484 from VineetReynolds/FORGE-1892
Browse files Browse the repository at this point in the history
FORGE-1892 and FORGE-1925
  • Loading branch information
gastaldi committed Jul 10, 2014
2 parents 3e63c62 + 12bbde5 commit 434dac6
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,92 +11,98 @@
import org.jboss.forge.addon.text.Inflector;
import org.jboss.forge.roaster.model.source.JavaClassSource;

import java.util.List;

/**
* Parameters for REST resource generation
*
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public class RestGenerationContext
{
private Project project;
private JavaClassSource entity;
private String targetPackageName;
private String contentType;
private String persistenceUnitName;
private Inflector inflector;

/**
* @return the project
*/
public Project getProject()
{
return project;
}

/**
* @return the entity
*/
public JavaClassSource getEntity()
{
return entity;
}

/**
* @return the targetPackageName
*/
public String getTargetPackageName()
{
return targetPackageName;
}

/**
* @return the contentType
*/
public String getContentType()
{
return contentType;
}

/**
* @return the persistenceUnitName
*/
public String getPersistenceUnitName()
{
return persistenceUnitName;
}

public Inflector getInflector()
{
return inflector;
}

public void setProject(Project project)
{
this.project = project;
}

public void setEntity(JavaClassSource entity)
{
this.entity = entity;
}

public void setTargetPackageName(String targetPackageName)
{
this.targetPackageName = targetPackageName;
}

public void setContentType(String contentType)
{
this.contentType = contentType;
}

public void setPersistenceUnitName(String persistenceUnitName)
{
this.persistenceUnitName = persistenceUnitName;
}

public void setInflector(Inflector inflector)
{
this.inflector = inflector;
}
private Project project;
private JavaClassSource entity;
private String targetPackageName;
private List<String> contentType;
private String persistenceUnitName;
private Inflector inflector;

/**
* @return the project
*/
public Project getProject()
{
return project;
}

/**
* @return the entity
*/
public JavaClassSource getEntity()
{
return entity;
}

/**
* @return the targetPackageName
*/
public String getTargetPackageName()
{
return targetPackageName;
}

/**
* @return the contentType
*/
public List<String> getContentType()
{
return contentType;
}

/**
* @return the persistenceUnitName
*/
public String getPersistenceUnitName()
{
return persistenceUnitName;
}

public Inflector getInflector()
{
return inflector;
}

public void setProject(Project project)
{
this.project = project;
}

public void setEntity(JavaClassSource entity)
{
this.entity = entity;
}

public void setTargetPackageName(String targetPackageName)
{
this.targetPackageName = targetPackageName;
}

public void setContentType(List<String> contentType)
{
if (contentType.size() < 1)
{
throw new IllegalArgumentException("At least one content type must be specified.");
}
this.contentType = contentType;
}

public void setPersistenceUnitName(String persistenceUnitName)
{
this.persistenceUnitName = persistenceUnitName;
}

public void setInflector(Inflector inflector)
{
this.inflector = inflector;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import org.jboss.forge.roaster.model.Method;
import org.jboss.forge.roaster.model.util.Strings;

import java.util.Iterator;
import java.util.List;

/**
* A utlity class that provides information about the project or the JPA entity. This is to be used in the JAX-RS
* Resource generators.
Expand Down Expand Up @@ -273,4 +276,28 @@ public static char getJpqlEntityVariable(String entityTable)
{
return entityTable.toLowerCase().charAt(0);
}

public static String getContentType(List<String> contentType)
{
StringBuilder contentTypeBuilder = new StringBuilder();
if(contentType.size() > 1)
{
contentTypeBuilder.append("{");
for(Iterator iter = contentType.iterator(); iter.hasNext();)
{
contentTypeBuilder.append('"').append(iter.next()).append('"');
if(iter.hasNext())
{
contentTypeBuilder.append(',');
}
}
contentTypeBuilder.append("}");
}
else
{
String contentTypeValue = contentType.get(0);
contentTypeBuilder.append('"').append(contentTypeValue).append('"');
}
return contentTypeBuilder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public List<JavaClassSource> generateFrom(RestGenerationContext context) throws
{
JavaClassSource 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 contentType = ResourceGeneratorUtil.getContentType(context.getContentType());
String idType = ResourceGeneratorUtil.resolveIdType(entity);
String persistenceUnitName = context.getPersistenceUnitName();
String idGetterName = ResourceGeneratorUtil.resolveIdGetterName(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public List<JavaClassSource> generateFrom(RestGenerationContext context) throws
JavaClassSource entity = context.getEntity();

Project project = context.getProject();
String contentType = context.getContentType();
String contentType = ResourceGeneratorUtil.getContentType(context.getContentType());
String idType = ResourceGeneratorUtil.resolveIdType(entity);
String persistenceUnitName = context.getPersistenceUnitName();
String idGetterName = ResourceGeneratorUtil.resolveIdGetterName(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@

package org.jboss.forge.addon.javaee.rest.ui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;
import javax.persistence.Id;
import javax.ws.rs.core.MediaType;
Expand All @@ -36,7 +30,10 @@
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.hints.InputType;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.UICompleter;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.input.UIInputMany;
import org.jboss.forge.addon.ui.input.UISelectMany;
import org.jboss.forge.addon.ui.input.UISelectOne;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
Expand All @@ -47,11 +44,17 @@
import org.jboss.forge.addon.ui.result.navigation.NavigationResultBuilder;
import org.jboss.forge.addon.ui.util.Categories;
import org.jboss.forge.addon.ui.util.Metadata;
import org.jboss.forge.furnace.util.Lists;
import org.jboss.forge.roaster.model.Member;
import org.jboss.forge.roaster.model.source.JavaClassSource;
import org.jboss.shrinkwrap.descriptor.api.persistence.PersistenceCommonDescriptor;
import org.jboss.shrinkwrap.descriptor.api.persistence.PersistenceUnitCommon;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Generates REST endpoints from JPA Entities
*
Expand All @@ -61,7 +64,7 @@ public class RestEndpointFromEntityCommand extends AbstractJavaEECommand impleme
{
@Inject
@WithAttributes(label = "Content Type", defaultValue = MediaType.APPLICATION_JSON, required = true)
private UISelectOne<String> contentType;
private UIInputMany<String> contentType;

@Inject
@WithAttributes(label = "Targets", required = true)
Expand Down Expand Up @@ -137,7 +140,15 @@ public String convert(JavaClassSource source)
// TODO: May detect where @Path resources are located
packageName.setDefaultValue(javaSourceFacet.getBasePackage() + ".rest");

contentType.setValueChoices(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML));
contentType.setCompleter(new UICompleter<String>() {
@Override
public Iterable<String> getCompletionProposals(UIContext context, InputComponent<?, String> input, String value) {
List<String> options = new ArrayList<>();
options.add(MediaType.APPLICATION_XML);
options.add(MediaType.APPLICATION_JSON);
return options;
}
});
generator.setDefaultValue(defaultResourceGenerator);
if (context.getProvider().isGUI())
{
Expand Down Expand Up @@ -224,7 +235,7 @@ private RestGenerationContext createContextFor(final UIContext context)
{
RestGenerationContext generationContext = new RestGenerationContext();
generationContext.setProject(getSelectedProject(context));
generationContext.setContentType(contentType.getValue());
generationContext.setContentType(Lists.toList(contentType.getValue()));
generationContext.setPersistenceUnitName(persistenceUnit.getValue());
generationContext.setTargetPackageName(packageName.getValue());
generationContext.setInflector(inflector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ${entityTable}Endpoint
private EntityManager em;

@POST
@Consumes("${contentType}")
@Consumes(${contentType})
public Response create(${entity.getName()} entity)
{
em.persist(entity);
Expand All @@ -45,7 +45,7 @@ public class ${entityTable}Endpoint

@GET
@Path("/{id:[0-9][0-9]*}")
@Produces("${contentType}")
@Produces(${contentType})
public Response findById(@PathParam("id") ${idType} id)
{
TypedQuery<${entity.getName()}> findByIdQuery = em.createQuery("${selectExpression} ${idClause} ${orderClause}", ${entity.getName()}.class);
Expand All @@ -63,7 +63,7 @@ public class ${entityTable}Endpoint
}

@GET
@Produces("${contentType}")
@Produces(${contentType})
public List<${entity.getName()}> listAll(@QueryParam("start") Integer startPosition, @QueryParam("max") Integer maxResult)
{
TypedQuery<${entity.getName()}> findAllQuery = em.createQuery("${selectExpression} ${orderClause}", ${entity.getName()}.class);
Expand All @@ -81,7 +81,7 @@ public class ${entityTable}Endpoint

@PUT
@Path("/{id:[0-9][0-9]*}")
@Consumes("${contentType}")
@Consumes(${contentType})
public Response update(${entity.getName()} entity)
{
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ private List<String> resolveWildcardSelectOptionValues(CommandLine commandLine,
}
}
}
else
{
resolvedOptionValues = optionValues;
}
return resolvedOptionValues;
}
}
Loading

0 comments on commit 434dac6

Please sign in to comment.