use of org.springframework.roo.project.LogicalPath in project spring-roo by spring-projects.
the class TypeManagementServiceImpl method addField.
public void addField(final FieldMetadata field, boolean evict) {
Validate.notNull(field, "Field metadata not provided");
// Obtain the physical type and ITD mutable details
PhysicalTypeMetadata ptm = null;
if (evict) {
ptm = (PhysicalTypeMetadata) metadataService.evictAndGet(field.getDeclaredByMetadataId());
} else {
ptm = (PhysicalTypeMetadata) metadataService.get(field.getDeclaredByMetadataId());
}
Validate.notNull(ptm, "Java source code unavailable for type %s", PhysicalTypeIdentifier.getFriendlyName(field.getDeclaredByMetadataId()));
final PhysicalTypeDetails ptd = ptm.getMemberHoldingTypeDetails();
Validate.notNull(ptd, "Java source code details unavailable for type %s", PhysicalTypeIdentifier.getFriendlyName(field.getDeclaredByMetadataId()));
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder((ClassOrInterfaceTypeDetails) ptd);
// Automatically add JSR 303 (Bean Validation API) support if there is
// no current JSR 303 support but a JSR 303 annotation is present
boolean jsr303Required = false;
for (final AnnotationMetadata annotation : field.getAnnotations()) {
if (annotation.getAnnotationType().getFullyQualifiedTypeName().startsWith("javax.validation")) {
jsr303Required = true;
break;
}
}
final LogicalPath path = PhysicalTypeIdentifier.getPath(cidBuilder.getDeclaredByMetadataId());
if (jsr303Required) {
// It's more likely the version below represents a later version
// than any specified in the user's own dependency list
projectOperations.addDependency(path.getModule(), new Dependency("javax.validation", "validation-api", null));
}
cidBuilder.addField(field);
createOrUpdateTypeOnDisk(cidBuilder.build());
}
use of org.springframework.roo.project.LogicalPath in project spring-roo by spring-projects.
the class JspMetadataListener method get.
public MetadataItem get(final String jspMetadataId) {
// Work out the MIDs of the other metadata we depend on
// NB: The JavaType and Path are to the corresponding web scaffold
// controller class
final String webScaffoldMetadataKey = WebScaffoldMetadata.createIdentifier(JspMetadata.getJavaType(jspMetadataId), JspMetadata.getPath(jspMetadataId));
final WebScaffoldMetadata webScaffoldMetadata = (WebScaffoldMetadata) getMetadataService().get(webScaffoldMetadataKey);
if (webScaffoldMetadata == null || !webScaffoldMetadata.isValid()) {
// to manage any JSPs at this time
return null;
}
final JavaType formBackingType = webScaffoldMetadata.getAnnotationValues().getFormBackingObject();
final MemberDetails memberDetails = getWebMetadataService().getMemberDetails(formBackingType);
final JavaTypeMetadataDetails formBackingTypeMetadataDetails = getWebMetadataService().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 = getWebMetadataService().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 = getTypeLocationService().getTypeDetails(formBackingType);
final LogicalPath formBackingTypePath = PhysicalTypeIdentifier.getPath(formBackingTypeDetails.getDeclaredByMetadataId());
getMetadataDependencyRegistry().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 = getProjectOperations().getPathResolver();
final LogicalPath webappPath = LogicalPath.getInstance(Path.SRC_MAIN_WEBAPP, path.getModule());
if (!getFileManager().exists(pathResolver.getIdentifier(webappPath, WEB_INF_VIEWS))) {
getJspOperations().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 = getWebMetadataService().getScaffoldEligibleFieldMetadata(formBackingType, memberDetails, jspMetadataId);
if (eligibleFields.isEmpty() && formBackingTypePersistenceMetadata.getRooIdentifierFields().isEmpty()) {
return null;
}
final JspViewManager viewManager = new JspViewManager(eligibleFields, webScaffoldMetadata.getAnnotationValues(), relatedTypeMd);
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 (!getFileManager().exists(destinationDirectory)) {
getFileManager().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";
getXmlRoundTripFileManager().writeToDiskIfNecessary(listPath1, viewManager.getListDocument());
getTilesOperations().addViewDefinition(controllerPath, webappPath, controllerPath + "/" + "list", TilesOperations.DEFAULT_TEMPLATE, WEB_INF_VIEWS + controllerPath + "/list.jspx");
final String showPath = destinationDirectory + "/show.jspx";
getXmlRoundTripFileManager().writeToDiskIfNecessary(showPath, viewManager.getShowDocument());
getTilesOperations().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";
getXmlRoundTripFileManager().writeToDiskIfNecessary(listPath, viewManager.getCreateDocument());
// Add 'create new' menu item
getMenuOperations().addMenuItem(categoryName, newMenuItemId, "global_menu_new", "/" + controllerPath + "?form", MenuOperations.DEFAULT_MENU_ITEM_PREFIX, webappPath);
getTilesOperations().addViewDefinition(controllerPath, webappPath, controllerPath + "/" + "create", TilesOperations.DEFAULT_TEMPLATE, WEB_INF_VIEWS + controllerPath + "/create.jspx");
} else {
getMenuOperations().cleanUpMenuItem(categoryName, new JavaSymbolName("new"), MenuOperations.DEFAULT_MENU_ITEM_PREFIX, webappPath);
getTilesOperations().removeViewDefinition(controllerPath + "/" + "create", controllerPath, webappPath);
}
if (webScaffoldMetadata.getAnnotationValues().isUpdate()) {
final String listPath = destinationDirectory + "/update.jspx";
getXmlRoundTripFileManager().writeToDiskIfNecessary(listPath, viewManager.getUpdateDocument());
getTilesOperations().addViewDefinition(controllerPath, webappPath, controllerPath + "/" + "update", TilesOperations.DEFAULT_TEMPLATE, WEB_INF_VIEWS + controllerPath + "/update.jspx");
} else {
getTilesOperations().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.getFieldForJavaBeanMethod(memberDetails, method);
if (field == null) {
continue;
}
final JavaSymbolName fieldName = field.getFieldName();
final String fieldResourceId = XmlUtils.convertId(resourceId + "." + fieldName.getSymbolName().toLowerCase());
if (getTypeLocationService().isInProject(method.getReturnType()) && getWebMetadataService().isRooIdentifier(method.getReturnType(), getWebMetadataService().getMemberDetails(method.getReturnType()))) {
final JavaTypePersistenceMetadataDetails typePersistenceMetadataDetails = getWebMetadataService().getJavaTypePersistenceMetadataDetails(method.getReturnType(), getWebMetadataService().getMemberDetails(method.getReturnType()), jspMetadataId);
if (typePersistenceMetadataDetails != null) {
for (final FieldMetadata f : typePersistenceMetadataDetails.getRooIdentifierFields()) {
final String sb = f.getFieldName().getReadableSymbolName();
properties.put(XmlUtils.convertId(resourceId + "." + javaTypePersistenceMetadataDetails.getIdentifierField().getFieldName().getSymbolName() + "." + f.getFieldName().getSymbolName().toLowerCase()), StringUtils.isNotBlank(sb) ? sb : fieldName.getSymbolName());
}
}
} else if (!method.getMethodName().equals(javaTypePersistenceMetadataDetails.getIdentifierAccessorMethod().getMethodName()) || javaTypePersistenceMetadataDetails.getVersionAccessorMethod() != null && !method.getMethodName().equals(javaTypePersistenceMetadataDetails.getVersionAccessorMethod().getMethodName())) {
final String sb = fieldName.getReadableSymbolName();
properties.put(fieldResourceId, StringUtils.isNotBlank(sb) ? sb : fieldName.getSymbolName());
}
}
if (javaTypePersistenceMetadataDetails.getFindAllMethod() != null) {
// Add 'list all' menu item
final JavaSymbolName listMenuItemId = new JavaSymbolName("list");
getMenuOperations().addMenuItem(categoryName, listMenuItemId, "global_menu_list", "/" + controllerPath + "?page=1&size=${empty param.size ? 10 : param.size}", MenuOperations.DEFAULT_MENU_ITEM_PREFIX, webappPath);
properties.put("menu_item_" + categoryName.getSymbolName().toLowerCase() + "_" + listMenuItemId.getSymbolName().toLowerCase() + "_label", new JavaSymbolName(plural).getReadableSymbolName());
} else {
getMenuOperations().cleanUpMenuItem(categoryName, new JavaSymbolName("list"), MenuOperations.DEFAULT_MENU_ITEM_PREFIX, webappPath);
}
final String controllerPhysicalTypeId = PhysicalTypeIdentifier.createIdentifier(JspMetadata.getJavaType(jspMetadataId), JspMetadata.getPath(jspMetadataId));
final PhysicalTypeMetadata controllerPhysicalTypeMd = (PhysicalTypeMetadata) getMetadataService().get(controllerPhysicalTypeId);
if (controllerPhysicalTypeMd == null) {
return null;
}
final MemberHoldingTypeDetails mhtd = controllerPhysicalTypeMd.getMemberHoldingTypeDetails();
if (mhtd == null) {
return null;
}
final List<String> allowedMenuItems = new ArrayList<String>();
if (MemberFindingUtils.getAnnotationOfType(mhtd.getAnnotations(), RooJavaType.ROO_WEB_FINDER) != null) {
// This controller is annotated with @RooWebFinder
final Set<FinderMetadataDetails> finderMethodsDetails = getWebMetadataService().getDynamicFinderMethodsAndFields(formBackingType, memberDetails, jspMetadataId);
if (finderMethodsDetails == null) {
return null;
}
for (final FinderMetadataDetails finderDetails : finderMethodsDetails) {
final String finderName = finderDetails.getFinderMethodMetadata().getMethodName().getSymbolName();
final String listPath = destinationDirectory + "/" + finderName + ".jspx";
// long (see ROO-1027)
if (listPath.length() > 244) {
continue;
}
getXmlRoundTripFileManager().writeToDiskIfNecessary(listPath, viewManager.getFinderDocument(finderDetails));
final JavaSymbolName finderLabel = new JavaSymbolName(finderName.replace("find" + plural + "By", ""));
// Add 'Find by' menu item
getMenuOperations().addMenuItem(categoryName, finderLabel, "global_menu_find", "/" + controllerPath + "?find=" + finderName.replace("find" + plural, "") + "&form" + "&page=1&size=${empty param.size ? 10 : param.size}", MenuOperations.FINDER_MENU_ITEM_PREFIX, webappPath);
properties.put("menu_item_" + categoryName.getSymbolName().toLowerCase() + "_" + finderLabel.getSymbolName().toLowerCase() + "_label", finderLabel.getReadableSymbolName());
allowedMenuItems.add(MenuOperations.FINDER_MENU_ITEM_PREFIX + categoryName.getSymbolName().toLowerCase() + "_" + finderLabel.getSymbolName().toLowerCase());
for (final JavaSymbolName paramName : finderDetails.getFinderMethodMetadata().getParameterNames()) {
properties.put(XmlUtils.convertId(resourceId + "." + paramName.getSymbolName().toLowerCase()), paramName.getReadableSymbolName());
}
getTilesOperations().addViewDefinition(controllerPath, webappPath, controllerPath + "/" + finderName, TilesOperations.DEFAULT_TEMPLATE, WEB_INF_VIEWS + controllerPath + "/" + finderName + ".jspx");
}
}
getMenuOperations().cleanUpFinderMenuItems(categoryName, allowedMenuItems, webappPath);
return new JspMetadata(jspMetadataId, webScaffoldMetadata);
}
use of org.springframework.roo.project.LogicalPath in project spring-roo by spring-projects.
the class JspOperationsImpl method installCommonViewArtefacts.
public void installCommonViewArtefacts(final String moduleName) {
Validate.isTrue(isProjectAvailable(), "Project metadata required");
final LogicalPath webappPath = Path.SRC_MAIN_WEBAPP.getModulePathId(moduleName);
// Install servers maven plugin
installMavenPlugins(moduleName);
// Install tiles config
updateConfiguration();
// Install styles
copyDirectoryContents("images/*.*", getPathResolver().getIdentifier(webappPath, "images"), false);
// Install styles
copyDirectoryContents("styles/*.css", getPathResolver().getIdentifier(webappPath, "styles"), false);
copyDirectoryContents("styles/*.properties", getPathResolver().getIdentifier(webappPath, "WEB-INF/classes"), false);
// Install layout
copyDirectoryContents("tiles/default.jspx", getPathResolver().getIdentifier(webappPath, "WEB-INF/layouts/"), false);
copyDirectoryContents("tiles/layouts.xml", getPathResolver().getIdentifier(webappPath, "WEB-INF/layouts/"), false);
copyDirectoryContents("tiles/header.jspx", getPathResolver().getIdentifier(webappPath, "WEB-INF/views/"), false);
copyDirectoryContents("tiles/menu.jspx", getPathResolver().getIdentifier(webappPath, "WEB-INF/views/"), false);
copyDirectoryContents("tiles/footer.jspx", getPathResolver().getIdentifier(webappPath, "WEB-INF/views/"), false);
copyDirectoryContents("tiles/views.xml", getPathResolver().getIdentifier(webappPath, "WEB-INF/views/"), false);
// Install common view files
copyDirectoryContents("*.jspx", getPathResolver().getIdentifier(webappPath, "WEB-INF/views/"), false);
// Install tags
copyDirectoryContents("tags/form/*.tagx", getPathResolver().getIdentifier(webappPath, "WEB-INF/tags/form"), false);
copyDirectoryContents("tags/form/fields/*.tagx", getPathResolver().getIdentifier(webappPath, "WEB-INF/tags/form/fields"), false);
copyDirectoryContents("tags/menu/*.tagx", getPathResolver().getIdentifier(webappPath, "WEB-INF/tags/menu"), false);
copyDirectoryContents("tags/util/*.tagx", getPathResolver().getIdentifier(webappPath, "WEB-INF/tags/util"), false);
// TODO: Rebuild with new views system
// Install default language 'en'
// installI18n(getI18nSupport().getLanguage(Locale.ENGLISH), webappPath);
// final String i18nDirectory =
// getPathResolver().getIdentifier(webappPath, "WEB-INF/i18n/application.properties");
// if (!fileManager.exists(i18nDirectory)) {
// try {
// final String projectName =
// getProjectOperations().getProjectName(getProjectOperations().getFocusedModuleName());
// fileManager.createFile(getPathResolver().getIdentifier(webappPath,
// "WEB-INF/i18n/application.properties"));
// /*getPropFileOperations()
// .addPropertyIfNotExists(webappPath,
// "WEB-INF/i18n/application.properties",
// "application_name",
// projectName.substring(0, 1).toUpperCase()
// + projectName.substring(1), true);*/
// } catch (final Exception e) {
// throw new IllegalStateException(
// "Encountered an error during copying of resources for MVC JSP addon.", e);
// }
// }
}
use of org.springframework.roo.project.LogicalPath in project spring-roo by spring-projects.
the class ControllerMetadataProviderImpl method getGovernorPhysicalTypeIdentifier.
@Override
protected String getGovernorPhysicalTypeIdentifier(final String metadataIdentificationString) {
final JavaType javaType = ControllerMetadata.getJavaType(metadataIdentificationString);
final LogicalPath path = ControllerMetadata.getPath(metadataIdentificationString);
return PhysicalTypeIdentifier.createIdentifier(javaType, path);
}
use of org.springframework.roo.project.LogicalPath in project spring-roo by spring-projects.
the class ControllerOperationsImpl method createJsonDeserializersIfDontExists.
/**
* Create the Json Desearializer utility class (annotated
* with @RooDeserializer) for target Entity and its related entities if they
* aren't created yet.
*
* @param entity
* @param module
* @param controllerPackage
*/
private void createJsonDeserializersIfDontExists(JavaType currentEntity, String module, JavaPackage controllerPackage) {
List<JavaType> entitiesToCreateSerializers = getParentAndChildrenRelatedEntities(currentEntity);
// Check if already exists a serializer for each entity
Set<ClassOrInterfaceTypeDetails> allDeserializer = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_DESERIALIZER);
for (JavaType entity : entitiesToCreateSerializers) {
EntityDeserializerAnnotationValues values;
boolean deserializerFound = false;
for (ClassOrInterfaceTypeDetails deserializer : allDeserializer) {
values = new EntityDeserializerAnnotationValues(deserializer);
if (entity.equals(values.getEntity())) {
// Found mixing. Nothing to do.
deserializerFound = true;
}
}
if (!deserializerFound) {
// Not found deserializer. Create it
ClassOrInterfaceTypeDetails serviceDetails = getServiceLocator().getService(entity);
Validate.notNull(serviceDetails, "Can't found service for Entity %s to generate " + "Serializer. If it is a related entity with the one to generate " + "controller, it needs a service.", entity.getFullyQualifiedTypeName());
// Build @RooDeserializer
List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
annotations = new ArrayList<AnnotationMetadataBuilder>();
AnnotationMetadataBuilder deserializerAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_DESERIALIZER);
deserializerAnnotation.addClassAttribute("entity", entity);
annotations.add(deserializerAnnotation);
JavaType deserializerClass = new JavaType(String.format("%s.%sDeserializer", controllerPackage.getFullyQualifiedPackageName(), entity.getSimpleTypeName()), module);
final LogicalPath deserializerPath = getPathResolver().getPath(module, Path.SRC_MAIN_JAVA);
final String resourceIdentifierItem = getTypeLocationService().getPhysicalTypeCanonicalPath(deserializerClass, deserializerPath);
final String declaredByMetadataIdItem = PhysicalTypeIdentifier.createIdentifier(deserializerClass, getPathResolver().getPath(resourceIdentifierItem));
ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataIdItem, Modifier.PUBLIC, deserializerClass, PhysicalTypeCategory.CLASS);
cidBuilder.setAnnotations(annotations);
/*
* Moved extend to java (instead ITD) because there were
* compilation problems when Mixin
* uses @JsonDeserialize(using=EntityDeserializer.class)
* annotation (requires extend of JsonDeseralizer)
*/
cidBuilder.addExtendsTypes(JavaType.wrapperOf(JSON_OBJECT_DESERIALIZER, entity));
FieldMetadata serviceField = EntityDeserializerMetadata.getFieldFor(declaredByMetadataIdItem, serviceDetails.getType());
FieldMetadata conversionServiceField = EntityDeserializerMetadata.getFieldFor(declaredByMetadataIdItem, SpringJavaType.CONVERSION_SERVICE);
cidBuilder.addField(serviceField);
cidBuilder.addField(conversionServiceField);
ConstructorMetadata constructor = EntityDeserializerMetadata.getConstructor(declaredByMetadataIdItem, serviceField, conversionServiceField);
cidBuilder.addConstructor(constructor);
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
}
}
}
Aggregations