use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class JspViewManager method getListDocument.
public Document getListDocument() {
final DocumentBuilder builder = XmlUtils.getDocumentBuilder();
final Document document = builder.newDocument();
// Add document namespaces
final Element div = new XmlElementBuilder("div", document).addAttribute("xmlns:page", "urn:jsptagdir:/WEB-INF/tags/form").addAttribute("xmlns:table", "urn:jsptagdir:/WEB-INF/tags/form/fields").addAttribute("xmlns:jsp", "http://java.sun.com/JSP/Page").addAttribute("version", "2.0").addChild(new XmlElementBuilder("jsp:directive.page", document).addAttribute("contentType", "text/html;charset=UTF-8").build()).addChild(new XmlElementBuilder("jsp:output", document).addAttribute("omit-xml-declaration", "yes").build()).build();
document.appendChild(div);
final Element fieldTable = new XmlElementBuilder("table:table", document).addAttribute("id", XmlUtils.convertId("l:" + formBackingType.getFullyQualifiedTypeName())).addAttribute("data", "${" + formBackingTypeMetadata.getPlural().toLowerCase() + "}").addAttribute("path", controllerPath).build();
if (!webScaffoldAnnotationValues.isUpdate()) {
fieldTable.setAttribute("update", "false");
}
if (!webScaffoldAnnotationValues.isDelete()) {
fieldTable.setAttribute("delete", "false");
}
if (!formBackingTypePersistenceMetadata.getIdentifierField().getFieldName().getSymbolName().equals("id")) {
fieldTable.setAttribute("typeIdFieldName", formBackingTypePersistenceMetadata.getIdentifierField().getFieldName().getSymbolName());
}
fieldTable.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(fieldTable));
int fieldCounter = 0;
for (final FieldMetadata field : fields) {
if (++fieldCounter < 7) {
final Element columnElement = new XmlElementBuilder("table:column", document).addAttribute("id", XmlUtils.convertId("c:" + formBackingType.getFullyQualifiedTypeName() + "." + field.getFieldName().getSymbolName())).addAttribute("property", uncapitalize(field.getFieldName().getSymbolName())).build();
final String fieldName = uncapitalize(field.getFieldName().getSymbolName());
if (field.getFieldType().equals(DATE)) {
columnElement.setAttribute("date", "true");
columnElement.setAttribute("dateTimePattern", "${" + entityName + "_" + fieldName.toLowerCase() + "_date_format}");
} else if (field.getFieldType().equals(CALENDAR)) {
columnElement.setAttribute("calendar", "true");
columnElement.setAttribute("dateTimePattern", "${" + entityName + "_" + fieldName.toLowerCase() + "_date_format}");
} else if (field.getFieldType().isCommonCollectionType() && field.getCustomData().get(CustomDataKeys.ONE_TO_MANY_FIELD) != null) {
continue;
}
columnElement.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(columnElement));
fieldTable.appendChild(columnElement);
}
}
// Create page:list element
final Element pageList = new XmlElementBuilder("page:list", document).addAttribute("id", XmlUtils.convertId("pl:" + formBackingType.getFullyQualifiedTypeName())).addAttribute("items", "${" + formBackingTypeMetadata.getPlural().toLowerCase() + "}").addChild(fieldTable).build();
pageList.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(pageList));
div.appendChild(pageList);
return document;
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class RepositoryJpaCustomImplMetadata method buildQuery.
/**
* Builds the search query
*
* @param bodyBuilder method body builder
* @param entityVariable name of the variable that contains the Q entity
* @param globalSearch global search variable name
* @param referencedFieldParamName
* @param referencedField
* @param referencedFieldIdentifierPathName
* @param formBeanType the JavaType which contains the fields to use for filtering.
* Can be null in findAll queries.
* @param formBeanParameterName the name of the search param
* @param returnType
* @param finderName the name of the finder. Only available when method is a
* projection/DTO finder.
* @param partTree
*/
private void buildQuery(InvocableMemberBodyBuilder bodyBuilder, String entityVariable, JavaSymbolName globalSearch, JavaSymbolName referencedFieldParamName, FieldMetadata referencedField, String referencedFieldIdentifierPathName, JavaType formBeanType, String formBeanParameterName, JavaType returnType, JavaSymbolName finderName, PartTree partTree) {
// Prepare leftJoin for compositions oneToOne
StringBuilder fetchJoins = new StringBuilder();
for (RelationInfo relationInfo : entityMetadata.getRelationInfos().values()) {
if (relationInfo.type == JpaRelationType.COMPOSITION && relationInfo.cardinality == Cardinality.ONE_TO_ONE) {
fetchJoins.append(".leftJoin(");
fetchJoins.append(entityVariable);
fetchJoins.append(".");
fetchJoins.append(relationInfo.fieldName);
fetchJoins.append(")");
}
}
// JPQLQuery query = from(qEntity);
bodyBuilder.appendFormalLine(String.format("%s query = from(%s)%s;", getNameOfJavaType(getJPQLQueryFor(entity)), entityVariable, fetchJoins));
bodyBuilder.newLine();
if (formBeanType != null) {
// Query for finder
// if (formSearch != null) {
bodyBuilder.appendFormalLine(String.format("if (%s != null) {", formBeanParameterName));
if (partTree != null) {
buildFormBeanFilterBody(bodyBuilder, formBeanType, formBeanParameterName, entityVariable, partTree);
} else {
// formBean is an entity, filter by all its fields
for (Entry<String, FieldMetadata> field : this.typesFieldsMetadata.get(formBeanType).entrySet()) {
// if (formSearch.getField() != null) {
String accessorMethodName = BeanInfoUtils.getAccessorMethodName(field.getValue()).getSymbolName();
bodyBuilder.appendIndent();
bodyBuilder.appendFormalLine(String.format("if (%s.%s() != null) {", formBeanParameterName, accessorMethodName));
// Get path field name from field mappings
String pathFieldName = getValueOfPairFor(this.typesFieldMaps.get(formBeanType), field.getKey());
// query.where(myEntity.field.eq(formBean.getField()));
bodyBuilder.appendIndent();
bodyBuilder.appendIndent();
bodyBuilder.appendFormalLine(String.format("query.where(%s.eq(%s.%s()));", pathFieldName, formBeanParameterName, accessorMethodName));
// }
bodyBuilder.appendIndent();
bodyBuilder.appendFormalLine("}");
}
}
// }
bodyBuilder.appendFormalLine("}");
bodyBuilder.newLine();
} else if (referencedFieldParamName != null && referencedFieldIdentifierPathName != null) {
// Query for reference
// Assert.notNull(referenced, "referenced is required");
bodyBuilder.appendFormalLine(String.format("%s.notNull(%s, \"%s is required\");", SpringJavaType.ASSERT.getNameIncludingTypeParameters(false, importResolver), referencedFieldParamName, referencedFieldParamName));
bodyBuilder.newLine();
// Query should include a where clause
if (referencedField.getAnnotation(JpaJavaType.MANY_TO_MANY) != null) {
// query.where(referencedFieldPath.contains(referencedFieldName));
bodyBuilder.appendFormalLine(String.format("query.where(%s.contains(%s));", referencedFieldIdentifierPathName, referencedFieldParamName));
} else {
// query.where(referencedFieldPath.eq(referencedFieldName));
bodyBuilder.appendFormalLine(String.format("query.where(%s.eq(%s));", referencedFieldIdentifierPathName, referencedFieldParamName));
}
}
// Path<?>[] paths = new Path[] { .... };
bodyBuilder.appendIndent();
final String pathType = getNameOfJavaType(QUERYDSL_PATH);
bodyBuilder.append(String.format("%s<?>[] paths = new %s<?>[] {", pathType, pathType));
List<String> toAppend = new ArrayList<String>();
// ... returnType.field1, returnType.field2);
if (!this.typesAreProjections.get(returnType)) {
// Return type is the same entity
Map<String, FieldMetadata> projectionFields = this.typesFieldsMetadata.get(returnType);
Iterator<Entry<String, FieldMetadata>> iterator = projectionFields.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, FieldMetadata> field = iterator.next();
String fieldName = field.getValue().getFieldName().getSymbolName();
toAppend.add(entityVariable + "." + fieldName);
}
} else {
// Return type is a projection
List<Pair<String, String>> projectionFields = this.typesFieldMaps.get(returnType);
Validate.notNull(projectionFields, "Couldn't get projection fields for %s", this.defaultReturnType);
Iterator<Pair<String, String>> iterator = projectionFields.iterator();
while (iterator.hasNext()) {
Entry<String, String> entry = iterator.next();
toAppend.add(entry.getValue());
}
}
bodyBuilder.append(StringUtils.join(toAppend, ','));
bodyBuilder.append("};");
bodyBuilder.newLine();
// applyGlobalSearch(search, query, paths);
bodyBuilder.appendFormalLine("applyGlobalSearch(globalSearch, query, paths);");
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class RepositoryJpaCustomImplMetadata method getFindAllByIdsInImpl.
/**
* Method that generates the findAllByIdsIn implementation method
* @param findAllByIdsInGlobalSearchMethod
* @param ids the entity id fields
* @param fields the entity fields to search for
*
* @return
*/
private MethodMetadata getFindAllByIdsInImpl(MethodMetadata findAllByIdsInGlobalSearchMethod, FieldMetadata idField, List<FieldMetadata> fields) {
// Define method name
JavaSymbolName methodName = findAllByIdsInGlobalSearchMethod.getMethodName();
// Define method parameter types
List<AnnotatedJavaType> parameterTypes = findAllByIdsInGlobalSearchMethod.getParameterTypes();
// Define method parameter names
List<JavaSymbolName> parameterNames = findAllByIdsInGlobalSearchMethod.getParameterNames();
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Use provided findAll method to generate its implementation
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, findAllByIdsInGlobalSearchMethod.getMethodName(), findAllByIdsInGlobalSearchMethod.getReturnType(), findAllByIdsInGlobalSearchMethod.getParameterTypes(), findAllByIdsInGlobalSearchMethod.getParameterNames(), null);
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Getting variable name to use in the code
JavaSymbolName globalSearch = parameterNames.get(1);
JavaSymbolName pageable = parameterNames.get(2);
String entity = this.entity.getSimpleTypeName();
String entityVariable = StringUtils.uncapitalize(entity);
// Types to import
JavaType projection = QUERYDSL_PROJECTIONS;
bodyBuilder.newLine();
// QEntity qEntity = QEntity.entity;
bodyBuilder.appendFormalLine(String.format("%1$s %2$s = %1$s.%2$s;", entityQtype.getNameIncludingTypeParameters(false, importResolver), entityVariable));
bodyBuilder.newLine();
// Construct query
buildQuery(bodyBuilder, entityVariable, globalSearch, null, null, null, null, null, this.defaultReturnType, null, null);
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("// Also, filter by the provided ids");
bodyBuilder.appendFormalLine("query.where(%s.%s.in(ids));", entityVariable, idField.getFieldName());
bodyBuilder.newLine();
// AttributeMappingBuilder mapping = buildMapper()
StringBuffer mappingBuilderLine = new StringBuffer();
mappingBuilderLine.append(String.format("%s mapping = buildMapper()", getNameOfJavaType(SpringletsJavaType.SPRINGLETS_QUERYDSL_REPOSITORY_SUPPORT_ATTRIBUTE_BUILDER)));
if (!this.typesAreProjections.get(this.defaultReturnType)) {
// Return type is the same entity
Iterator<FieldMetadata> iterator = fields.iterator();
while (iterator.hasNext()) {
FieldMetadata field = iterator.next();
String fieldName = field.getFieldName().getSymbolName();
mappingBuilderLine.append(String.format("\n\t\t\t.map(%s, %s.%s)", getConstantForField(fieldName).getFieldName(), entityVariable, fieldName));
}
} else {
// Return type is a projection
List<Pair<String, String>> projectionFields = this.typesFieldMaps.get(this.defaultReturnType);
Iterator<Pair<String, String>> iterator = projectionFields.iterator();
while (iterator.hasNext()) {
Entry<String, String> entry = iterator.next();
mappingBuilderLine.append(String.format("\n\t\t\t.map(%s, %s)", getConstantForField(entry.getKey()).getFieldName(), entry.getValue()));
}
}
mappingBuilderLine.append(";");
bodyBuilder.appendFormalLine(mappingBuilderLine.toString());
bodyBuilder.newLine();
// applyPagination(pageable, query, mapping);
bodyBuilder.appendFormalLine(String.format("applyPagination(%s, query, mapping);", pageable));
// applyOrderById(query);
bodyBuilder.appendFormalLine("applyOrderById(query);");
bodyBuilder.newLine();
buildQueryResult(bodyBuilder, pageable, entityVariable, projection, this.defaultReturnType);
// Sets body to generated method
methodBuilder.setBodyBuilder(bodyBuilder);
// Build and return a MethodMetadata
return methodBuilder.build();
// instance
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class RepositoryJpaCustomImplMetadata method getCustomFindersImpl.
/**
* Method that generates implementation methods for each finder which return type
* is a projection or argument is a DTO.
*
* @param methodInfo
* @return
*/
private MethodMetadata getCustomFindersImpl(Pair<MethodMetadata, PartTree> methodInfo, List<FieldMetadata> fields) {
MethodMetadata method = methodInfo.getLeft();
// Define method name
JavaSymbolName methodName = method.getMethodName();
// Define method parameter types
List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
// Define method parameter names
List<JavaSymbolName> parameterNames = method.getParameterNames();
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Getting variable name to use in the code
JavaSymbolName globalSearch = getParameterNameFor(method, SpringletsJavaType.SPRINGLETS_GLOBAL_SEARCH);
JavaSymbolName pageable = getParameterNameFor(method, SpringJavaType.PAGEABLE);
String entity = this.entity.getSimpleTypeName();
String entityVariable = StringUtils.uncapitalize(entity);
JavaType finderParamType = parameterTypes.get(0).getJavaType();
String finderParamName = parameterNames.get(0).getSymbolName();
// Types to import
JavaType returnType = getDomainTypeOfFinderMethod(method);
bodyBuilder.newLine();
// QEntity qEntity = QEntity.entity;
bodyBuilder.appendFormalLine(String.format("%1$s %2$s = %1$s.%2$s;", getNameOfJavaType(entityQtype), entityVariable));
bodyBuilder.newLine();
// Construct query
buildQuery(bodyBuilder, entityVariable, globalSearch, null, null, null, finderParamType, finderParamName, returnType, method.getMethodName(), methodInfo.getRight());
bodyBuilder.newLine();
// AttributeMappingBuilder mapping = buildMapper()
StringBuffer mappingBuilderLine = new StringBuffer();
mappingBuilderLine.append(String.format("%s mapping = buildMapper()", getNameOfJavaType(SpringletsJavaType.SPRINGLETS_QUERYDSL_REPOSITORY_SUPPORT_ATTRIBUTE_BUILDER)));
// .map(entiyVarName, varName) ...
if (!this.typesAreProjections.get(returnType)) {
// Return type is the same entity
Iterator<FieldMetadata> iterator = fields.iterator();
while (iterator.hasNext()) {
FieldMetadata field = iterator.next();
String fieldName = field.getFieldName().getSymbolName();
mappingBuilderLine.append(String.format("\n\t\t\t.map(%s, %s.%s)", getConstantForField(fieldName).getFieldName(), entityVariable, fieldName));
}
} else {
// Return type is a projection
List<Pair<String, String>> projectionFields = this.typesFieldMaps.get(returnType);
Iterator<Pair<String, String>> iterator = projectionFields.iterator();
while (iterator.hasNext()) {
Entry<String, String> entry = iterator.next();
mappingBuilderLine.append(String.format("\n\t\t\t.map(%s, %s)", getConstantForField(entry.getKey()).getFieldName(), entry.getValue()));
}
}
mappingBuilderLine.append(";");
bodyBuilder.appendFormalLine(mappingBuilderLine.toString());
bodyBuilder.newLine();
// applyPagination(pageable, query, mapping);
bodyBuilder.appendFormalLine(String.format("applyPagination(%s, query, mapping);", pageable));
// applyOrderById(query);
bodyBuilder.appendFormalLine("applyOrderById(query);");
bodyBuilder.newLine();
buildQueryResult(bodyBuilder, pageable, entityVariable, QUERYDSL_PROJECTIONS, returnType);
// Use provided finder method to generate its implementation
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, methodInfo.getLeft().getReturnType(), parameterTypes, parameterNames, bodyBuilder);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class RepositoryJpaCustomImplMetadata method getFindAllImpl.
/**
* Method that generates the findAll implementation method
* @param findAllGlobalSearchMethod
* @param ids the entity id fields
* @param fields the entity fields to search for
*
* @return
*/
private MethodMetadata getFindAllImpl(MethodMetadata findAllGlobalSearchMethod, FieldMetadata idField, List<FieldMetadata> fields) {
// Define method name
JavaSymbolName methodName = findAllGlobalSearchMethod.getMethodName();
// Define method parameter types
List<AnnotatedJavaType> parameterTypes = findAllGlobalSearchMethod.getParameterTypes();
// Define method parameter names
List<JavaSymbolName> parameterNames = findAllGlobalSearchMethod.getParameterNames();
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Use provided findAll method to generate its implementation
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, findAllGlobalSearchMethod.getMethodName(), findAllGlobalSearchMethod.getReturnType(), findAllGlobalSearchMethod.getParameterTypes(), findAllGlobalSearchMethod.getParameterNames(), null);
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Getting variable name to use in the code
JavaSymbolName globalSearch = parameterNames.get(0);
JavaSymbolName pageable = parameterNames.get(1);
String entity = this.entity.getSimpleTypeName();
String entityVariable = StringUtils.uncapitalize(entity);
// Types to import
JavaType projection = QUERYDSL_PROJECTIONS;
bodyBuilder.newLine();
// QEntity qEntity = QEntity.entity;
bodyBuilder.appendFormalLine(String.format("%1$s %2$s = %1$s.%2$s;", entityQtype.getNameIncludingTypeParameters(false, importResolver), entityVariable));
bodyBuilder.newLine();
// Construct query
buildQuery(bodyBuilder, entityVariable, globalSearch, null, null, null, null, null, this.defaultReturnType, null, null);
bodyBuilder.newLine();
// AttributeMappingBuilder mapping = buildMapper()
StringBuffer mappingBuilderLine = new StringBuffer();
mappingBuilderLine.append(String.format("%s mapping = buildMapper()", getNameOfJavaType(SpringletsJavaType.SPRINGLETS_QUERYDSL_REPOSITORY_SUPPORT_ATTRIBUTE_BUILDER)));
if (!this.typesAreProjections.get(this.defaultReturnType)) {
// Return type is the same entity
Iterator<FieldMetadata> iterator = fields.iterator();
while (iterator.hasNext()) {
FieldMetadata field = iterator.next();
String fieldName = field.getFieldName().getSymbolName();
mappingBuilderLine.append(String.format("\n\t\t\t.map(%s, %s.%s)", getConstantForField(fieldName).getFieldName(), entityVariable, fieldName));
}
} else {
// Return type is a projection
List<Pair<String, String>> projectionFields = this.typesFieldMaps.get(this.defaultReturnType);
Iterator<Pair<String, String>> iterator = projectionFields.iterator();
while (iterator.hasNext()) {
Entry<String, String> entry = iterator.next();
mappingBuilderLine.append(String.format("\n\t\t\t.map(%s, %s)", getConstantForField(entry.getKey()).getFieldName(), entry.getValue()));
}
}
mappingBuilderLine.append(";");
bodyBuilder.appendFormalLine(mappingBuilderLine.toString());
bodyBuilder.newLine();
// applyPagination(pageable, query, mapping);
bodyBuilder.appendFormalLine(String.format("applyPagination(%s, query, mapping);", pageable));
// applyOrderById(query);
bodyBuilder.appendFormalLine("applyOrderById(query);");
bodyBuilder.newLine();
buildQueryResult(bodyBuilder, pageable, entityVariable, projection, this.defaultReturnType);
// Sets body to generated method
methodBuilder.setBodyBuilder(bodyBuilder);
// Build and return a MethodMetadata
return methodBuilder.build();
// instance
}
Aggregations