use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class RepositoryJpaIntegrationTestMetadata method getFindOneTestMethod.
/**
* Builds a method to test find one method.
*
* @return {@link MethodMetadata}
*/
private MethodMetadata getFindOneTestMethod() {
final JavaSymbolName methodName = new JavaSymbolName(String.format("findOneShouldReturnExisting%s", this.entity.getSimpleTypeName()));
MethodMetadata method = getGovernorMethod(methodName);
if (method != null) {
return method;
}
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Setup
bodyBuilder.appendFormalLine("// Setup");
// Long id = getRandomPetId();
bodyBuilder.appendFormalLine("%s id = %s();", getNameOfJavaType(this.identifierType), this.getRandomIdMethodName);
// Exercise
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("// Exercise");
// Pet pet = repository.findOne(id);
bodyBuilder.appendFormalLine("%s %s = %s().findOne(id);", getNameOfJavaType(this.entity), entityVar, getAccessorMethod(this.repositoryField).getMethodName());
// Verify
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("// Verify");
// assertThat(pet).as("Check that findOne illegally returned null for id %s", id).isNotNull();
bodyBuilder.appendFormalLine("%s(%s).as(\"Check that findOne illegally returned null for id %s\", id).isNotNull();", getNameOfJavaType(ASSERT_THAT), this.entityVar, "%s");
// assertThat(id).as("Check the identifier of the found 'Pet' is the same used to look for it")
bodyBuilder.appendFormalLine("%s(id).as(\"Check the identifier of the found '%s' is the same used to look for it\")", getNameOfJavaType(ASSERT_THAT), getNameOfJavaType(this.entity));
// .isEqualTo(pet.getId());
bodyBuilder.indent();
bodyBuilder.appendFormalLine(".isEqualTo(%s.%s());", this.entityVar, this.identifierAccessorMethodName);
bodyBuilder.reset();
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
// Add @Test
methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
return methodBuilder.build();
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class ServiceImplMetadata method builSaveMethodBody.
/**
* Build "save" method body which delegates on repository
*
* @param methodToBeImplemented
* @param isBatch
* @param isDelete
* @return
*/
private InvocableMemberBodyBuilder builSaveMethodBody(final MethodMetadata methodToBeImplemented) {
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
final JavaSymbolName param0 = methodToBeImplemented.getParameterNames().get(0);
/*
* // Ensure the relationships are maintained
* entity.addToRelatedEntity(entity.getRelatedEntity());
*/
boolean commentAdded = false;
Map<String, RelationInfo> relationInfos = entityMetadata.getRelationInfos();
for (Entry<String, RelationInfo> entry : relationInfos.entrySet()) {
RelationInfo info = entry.getValue();
if (info.cardinality == Cardinality.ONE_TO_ONE) {
if (!commentAdded) {
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("// Ensure the relationships are maintained");
commentAdded = true;
}
bodyBuilder.appendFormalLine("%s.%s(%s.get%s());", param0, info.addMethod.getMethodName(), param0, StringUtils.capitalize(entry.getKey()));
bodyBuilder.newLine();
}
}
bodyBuilder.appendFormalLine("%s %s().%s(%s);", methodToBeImplemented.getReturnType().equals(JavaType.VOID_PRIMITIVE) ? "" : "return", getAccessorMethod(repositoryFieldMetadata).getMethodName(), methodToBeImplemented.getMethodName().getSymbolName(), param0);
return bodyBuilder;
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class ServiceImplMetadata method getIdentifierTypeGetterMethod.
/**
* Builds a method which returns the class of the entity identifier JavaType.
*
* @return MethodMetadataBuilder
*/
private MethodMetadata getIdentifierTypeGetterMethod() {
List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
MethodMetadata existingMethod = getGovernorMethod(GET_ID_TYPE_METHOD_NAME, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// return IDENTIFIER_TYPE.class;
bodyBuilder.appendFormalLine("return %s.class;", getNameOfJavaType(this.entityIdentifierType));
// Use the MethodMetadataBuilder for easy creation of MethodMetadata
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, GET_ID_TYPE_METHOD_NAME, JavaType.wrapperOf(JavaType.CLASS, this.serviceMetadata.getIdType()), parameterTypes, parameterNames, bodyBuilder);
// Build and return a MethodMetadata instance
return methodBuilder.build();
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class ServiceImplMetadata method getSetRelation.
private MethodMetadata getSetRelation(MethodMetadata methodToBeImplemented, RelationInfo relationInfo) {
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Prepare constants
final JavaSymbolName param0 = methodToBeImplemented.getParameterNames().get(0);
final JavaType childType = relationInfo.childType;
final FieldMetadata childServideField = requiredServiceFieldByEntity.get(childType);
final String childTypeNameJavaType = getNameOfJavaType(childType);
final JavaSymbolName param1 = methodToBeImplemented.getParameterNames().get(1);
final String saveMethod = serviceMetadata.getCurrentSaveMethod().getMethodName().getSymbolName();
String childListVariable = "items";
// List<{childType}> {parentFieldName} =
// {childService}.findAll({param1});
bodyBuilder.appendFormalLine("%s<%s> %s = %s().findAll(%s);", getNameOfJavaType(JavaType.LIST), childTypeNameJavaType, childListVariable, getAccessorMethod(childServideField).getMethodName(), param1);
// Set<{childType}> currents = {param0}.get{rel.property}();
bodyBuilder.appendFormalLine("%s currents = %s.get%s();", getNameOfJavaType(relationInfo.fieldMetadata.getFieldType()), param0, relationInfo.fieldMetadata.getFieldName().getSymbolNameCapitalisedFirstLetter());
// Set<{childType}> toRemove = new
// HashSet<{childType}>({parentFieldName});
bodyBuilder.appendFormalLine("%s<%s> toRemove = new %s<%s>();", getNameOfJavaType(JavaType.SET), childTypeNameJavaType, getNameOfJavaType(JdkJavaType.HASH_SET), childTypeNameJavaType);
// for (Iterator<{childType}> iterator = current.iterator();
// iterator.hasNext();) {
bodyBuilder.appendFormalLine("for (%s<%s> iterator = currents.iterator(); iterator.hasNext();) {", getNameOfJavaType(JavaType.ITERATOR), childTypeNameJavaType);
bodyBuilder.indent();
final String itemName = "next".concat(StringUtils.capitalize(childType.getSimpleTypeName()));
// {childType} {itemName} = iterator.next();
bodyBuilder.appendFormalLine("%s %s = iterator.next();", childTypeNameJavaType, itemName);
// if ({childListVariable}.contains({itemName})) {
bodyBuilder.appendFormalLine("if (%s.contains(%s)) {", childListVariable, itemName);
bodyBuilder.indent();
// {childListVariable}.remove({itemName});
bodyBuilder.appendFormalLine("%s.remove(%s);", childListVariable, itemName);
bodyBuilder.indentRemove();
// } else {
bodyBuilder.appendFormalLine("} else {");
bodyBuilder.indent();
// toRemove.add(product);
bodyBuilder.appendFormalLine("toRemove.add(%s);", itemName);
bodyBuilder.indentRemove();
// }
bodyBuilder.appendFormalLine("}");
// }
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
// {param0}.{removeFromMethod}(toRemove);
bodyBuilder.appendFormalLine("%s.%s(toRemove);", param0, relationInfo.removeMethod.getMethodName());
// {param0}.{addToMethod}({childListVariable);
bodyBuilder.appendFormalLine("%s.%s(%s);", param0, relationInfo.addMethod.getMethodName(), childListVariable);
// Concurrency control
if (this.entityMetadata.getCurrentVersionField() != null) {
// // Force the version update of the parent side to know that the parent has changed
bodyBuilder.appendFormalLine("// Force the version update of the parent side to know that the parent has changed");
// // because it has new books assigned
bodyBuilder.appendFormalLine("// because it has new books assigned");
// {param0}.setVersion({param0}.getVersion() + 1);
bodyBuilder.appendFormalLine("%s.setVersion(%s.getVersion() + 1);", param0, param0);
}
// return {repoField}.{saveMethod}({param0});
bodyBuilder.appendFormalLine("return %s().%s(%s);", getAccessorMethod(repositoryFieldMetadata).getMethodName(), saveMethod, param0);
return getMethod(methodToBeImplemented, true, bodyBuilder);
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class ServiceImplMetadata method buildFindOneForUpdateBody.
/**
* Build "findOneForUpdate" method body which delegates on repository
*
* @param methodToBeImplemented
* @return
*/
private InvocableMemberBodyBuilder buildFindOneForUpdateBody(MethodMetadata methodToBeImplemented) {
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
final JavaSymbolName param0 = methodToBeImplemented.getParameterNames().get(0);
// return entityRepository.findOneDetached(id);
bodyBuilder.appendFormalLine("%s %s().%s(%s);", methodToBeImplemented.getReturnType().equals(JavaType.VOID_PRIMITIVE) ? "" : "return", getAccessorMethod(repositoryFieldMetadata).getMethodName(), FIND_ONE_DETACHED.getSymbolName(), param0);
return bodyBuilder;
}
Aggregations