return null;
}
final JavaType formBackingType = webScaffoldMetadata
.getAnnotationValues().getFormBackingObject();
final MemberDetails memberDetails = webMetadataService
.getMemberDetails(formBackingType);
final JavaTypeMetadataDetails formBackingTypeMetadataDetails = webMetadataService
.getJavaTypeMetadataDetails(formBackingType, memberDetails,
jspMetadataId);
Validate.notNull(formBackingTypeMetadataDetails,
"Unable to obtain metadata for type %s",
formBackingType.getFullyQualifiedTypeName());
formBackingObjectTypesToLocalMids.put(formBackingType, jspMetadataId);
final SortedMap<JavaType, JavaTypeMetadataDetails> relatedTypeMd = webMetadataService
.getRelatedApplicationTypeMetadata(formBackingType,
memberDetails, jspMetadataId);
final JavaTypeMetadataDetails formbackingTypeMetadata = relatedTypeMd
.get(formBackingType);
Validate.notNull(formbackingTypeMetadata,
"Form backing type metadata required");
final JavaTypePersistenceMetadataDetails formBackingTypePersistenceMetadata = formbackingTypeMetadata
.getPersistenceDetails();
if (formBackingTypePersistenceMetadata == null) {
return null;
}
final ClassOrInterfaceTypeDetails formBackingTypeDetails = typeLocationService
.getTypeDetails(formBackingType);
final LogicalPath formBackingTypePath = PhysicalTypeIdentifier
.getPath(formBackingTypeDetails.getDeclaredByMetadataId());
metadataDependencyRegistry.registerDependency(PhysicalTypeIdentifier
.createIdentifier(formBackingType, formBackingTypePath),
JspMetadata.createIdentifier(formBackingType,
formBackingTypePath));
final LogicalPath path = JspMetadata.getPath(jspMetadataId);
// Install web artifacts only if Spring MVC config is missing
// TODO: Remove this call when 'controller' commands are gone
final PathResolver pathResolver = projectOperations.getPathResolver();
final LogicalPath webappPath = LogicalPath.getInstance(
Path.SRC_MAIN_WEBAPP, path.getModule());
if (!fileManager.exists(pathResolver.getIdentifier(webappPath,
WEB_INF_VIEWS))) {
jspOperations.installCommonViewArtefacts(path.getModule());
}
installImage(webappPath, "images/show.png");
if (webScaffoldMetadata.getAnnotationValues().isUpdate()) {
installImage(webappPath, "images/update.png");
}
if (webScaffoldMetadata.getAnnotationValues().isDelete()) {
installImage(webappPath, "images/delete.png");
}
final List<FieldMetadata> eligibleFields = webMetadataService
.getScaffoldEligibleFieldMetadata(formBackingType,
memberDetails, jspMetadataId);
if (eligibleFields.isEmpty()
&& formBackingTypePersistenceMetadata.getRooIdentifierFields()
.isEmpty()) {
return null;
}
final JspViewManager viewManager = new JspViewManager(eligibleFields,
webScaffoldMetadata.getAnnotationValues(), relatedTypeMd, typeLocationService);
String controllerPath = webScaffoldMetadata.getAnnotationValues()
.getPath();
if (controllerPath.startsWith("/")) {
controllerPath = controllerPath.substring(1);
}
// Make the holding directory for this controller
final String destinationDirectory = pathResolver.getIdentifier(
webappPath, WEB_INF_VIEWS + controllerPath);
if (!fileManager.exists(destinationDirectory)) {
fileManager.createDirectory(destinationDirectory);
}
else {
final File file = new File(destinationDirectory);
Validate.isTrue(file.isDirectory(),
"%s is a file, when a directory was expected",
destinationDirectory);
}
// By now we have a directory to put the JSPs inside
final String listPath1 = destinationDirectory + "/list.jspx";
xmlRoundTripFileManager.writeToDiskIfNecessary(listPath1,
viewManager.getListDocument());
tilesOperations.addViewDefinition(controllerPath, webappPath,
controllerPath + "/" + "list",
TilesOperations.DEFAULT_TEMPLATE, WEB_INF_VIEWS
+ controllerPath + "/list.jspx");
final String showPath = destinationDirectory + "/show.jspx";
xmlRoundTripFileManager.writeToDiskIfNecessary(showPath,
viewManager.getShowDocument());
tilesOperations.addViewDefinition(controllerPath, webappPath,
controllerPath + "/" + "show",
TilesOperations.DEFAULT_TEMPLATE, WEB_INF_VIEWS
+ controllerPath + "/show.jspx");
final Map<String, String> properties = new LinkedHashMap<String, String>();
final JavaSymbolName categoryName = new JavaSymbolName(
formBackingType.getSimpleTypeName());
properties.put("menu_category_"
+ categoryName.getSymbolName().toLowerCase() + "_label",
categoryName.getReadableSymbolName());
final JavaSymbolName newMenuItemId = new JavaSymbolName("new");
properties.put("menu_item_"
+ categoryName.getSymbolName().toLowerCase() + "_"
+ newMenuItemId.getSymbolName().toLowerCase() + "_label",
new JavaSymbolName(formBackingType.getSimpleTypeName())
.getReadableSymbolName());
if (webScaffoldMetadata.getAnnotationValues().isCreate()) {
final String listPath = destinationDirectory + "/create.jspx";
xmlRoundTripFileManager.writeToDiskIfNecessary(listPath,
viewManager.getCreateDocument());
// Add 'create new' menu item
menuOperations.addMenuItem(categoryName, newMenuItemId,
"global_menu_new", "/" + controllerPath + "?form",
MenuOperations.DEFAULT_MENU_ITEM_PREFIX, webappPath);
tilesOperations.addViewDefinition(controllerPath, webappPath,
controllerPath + "/" + "create",
TilesOperations.DEFAULT_TEMPLATE, WEB_INF_VIEWS
+ controllerPath + "/create.jspx");
}
else {
menuOperations
.cleanUpMenuItem(categoryName, new JavaSymbolName("new"),
MenuOperations.DEFAULT_MENU_ITEM_PREFIX, webappPath);
tilesOperations.removeViewDefinition(controllerPath + "/"
+ "create", controllerPath, webappPath);
}
if (webScaffoldMetadata.getAnnotationValues().isUpdate()) {
final String listPath = destinationDirectory + "/update.jspx";
xmlRoundTripFileManager.writeToDiskIfNecessary(listPath,
viewManager.getUpdateDocument());
tilesOperations.addViewDefinition(controllerPath, webappPath,
controllerPath + "/" + "update",
TilesOperations.DEFAULT_TEMPLATE, WEB_INF_VIEWS
+ controllerPath + "/update.jspx");
}
else {
tilesOperations.removeViewDefinition(controllerPath + "/"
+ "update", controllerPath, webappPath);
}
// Setup labels for i18n support
final String resourceId = XmlUtils.convertId("label."
+ formBackingType.getFullyQualifiedTypeName().toLowerCase());
properties.put(resourceId,
new JavaSymbolName(formBackingType.getSimpleTypeName())
.getReadableSymbolName());
final String pluralResourceId = XmlUtils.convertId(resourceId
+ ".plural");
final String plural = formBackingTypeMetadataDetails.getPlural();
properties.put(pluralResourceId,
new JavaSymbolName(plural).getReadableSymbolName());
final JavaTypePersistenceMetadataDetails javaTypePersistenceMetadataDetails = formBackingTypeMetadataDetails
.getPersistenceDetails();
Validate.notNull(javaTypePersistenceMetadataDetails,
"Unable to determine persistence metadata for type %s",
formBackingType.getFullyQualifiedTypeName());
if (!javaTypePersistenceMetadataDetails.getRooIdentifierFields()
.isEmpty()) {
for (final FieldMetadata idField : javaTypePersistenceMetadataDetails
.getRooIdentifierFields()) {
properties.put(
XmlUtils.convertId(resourceId
+ "."
+ javaTypePersistenceMetadataDetails
.getIdentifierField().getFieldName()
.getSymbolName()
+ "."
+ idField.getFieldName().getSymbolName()
.toLowerCase()), idField.getFieldName()
.getReadableSymbolName());
}
}
// If no auto generated value for id, put name in i18n
if (javaTypePersistenceMetadataDetails.getIdentifierField()
.getAnnotation(JpaJavaType.GENERATED_VALUE) == null) {
properties.put(
XmlUtils.convertId(resourceId
+ "."
+ javaTypePersistenceMetadataDetails
.getIdentifierField().getFieldName()
.getSymbolName().toLowerCase()),
javaTypePersistenceMetadataDetails.getIdentifierField()
.getFieldName().getReadableSymbolName());
}
for (final MethodMetadata method : memberDetails.getMethods()) {
if (!BeanInfoUtils.isAccessorMethod(method)) {
continue;
}
final FieldMetadata field = BeanInfoUtils