Skip to content

Commit

Permalink
FORGE-1074 Removed the generation of the setter for Ids
Browse files Browse the repository at this point in the history
This was not required, and hence the update method that processes PUT requests no longer (re)sets the Id of the entity.
  • Loading branch information
VineetReynolds committed Aug 2, 2013
1 parent 02fdb5c commit 48431eb
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.jboss.forge.parser.java.JavaSource;
import org.jboss.forge.parser.java.Member;
import org.jboss.forge.parser.java.Method;
import org.jboss.forge.parser.java.Parameter;
import org.jboss.forge.parser.java.util.Strings;
import org.jboss.forge.parser.java.util.Types;
import org.jboss.forge.project.Project;
Expand Down Expand Up @@ -154,7 +153,6 @@ public void endpointFromEntity(
+ "] is not supported by endpoint generation.");
continue;
}
String idSetterName = resolveIdSetterName(entity);
String idGetterName = resolveIdGetterName(entity);

freemarker.template.Configuration freemarkerConfig = new freemarker.template.Configuration();
Expand All @@ -164,7 +162,6 @@ public void endpointFromEntity(
Map<Object, Object> map = new HashMap<Object, Object>();
map.put("entity", entity);
map.put("idType", idType);
map.put("setIdStatement", idSetterName);
map.put("getIdStatement", idGetterName);
map.put("contentType", contentType);
String persistenceUnitName = getPersistenceUnitName();
Expand Down Expand Up @@ -237,79 +234,6 @@ private String resolveIdType(JavaClass entity)
return "Object";
}

private String resolveIdSetterName(JavaClass entity)
{
String result = null;

for (Member<JavaClass, ?> member : entity.getMembers())
{
if (member.hasAnnotation(Id.class))
{
String name = member.getName();
String type = null;
if (member instanceof Method)
{
type = ((Method<?>) member).getReturnType();
if (name.startsWith("get"))
{
name = name.substring(2);
}
}
else if (member instanceof Field)
{
type = ((Field<?>) member).getType();
}

if (type != null)
{
for (Method<JavaClass> method : entity.getMethods())
{
// It's a setter
if (method.getParameters().size() == 1 && method.getReturnType() == null)
{
Parameter<JavaClass> param = method.getParameters().get(0);

// The type matches ID field's type
if (type.equals(param.getType()))
{
if (method.getName().toLowerCase().contains(name.toLowerCase()))
{
result = method.getName() + "(id)";
break;
}
}
}
}
}

if (result != null)
{
break;
}
else if (type != null && member.isPublic())
{
String memberName = member.getName();
// Cheat a little if the member is public
if (member instanceof Method && memberName.startsWith("get"))
{
memberName = memberName.substring(3);
memberName = Strings.uncapitalize(memberName);
}
result = memberName + " = id";
}
}
}

if (result == null)
{
throw new RuntimeException("Could not determine @Id field and setter method for @Entity ["
+ entity.getQualifiedName()
+ "]. Aborting.");
}

return result;
}

private String resolveIdGetterName(JavaClass entity)
{
String result = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ public class ${entityTable}Endpoint
@PUT
@Path("/{id:[0-9][0-9]*}")
@Consumes("${contentType}")
public Response update(@PathParam("id") ${idType} id, ${entity.getName()} entity)
public Response update(${entity.getName()} entity)
{
entity.${setIdStatement};
entity = em.merge(entity);
return Response.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ public void testCreateEndpointNonStandardId() throws Exception
assertEquals("/users", endpoint.getAnnotation(Path.class).getStringValue());
assertEquals("forge-default",
endpoint.getField("em").getAnnotation(PersistenceContext.class).getStringValue("unitName"));
assertTrue(endpoint.toString().contains("entity.setObjectId(id);"));
getShell().execute("build");
}

Expand All @@ -205,7 +204,6 @@ public void testCreateEndpointPrimitiveNonStandardId() throws Exception
assertEquals("/user2s", endpoint.getAnnotation(Path.class).getStringValue());
assertEquals("forge-default",
endpoint.getField("em").getAnnotation(PersistenceContext.class).getStringValue("unitName"));
assertTrue(endpoint.toString().contains("entity.setObjectId(id);"));
getShell().execute("build");
}

Expand All @@ -232,7 +230,6 @@ public void testCreateEndpointPrimitiveNonStandardGetterId() throws Exception
assertEquals("/user3s", endpoint.getAnnotation(Path.class).getStringValue());
assertEquals("forge-default",
endpoint.getField("em").getAnnotation(PersistenceContext.class).getStringValue("unitName"));
assertTrue(endpoint.toString().contains("entity.setObjectId(id);"));
getShell().execute("build");
}

Expand Down

0 comments on commit 48431eb

Please sign in to comment.