Search in sources :

Example 11 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class SerializableMetadata method createSerialVersionField.

/**
 * Generates a field to store the serialization ID
 *
 * @return a non-<code>null</code> field
 */
private FieldMetadataBuilder createSerialVersionField() {
    final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(getId(), PRIVATE | STATIC | FINAL, SERIAL_VERSION_FIELD, LONG_PRIMITIVE, DEFAULT_SERIAL_VERSION);
    fieldBuilder.getCustomData().put(CustomDataKeys.SERIAL_VERSION_UUID_FIELD, true);
    return fieldBuilder;
}
Also used : FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 12 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class JpaOperationsImpl method getVersionField.

/**
 * This method generates the version field using the provided values
 *
 * @param entity
 * @param versionField
 * @param versionType
 * @param versionColumn
 * @return
 */
private FieldMetadata getVersionField(final JavaType entity, String versionField, final JavaType versionType, final String versionColumn) {
    if (StringUtils.isEmpty(versionField)) {
        versionField = "version";
    }
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    annotations.add(new AnnotationMetadataBuilder(VERSION));
    if (StringUtils.isNotEmpty(versionColumn)) {
        final AnnotationMetadataBuilder columnBuilder = new AnnotationMetadataBuilder(COLUMN);
        columnBuilder.addStringAttribute("name", versionColumn);
        annotations.add(columnBuilder);
    }
    FieldDetails versionFieldDetails = new FieldDetails(getTypeLocationService().getPhysicalTypeIdentifier(entity), versionType, new JavaSymbolName(versionField));
    versionFieldDetails.setModifiers(Modifier.PRIVATE);
    versionFieldDetails.addAnnotations(annotations);
    return new FieldMetadataBuilder(versionFieldDetails).build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) FieldDetails(org.springframework.roo.classpath.details.FieldDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 13 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class JpaEntityMetadata method getIterableToRemoveCantBeNullConstant.

/**
 * This method obtains the constant that contains the message
 * "The given Iterable of items to remove can't be null!".
 *
 * @return name of the generated constant
 */
private FieldMetadata getIterableToRemoveCantBeNullConstant() {
    // Check if iterableCantBeNullConstant already exists
    if (iterableToRemoveCantBeNullConstant != null) {
        return iterableToRemoveCantBeNullConstant;
    }
    // If not exists, generate a new one and include into builder
    FieldMetadataBuilder constant = new FieldMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.STATIC + Modifier.FINAL, new JavaSymbolName("ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE"), JavaType.STRING, "\"The given Iterable of items to add can't be null!\"");
    iterableToRemoveCantBeNullConstant = constant.build();
    return iterableToRemoveCantBeNullConstant;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 14 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class NewUpdateCompilationUnitTest method testSimpleClassAddField.

@Test
public void testSimpleClassAddField() throws Exception {
    // Set up
    final File file = getResource(SIMPLE_CLASS_FILE_PATH);
    final String fileContents = getResourceContents(file);
    final ClassOrInterfaceTypeDetails simpleInterfaceDetails = typeParsingService.getTypeFromString(fileContents, SIMPLE_CLASS_DECLARED_BY_MID, SIMPLE_CLASS_TYPE);
    final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(SIMPLE_CLASS_DECLARED_BY_MID, Modifier.PRIVATE, new JavaSymbolName("newFieldAddedByCode"), new JavaType(String.class), "\"Create by code\"");
    final ClassOrInterfaceTypeDetails newSimpleInterfaceDetails = addField(simpleInterfaceDetails, fieldBuilder.build());
    // Invoke
    final String result = typeParsingService.updateAndGetCompilationUnitContents(file.getCanonicalPath(), newSimpleInterfaceDetails);
    saveResult(file, result, "-addedField");
    checkSimpleClass(result);
    assertTrue(result.contains("private String newFieldAddedByCode = \"Create by code\";"));
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) File(java.io.File) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) Test(org.junit.Test)

Example 15 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class SeleniumOperationsImpl method generateTest.

/**
 * Creates a new Selenium testcase
 *
 * @param controller the JavaType of the controller under test (required)
 * @param name the name of the test case (optional)
 * @param serverURL the URL of the Selenium server (optional)
 */
