Search in sources :

Example 21 with PartTree

use of org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree in project spring-roo by spring-projects.

the class PartTreeUnitTest method validateEmptyOperatorParameters.

@Test
public void validateEmptyOperatorParameters() throws Exception {
    List<FinderParameter> parameters = new ArrayList<FinderParameter>();
    parameters.add(new FinderParameter(JavaType.INT_OBJECT, new JavaSymbolName("number")));
    assertEqualsParameters(parameters, new PartTree("findByNumber", memberDetails).getParameters());
    parameters.add(new FinderParameter(JavaType.INT_OBJECT, new JavaSymbolName("number2")));
    assertEqualsParameters(parameters, new PartTree("findByNumberAndNumberOrderByNumber", memberDetails).getParameters());
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FinderParameter(org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.FinderParameter) ArrayList(java.util.ArrayList) PartTree(org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree) Test(org.junit.Test)

Example 22 with PartTree

use of org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree in project spring-roo by spring-projects.

the class PartTreeUnitTest method identifiesFindFirstKUsersExplicit.

@Test
public void identifiesFindFirstKUsersExplicit() {
    PartTree partTree = new PartTree("findFirst10NumberByText", memberDetails);
    assertTrue(partTree.isValid() && partTree.getMaxResults() == 10);
    partTree = new PartTree("findTop10NumberByText", memberDetails);
    assertTrue(partTree.isValid() && partTree.getMaxResults() == 10);
}
Also used : PartTree(org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree) Test(org.junit.Test)

Example 23 with PartTree

use of org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree in project spring-roo by spring-projects.

the class PartTreeUnitTest method disablesFindFirstKImplicitIfNotPresent.

@Test
public void disablesFindFirstKImplicitIfNotPresent() {
    PartTree partTree = new PartTree("findByText", memberDetails);
    assertTrue(partTree.isValid() && partTree.getMaxResults() == null);
}
Also used : PartTree(org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree) Test(org.junit.Test)

Example 24 with PartTree

use of org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree in project spring-roo by spring-projects.

the class PartTreeUnitTest method identifiesFindFirst1Explicit.

@Test
public void identifiesFindFirst1Explicit() {
    PartTree partTree = new PartTree("findFirst1ByText", memberDetails);
    assertTrue(partTree.isValid() && partTree.getMaxResults() == 1);
    partTree = new PartTree("findTop1ByText", memberDetails);
    assertTrue(partTree.isValid() && partTree.getMaxResults() == 1);
}
Also used : PartTree(org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree) Test(org.junit.Test)

Example 25 with PartTree

use of org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree in project spring-roo by spring-projects.

the class FinderCommands method installFinders.

@CliCommand(value = "finder add", help = "Installs a finder in the given target (must be an entity). This command needs an existing " + "repository for the target entity, you can create it with `repository jpa` command. The " + "finder will be added to targeted entity associated repository and associated service if " + "exists or when it will be created.")
public void installFinders(@CliOption(key = "entity", mandatory = true, unspecifiedDefaultValue = "*", optionContext = PROJECT, help = "The entity for which the finders are generated. When working on a mono module project, " + "simply specify the name of the entity. If you consider it necessary, you can also " + "specify the package. Ex.: `--class ~.domain.MyEntity` (where `~` is the base package). " + "When working with multiple modules, you should specify the name of the class and the " + "module where it is. Ex.: `--class model:~.domain.MyEntity`. If the module is not " + "specified, it is assumed that the entity is in the module which has the focus.") final JavaType entity, @CliOption(key = "name", mandatory = true, help = "The finder string defined as a Spring Data query. Use Spring Data JPA nomenclature. " + "Possible values are: any finder name following Spring Data nomenclature. " + "This option will not be available until `--entity` is specified.") final JavaSymbolName finderName, @CliOption(key = "formBean", mandatory = true, help = "The finder's search parameter. Should be a DTO and it must have at least same fields " + "(name and type) as those included in the finder `--name`, which can be target entity" + " fields or related entity fields. " + "Possible values are: any of the DTO's in the project. " + "This option is mandatory if `--returnType` is specified and its a projection. " + "This option is not available if `--entity` parameter has not been specified before or " + "if it does not exist any DTO in generated project. " + "Default if option not present: the entity specified in `--entity` option.") final JavaType formBean, @CliOption(key = "returnType", mandatory = false, optionContext = PROJECT, help = "The finder's results return type. " + "Possible values are: Projection classes annotated with `@RooEntityProjection` and " + "related to the entity specified in `--entity` option (use `entity projection` command), " + "or the same entity. " + "This option is not available if `--entity` parameter has not been specified before or " + "if it does not exist any Projection class associated to the targeted entity. " + "Default if not present: the default return type of the repository related to the entity, " + "which can be specified with `--defaultReturnType` parameter in `repository jpa` command.") JavaType returnType) {
    // Check if specified finderName follows Spring Data nomenclature
    PartTree partTree = new PartTree(finderName.getSymbolName(), getEntityDetails(entity), this);
    // If generated partTree is not valid, shows an exception
    Validate.isTrue(partTree.isValid(), "--name parameter must follow Spring Data nomenclature. Please, write a valid value using autocomplete feature (TAB or CTRL + Space)");
    // related repository
    if (returnType == null) {
        // Obtain the related repository metadata
        RepositoryJpaMetadata repositoryMetadata = getRepositoryJpaLocator().getFirstRepositoryMetadata(entity);
        Validate.notNull(repositoryMetadata, "ERROR: You must create a repository related with this entity before to generate a finder");
        // Use the repository metadata to obtain the default return type
        returnType = repositoryMetadata.getDefaultReturnType();
    }
    Validate.notNull(returnType, "ERROR: The new finder must define a returnType");
    // Check if the returnType is an entity. If is is not an entity validate
    // that the formBean is not null
    ClassOrInterfaceTypeDetails type = getTypeLocationService().getTypeDetails(returnType);
    if (type.getAnnotation(RooJavaType.ROO_JPA_ENTITY) == null) {
        Validate.notNull(formBean, "--formBean is requied when --returnType parameter is a projection.");
    }
    finderOperations.installFinder(entity, finderName, formBean, returnType);
}
Also used : RepositoryJpaMetadata(org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) PartTree(org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree) CliCommand(org.springframework.roo.shell.CliCommand)

Aggregations

PartTree (org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree)26 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)10 JavaType (org.springframework.roo.model.JavaType)10 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)8 FinderParameter (org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.FinderParameter)6 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)6 List (java.util.List)4 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)4 RooJavaType (org.springframework.roo.model.RooJavaType)4 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)3 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)3 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)3 Pair (org.apache.commons.lang3.tuple.Pair)2 RepositoryJpaMetadata (org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata)2 FinderMethod (org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.FinderMethod)2 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)2 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)2