Search in sources :

Example 46 with FieldMetadataBuilder

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

the class IdentifierMetadata method getAccessors.

/**
 * Locates the accessor methods.
 * <p>
 * If {@link #getFieldBuilders()} returns fields created by this ITD, public
 * accessors will automatically be produced in the declaring class.
 *
 * @param fields
 * @return the accessors (never returns null)
 */
private List<MethodMetadataBuilder> getAccessors(final List<FieldMetadataBuilder> fields) {
    final List<MethodMetadataBuilder> accessors = new ArrayList<MethodMetadataBuilder>();
    // Compute the names of the accessors that will be produced
    for (final FieldMetadataBuilder field : fields) {
        final JavaSymbolName requiredAccessorName = BeanInfoUtils.getAccessorMethodName(field.getFieldName(), field.getFieldType());
        final MethodMetadata accessor = getGovernorMethod(requiredAccessorName);
        if (accessor == null) {
            accessors.add(getAccessorMethod(field.getFieldName(), field.getFieldType()));
        } else {
            Validate.isTrue(Modifier.isPublic(accessor.getModifier()), "User provided field but failed to provide a public '%s()' method in '%s'", requiredAccessorName.getSymbolName(), destination.getFullyQualifiedTypeName());
            accessors.add(new MethodMetadataBuilder(accessor));
        }
    }
    return accessors;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 47 with FieldMetadataBuilder

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

the class JmsOperationsImpl method addJmsSender.

@Override
public void addJmsSender(String destinationName, JavaType classSelected, String jndiConnectionFactory, String profile, boolean force) {
    // Check that module included in destionationName is an application module
    String module = "";
    String destination = "";
    if (getProjectOperations().isMultimoduleProject()) {
        Collection<String> moduleNames = getTypeLocationService().getModuleNames(ModuleFeatureName.APPLICATION);
        // if only have one module, select this, else check the parameter
        if (moduleNames.size() > 1) {
            // user select a module
            if (destinationName.contains(":")) {
                int charSeparation = destinationName.indexOf(":");
                if (charSeparation > 0 && destinationName.length() > charSeparation) {
                    module = destinationName.substring(0, charSeparation);
                    destination = destinationName.substring(charSeparation + 1, destinationName.length());
                    if (!moduleNames.contains(module)) {
                        // error, is necessary select an application module
                        throw new IllegalArgumentException(String.format("Module '%s' must be of application type. Select one in --destinationName parameter", module));
                    }
                } else {
                    // error, is necessary select an application module and destination
                    throw new IllegalArgumentException(String.format("--destinationName parameter must be composed by [application type module]:[destination] or focus module must be application type module and only write the destination name"));
                }
            } else {
                // module not selected, check if focus module is application type
                Pom focusedModule = getProjectOperations().getFocusedModule();
                if (getTypeLocationService().hasModuleFeature(focusedModule, ModuleFeatureName.APPLICATION)) {
                    module = focusedModule.getModuleName();
                    destination = destinationName;
                } else {
                    throw new IllegalArgumentException(String.format("--destinationName parameter must be composed by [application type module]:[destination] or focus module must be application type module and only write the destination name"));
                }
            }
        } else {
            if (moduleNames.isEmpty()) {
                // error, is necessary select an application module
                throw new IllegalArgumentException(String.format("Is necessary to have at least an application type module."));
            } else {
                module = moduleNames.iterator().next();
                destination = destinationName;
            }
        }
    } else {
        destination = destinationName;
    }
    // Add jms springlets dependecies
    getProjectOperations().addDependency(classSelected.getModule(), DEPENDENCY_SPRINGLETS_JMS);
    // Include springlets-boot-starter-mail in module
    getProjectOperations().addDependency(module, DEPENDENCY_SPRINGLETS_STARTER_JMS);
    // Include property version
    getProjectOperations().addProperty("", PROPERTY_SPRINGLETS_VERSION);
    // Add instance of springlets service to service defined by parameter
    // Add JavaMailSender to service
    final ClassOrInterfaceTypeDetails serviceTypeDetails = getTypeLocationService().getTypeDetails(classSelected);
    Validate.isTrue(serviceTypeDetails != null, "Cannot locate source for '%s'", classSelected.getFullyQualifiedTypeName());
    final String declaredByMetadataId = serviceTypeDetails.getDeclaredByMetadataId();
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(serviceTypeDetails);
    // Create service field
    cidBuilder.addField(new FieldMetadataBuilder(declaredByMetadataId, PRIVATE, Arrays.asList(new AnnotationMetadataBuilder(AUTOWIRED)), new JavaSymbolName("jmsSendingService"), SpringletsJavaType.SPRINGLETS_JMS_SENDING_SERVICE));
    // Set destionation property name
    StringBuffer destinationNamePropertyName = new StringBuffer(JMS_PROPERTY_DESTINATION_NAME_PREFIX);
    destinationNamePropertyName.append(destination.replaceAll("/", "."));
    destinationNamePropertyName.append(JMS_PROPERTY_DESTINATION_NAME_SUFIX);
    StringBuffer destionationNameVar = new StringBuffer(JMS_VAR_DESTINATION_NAME_PREFIX);
    if (destination.contains("/")) {
        // Delete char '/' and each word
        String[] destinationNameArray = destination.split("/");
        for (String destinationFragment : destinationNameArray) {
            destionationNameVar.append(StringUtils.capitalize(destinationFragment));
        }
    } else {
        destionationNameVar.append(StringUtils.capitalize(destination));
    }
    destionationNameVar.append(JMS_VAR_DESTINATION_NAME_SUFIX);
    // Adding @Value annotation
    AnnotationMetadataBuilder valueAnnotation = new AnnotationMetadataBuilder(SpringJavaType.VALUE);
    valueAnnotation.addStringAttribute("value", "${".concat(destinationNamePropertyName.toString()).concat("}"));
    // Add instance of destination name
    cidBuilder.addField(new FieldMetadataBuilder(declaredByMetadataId, PRIVATE, Arrays.asList(valueAnnotation), new JavaSymbolName(destionationNameVar.toString()), JavaType.STRING));
    // Write both, springlets service and destination instance
    getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
    // Set properties
    setProperties(destination, destinationNamePropertyName.toString(), jndiConnectionFactory, module, profile, force);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) Pom(org.springframework.roo.project.maven.Pom) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 48 with FieldMetadataBuilder

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

the class JspViewManager method getCreateDocument.

public Document getCreateDocument() {
    final DocumentBuilder builder = XmlUtils.getDocumentBuilder();
    final Document document = builder.newDocument();
    // Add document namespaces
    final Element div = (Element) document.appendChild(new XmlElementBuilder("div", document).addAttribute("xmlns:form", "urn:jsptagdir:/WEB-INF/tags/form").addAttribute("xmlns:field", "urn:jsptagdir:/WEB-INF/tags/form/fields").addAttribute("xmlns:jsp", "http://java.sun.com/JSP/Page").addAttribute("xmlns:c", "http://java.sun.com/jsp/jstl/core").addAttribute("xmlns:spring", "http://www.springframework.org/tags").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());
    // Add form create element
    final Element formCreate = new XmlElementBuilder("form:create", document).addAttribute("id", XmlUtils.convertId("fc:" + formBackingType.getFullyQualifiedTypeName())).addAttribute("modelAttribute", entityName).addAttribute("path", controllerPath).addAttribute("render", "${empty dependencies}").build();
    if (!controllerPath.equalsIgnoreCase(formBackingType.getSimpleTypeName())) {
        formCreate.setAttribute("path", controllerPath);
    }
    final List<FieldMetadata> formFields = new ArrayList<FieldMetadata>();
    final List<FieldMetadata> fieldCopy = new ArrayList<FieldMetadata>(fields);
    // Handle Roo identifiers
    if (!formBackingTypePersistenceMetadata.getRooIdentifierFields().isEmpty()) {
        final String identifierFieldName = formBackingTypePersistenceMetadata.getIdentifierField().getFieldName().getSymbolName();
        formCreate.setAttribute("compositePkField", identifierFieldName);
        for (final FieldMetadata embeddedField : formBackingTypePersistenceMetadata.getRooIdentifierFields()) {
            final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(embeddedField);
            fieldBuilder.setFieldName(new JavaSymbolName(identifierFieldName + "." + embeddedField.getFieldName().getSymbolName()));
            for (int i = 0; i < fieldCopy.size(); i++) {
                // Make sure form fields are not presented twice.
                if (fieldCopy.get(i).getFieldName().equals(embeddedField.getFieldName())) {
                    fieldCopy.remove(i);
                    break;
                }
            }
            formFields.add(fieldBuilder.build());
        }
    }
    formFields.addAll(fieldCopy);
    // If identifier manually assigned, show it in creation
    if (formBackingTypePersistenceMetadata.getIdentifierField().getAnnotation(JpaJavaType.GENERATED_VALUE) == null) {
        formFields.add(formBackingTypePersistenceMetadata.getIdentifierField());
    }
    createFieldsForCreateAndUpdate(formFields, document, formCreate, true);
    formCreate.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(formCreate));
    final Element dependency = new XmlElementBuilder("form:dependency", document).addAttribute("id", XmlUtils.convertId("d:" + formBackingType.getFullyQualifiedTypeName())).addAttribute("render", "${not empty dependencies}").addAttribute("dependencies", "${dependencies}").build();
    dependency.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(dependency));
    div.appendChild(formCreate);
    div.appendChild(dependency);
    return document;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) XmlElementBuilder(org.springframework.roo.support.util.XmlElementBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 49 with FieldMetadataBuilder

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

the class ExceptionsMetadata method getMessageSourceField.

private FieldMetadata getMessageSourceField(Boolean controller) {
    // Return current MessageSource field if already exists
    if (this.messageSourceField != null) {
        return this.messageSourceField;
    }
    // Preparing @Autowired annotation
    List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    if (controller) {
        // MessageSource field must be annotated on controllers
        annotations.add(new AnnotationMetadataBuilder(SpringJavaType.AUTOWIRED));
    }
    // Generating field
    FieldMetadataBuilder field = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, annotations, new JavaSymbolName("exceptionMessageSource"), SpringJavaType.MESSAGE_SOURCE);
    this.messageSourceField = field.build();
    return this.messageSourceField;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 50 with FieldMetadataBuilder

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

the class JavaParserFieldMetadataBuilder method build.

@Override
public FieldMetadata build() {
    final FieldMetadataBuilder fieldMetadataBuilder = new FieldMetadataBuilder(declaredByMetadataId);
    fieldMetadataBuilder.setAnnotations(annotations);
    fieldMetadataBuilder.setFieldInitializer(fieldInitializer);
    fieldMetadataBuilder.setFieldName(fieldName);
    fieldMetadataBuilder.setFieldType(fieldType);
    fieldMetadataBuilder.setModifier(modifier);
    return fieldMetadataBuilder.build();
}
Also used : FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

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