@Override
public void generateTest(final JavaType controller, String name, String serverURL) {
    Validate.notNull(controller, "Controller type required");
    final ClassOrInterfaceTypeDetails controllerTypeDetails = typeLocationService.getTypeDetails(controller);
    Validate.notNull(controllerTypeDetails, "Class or interface type details for type '%s' could not be resolved", controller);
    final LogicalPath path = PhysicalTypeIdentifier.getPath(controllerTypeDetails.getDeclaredByMetadataId());
    final String webScaffoldMetadataIdentifier = WebScaffoldMetadata.createIdentifier(controller, path);
    final WebScaffoldMetadata webScaffoldMetadata = (WebScaffoldMetadata) metadataService.get(webScaffoldMetadataIdentifier);
    Validate.notNull(webScaffoldMetadata, "Web controller '%s' does not appear to be an automatic, scaffolded controller", controller.getFullyQualifiedTypeName());
    // allow the creation of new instances for the form backing object
    if (!webScaffoldMetadata.getAnnotationValues().isCreate()) {
        LOGGER.warning("The controller you specified does not allow the creation of new instances of the form backing object. No Selenium tests created.");
        return;
    }
    if (!serverURL.endsWith("/")) {
        serverURL = serverURL + "/";
    }
    final JavaType formBackingType = webScaffoldMetadata.getAnnotationValues().getFormBackingObject();
    final String relativeTestFilePath = "selenium/test-" + formBackingType.getSimpleTypeName().toLowerCase() + ".xhtml";
    final String seleniumPath = pathResolver.getFocusedIdentifier(Path.SRC_MAIN_WEBAPP, relativeTestFilePath);
    final InputStream templateInputStream = FileUtils.getInputStream(getClass(), "selenium-template.xhtml");
    Validate.notNull(templateInputStream, "Could not acquire selenium.xhtml template");
    final Document document = XmlUtils.readXml(templateInputStream);
    final Element root = (Element) document.getLastChild();
    if (root == null || !"html".equals(root.getNodeName())) {
        throw new IllegalArgumentException("Could not parse selenium test case template file!");
    }
    name = name != null ? name : "Selenium test for " + controller.getSimpleTypeName();
    XmlUtils.findRequiredElement("/html/head/title", root).setTextContent(name);
    XmlUtils.findRequiredElement("/html/body/table/thead/tr/td", root).setTextContent(name);
    final Element tbody = XmlUtils.findRequiredElement("/html/body/table/tbody", root);
    tbody.appendChild(openCommand(document, serverURL + projectOperations.getProjectName(projectOperations.getFocusedModuleName()) + "/" + webScaffoldMetadata.getAnnotationValues().getPath() + "?form"));
    final ClassOrInterfaceTypeDetails formBackingTypeDetails = typeLocationService.getTypeDetails(formBackingType);
    Validate.notNull(formBackingType, "Class or interface type details for type '%s' could not be resolved", formBackingType);
    final MemberDetails memberDetails = memberDetailsScanner.getMemberDetails(getClass().getName(), formBackingTypeDetails);
    // Add composite PK identifier fields if needed
    for (final FieldMetadata field : persistenceMemberLocator.getEmbeddedIdentifierFields(formBackingType)) {
        final JavaType fieldType = field.getFieldType();
        if (!fieldType.isCommonCollectionType() && !isSpecialType(fieldType)) {
            final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(field);
            final String fieldName = field.getFieldName().getSymbolName();
            fieldBuilder.setFieldName(new JavaSymbolName(fieldName + "." + fieldName));
            tbody.appendChild(typeCommand(document, fieldBuilder.build()));
        }
    }
    // Add all other fields
    final List<FieldMetadata> fields = webMetadataService.getScaffoldEligibleFieldMetadata(formBackingType, memberDetails, null);
    for (final FieldMetadata field : fields) {
        final JavaType fieldType = field.getFieldType();
        if (!fieldType.isCommonCollectionType() && !isSpecialType(fieldType)) {
            tbody.appendChild(typeCommand(document, field));
        }
    }
    tbody.appendChild(clickAndWaitCommand(document, "//input[@id = 'proceed']"));
    // Add verifications for all other fields
    for (final FieldMetadata field : fields) {
        final JavaType fieldType = field.getFieldType();
        if (!fieldType.isCommonCollectionType() && !isSpecialType(fieldType)) {
            tbody.appendChild(verifyTextCommand(document, formBackingType, field));
        }
    }
    fileManager.createOrUpdateTextFileIfRequired(seleniumPath, XmlUtils.nodeToString(document), false);
    manageTestSuite(relativeTestFilePath, name, serverURL);
    // Install selenium-maven-plugin
    installMavenPlugin();
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) LogicalPath(org.springframework.roo.project.LogicalPath) Document(org.w3c.dom.Document) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) SpringJavaType(org.springframework.roo.model.SpringJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) WebScaffoldMetadata(org.springframework.roo.addon.web.mvc.controller.addon.scaffold.WebScaffoldMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails)

Aggregations

FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)66 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)48 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)40 ArrayList (java.util.ArrayList)21 JavaType (org.springframework.roo.model.JavaType)19 JdkJavaType (org.springframework.roo.model.JdkJavaType)10 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)8 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)7 FieldDetails (org.springframework.roo.classpath.details.FieldDetails)7 ForeignKey (org.springframework.roo.addon.dbre.addon.model.ForeignKey)5 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)5 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)5 LinkedHashMap (java.util.LinkedHashMap)4 Reference (org.springframework.roo.addon.dbre.addon.model.Reference)4 Table (org.springframework.roo.addon.dbre.addon.model.Table)4 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)4 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)4 CollectionField (org.springframework.roo.classpath.operations.jsr303.CollectionField)4 DateField (org.springframework.roo.classpath.operations.jsr303.DateField)4 EnumDetails (org.springframework.roo.model.EnumDetails)4