How to use LocalDate in VRaptor4?

1

I'm trying to make a simple application in VRaptor 4.

In an entity, I have two attributes, one of type LocalDate and another of type LocalTime . I added the VRaptor Java 8 plugin, which should do the data conversion for the types mentioned, but this does not happen.

I searched the plugin and did not have anything teaching how to use it. It looks like the converters are not even being activated. How should I use this plugin? Should I create a convert even using the plugin?

The pom.xml:

<properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>br.com.caelum</groupId>
            <artifactId>vraptor</artifactId>
            <version>4.2.0-RC5</version>
        </dependency>
        <dependency>
            <groupId>br.com.caelum.vraptor</groupId>
            <artifactId>vraptor-java8</artifactId>
            <version>4.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.3.3.Final</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>my-tasks</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArguments>
                        <parameters />
                    </compilerArguments>
                </configuration>
            </plugin>
        </plugins>
    </build>

My entity:

@RequestScoped
@Entity
public class Task {
    @Id
    @GeneratedValue
    private Long id;

    @Basic(optional = false)
    @Column(nullable = false, length = 50)
    private String name;

    @Basic(optional = false)
    @Column(nullable = false, columnDefinition = "DATE")
    private LocalDate date;

    @Basic(optional = true)
    @Column(nullable = true, columnDefinition = "TIME")
    private LocalTime hour;

    @Basic(optional = true)
    @Column(nullable = true, length = 100)
    private String details;

    @Basic(optional = false)
    @Column(nullable = false)
    private boolean finished = false;

    @Basic(optional = false)
    @ManyToOne
    private User user;
//getters and setters
}

My form:

<form method="POST" action="${linkTo[TaskController].register}">
    <c:if test="${not empty errorMessage}">
        <c:import url="../commons/msgErrorAlert.jsp" />
    </c:if>

    <!--tarefa-->
    <div>
        <label for="task.name">Tarefa</label>
        <input type="text" name="task.name" value="${task.name}">
        <span>${errors.from('task.name')}</span>
    </div>
    <!--data-->
    <div>
        <label for="task.date">Data</label>
        <input type="date" name="task.date" value="${task.date}"> 
        <span>${errors.from('task.date')}</span>
    </div>
    <!--horario-->
    <div>
        <label for="task.hour">Horário</label>
        <input type="time" name="task.hour" value="${task.hour}"> 
        <span>${errors.from('task.hour')}</span>
    </div>
    <!--observações-->
    <div>
        <label for="task.details">Observações</label>
        <textarea name="task.details" rows="3">${task.details}</textarea>
        <span>${errors.from('task.details')}</span>
    </div>

    <div>
        <button type="submit">
            Cadastrar
        </button>
        <button type="reset">
            Limpar
        </button>
    </div>
</form>

My controller:

@Controller
@Path("/tarefa")
public class TaskController {

    private Result result;
    private ISession session;
    private ITaskValidator validator;
    private ITaskService taskService;

    @Deprecated
    public TaskController() {
        this(null, null, null, null);
    }

    @Inject
    public TaskController(Result result, ISession session, ITaskValidator validator, ITaskService taskService) {
        this.result = result;
        this.session = session;
        this.validator = validator;
        this.taskService = taskService;
    }

    @Get("/cadastro")
    public void register() {
        result.include("title", "Cadastro de tarefa");
    }

    @Post("/cadastro")
    public void register(Task task) {
        validator.validate(task);
        validator.onErrorRedirectTo(this).register();

        task.setFinished(false);
        task.setUser(session.getUser());
        /*
        método ainda não implementado
        taskservice.createTask(task);
        */
    }   
}

The validator:

public class TaskValidator implements ITaskValidator {

    private Validator validator;

    @Deprecated
    public TaskValidator() {
        this(null);
    }

    @Inject
    public TaskValidator(Validator validator) {
        this.validator = validator;
    }

    @Override
    public void validate(Task task) {
        validator.validate(task);

        //nome
        if(task.getName() == null || task.getName().length() < 5 || task.getName().length() > 50) {
            validator.add(new I18nMessage("task.name", "task.name.invalid"));
        }
        //data
        if(task.getDate() == null) {
            validator.add(new I18nMessage("task.date", "task.date.null"));
        }
        if(task.getDate() != null && task.getDate().isBefore(LocalDate.now())) {
            validator.add(new I18nMessage("task.date", "task.date.past"));
        }
        //horario
        if((task.getDate() != null && !task.getDate().isBefore(LocalDate.now())) && task.getHour().isBefore(LocalTime.now())) {
            validator.add(new I18nMessage("task.hour", "task.hour"));
        } 
        if(task.getDate() == null && task.getHour() != null) {
            validator.add(new I18nMessage("task.date", "task.hour.without.date"));
        }
        //observaçoes
        if(task.getDetails() != null && (task.getDetails().length() < 5 || task.getDetails().length() > 100)) {
            validator.add(new I18nMessage("task.details", "task.details.length"));
        }
    }

    @Override
    public <T> T onErrorRedirectTo(T controller) {
        return validator.onErrorRedirectTo(controller);
    }
}

When I send the data through the form, it returns me the warning of task.getDate() == null , created in validator .

    
asked by anonymous 30.07.2018 / 00:08

0 answers