BAEL-434 Moved project from roo to spring-roo folder. (#1673)
* Code for Dependency Injection Article. * Added Java based configuration. Downloaded formatter.xml and reformatted all changed files. Manually changed tab into 4 spaces in XML configuration files. * BAEL-434 - Spring Roo project files generated by Spring Roo. No formatting applied. Added POM, java and resources folders. * Moved project from roo to spring-roo folder.
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* = RooApplication
|
||||
*
|
||||
* TODO Auto-generated class documentation
|
||||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class RooApplication {
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RooApplication.class, args);
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.config;
|
||||
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryConfiguration;
|
||||
|
||||
/**
|
||||
* = SpringDataJpaDetachableRepositoryConfiguration
|
||||
TODO Auto-generated class documentation
|
||||
*
|
||||
*/
|
||||
@RooJpaRepositoryConfiguration
|
||||
public class SpringDataJpaDetachableRepositoryConfiguration {
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.config;
|
||||
|
||||
import com.baeldung.RooApplication;
|
||||
import com.baeldung.config.SpringDataJpaDetachableRepositoryConfiguration;
|
||||
import io.springlets.data.jpa.repository.support.DetachableJpaRepositoryImpl;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
privileged aspect SpringDataJpaDetachableRepositoryConfiguration_Roo_Jpa_Repository_Configuration {
|
||||
|
||||
declare @type: SpringDataJpaDetachableRepositoryConfiguration: @Configuration;
|
||||
|
||||
declare @type: SpringDataJpaDetachableRepositoryConfiguration: @EnableJpaRepositories(repositoryBaseClass = DetachableJpaRepositoryImpl.class, basePackageClasses = RooApplication.class);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.config;
|
||||
import org.springframework.roo.addon.web.mvc.controller.annotations.config.RooWebMvcConfiguration;
|
||||
import org.springframework.roo.addon.web.mvc.thymeleaf.annotations.RooWebMvcThymeleafUIConfiguration;
|
||||
|
||||
/**
|
||||
* = WebMvcConfiguration
|
||||
TODO Auto-generated class documentation
|
||||
*
|
||||
*/
|
||||
@RooWebMvcConfiguration(defaultLanguage = "en")
|
||||
@RooWebMvcThymeleafUIConfiguration
|
||||
public class WebMvcConfiguration {
|
||||
}
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.config;
|
||||
|
||||
import com.baeldung.config.WebMvcConfiguration;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.thymeleaf.TemplateEngine;
|
||||
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
|
||||
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
||||
import org.thymeleaf.templatemode.TemplateMode;
|
||||
|
||||
privileged aspect WebMvcConfiguration_Roo_ThymeleafUIConfiguration {
|
||||
|
||||
declare parents: WebMvcConfiguration implements ApplicationContextAware;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
@Autowired
|
||||
private ThymeleafProperties WebMvcConfiguration.thymeleafProperties;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
@Autowired
|
||||
private TemplateEngine WebMvcConfiguration.templateEngine;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
private ApplicationContext WebMvcConfiguration.applicationContext;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return ThymeleafProperties
|
||||
*/
|
||||
public ThymeleafProperties WebMvcConfiguration.getThymeleafProperties() {
|
||||
return thymeleafProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return TemplateEngine
|
||||
*/
|
||||
public TemplateEngine WebMvcConfiguration.getTemplateEngine() {
|
||||
return templateEngine;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return ApplicationContext
|
||||
*/
|
||||
public ApplicationContext WebMvcConfiguration.getApplicationContext() {
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param applicationContext
|
||||
*/
|
||||
public void WebMvcConfiguration.setApplicationContext(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return ThymeleafViewResolver
|
||||
*/
|
||||
@Bean
|
||||
public ThymeleafViewResolver WebMvcConfiguration.javascriptThymeleafViewResolver() {
|
||||
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
|
||||
resolver.setTemplateEngine(getTemplateEngine());
|
||||
resolver.setCharacterEncoding("UTF-8");
|
||||
resolver.setContentType("application/javascript");
|
||||
resolver.setViewNames(new String[] {"*.js"});
|
||||
resolver.setCache(getThymeleafProperties().isCache());
|
||||
return resolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return SpringResourceTemplateResolver
|
||||
*/
|
||||
@Bean
|
||||
public SpringResourceTemplateResolver WebMvcConfiguration.javascriptTemplateResolver() {
|
||||
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
|
||||
resolver.setApplicationContext(getApplicationContext());
|
||||
resolver.setPrefix("classpath:/templates/fragments/js/");
|
||||
resolver.setTemplateMode(TemplateMode.JAVASCRIPT);
|
||||
resolver.setCharacterEncoding("UTF-8");
|
||||
resolver.setCheckExistence(true);
|
||||
resolver.setCacheable(getThymeleafProperties().isCache());
|
||||
return resolver;
|
||||
}
|
||||
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.config;
|
||||
|
||||
import com.baeldung.config.WebMvcConfiguration;
|
||||
import io.tracee.binding.springmvc.TraceeInterceptor;
|
||||
import java.lang.Override;
|
||||
import java.util.Locale;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
||||
|
||||
privileged aspect WebMvcConfiguration_Roo_WebMvcConfiguration {
|
||||
|
||||
declare parents: WebMvcConfiguration extends WebMvcConfigurerAdapter;
|
||||
|
||||
declare @type: WebMvcConfiguration: @Configuration;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return LocalValidatorFactoryBean
|
||||
*/
|
||||
@Primary
|
||||
@Bean
|
||||
public LocalValidatorFactoryBean WebMvcConfiguration.validator() {
|
||||
return new LocalValidatorFactoryBean();
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return LocaleResolver
|
||||
*/
|
||||
@Bean
|
||||
public LocaleResolver WebMvcConfiguration.localeResolver() {
|
||||
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
|
||||
localeResolver.setDefaultLocale(new Locale("en"));
|
||||
return localeResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return LocaleChangeInterceptor
|
||||
*/
|
||||
@Bean
|
||||
public LocaleChangeInterceptor WebMvcConfiguration.localeChangeInterceptor() {
|
||||
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
|
||||
localeChangeInterceptor.setParamName("lang");
|
||||
return localeChangeInterceptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param registry
|
||||
*/
|
||||
@Override
|
||||
public void WebMvcConfiguration.addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(localeChangeInterceptor());
|
||||
registry.addInterceptor(new TraceeInterceptor());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.config.jackson;
|
||||
import org.springframework.roo.addon.web.mvc.controller.annotations.config.RooDomainModelModule;
|
||||
|
||||
/**
|
||||
* = DomainModelModule
|
||||
TODO Auto-generated class documentation
|
||||
*
|
||||
*/
|
||||
@RooDomainModelModule
|
||||
public class DomainModelModule {
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.config.jackson;
|
||||
|
||||
import com.baeldung.config.jackson.DomainModelModule;
|
||||
import com.baeldung.domain.Book;
|
||||
import com.baeldung.web.BookJsonMixin;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import org.springframework.boot.jackson.JsonComponent;
|
||||
|
||||
privileged aspect DomainModelModule_Roo_DomainModelModule {
|
||||
|
||||
declare parents: DomainModelModule extends SimpleModule;
|
||||
|
||||
declare @type: DomainModelModule: @JsonComponent;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated constructor documentation
|
||||
*
|
||||
*/
|
||||
public DomainModelModule.new() {
|
||||
// Mixin registration
|
||||
|
||||
setMixInAnnotation(Book.class, BookJsonMixin.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.domain;
|
||||
import org.springframework.roo.addon.javabean.annotations.RooEquals;
|
||||
import org.springframework.roo.addon.javabean.annotations.RooJavaBean;
|
||||
import org.springframework.roo.addon.javabean.annotations.RooToString;
|
||||
import org.springframework.roo.addon.jpa.annotations.entity.RooJpaEntity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* = Book
|
||||
TODO Auto-generated class documentation
|
||||
*
|
||||
*/
|
||||
@RooJavaBean
|
||||
@RooToString
|
||||
@RooJpaEntity
|
||||
@RooEquals(isJpaEntity = true)
|
||||
public class Book {
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
@Version
|
||||
private Integer version;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
@NotNull
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
@NotNull
|
||||
private String author;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
@NotNull
|
||||
private String isbn;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.domain;
|
||||
|
||||
import com.baeldung.domain.Book;
|
||||
import java.util.Objects;
|
||||
|
||||
privileged aspect Book_Roo_Equals {
|
||||
|
||||
/**
|
||||
* This `equals` implementation is specific for JPA entities and uses
|
||||
* the entity identifier for it, following the article in
|
||||
* https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
|
||||
*
|
||||
* @param obj
|
||||
* @return Boolean
|
||||
*/
|
||||
public boolean Book.equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
// instanceof is false if the instance is null
|
||||
if (!(obj instanceof Book)) {
|
||||
return false;
|
||||
}
|
||||
return getId() != null && Objects.equals(getId(), ((Book) obj).getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* This `hashCode` implementation is specific for JPA entities and uses a fixed `int` value to be able
|
||||
* to identify the entity in collections after a new id is assigned to the entity, following the article in
|
||||
* https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
|
||||
*
|
||||
* @return Integer
|
||||
*/
|
||||
public int Book.hashCode() {
|
||||
return 31;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.domain;
|
||||
|
||||
import com.baeldung.domain.Book;
|
||||
|
||||
privileged aspect Book_Roo_JavaBean {
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return Long
|
||||
*/
|
||||
public Long Book.getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
public void Book.setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return Integer
|
||||
*/
|
||||
public Integer Book.getVersion() {
|
||||
return this.version;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param version
|
||||
*/
|
||||
public void Book.setVersion(Integer version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String Book.getTitle() {
|
||||
return this.title;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param title
|
||||
*/
|
||||
public void Book.setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String Book.getAuthor() {
|
||||
return this.author;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param author
|
||||
*/
|
||||
public void Book.setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String Book.getIsbn() {
|
||||
return this.isbn;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param isbn
|
||||
*/
|
||||
public void Book.setIsbn(String isbn) {
|
||||
this.isbn = isbn;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.domain;
|
||||
|
||||
import com.baeldung.domain.Book;
|
||||
import io.springlets.format.EntityFormat;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
privileged aspect Book_Roo_Jpa_Entity {
|
||||
|
||||
declare @type: Book: @Entity;
|
||||
|
||||
declare @type: Book: @EntityFormat;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String Book.ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String Book.ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.domain;
|
||||
|
||||
import com.baeldung.domain.Book;
|
||||
|
||||
privileged aspect Book_Roo_ToString {
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String Book.toString() {
|
||||
return "Book {" +
|
||||
"id='" + id + '\'' +
|
||||
", version='" + version + '\'' +
|
||||
", title='" + title + '\'' +
|
||||
", author='" + author + '\'' +
|
||||
", isbn='" + isbn + '\'' +
|
||||
", ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE='" + ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE + '\'' +
|
||||
", ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE='" + ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE + '\'' + "}" + super.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.repository;
|
||||
import com.baeldung.domain.Book;
|
||||
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepository;
|
||||
|
||||
/**
|
||||
* = BookRepository
|
||||
TODO Auto-generated class documentation
|
||||
*
|
||||
*/
|
||||
@RooJpaRepository(entity = Book.class)
|
||||
public interface BookRepository {
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.repository;
|
||||
import com.baeldung.domain.Book;
|
||||
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustom;
|
||||
|
||||
/**
|
||||
* = BookRepositoryCustom
|
||||
TODO Auto-generated class documentation
|
||||
*
|
||||
*/
|
||||
@RooJpaRepositoryCustom(entity = Book.class)
|
||||
public interface BookRepositoryCustom {
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.repository;
|
||||
|
||||
import com.baeldung.domain.Book;
|
||||
import com.baeldung.repository.BookRepositoryCustom;
|
||||
import io.springlets.data.domain.GlobalSearch;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
privileged aspect BookRepositoryCustom_Roo_Jpa_Repository_Custom {
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param globalSearch
|
||||
* @param pageable
|
||||
* @return Page
|
||||
*/
|
||||
public abstract Page<Book> BookRepositoryCustom.findAll(GlobalSearch globalSearch, Pageable pageable);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
|
||||
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
|
||||
import com.baeldung.domain.Book;
|
||||
|
||||
/**
|
||||
* = BookRepositoryImpl
|
||||
*
|
||||
* TODO Auto-generated class documentation
|
||||
*
|
||||
*/
|
||||
@RooJpaRepositoryCustomImpl(repository = BookRepositoryCustom.class)
|
||||
public class BookRepositoryImpl extends QueryDslRepositorySupportExt<Book> {
|
||||
|
||||
/**
|
||||
* TODO Auto-generated constructor documentation
|
||||
*/
|
||||
BookRepositoryImpl() {
|
||||
super(Book.class);
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.repository;
|
||||
|
||||
import com.baeldung.domain.Book;
|
||||
import com.baeldung.domain.QBook;
|
||||
import com.baeldung.repository.BookRepositoryCustom;
|
||||
import com.baeldung.repository.BookRepositoryImpl;
|
||||
import com.querydsl.core.types.Path;
|
||||
import com.querydsl.jpa.JPQLQuery;
|
||||
import io.springlets.data.domain.GlobalSearch;
|
||||
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
privileged aspect BookRepositoryImpl_Roo_Jpa_Repository_Impl {
|
||||
|
||||
declare parents: BookRepositoryImpl implements BookRepositoryCustom;
|
||||
|
||||
declare @type: BookRepositoryImpl: @Transactional(readOnly = true);
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String BookRepositoryImpl.TITLE = "title";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String BookRepositoryImpl.AUTHOR = "author";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String BookRepositoryImpl.ISBN = "isbn";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param globalSearch
|
||||
* @param pageable
|
||||
* @return Page
|
||||
*/
|
||||
public Page<Book> BookRepositoryImpl.findAll(GlobalSearch globalSearch, Pageable pageable) {
|
||||
|
||||
QBook book = QBook.book;
|
||||
|
||||
JPQLQuery<Book> query = from(book);
|
||||
|
||||
Path<?>[] paths = new Path<?>[] {book.title,book.author,book.isbn};
|
||||
applyGlobalSearch(globalSearch, query, paths);
|
||||
|
||||
AttributeMappingBuilder mapping = buildMapper()
|
||||
.map(TITLE, book.title)
|
||||
.map(AUTHOR, book.author)
|
||||
.map(ISBN, book.isbn);
|
||||
|
||||
applyPagination(pageable, query, mapping);
|
||||
applyOrderById(query);
|
||||
|
||||
return loadPage(query, pageable, book);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.repository;
|
||||
|
||||
import com.baeldung.domain.Book;
|
||||
import com.baeldung.repository.BookRepository;
|
||||
import com.baeldung.repository.BookRepositoryCustom;
|
||||
import io.springlets.data.jpa.repository.DetachableJpaRepository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
privileged aspect BookRepository_Roo_Jpa_Repository {
|
||||
|
||||
declare parents: BookRepository extends DetachableJpaRepository<Book, Long>;
|
||||
|
||||
declare parents: BookRepository extends BookRepositoryCustom;
|
||||
|
||||
declare @type: BookRepository: @Transactional(readOnly = true);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.service.api;
|
||||
import com.baeldung.domain.Book;
|
||||
import org.springframework.roo.addon.layers.service.annotations.RooService;
|
||||
|
||||
/**
|
||||
* = BookService
|
||||
TODO Auto-generated class documentation
|
||||
*
|
||||
*/
|
||||
@RooService(entity = Book.class)
|
||||
public interface BookService {
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.service.api;
|
||||
|
||||
import com.baeldung.domain.Book;
|
||||
import com.baeldung.service.api.BookService;
|
||||
import io.springlets.data.domain.GlobalSearch;
|
||||
import io.springlets.format.EntityResolver;
|
||||
import java.util.List;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
privileged aspect BookService_Roo_Service {
|
||||
|
||||
declare parents: BookService extends EntityResolver<Book, Long>;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param id
|
||||
* @return Book
|
||||
*/
|
||||
public abstract Book BookService.findOne(Long id);
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param book
|
||||
*/
|
||||
public abstract void BookService.delete(Book book);
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param entities
|
||||
* @return List
|
||||
*/
|
||||
public abstract List<Book> BookService.save(Iterable<Book> entities);
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param ids
|
||||
*/
|
||||
public abstract void BookService.delete(Iterable<Long> ids);
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param entity
|
||||
* @return Book
|
||||
*/
|
||||
public abstract Book BookService.save(Book entity);
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param id
|
||||
* @return Book
|
||||
*/
|
||||
public abstract Book BookService.findOneForUpdate(Long id);
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param ids
|
||||
* @return List
|
||||
*/
|
||||
public abstract List<Book> BookService.findAll(Iterable<Long> ids);
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return List
|
||||
*/
|
||||
public abstract List<Book> BookService.findAll();
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return Long
|
||||
*/
|
||||
public abstract long BookService.count();
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param globalSearch
|
||||
* @param pageable
|
||||
* @return Page
|
||||
*/
|
||||
public abstract Page<Book> BookService.findAll(GlobalSearch globalSearch, Pageable pageable);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.service.impl;
|
||||
import com.baeldung.service.api.BookService;
|
||||
import org.springframework.roo.addon.layers.service.annotations.RooServiceImpl;
|
||||
|
||||
/**
|
||||
* = BookServiceImpl
|
||||
TODO Auto-generated class documentation
|
||||
*
|
||||
*/
|
||||
@RooServiceImpl(service = BookService.class)
|
||||
public class BookServiceImpl implements BookService {
|
||||
}
|
||||
+177
@@ -0,0 +1,177 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.service.impl;
|
||||
|
||||
import com.baeldung.domain.Book;
|
||||
import com.baeldung.repository.BookRepository;
|
||||
import com.baeldung.service.impl.BookServiceImpl;
|
||||
import io.springlets.data.domain.GlobalSearch;
|
||||
import java.util.List;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
privileged aspect BookServiceImpl_Roo_Service_Impl {
|
||||
|
||||
declare @type: BookServiceImpl: @Service;
|
||||
|
||||
declare @type: BookServiceImpl: @Transactional(readOnly = true);
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
private BookRepository BookServiceImpl.bookRepository;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated constructor documentation
|
||||
*
|
||||
* @param bookRepository
|
||||
*/
|
||||
@Autowired
|
||||
public BookServiceImpl.new(BookRepository bookRepository) {
|
||||
setBookRepository(bookRepository);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return BookRepository
|
||||
*/
|
||||
public BookRepository BookServiceImpl.getBookRepository() {
|
||||
return bookRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param bookRepository
|
||||
*/
|
||||
public void BookServiceImpl.setBookRepository(BookRepository bookRepository) {
|
||||
this.bookRepository = bookRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param book
|
||||
*/
|
||||
@Transactional
|
||||
public void BookServiceImpl.delete(Book book) {
|
||||
getBookRepository().delete(book);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param entities
|
||||
* @return List
|
||||
*/
|
||||
@Transactional
|
||||
public List<Book> BookServiceImpl.save(Iterable<Book> entities) {
|
||||
return getBookRepository().save(entities);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param ids
|
||||
*/
|
||||
@Transactional
|
||||
public void BookServiceImpl.delete(Iterable<Long> ids) {
|
||||
List<Book> toDelete = getBookRepository().findAll(ids);
|
||||
getBookRepository().deleteInBatch(toDelete);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param entity
|
||||
* @return Book
|
||||
*/
|
||||
@Transactional
|
||||
public Book BookServiceImpl.save(Book entity) {
|
||||
return getBookRepository().save(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param id
|
||||
* @return Book
|
||||
*/
|
||||
public Book BookServiceImpl.findOne(Long id) {
|
||||
return getBookRepository().findOne(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param id
|
||||
* @return Book
|
||||
*/
|
||||
public Book BookServiceImpl.findOneForUpdate(Long id) {
|
||||
return getBookRepository().findOneDetached(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param ids
|
||||
* @return List
|
||||
*/
|
||||
public List<Book> BookServiceImpl.findAll(Iterable<Long> ids) {
|
||||
return getBookRepository().findAll(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return List
|
||||
*/
|
||||
public List<Book> BookServiceImpl.findAll() {
|
||||
return getBookRepository().findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return Long
|
||||
*/
|
||||
public long BookServiceImpl.count() {
|
||||
return getBookRepository().count();
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param globalSearch
|
||||
* @param pageable
|
||||
* @return Page
|
||||
*/
|
||||
public Page<Book> BookServiceImpl.findAll(GlobalSearch globalSearch, Pageable pageable) {
|
||||
return getBookRepository().findAll(globalSearch, pageable);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return Class
|
||||
*/
|
||||
public Class<Book> BookServiceImpl.getEntityType() {
|
||||
return Book.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return Class
|
||||
*/
|
||||
public Class<Long> BookServiceImpl.getIdType() {
|
||||
return Long.class;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.web;
|
||||
import com.baeldung.domain.Book;
|
||||
import com.baeldung.service.api.BookService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.jackson.JsonObjectDeserializer;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.roo.addon.web.mvc.controller.annotations.config.RooDeserializer;
|
||||
|
||||
/**
|
||||
* = BookDeserializer
|
||||
TODO Auto-generated class documentation
|
||||
*
|
||||
*/
|
||||
@RooDeserializer(entity = Book.class)
|
||||
public class BookDeserializer extends JsonObjectDeserializer<Book> {
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
private BookService bookService;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
private ConversionService conversionService;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated constructor documentation
|
||||
*
|
||||
* @param bookService
|
||||
* @param conversionService
|
||||
*/
|
||||
@Autowired
|
||||
public BookDeserializer(@Lazy BookService bookService, ConversionService conversionService) {
|
||||
this.bookService = bookService;
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.web;
|
||||
|
||||
import com.baeldung.domain.Book;
|
||||
import com.baeldung.service.api.BookService;
|
||||
import com.baeldung.web.BookDeserializer;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.ObjectCodec;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.springlets.web.NotFoundException;
|
||||
import java.io.IOException;
|
||||
import org.springframework.boot.jackson.JsonComponent;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
|
||||
privileged aspect BookDeserializer_Roo_EntityDeserializer {
|
||||
|
||||
declare @type: BookDeserializer: @JsonComponent;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return BookService
|
||||
*/
|
||||
public BookService BookDeserializer.getBookService() {
|
||||
return bookService;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param bookService
|
||||
*/
|
||||
public void BookDeserializer.setBookService(BookService bookService) {
|
||||
this.bookService = bookService;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return ConversionService
|
||||
*/
|
||||
public ConversionService BookDeserializer.getConversionService() {
|
||||
return conversionService;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param conversionService
|
||||
*/
|
||||
public void BookDeserializer.setConversionService(ConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param jsonParser
|
||||
* @param context
|
||||
* @param codec
|
||||
* @param tree
|
||||
* @return Book
|
||||
* @throws IOException
|
||||
*/
|
||||
public Book BookDeserializer.deserializeObject(JsonParser jsonParser, DeserializationContext context, ObjectCodec codec, JsonNode tree) throws IOException {
|
||||
String idText = tree.asText();
|
||||
Long id = conversionService.convert(idText, Long.class);
|
||||
Book book = bookService.findOne(id);
|
||||
if (book == null) {
|
||||
throw new NotFoundException("Book not found");
|
||||
}
|
||||
return book;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.web;
|
||||
import com.baeldung.domain.Book;
|
||||
import org.springframework.roo.addon.web.mvc.controller.annotations.config.RooJsonMixin;
|
||||
|
||||
/**
|
||||
* = BookJsonMixin
|
||||
TODO Auto-generated class documentation
|
||||
*
|
||||
*/
|
||||
@RooJsonMixin(entity = Book.class)
|
||||
public abstract class BookJsonMixin {
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.web;
|
||||
import com.baeldung.domain.Book;
|
||||
import org.springframework.roo.addon.web.mvc.controller.annotations.ControllerType;
|
||||
import org.springframework.roo.addon.web.mvc.controller.annotations.RooController;
|
||||
import org.springframework.roo.addon.web.mvc.thymeleaf.annotations.RooThymeleaf;
|
||||
|
||||
/**
|
||||
* = BooksCollectionThymeleafController
|
||||
TODO Auto-generated class documentation
|
||||
*
|
||||
*/
|
||||
@RooController(entity = Book.class, type = ControllerType.COLLECTION)
|
||||
@RooThymeleaf
|
||||
public class BooksCollectionThymeleafController {
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.web;
|
||||
|
||||
import com.baeldung.service.api.BookService;
|
||||
import com.baeldung.web.BooksCollectionThymeleafController;
|
||||
|
||||
privileged aspect BooksCollectionThymeleafController_Roo_Controller {
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
private BookService BooksCollectionThymeleafController.bookService;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return BookService
|
||||
*/
|
||||
public BookService BooksCollectionThymeleafController.getBookService() {
|
||||
return bookService;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param bookService
|
||||
*/
|
||||
public void BooksCollectionThymeleafController.setBookService(BookService bookService) {
|
||||
this.bookService = bookService;
|
||||
}
|
||||
|
||||
}
|
||||
+470
@@ -0,0 +1,470 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.web;
|
||||
|
||||
import ar.com.fdvs.dj.core.DynamicJasperHelper;
|
||||
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
|
||||
import ar.com.fdvs.dj.domain.builders.ColumnBuilderException;
|
||||
import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
|
||||
import com.baeldung.domain.Book;
|
||||
import com.baeldung.service.api.BookService;
|
||||
import com.baeldung.web.BooksCollectionThymeleafController;
|
||||
import com.baeldung.web.BooksItemThymeleafController;
|
||||
import com.baeldung.web.BooksItemThymeleafLinkFactory;
|
||||
import com.baeldung.web.reports.ExportingErrorException;
|
||||
import com.baeldung.web.reports.JasperReportsCsvExporter;
|
||||
import com.baeldung.web.reports.JasperReportsExporter;
|
||||
import com.baeldung.web.reports.JasperReportsPdfExporter;
|
||||
import com.baeldung.web.reports.JasperReportsXlsExporter;
|
||||
import io.springlets.data.domain.GlobalSearch;
|
||||
import io.springlets.data.web.datatables.ConvertedDatatablesData;
|
||||
import io.springlets.data.web.datatables.Datatables;
|
||||
import io.springlets.data.web.datatables.DatatablesColumns;
|
||||
import io.springlets.data.web.datatables.DatatablesPageable;
|
||||
import io.springlets.data.web.select2.Select2DataSupport;
|
||||
import io.springlets.data.web.select2.Select2DataWithConversion;
|
||||
import io.springlets.web.mvc.util.ControllerMethodLinkBuilderFactory;
|
||||
import io.springlets.web.mvc.util.MethodLinkBuilderFactory;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Locale;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.Valid;
|
||||
import net.sf.jasperreports.engine.JRDataSource;
|
||||
import net.sf.jasperreports.engine.JRException;
|
||||
import net.sf.jasperreports.engine.JasperPrint;
|
||||
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.web.PageableDefault;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.InitBinder;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
|
||||
privileged aspect BooksCollectionThymeleafController_Roo_Thymeleaf {
|
||||
|
||||
declare @type: BooksCollectionThymeleafController: @Controller;
|
||||
|
||||
declare @type: BooksCollectionThymeleafController: @RequestMapping(value = "/books", name = "BooksCollectionThymeleafController", produces = MediaType.TEXT_HTML_VALUE);
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
private MessageSource BooksCollectionThymeleafController.messageSource;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
private MethodLinkBuilderFactory<BooksItemThymeleafController> BooksCollectionThymeleafController.itemLink;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
private ConversionService BooksCollectionThymeleafController.conversionService;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated constructor documentation
|
||||
*
|
||||
* @param bookService
|
||||
* @param conversionService
|
||||
* @param messageSource
|
||||
* @param linkBuilder
|
||||
*/
|
||||
@Autowired
|
||||
public BooksCollectionThymeleafController.new(BookService bookService, ConversionService conversionService, MessageSource messageSource, ControllerMethodLinkBuilderFactory linkBuilder) {
|
||||
setBookService(bookService);
|
||||
setConversionService(conversionService);
|
||||
setMessageSource(messageSource);
|
||||
setItemLink(linkBuilder.of(BooksItemThymeleafController.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return MessageSource
|
||||
*/
|
||||
public MessageSource BooksCollectionThymeleafController.getMessageSource() {
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param messageSource
|
||||
*/
|
||||
public void BooksCollectionThymeleafController.setMessageSource(MessageSource messageSource) {
|
||||
this.messageSource = messageSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return MethodLinkBuilderFactory
|
||||
*/
|
||||
public MethodLinkBuilderFactory<BooksItemThymeleafController> BooksCollectionThymeleafController.getItemLink() {
|
||||
return itemLink;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param itemLink
|
||||
*/
|
||||
public void BooksCollectionThymeleafController.setItemLink(MethodLinkBuilderFactory<BooksItemThymeleafController> itemLink) {
|
||||
this.itemLink = itemLink;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return ConversionService
|
||||
*/
|
||||
public ConversionService BooksCollectionThymeleafController.getConversionService() {
|
||||
return conversionService;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param conversionService
|
||||
*/
|
||||
public void BooksCollectionThymeleafController.setConversionService(ConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param model
|
||||
* @return ModelAndView
|
||||
*/
|
||||
@GetMapping(name = "list")
|
||||
public ModelAndView BooksCollectionThymeleafController.list(Model model) {
|
||||
return new ModelAndView("/books/list");
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param datatablesColumns
|
||||
* @param search
|
||||
* @param pageable
|
||||
* @param draw
|
||||
* @return ResponseEntity
|
||||
*/
|
||||
@GetMapping(produces = Datatables.MEDIA_TYPE, name = "datatables", value = "/dt")
|
||||
@ResponseBody
|
||||
public ResponseEntity<ConvertedDatatablesData<Book>> BooksCollectionThymeleafController.datatables(DatatablesColumns datatablesColumns, GlobalSearch search, DatatablesPageable pageable, @RequestParam("draw") Integer draw) {
|
||||
Page<Book> books = getBookService().findAll(search, pageable);
|
||||
long totalBooksCount = books.getTotalElements();
|
||||
if (search != null && StringUtils.isNotBlank(search.getText())) {
|
||||
totalBooksCount = getBookService().count();
|
||||
}
|
||||
ConvertedDatatablesData<Book> datatablesData = new ConvertedDatatablesData<Book>(books, totalBooksCount, draw, getConversionService(), datatablesColumns);
|
||||
return ResponseEntity.ok(datatablesData);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param search
|
||||
* @param pageable
|
||||
* @param locale
|
||||
* @return ResponseEntity
|
||||
*/
|
||||
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE, name = "select2", value = "/s2")
|
||||
@ResponseBody
|
||||
public ResponseEntity<Select2DataSupport<Book>> BooksCollectionThymeleafController.select2(GlobalSearch search, Pageable pageable, Locale locale) {
|
||||
Page<Book> books = getBookService().findAll(search, pageable);
|
||||
String idExpression = "#{id}";
|
||||
Select2DataSupport<Book> select2Data = new Select2DataWithConversion<Book>(books, idExpression, getConversionService());
|
||||
return ResponseEntity.ok(select2Data);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param dataBinder
|
||||
*/
|
||||
@InitBinder("book")
|
||||
public void BooksCollectionThymeleafController.initBookBinder(WebDataBinder dataBinder) {
|
||||
dataBinder.setDisallowedFields("id");
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param model
|
||||
*/
|
||||
public void BooksCollectionThymeleafController.populateFormats(Model model) {
|
||||
model.addAttribute("application_locale", LocaleContextHolder.getLocale().getLanguage());
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param model
|
||||
*/
|
||||
public void BooksCollectionThymeleafController.populateForm(Model model) {
|
||||
populateFormats(model);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param book
|
||||
* @param result
|
||||
* @param model
|
||||
* @return ModelAndView
|
||||
*/
|
||||
@PostMapping(name = "create")
|
||||
public ModelAndView BooksCollectionThymeleafController.create(@Valid @ModelAttribute Book book, BindingResult result, Model model) {
|
||||
if (result.hasErrors()) {
|
||||
populateForm(model);
|
||||
|
||||
return new ModelAndView("/books/create");
|
||||
}
|
||||
Book newBook = getBookService().save(book);
|
||||
UriComponents showURI = getItemLink().to(BooksItemThymeleafLinkFactory.SHOW).with("book", newBook.getId()).toUri();
|
||||
return new ModelAndView("redirect:" + showURI.toUriString());
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param model
|
||||
* @return ModelAndView
|
||||
*/
|
||||
@GetMapping(value = "/create-form", name = "createForm")
|
||||
public ModelAndView BooksCollectionThymeleafController.createForm(Model model) {
|
||||
populateForm(model);
|
||||
|
||||
model.addAttribute("book", new Book());
|
||||
return new ModelAndView("books/create");
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param ids
|
||||
* @return ResponseEntity
|
||||
*/
|
||||
@DeleteMapping(value = "/batch/{ids}", name = "deleteBatch")
|
||||
@ResponseBody
|
||||
public ResponseEntity<?> BooksCollectionThymeleafController.deleteBatch(@PathVariable("ids") Collection<Long> ids) {
|
||||
|
||||
getBookService().delete(ids);
|
||||
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that obtains the filtered and ordered records using the Datatables information and
|
||||
* export them to a new report file. (It ignores the current pagination).
|
||||
*
|
||||
* To generate the report file it uses the `DynamicJasper` library
|
||||
* (http://dynamicjasper.com). This library allows developers to generate reports dynamically
|
||||
* without use an specific template to each entity.
|
||||
*
|
||||
* To customize the appearance of ALL generated reports, you could customize the
|
||||
* "export_default.jrxml" template located in "src/main/resources/templates/reports/". However,
|
||||
* if you want to customize the appearance of this specific report, you could create a new
|
||||
* ".jrxml" file and provide it to the library replacing the `builder.setTemplateFile();`
|
||||
* operation used in this implementation.
|
||||
*
|
||||
* @param search GlobalSearch that contains the filter provided by the Datatables component.
|
||||
* @param pageable Pageable that contains the Sort info provided by the Datatabes component.
|
||||
* @param datatablesColumns Columns displayed in the Datatables component.
|
||||
* @param response The HttpServletResponse.
|
||||
* @param exporter An specific JasperReportsExporter to be used during export process.
|
||||
* @param fileName The final filename to use.
|
||||
* @param locale The current Locale in the view context.
|
||||
*/
|
||||
public void BooksCollectionThymeleafController.export(GlobalSearch search, @PageableDefault(size = 2147483647) Pageable pageable, String[] datatablesColumns, HttpServletResponse response, JasperReportsExporter exporter, String fileName, Locale locale) {
|
||||
// Obtain the filtered and ordered elements
|
||||
Page<Book> books = getBookService().findAll(search, pageable);
|
||||
|
||||
// Prevent generation of reports with empty data
|
||||
if (books == null || books.getContent().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Creates a new ReportBuilder using DynamicJasper library
|
||||
FastReportBuilder builder = new FastReportBuilder();
|
||||
|
||||
// IMPORTANT: By default, this application uses "export_default.jrxml"
|
||||
// to generate all reports. If you want to customize this specific report,
|
||||
// create a new ".jrxml" template and customize it. (Take in account the
|
||||
// DynamicJasper restrictions:
|
||||
// http://dynamicjasper.com/2010/10/06/how-to-use-custom-jrxml-templates/)
|
||||
builder.setTemplateFile("templates/reports/export_default.jrxml");
|
||||
|
||||
// The generated report will display the same columns as the Datatables component.
|
||||
// However, this is not mandatory. You could edit this code if you want to ignore
|
||||
// the provided datatablesColumns
|
||||
if (datatablesColumns != null) {
|
||||
for (String column : datatablesColumns) {
|
||||
// Delegates in addColumnToReportBuilder to include each datatables column
|
||||
// to the report builder
|
||||
addColumnToReportBuilder(column, builder, locale, fileName);
|
||||
}
|
||||
}
|
||||
|
||||
// This property resizes the columns to use full width page.
|
||||
// Set false value if you want to use the specific width of each column.
|
||||
builder.setUseFullPageWidth(true);
|
||||
|
||||
// Creates a new Jasper Reports Datasource using the obtained elements
|
||||
JRDataSource ds = new JRBeanCollectionDataSource(books.getContent());
|
||||
|
||||
// Generates the JasperReport
|
||||
JasperPrint jp;
|
||||
try {
|
||||
jp = DynamicJasperHelper.generateJasperPrint(builder.build(), new ClassicLayoutManager(), ds);
|
||||
}
|
||||
catch (JRException e) {
|
||||
String errorMessage = getMessageSource().getMessage("error_exportingErrorException",
|
||||
new Object[] {StringUtils.substringAfterLast(fileName, ".").toUpperCase()},
|
||||
String.format("Error while exporting data to StringUtils file", StringUtils.
|
||||
substringAfterLast(fileName, ".").toUpperCase()), locale);
|
||||
throw new ExportingErrorException(errorMessage);
|
||||
}
|
||||
|
||||
// Converts the JaspertReport element to a ByteArrayOutputStream and
|
||||
// write it into the response stream using the provided JasperReportExporter
|
||||
try {
|
||||
exporter.export(jp, fileName, response);
|
||||
}
|
||||
catch (JRException e) {
|
||||
String errorMessage = getMessageSource().getMessage("error_exportingErrorException",
|
||||
new Object[] {StringUtils.substringAfterLast(fileName, ".").toUpperCase()},
|
||||
String.format("Error while exporting data to StringUtils file", StringUtils.
|
||||
substringAfterLast(fileName, ".").toUpperCase()), locale);
|
||||
throw new ExportingErrorException(errorMessage);
|
||||
}
|
||||
catch (IOException e) {
|
||||
String errorMessage = getMessageSource().getMessage("error_exportingErrorException",
|
||||
new Object[] {StringUtils.substringAfterLast(fileName, ".").toUpperCase()},
|
||||
String.format("Error while exporting data to StringUtils file", StringUtils.
|
||||
substringAfterLast(fileName, ".").toUpperCase()), locale);
|
||||
throw new ExportingErrorException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* It delegates in the `export` method providing the necessary information
|
||||
* to generate a CSV report.
|
||||
*
|
||||
* @param search The GlobalSearch that contains the filter provided by the Datatables component
|
||||
* @param pageable The Pageable that contains the Sort info provided by the Datatabes component
|
||||
* @param datatablesColumns The Columns displayed in the Datatables component
|
||||
* @param response The HttpServletResponse
|
||||
* @return ResponseEntity
|
||||
*/
|
||||
@GetMapping(name = "exportCsv", value = "/export/csv")
|
||||
@ResponseBody
|
||||
public ResponseEntity<?> BooksCollectionThymeleafController.exportCsv(GlobalSearch search, @PageableDefault(size = 2147483647) Pageable pageable, @RequestParam("datatablesColumns") String[] datatablesColumns, HttpServletResponse response, Locale locale) {
|
||||
export(search, pageable, datatablesColumns, response, new JasperReportsCsvExporter(), "books_report.csv", locale);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* It delegates in the `export` method providing the necessary information
|
||||
* to generate a PDF report.
|
||||
*
|
||||
* @param search The GlobalSearch that contains the filter provided by the Datatables component
|
||||
* @param pageable The Pageable that contains the Sort info provided by the Datatabes component
|
||||
* @param datatablesColumns The Columns displayed in the Datatables component
|
||||
* @param response The HttpServletResponse
|
||||
* @return ResponseEntity
|
||||
*/
|
||||
@GetMapping(name = "exportPdf", value = "/export/pdf")
|
||||
@ResponseBody
|
||||
public ResponseEntity<?> BooksCollectionThymeleafController.exportPdf(GlobalSearch search, @PageableDefault(size = 2147483647) Pageable pageable, @RequestParam("datatablesColumns") String[] datatablesColumns, HttpServletResponse response, Locale locale) {
|
||||
export(search, pageable, datatablesColumns, response, new JasperReportsPdfExporter(), "books_report.pdf", locale);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* It delegates in the `export` method providing the necessary information
|
||||
* to generate a XLS report.
|
||||
*
|
||||
* @param search The GlobalSearch that contains the filter provided by the Datatables component
|
||||
* @param pageable The Pageable that contains the Sort info provided by the Datatabes component
|
||||
* @param datatablesColumns The Columns displayed in the Datatables component
|
||||
* @param response The HttpServletResponse
|
||||
* @return ResponseEntity
|
||||
*/
|
||||
@GetMapping(name = "exportXls", value = "/export/xls")
|
||||
@ResponseBody
|
||||
public ResponseEntity<?> BooksCollectionThymeleafController.exportXls(GlobalSearch search, @PageableDefault(size = 2147483647) Pageable pageable, @RequestParam("datatablesColumns") String[] datatablesColumns, HttpServletResponse response, Locale locale) {
|
||||
export(search, pageable, datatablesColumns, response, new JasperReportsXlsExporter(), "books_report.xls", locale);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method contains all the entity fields that are able to be displayed in a
|
||||
* report. The developer could add a new column to the report builder providing the
|
||||
* field name and the builder where the new field will be added as column.
|
||||
*
|
||||
* @param columnName the field name to show as column
|
||||
* @param builder The builder where the new field will be added as column.
|
||||
*/
|
||||
public void BooksCollectionThymeleafController.addColumnToReportBuilder(String columnName, FastReportBuilder builder, Locale locale, String fileName) {
|
||||
try {
|
||||
if (columnName.equals("id")) {
|
||||
builder.addColumn(getMessageSource().getMessage("label_book_id", null, "Id", locale), "id", Long.class.getName(), 50);
|
||||
}
|
||||
else if (columnName.equals("version")) {
|
||||
builder.addColumn(getMessageSource().getMessage("label_book_version", null, "Version", locale), "version", Integer.class.getName(), 100);
|
||||
}
|
||||
else if (columnName.equals("title")) {
|
||||
builder.addColumn(getMessageSource().getMessage("label_book_title", null, "Title", locale), "title", String.class.getName(), 100);
|
||||
}
|
||||
else if (columnName.equals("author")) {
|
||||
builder.addColumn(getMessageSource().getMessage("label_book_author", null, "Author", locale), "author", String.class.getName(), 100);
|
||||
}
|
||||
else if (columnName.equals("isbn")) {
|
||||
builder.addColumn(getMessageSource().getMessage("label_book_isbn", null, "Isbn", locale), "isbn", String.class.getName(), 100);
|
||||
}
|
||||
}
|
||||
catch (ColumnBuilderException e) {
|
||||
String errorMessage = getMessageSource().getMessage("error_exportingErrorException",
|
||||
new Object[] {StringUtils.substringAfterLast(fileName, ".").toUpperCase()},
|
||||
String.format("Error while exporting data to StringUtils file", StringUtils.
|
||||
substringAfterLast(fileName, ".").toUpperCase()), locale);
|
||||
throw new ExportingErrorException(errorMessage);
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
String errorMessage = getMessageSource().getMessage("error_exportingErrorException",
|
||||
new Object[] {StringUtils.substringAfterLast(fileName, ".").toUpperCase()},
|
||||
String.format("Error while exporting data to StringUtils file", StringUtils.
|
||||
substringAfterLast(fileName, ".").toUpperCase()), locale);
|
||||
throw new ExportingErrorException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.web;
|
||||
import org.springframework.roo.addon.web.mvc.thymeleaf.annotations.RooLinkFactory;
|
||||
|
||||
/**
|
||||
* = BooksCollectionThymeleafLinkFactory
|
||||
TODO Auto-generated class documentation
|
||||
*
|
||||
*/
|
||||
@RooLinkFactory(controller = BooksCollectionThymeleafController.class)
|
||||
public class BooksCollectionThymeleafLinkFactory {
|
||||
}
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.web;
|
||||
|
||||
import com.baeldung.web.BooksCollectionThymeleafController;
|
||||
import com.baeldung.web.BooksCollectionThymeleafLinkFactory;
|
||||
import io.springlets.web.mvc.util.MethodLinkFactory;
|
||||
import io.springlets.web.mvc.util.SpringletsMvcUriComponentsBuilder;
|
||||
import java.util.Map;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
|
||||
privileged aspect BooksCollectionThymeleafLinkFactory_Roo_LinkFactory {
|
||||
|
||||
declare parents: BooksCollectionThymeleafLinkFactory implements MethodLinkFactory<BooksCollectionThymeleafController>;
|
||||
|
||||
declare @type: BooksCollectionThymeleafLinkFactory: @Component;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String BooksCollectionThymeleafLinkFactory.LIST = "list";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String BooksCollectionThymeleafLinkFactory.DATATABLES = "datatables";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String BooksCollectionThymeleafLinkFactory.SELECT2 = "select2";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String BooksCollectionThymeleafLinkFactory.CREATE = "create";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String BooksCollectionThymeleafLinkFactory.CREATEFORM = "createForm";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String BooksCollectionThymeleafLinkFactory.DELETEBATCH = "deleteBatch";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String BooksCollectionThymeleafLinkFactory.EXPORTCSV = "exportCsv";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String BooksCollectionThymeleafLinkFactory.EXPORTPDF = "exportPdf";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String BooksCollectionThymeleafLinkFactory.EXPORTXLS = "exportXls";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return Class
|
||||
*/
|
||||
public Class<BooksCollectionThymeleafController> BooksCollectionThymeleafLinkFactory.getControllerClass() {
|
||||
return BooksCollectionThymeleafController.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param methodName
|
||||
* @param parameters
|
||||
* @param pathVariables
|
||||
* @return UriComponents
|
||||
*/
|
||||
public UriComponents BooksCollectionThymeleafLinkFactory.toUri(String methodName, Object[] parameters, Map<String, Object> pathVariables) {
|
||||
if (methodName.equals(LIST)) {
|
||||
return SpringletsMvcUriComponentsBuilder.fromMethodCall(SpringletsMvcUriComponentsBuilder.on(getControllerClass()).list(null)).buildAndExpand(pathVariables);
|
||||
}
|
||||
if (methodName.equals(DATATABLES)) {
|
||||
return SpringletsMvcUriComponentsBuilder.fromMethodCall(SpringletsMvcUriComponentsBuilder.on(getControllerClass()).datatables(null, null, null, null)).buildAndExpand(pathVariables);
|
||||
}
|
||||
if (methodName.equals(SELECT2)) {
|
||||
return SpringletsMvcUriComponentsBuilder.fromMethodCall(SpringletsMvcUriComponentsBuilder.on(getControllerClass()).select2(null, null, null)).buildAndExpand(pathVariables);
|
||||
}
|
||||
if (methodName.equals(CREATE)) {
|
||||
return SpringletsMvcUriComponentsBuilder.fromMethodCall(SpringletsMvcUriComponentsBuilder.on(getControllerClass()).create(null, null, null)).buildAndExpand(pathVariables);
|
||||
}
|
||||
if (methodName.equals(CREATEFORM)) {
|
||||
return SpringletsMvcUriComponentsBuilder.fromMethodCall(SpringletsMvcUriComponentsBuilder.on(getControllerClass()).createForm(null)).buildAndExpand(pathVariables);
|
||||
}
|
||||
if (methodName.equals(DELETEBATCH)) {
|
||||
return SpringletsMvcUriComponentsBuilder.fromMethodCall(SpringletsMvcUriComponentsBuilder.on(getControllerClass()).deleteBatch(null)).buildAndExpand(pathVariables);
|
||||
}
|
||||
if (methodName.equals(EXPORTCSV)) {
|
||||
return SpringletsMvcUriComponentsBuilder.fromMethodCall(SpringletsMvcUriComponentsBuilder.on(getControllerClass()).exportCsv(null, null, null, null, null)).buildAndExpand(pathVariables);
|
||||
}
|
||||
if (methodName.equals(EXPORTPDF)) {
|
||||
return SpringletsMvcUriComponentsBuilder.fromMethodCall(SpringletsMvcUriComponentsBuilder.on(getControllerClass()).exportPdf(null, null, null, null, null)).buildAndExpand(pathVariables);
|
||||
}
|
||||
if (methodName.equals(EXPORTXLS)) {
|
||||
return SpringletsMvcUriComponentsBuilder.fromMethodCall(SpringletsMvcUriComponentsBuilder.on(getControllerClass()).exportXls(null, null, null, null, null)).buildAndExpand(pathVariables);
|
||||
}
|
||||
throw new IllegalArgumentException("Invalid method name: " + methodName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.web;
|
||||
import com.baeldung.domain.Book;
|
||||
import org.springframework.roo.addon.web.mvc.controller.annotations.ControllerType;
|
||||
import org.springframework.roo.addon.web.mvc.controller.annotations.RooController;
|
||||
import org.springframework.roo.addon.web.mvc.thymeleaf.annotations.RooThymeleaf;
|
||||
|
||||
/**
|
||||
* = BooksItemThymeleafController
|
||||
TODO Auto-generated class documentation
|
||||
*
|
||||
*/
|
||||
@RooController(entity = Book.class, type = ControllerType.ITEM)
|
||||
@RooThymeleaf
|
||||
public class BooksItemThymeleafController {
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.web;
|
||||
|
||||
import com.baeldung.service.api.BookService;
|
||||
import com.baeldung.web.BooksItemThymeleafController;
|
||||
|
||||
privileged aspect BooksItemThymeleafController_Roo_Controller {
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
private BookService BooksItemThymeleafController.bookService;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return BookService
|
||||
*/
|
||||
public BookService BooksItemThymeleafController.getBookService() {
|
||||
return bookService;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param bookService
|
||||
*/
|
||||
public void BooksItemThymeleafController.setBookService(BookService bookService) {
|
||||
this.bookService = bookService;
|
||||
}
|
||||
|
||||
}
|
||||
+251
@@ -0,0 +1,251 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.web;
|
||||
|
||||
import com.baeldung.domain.Book;
|
||||
import com.baeldung.service.api.BookService;
|
||||
import com.baeldung.web.BooksItemThymeleafController;
|
||||
import com.baeldung.web.BooksItemThymeleafLinkFactory;
|
||||
import io.springlets.web.NotFoundException;
|
||||
import io.springlets.web.mvc.util.ControllerMethodLinkBuilderFactory;
|
||||
import io.springlets.web.mvc.util.MethodLinkBuilderFactory;
|
||||
import java.util.Locale;
|
||||
import javax.validation.Valid;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.InitBinder;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
|
||||
privileged aspect BooksItemThymeleafController_Roo_Thymeleaf {
|
||||
|
||||
declare @type: BooksItemThymeleafController: @Controller;
|
||||
|
||||
declare @type: BooksItemThymeleafController: @RequestMapping(value = "/books/{book}", name = "BooksItemThymeleafController", produces = MediaType.TEXT_HTML_VALUE);
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
private MessageSource BooksItemThymeleafController.messageSource;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
private MethodLinkBuilderFactory<BooksItemThymeleafController> BooksItemThymeleafController.itemLink;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated constructor documentation
|
||||
*
|
||||
* @param bookService
|
||||
* @param messageSource
|
||||
* @param linkBuilder
|
||||
*/
|
||||
@Autowired
|
||||
public BooksItemThymeleafController.new(BookService bookService, MessageSource messageSource, ControllerMethodLinkBuilderFactory linkBuilder) {
|
||||
setBookService(bookService);
|
||||
setMessageSource(messageSource);
|
||||
setItemLink(linkBuilder.of(BooksItemThymeleafController.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return MessageSource
|
||||
*/
|
||||
public MessageSource BooksItemThymeleafController.getMessageSource() {
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param messageSource
|
||||
*/
|
||||
public void BooksItemThymeleafController.setMessageSource(MessageSource messageSource) {
|
||||
this.messageSource = messageSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return MethodLinkBuilderFactory
|
||||
*/
|
||||
public MethodLinkBuilderFactory<BooksItemThymeleafController> BooksItemThymeleafController.getItemLink() {
|
||||
return itemLink;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param itemLink
|
||||
*/
|
||||
public void BooksItemThymeleafController.setItemLink(MethodLinkBuilderFactory<BooksItemThymeleafController> itemLink) {
|
||||
this.itemLink = itemLink;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param id
|
||||
* @param locale
|
||||
* @param method
|
||||
* @return Book
|
||||
*/
|
||||
@ModelAttribute
|
||||
public Book BooksItemThymeleafController.getBook(@PathVariable("book") Long id, Locale locale, HttpMethod method) {
|
||||
Book book = null;
|
||||
if (HttpMethod.PUT.equals(method)) {
|
||||
book = bookService.findOneForUpdate(id);
|
||||
} else {
|
||||
book = bookService.findOne(id);
|
||||
}
|
||||
|
||||
if (book == null) {
|
||||
String message = messageSource.getMessage("error_NotFound", new Object[] {"Book", id}, "The record couldn't be found", locale);
|
||||
throw new NotFoundException(message);
|
||||
}
|
||||
return book;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param book
|
||||
* @param model
|
||||
* @return ModelAndView
|
||||
*/
|
||||
@GetMapping(name = "show")
|
||||
public ModelAndView BooksItemThymeleafController.show(@ModelAttribute Book book, Model model) {
|
||||
model.addAttribute("book", book);
|
||||
return new ModelAndView("books/show");
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param book
|
||||
* @param model
|
||||
* @return ModelAndView
|
||||
*/
|
||||
@GetMapping(value = "/inline", name = "showInline")
|
||||
public ModelAndView BooksItemThymeleafController.showInline(@ModelAttribute Book book, Model model) {
|
||||
model.addAttribute("book", book);
|
||||
return new ModelAndView("books/showInline :: inline-content");
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param dataBinder
|
||||
*/
|
||||
@InitBinder("book")
|
||||
public void BooksItemThymeleafController.initBookBinder(WebDataBinder dataBinder) {
|
||||
dataBinder.setDisallowedFields("id");
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param model
|
||||
*/
|
||||
public void BooksItemThymeleafController.populateFormats(Model model) {
|
||||
model.addAttribute("application_locale", LocaleContextHolder.getLocale().getLanguage());
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param model
|
||||
*/
|
||||
public void BooksItemThymeleafController.populateForm(Model model) {
|
||||
populateFormats(model);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param book
|
||||
* @param model
|
||||
* @return ModelAndView
|
||||
*/
|
||||
@GetMapping(value = "/edit-form", name = "editForm")
|
||||
public ModelAndView BooksItemThymeleafController.editForm(@ModelAttribute Book book, Model model) {
|
||||
populateForm(model);
|
||||
|
||||
model.addAttribute("book", book);
|
||||
return new ModelAndView("books/edit");
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param book
|
||||
* @param version
|
||||
* @param concurrencyControl
|
||||
* @param result
|
||||
* @param model
|
||||
* @return ModelAndView
|
||||
*/
|
||||
@PutMapping(name = "update")
|
||||
public ModelAndView BooksItemThymeleafController.update(@Valid @ModelAttribute Book book, @RequestParam("version") Integer version, @RequestParam(value = "concurrency", required = false, defaultValue = "") String concurrencyControl, BindingResult result, Model model) {
|
||||
// Check if provided form contain errors
|
||||
if (result.hasErrors()) {
|
||||
populateForm(model);
|
||||
|
||||
return new ModelAndView("books/edit");
|
||||
}
|
||||
// Concurrency control
|
||||
Book existingBook = getBookService().findOne(book.getId());
|
||||
if(book.getVersion() != existingBook.getVersion() && StringUtils.isEmpty(concurrencyControl)){
|
||||
populateForm(model);
|
||||
model.addAttribute("book", book);
|
||||
model.addAttribute("concurrency", true);
|
||||
return new ModelAndView("books/edit");
|
||||
} else if(book.getVersion() != existingBook.getVersion() && "discard".equals(concurrencyControl)){
|
||||
populateForm(model);
|
||||
model.addAttribute("book", existingBook);
|
||||
model.addAttribute("concurrency", false);
|
||||
return new ModelAndView("books/edit");
|
||||
} else if(book.getVersion() != existingBook.getVersion() && "apply".equals(concurrencyControl)){
|
||||
// Update the version field to be able to override the existing values
|
||||
book.setVersion(existingBook.getVersion());
|
||||
}
|
||||
Book savedBook = getBookService().save(book);
|
||||
UriComponents showURI = getItemLink().to(BooksItemThymeleafLinkFactory.SHOW).with("book", savedBook.getId()).toUri();
|
||||
return new ModelAndView("redirect:" + showURI.toUriString());
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param book
|
||||
* @return ResponseEntity
|
||||
*/
|
||||
@ResponseBody
|
||||
@DeleteMapping(name = "delete")
|
||||
public ResponseEntity<?> BooksItemThymeleafController.delete(@ModelAttribute Book book) {
|
||||
getBookService().delete(book);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.web;
|
||||
import org.springframework.roo.addon.web.mvc.thymeleaf.annotations.RooLinkFactory;
|
||||
|
||||
/**
|
||||
* = BooksItemThymeleafLinkFactory
|
||||
TODO Auto-generated class documentation
|
||||
*
|
||||
*/
|
||||
@RooLinkFactory(controller = BooksItemThymeleafController.class)
|
||||
public class BooksItemThymeleafLinkFactory {
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.web;
|
||||
|
||||
import com.baeldung.web.BooksItemThymeleafController;
|
||||
import com.baeldung.web.BooksItemThymeleafLinkFactory;
|
||||
import io.springlets.web.mvc.util.MethodLinkFactory;
|
||||
import io.springlets.web.mvc.util.SpringletsMvcUriComponentsBuilder;
|
||||
import java.util.Map;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
|
||||
privileged aspect BooksItemThymeleafLinkFactory_Roo_LinkFactory {
|
||||
|
||||
declare parents: BooksItemThymeleafLinkFactory implements MethodLinkFactory<BooksItemThymeleafController>;
|
||||
|
||||
declare @type: BooksItemThymeleafLinkFactory: @Component;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String BooksItemThymeleafLinkFactory.SHOW = "show";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String BooksItemThymeleafLinkFactory.SHOWINLINE = "showInline";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String BooksItemThymeleafLinkFactory.EDITFORM = "editForm";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String BooksItemThymeleafLinkFactory.UPDATE = "update";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated attribute documentation
|
||||
*
|
||||
*/
|
||||
public static final String BooksItemThymeleafLinkFactory.DELETE = "delete";
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @return Class
|
||||
*/
|
||||
public Class<BooksItemThymeleafController> BooksItemThymeleafLinkFactory.getControllerClass() {
|
||||
return BooksItemThymeleafController.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param methodName
|
||||
* @param parameters
|
||||
* @param pathVariables
|
||||
* @return UriComponents
|
||||
*/
|
||||
public UriComponents BooksItemThymeleafLinkFactory.toUri(String methodName, Object[] parameters, Map<String, Object> pathVariables) {
|
||||
if (methodName.equals(SHOW)) {
|
||||
return SpringletsMvcUriComponentsBuilder.fromMethodCall(SpringletsMvcUriComponentsBuilder.on(getControllerClass()).show(null, null)).buildAndExpand(pathVariables);
|
||||
}
|
||||
if (methodName.equals(SHOWINLINE)) {
|
||||
return SpringletsMvcUriComponentsBuilder.fromMethodCall(SpringletsMvcUriComponentsBuilder.on(getControllerClass()).showInline(null, null)).buildAndExpand(pathVariables);
|
||||
}
|
||||
if (methodName.equals(EDITFORM)) {
|
||||
return SpringletsMvcUriComponentsBuilder.fromMethodCall(SpringletsMvcUriComponentsBuilder.on(getControllerClass()).editForm(null, null)).buildAndExpand(pathVariables);
|
||||
}
|
||||
if (methodName.equals(UPDATE)) {
|
||||
return SpringletsMvcUriComponentsBuilder.fromMethodCall(SpringletsMvcUriComponentsBuilder.on(getControllerClass()).update(null, null, null, null, null)).buildAndExpand(pathVariables);
|
||||
}
|
||||
if (methodName.equals(DELETE)) {
|
||||
return SpringletsMvcUriComponentsBuilder.fromMethodCall(SpringletsMvcUriComponentsBuilder.on(getControllerClass()).delete(null)).buildAndExpand(pathVariables);
|
||||
}
|
||||
throw new IllegalArgumentException("Invalid method name: " + methodName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.web;
|
||||
import org.springframework.roo.addon.web.mvc.thymeleaf.annotations.RooThymeleafMainController;
|
||||
|
||||
/**
|
||||
* = MainController
|
||||
TODO Auto-generated class documentation
|
||||
*
|
||||
*/
|
||||
@RooThymeleafMainController
|
||||
public class MainController {
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
|
||||
// You may push code into the target .java compilation unit if you wish to edit any member(s).
|
||||
|
||||
package com.baeldung.web;
|
||||
|
||||
import com.baeldung.web.MainController;
|
||||
import io.springlets.web.NotFoundException;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
privileged aspect MainController_Roo_Thymeleaf_MainController {
|
||||
|
||||
declare @type: MainController: @Controller;
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param model
|
||||
* @return String
|
||||
*/
|
||||
@GetMapping("/")
|
||||
public String MainController.index(Model model) {
|
||||
model.addAttribute("application_locale", LocaleContextHolder.getLocale().getLanguage());
|
||||
return "index";
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param model
|
||||
* @return String
|
||||
*/
|
||||
@GetMapping("/accessibility")
|
||||
public String MainController.accessibility(Model model) {
|
||||
model.addAttribute("application_locale", LocaleContextHolder.getLocale().getLanguage());
|
||||
return "accessibility";
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Auto-generated method documentation
|
||||
*
|
||||
* @param template
|
||||
* @return String
|
||||
*/
|
||||
@RequestMapping(value = "/js/{template}.js", method = RequestMethod.GET)
|
||||
public String MainController.javascriptTemplates(@PathVariable("template") String template) {
|
||||
if (StringUtils.hasLength(template)) {
|
||||
return template.concat(".js");
|
||||
}
|
||||
throw new NotFoundException("File not found");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.web.reports;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public class ExportingErrorException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 4075788919321977605L;
|
||||
|
||||
public ExportingErrorException() {
|
||||
super("Error while trying to export data to file.");
|
||||
}
|
||||
|
||||
public ExportingErrorException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.baeldung.web.reports;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import net.sf.jasperreports.engine.JRException;
|
||||
import net.sf.jasperreports.engine.JRExporterParameter;
|
||||
import net.sf.jasperreports.engine.JasperPrint;
|
||||
import net.sf.jasperreports.engine.export.JRCsvExporter;
|
||||
|
||||
/**
|
||||
* = JasperReportsCsvExporter
|
||||
*
|
||||
* A JasperReports exporter to export the report in CSV formats. This class
|
||||
* implements the interface {@link JasperReportsExporter}
|
||||
*/
|
||||
public class JasperReportsCsvExporter implements JasperReportsExporter {
|
||||
|
||||
/**
|
||||
* Generates a ByteArrayOutputStream from the provided JasperReport using
|
||||
* the {@link JRCsvExporter}. After that, the generated bytes array is
|
||||
* written in the {@link HttpServletResponse}
|
||||
*
|
||||
* @param jp
|
||||
* The generated JasperReport.
|
||||
* @param fileName
|
||||
* The fileName of the exported JasperReport
|
||||
* @param response
|
||||
* The HttpServletResponse where generated report has been
|
||||
* written
|
||||
* @throws JRException
|
||||
* during JasperReport export.
|
||||
* @throws IOException
|
||||
* when writes the ByteArrayOutputStream into the
|
||||
* HttpServletResponse
|
||||
*/
|
||||
@Override
|
||||
public void export(JasperPrint jp, String fileName, HttpServletResponse response) throws JRException, IOException {
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
// Create a JRCsvExporter instance
|
||||
JRCsvExporter exporter = new JRCsvExporter();
|
||||
|
||||
// Here we assign the parameters jp and baos to the exporter
|
||||
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
|
||||
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
|
||||
|
||||
// Retrieve the exported report in PDF format
|
||||
exporter.exportReport();
|
||||
|
||||
// Specifies the response header
|
||||
response.setHeader("Content-Disposition", "inline; filename=" + fileName);
|
||||
|
||||
// Make sure to set the correct content type
|
||||
// Each format has its own content type
|
||||
response.setContentType("text/csv");
|
||||
response.setContentLength(baos.size());
|
||||
|
||||
// Retrieve the output stream
|
||||
ServletOutputStream outputStream = response.getOutputStream();
|
||||
// Write to the output stream
|
||||
baos.writeTo(outputStream);
|
||||
// Flush the stream
|
||||
outputStream.flush();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.web.reports;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import net.sf.jasperreports.engine.JRException;
|
||||
import net.sf.jasperreports.engine.JasperPrint;
|
||||
import net.sf.jasperreports.export.Exporter;
|
||||
|
||||
/**
|
||||
* = JasperReportsExporter
|
||||
*
|
||||
* This interface defines the operations for a JasperReport exporter.
|
||||
*
|
||||
* JasperReports library already provides an Exporter interface called
|
||||
* {@link Exporter}. However, it doesn't provides an operation that writes the
|
||||
* exported JasperReport into the {@link HttpServletResponse}.
|
||||
*/
|
||||
public interface JasperReportsExporter {
|
||||
|
||||
/**
|
||||
* This operation must be implemented by every JasperReport exporter to be
|
||||
* able to write a generated report into a the {@link HttpServletResponse}}.
|
||||
*
|
||||
* @param jp
|
||||
* The generated JasperReport.
|
||||
* @param fileName
|
||||
* The fileName of the exported JasperReport
|
||||
* @param response
|
||||
* The HttpServletResponse where generated report has been
|
||||
* written
|
||||
* @throws JRException
|
||||
* during JasperReport export.
|
||||
* @throws IOException
|
||||
* when writes the ByteArrayOutputStream into the
|
||||
* HttpServletResponse
|
||||
*/
|
||||
public void export(JasperPrint jp, String fileName, HttpServletResponse response) throws JRException, IOException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.baeldung.web.reports;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import net.sf.jasperreports.engine.JRException;
|
||||
import net.sf.jasperreports.engine.JRExporterParameter;
|
||||
import net.sf.jasperreports.engine.JasperPrint;
|
||||
import net.sf.jasperreports.engine.export.JRPdfExporter;
|
||||
|
||||
/**
|
||||
* = JasperReportsPdfExporter
|
||||
*
|
||||
* A JasperReports exporter to export the report in PDF formats. This class
|
||||
* implements the interface {@link JasperReportsExporter}
|
||||
*/
|
||||
public class JasperReportsPdfExporter implements JasperReportsExporter {
|
||||
|
||||
/**
|
||||
* Generates a ByteArrayOutputStream from the provided JasperReport using
|
||||
* the {@link JRPdfExporter}. After that, the generated bytes array is
|
||||
* written in the {@link HttpServletResponse}
|
||||
*
|
||||
* @param jp
|
||||
* The generated JasperReport.
|
||||
* @param fileName
|
||||
* The fileName of the exported JasperReport
|
||||
* @param response
|
||||
* The HttpServletResponse where generated report has been
|
||||
* written
|
||||
* @throws JRException
|
||||
* during JasperReport export.
|
||||
* @throws IOException
|
||||
* when writes the ByteArrayOutputStream into the
|
||||
* HttpServletResponse
|
||||
*/
|
||||
@Override
|
||||
public void export(JasperPrint jp, String fileName, HttpServletResponse response) throws JRException, IOException {
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
// Create a JRPdfExporter instance
|
||||
JRPdfExporter exporter = new JRPdfExporter();
|
||||
|
||||
// Here we assign the parameters jp and baos to the exporter
|
||||
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
|
||||
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
|
||||
|
||||
// Retrieve the exported report in PDF format
|
||||
exporter.exportReport();
|
||||
|
||||
// Specifies the response header
|
||||
response.setHeader("Content-Disposition", "inline; filename=" + fileName);
|
||||
|
||||
// Make sure to set the correct content type
|
||||
// Each format has its own content type
|
||||
response.setContentType("application/pdf");
|
||||
response.setContentLength(baos.size());
|
||||
|
||||
// Retrieve the output stream
|
||||
ServletOutputStream outputStream = response.getOutputStream();
|
||||
// Write to the output stream
|
||||
baos.writeTo(outputStream);
|
||||
// Flush the stream
|
||||
outputStream.flush();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.baeldung.web.reports;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import net.sf.jasperreports.engine.JRException;
|
||||
import net.sf.jasperreports.engine.JRExporterParameter;
|
||||
import net.sf.jasperreports.engine.JasperPrint;
|
||||
import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
|
||||
import net.sf.jasperreports.engine.export.JRXlsExporter;
|
||||
|
||||
/**
|
||||
* = JasperReportsXlsExporter
|
||||
*
|
||||
* A JasperReports exporter to export the report in XLS formats. This class
|
||||
* implements the interface {@link JasperReportsExporter}
|
||||
*/
|
||||
public class JasperReportsXlsExporter implements JasperReportsExporter {
|
||||
|
||||
/**
|
||||
* Generates a ByteArrayOutputStream from the provided JasperReport using
|
||||
* the {@link JRXlsExporter}. After that, the generated bytes array is
|
||||
* written in the {@link HttpServletResponse}
|
||||
*
|
||||
* @param jp
|
||||
* The generated JasperReport.
|
||||
* @param fileName
|
||||
* The fileName of the exported JasperReport
|
||||
* @param response
|
||||
* The HttpServletResponse where generated report has been
|
||||
* written
|
||||
* @throws JRException
|
||||
* during JasperReport export.
|
||||
* @throws IOException
|
||||
* when writes the ByteArrayOutputStream into the
|
||||
* HttpServletResponse
|
||||
*/
|
||||
@Override
|
||||
public void export(JasperPrint jp, String fileName, HttpServletResponse response) throws JRException, IOException {
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
// Create a JRXlsExporter instance
|
||||
JRXlsExporter exporter = new JRXlsExporter();
|
||||
|
||||
// Here we assign the parameters jp and baos to the exporter
|
||||
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
|
||||
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
|
||||
|
||||
// Excel specific parameters
|
||||
exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
|
||||
exporter.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
|
||||
exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
|
||||
|
||||
// Retrieve the exported report in PDF format
|
||||
exporter.exportReport();
|
||||
|
||||
// Specifies the response header
|
||||
response.setHeader("Content-Disposition", "inline; filename=" + fileName);
|
||||
|
||||
// Make sure to set the correct content type
|
||||
// Each format has its own content type
|
||||
response.setContentType("application/vnd.ms-excel");
|
||||
response.setContentLength(baos.size());
|
||||
|
||||
// Retrieve the output stream
|
||||
ServletOutputStream outputStream = response.getOutputStream();
|
||||
// Write to the output stream
|
||||
baos.writeTo(outputStream);
|
||||
// Flush the stream
|
||||
outputStream.flush();
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user