diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..5d84aa3 --- /dev/null +++ b/README.txt @@ -0,0 +1 @@ +Exemplos de como paginacao sobe demanda com displaytag. \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a96f86d --- /dev/null +++ b/pom.xml @@ -0,0 +1,47 @@ + + 4.0.0 + br.com.javacia.exemplos + mentawai-displaytag + 0.0.1-SNAPSHOT + war + + + + + junit + junit + 4.8.1 + test + + + + + + org.mockito + mockito-core + 1.8.5 + test + + + + me.soliveirajr + mentawai + 2.2.3-SNAPSHOT + + + + + javax.servlet + jstl + 1.2 + + + + + displaytag + displaytag + 1.2 + + + \ No newline at end of file diff --git a/src/main/java/br/com/javacia/mentawai/displaytag/model/pojo/Cliente.java b/src/main/java/br/com/javacia/mentawai/displaytag/model/pojo/Cliente.java new file mode 100644 index 0000000..6adbddc --- /dev/null +++ b/src/main/java/br/com/javacia/mentawai/displaytag/model/pojo/Cliente.java @@ -0,0 +1,97 @@ +package br.com.javacia.mentawai.displaytag.model.pojo; + +import java.io.Serializable; + +public class Cliente implements Serializable { + private static final long serialVersionUID = 1L; + + private int idCliente; + + private String razaoSocial; + + private String nomeFantasia; + + private boolean ativo; + + + public Cliente( String razaoSocial, String nomeFantasia, boolean ativo) { + this.razaoSocial = razaoSocial; + this.nomeFantasia = nomeFantasia; + this.ativo = ativo; + } + + public Cliente(int idCliente, String razaoSocial, String nomeFantasia, boolean ativo) { + this(razaoSocial, nomeFantasia, ativo); + this.idCliente = idCliente; + } + + public Cliente() { + } + + public Cliente(int idCliente) { + this.idCliente = idCliente; + } + + public int getIdCliente() { + return idCliente; + } + + public String getRazaoSocial() { + return razaoSocial; + } + + public void setRazaoSocial(String razaoSocial) { + this.razaoSocial = razaoSocial; + } + + public String getNomeFantasia() { + return nomeFantasia; + } + + public void setNomeFantasia(String nomeFantasia) { + this.nomeFantasia = nomeFantasia; + } + + public boolean isAtivo() { + return ativo; + } + + public void setAtivo(boolean ativo) { + this.ativo = ativo; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getNomeFantasia() == null) ? 0 : getNomeFantasia().hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof Cliente)) { + return false; + } + Cliente other = (Cliente) obj; + if (getNomeFantasia() == null) { + if (other.getNomeFantasia() != null) { + return false; + } + } else if (!getNomeFantasia().equals(other.getNomeFantasia())) { + return false; + } + return true; + } + + public void setIdCliente(int idCliente) { + this.idCliente = idCliente; + } + +} diff --git a/src/main/java/br/com/javacia/mentawai/displaytag/model/service/ClienteService.java b/src/main/java/br/com/javacia/mentawai/displaytag/model/service/ClienteService.java new file mode 100644 index 0000000..b18ab6f --- /dev/null +++ b/src/main/java/br/com/javacia/mentawai/displaytag/model/service/ClienteService.java @@ -0,0 +1,122 @@ +package br.com.javacia.mentawai.displaytag.model.service; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; + +import org.mentawai.filter.Pagination; + +import br.com.javacia.mentawai.displaytag.model.pojo.Cliente; + +public class ClienteService { + + private static List clientes = new ArrayList(); + + public void save(Cliente cliente) { + + if(cliente.getIdCliente() > 0){ + + clientes.set(cliente.getIdCliente() - 1, cliente); + + }else{ + + clientes.add(cliente); + cliente.setIdCliente(clientes.size()); + } + + } + + public Cliente load(Cliente cliente) { + return clientes.get(cliente.getIdCliente() - 1); + } + + public List listByHint(Cliente cliente, Pagination pagination) { + if(clientes.size() == 0){ + return clientes; + } + + List listResult = filterByHint(cliente); + + int limitRecord = pagination.getInitRecord() + pagination.getLimitRecord(); + + if(limitRecord > listResult.size() ){ + limitRecord = listResult.size(); + } + + List subList = listResult.subList(pagination.getInitRecord(), limitRecord); + + Collections.sort(subList, new ClienteComparator(pagination.getSortAttribute(), pagination.isDesc())); + + return subList; + } + + private class ClienteComparator implements Comparator{ + + private boolean desc; + private String sortAttribute; + + public ClienteComparator(String sortAttribute, boolean desc) { + this.sortAttribute = sortAttribute; + this.desc = desc; + } + + public int compare(Cliente o1, Cliente o2) { + int compareTo = -1; + + if ("nomeFantasia".equals(sortAttribute)) { + compareTo = o1.getNomeFantasia().compareTo(o2.getNomeFantasia()); + } else if ("razaoSocial".equals(sortAttribute)) { + compareTo = o1.getRazaoSocial().compareTo(o2.getRazaoSocial()); + } + + if (desc) { + return compareTo * -1; + } else { + return compareTo; + } + } + + } + + protected List filterByHint(Cliente clienteHint) { + List clientesResult = new ArrayList(clientes); + + Iterator iterator = clientesResult.iterator(); + + while (iterator.hasNext()) { + Cliente cliente = (Cliente) iterator.next(); + + if(startsWithHint(cliente.getNomeFantasia(), clienteHint.getNomeFantasia()) && + startsWithHint(cliente.getRazaoSocial(), clienteHint.getRazaoSocial())){ + + continue; + + } + + iterator.remove(); + + } + + return clientesResult; + } + + private boolean startsWithHint(String value, String valueHint) { + + if(value == null || "".equals(value)){ + return true; + } + + valueHint = valueHint == null ? "" : valueHint; + + return value.startsWith(valueHint); + } + + public int countByHint(Cliente cliente) { + List listResult = filterByHint(cliente); + + return listResult.size(); + } + +} diff --git a/src/main/java/br/com/javacia/mentawai/displaytag/web/ApplicationManagerSys.java b/src/main/java/br/com/javacia/mentawai/displaytag/web/ApplicationManagerSys.java new file mode 100644 index 0000000..ccd1b77 --- /dev/null +++ b/src/main/java/br/com/javacia/mentawai/displaytag/web/ApplicationManagerSys.java @@ -0,0 +1,55 @@ +package br.com.javacia.mentawai.displaytag.web; + +import org.mentawai.core.ActionConfig; +import org.mentawai.filter.OVFilter; +import org.mentawai.filter.PaginationDisplayTagFilter; +import org.mentawai.filter.VOFilter; +import org.mentawai.i18n.LocaleManager; +import org.mentawai.list.BaseListData; +import org.mentawai.list.ListManager; + +import br.com.javacia.mentawai.displaytag.model.pojo.Cliente; +import br.com.javacia.mentawai.displaytag.web.action.ClienteAction; + +public class ApplicationManagerSys extends org.mentawai.core.ApplicationManager { + + + + @Override + public void loadLocales() { + LocaleManager.add("pt_BR"); + } + + @Override + public void loadActions() { + + /********************************************************* + * Crud Cliente + *********************************************************/ + ActionConfig formCliente = action("/cadastro/cliente", ClienteAction.class, "form") + .fwdOk("/jsp/cadastro/cliente/form.jsp"); + + action("/cadastro/cliente", ClienteAction.class, "save") + .filter(new VOFilter(Cliente.class)) + .redirOk("/cadastro/cliente.form.mtw?status=true") + .fwdError("/jsp/cadastro/cliente/form.jsp") + .chainError(formCliente); + + action("/cadastro/cliente", ClienteAction.class, "edit") + .filter(new VOFilter(Cliente.class)) + .filter(new OVFilter("cliente")) + .fwdOk("/jsp/cadastro/cliente/form.jsp") + .redirError("/jsp/error/404.jsp"); + + action("/cadastro/cliente", ClienteAction.class, "list") + .filter(new PaginationDisplayTagFilter("table", "nomeFantasia")) + .filter(new VOFilter("cliente", Cliente.class)) + .fwdOk("/jsp/cadastro/cliente/list.jsp"); + + } + + @Override + public void loadLists() throws Exception { + ListManager.addList(new BaseListData("listaSimNao", BaseListData.ORDER_BY_ID)); + } +} diff --git a/src/main/java/br/com/javacia/mentawai/displaytag/web/action/ClienteAction.java b/src/main/java/br/com/javacia/mentawai/displaytag/web/action/ClienteAction.java new file mode 100644 index 0000000..94b27cf --- /dev/null +++ b/src/main/java/br/com/javacia/mentawai/displaytag/web/action/ClienteAction.java @@ -0,0 +1,45 @@ +package br.com.javacia.mentawai.displaytag.web.action; + +import org.mentawai.core.BaseAction; +import org.mentawai.filter.Pagination; + +import br.com.javacia.mentawai.displaytag.model.pojo.Cliente; +import br.com.javacia.mentawai.displaytag.model.service.ClienteService; + +public class ClienteAction extends BaseAction { + + private ClienteService clienteService; + + public ClienteAction(){ + this(new ClienteService()); + } + + public ClienteAction(ClienteService clienteService){ + this.clienteService = clienteService; + } + + public void form() {} + + public String save(Cliente cliente) { + + clienteService.save(cliente); + + return SUCCESS; + } + + public String edit(Cliente cliente) { + + output.setValue("cliente", clienteService.load(cliente)); + + return SUCCESS; + } + + public String list(Cliente cliente, Pagination pagination) { + output.setValue("clientes", clienteService.listByHint(cliente, pagination)); + output.setValue("countClientes", (int) clienteService.countByHint(cliente)); + + return SUCCESS; + + } + +} diff --git a/src/main/resources/displaytag.properties b/src/main/resources/displaytag.properties new file mode 100644 index 0000000..88e2ef9 --- /dev/null +++ b/src/main/resources/displaytag.properties @@ -0,0 +1,2 @@ +locale.resolver=org.mentawai.i18n.I18nDisplaytagAdapter +locale.provider=org.mentawai.i18n.I18nDisplaytagAdapter \ No newline at end of file diff --git a/src/main/resources/displaytag_en_US.properties b/src/main/resources/displaytag_en_US.properties new file mode 100644 index 0000000..b601879 --- /dev/null +++ b/src/main/resources/displaytag_en_US.properties @@ -0,0 +1,43 @@ +vbasic.show.header = true +basic.empty.showtable = false +basic.msg.empty_list = Nothing found to display. +basic.msg.empty_list_row =Nothing found to display. +error.msg.invalid_page=invalid page + +sort.amount = page + +export.banner =Export options: {0} +export.banner.sepchar = | + +paging.banner.placement = bottom +paging.banner.item_name item +paging.banner.items_name = itens +paging.banner.no_item_found = No {0} found. +paging.banner.one_item_found = One {0} found. +paging.banner.all_items_found = {0} {1} found, displaying all {2}. +paging.banner.some_items_found ={0} {1} found, displaying {2} to {3}. +paging.banner.group_size = 5 +paging.banner.full =[First/Prev] {0} [Next/Last] +paging.banner.first = [First/Prev] {0} [Next/Last] +paging.banner.last = [First/Prev] {0} [Next/Last] +paging.banner.onepage = {0} +paging.banner.page.selected = {0} +paging.banner.page.link = {0} +paging.banner.page.separator = , \ + +export.types = csv excel xml pdf +export.pdf = true +export.amount = list +export.decorated = false +export.csv.filename= csvreport.csv +export.excel.filename= excelreport.xls +export.xml.filename= xmlreport.xml +export.pdf.filename= pdfreport.pdf + +css.tr.even = even +css.tr.odd = odd +css.th.sorted = sorted +css.th.ascending = order1 +css.th.descending = order2 +css.table = displaytagTable +css.th.sortable = sortable \ No newline at end of file diff --git a/src/main/resources/displaytag_pt_BR.properties b/src/main/resources/displaytag_pt_BR.properties new file mode 100644 index 0000000..2174191 --- /dev/null +++ b/src/main/resources/displaytag_pt_BR.properties @@ -0,0 +1,44 @@ +basic.show.header = true +basic.empty.showtable = false +basic.msg.empty_list = Nenhum registro encontrado. +basic.msg.empty_list_row = Nenhum registro encontrado. + +sort.amount = page + +export.banner = Exportar para: {0} +export.banner.sepchar = | + +paging.banner.placement = bottom +paging.banner.item_name item +paging.banner.items_name = itens +paging.banner.no_item_found = Nenhum {0} encontrado. +paging.banner.one_item_found = Um {0} encontrado. +paging.banner.all_items_found = {0} {1} encontrados, mostrando todos os {2}. +paging.banner.some_items_found = {0} {1} encontrados, mostrando de {2} até {3}. +paging.banner.group_size = 5 +paging.banner.full = [Primeira / Anterior] {0} [Próxima / Última] +paging.banner.first = [Primeira / Anterior] {0} [Próxima / Última] +paging.banner.last = [Primeira / Anterior] {0} [Próxima / Última] +paging.banner.onepage = +paging.banner.page.selected = {0} +paging.banner.page.link = {0} +paging.banner.page.separator = , + +factory.requestHelper = org.displaytag.util.DefaultRequestHelperFactory + +export.types = csv excel xml pdf +export.pdf = true +export.amount = list +export.decorated = false +export.csv.filename= csvreport.csv +export.excel.filename= excelreport.xls +export.xml.filename= xmlreport.xml +export.pdf.filename= pdfreport.pdf + +css.tr.even = even +css.tr.odd = odd +css.th.sorted = sorted +css.th.ascending = order1 +css.th.descending = order2 +css.table = displaytagTable +css.th.sortable = sortable \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..d016d41 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,26 @@ + + + Exemplo Mentawai e Displaytag. + + + Controller + org.mentawai.core.Controller + + Application Manager Mentawai + applicationManager + br.com.javacia.mentawai.displaytag.web.ApplicationManagerSys + + 1 + + + Controller + *.mtw + + + + index.jsp + + \ No newline at end of file diff --git a/src/main/webapp/i18n/master_pt_BR.i18n b/src/main/webapp/i18n/master_pt_BR.i18n new file mode 100644 index 0000000..d070906 --- /dev/null +++ b/src/main/webapp/i18n/master_pt_BR.i18n @@ -0,0 +1,16 @@ +razao.social=Razao Social +nome.fantasia=Nome Fantasia +novo=Novo +listar=Listar +salvar=Salvar +ativo=Ativo +filtros=Filtros +filtrar=Filtrar +sim=Sim +nao=Nao +editar=Editar + +salvo.com.sucesso=Salvo com sucesso. + +title.cadastro.cliente=Cadastro de Clientes. +title.listar.cliente=Listagem de Clientes. diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp new file mode 100644 index 0000000..c4593e2 --- /dev/null +++ b/src/main/webapp/index.jsp @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/main/webapp/jsp/cadastro/cliente/form.jsp b/src/main/webapp/jsp/cadastro/cliente/form.jsp new file mode 100644 index 0000000..3185976 --- /dev/null +++ b/src/main/webapp/jsp/cadastro/cliente/form.jsp @@ -0,0 +1,70 @@ +<%@ taglib uri="http://www.mentaframework.org/tags-mtw/" prefix="mtw"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + " /> + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/webapp/jsp/cadastro/cliente/list.jsp b/src/main/webapp/jsp/cadastro/cliente/list.jsp new file mode 100644 index 0000000..074fd07 --- /dev/null +++ b/src/main/webapp/jsp/cadastro/cliente/list.jsp @@ -0,0 +1,63 @@ +<%@ taglib uri="http://www.mentaframework.org/tags-mtw/" prefix="mtw"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> +<%@ taglib uri="http://displaytag.sf.net" prefix="display"%> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + " /> + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/webapp/jsp/cadastro/cliente/table.jsp b/src/main/webapp/jsp/cadastro/cliente/table.jsp new file mode 100644 index 0000000..f0c2cf2 --- /dev/null +++ b/src/main/webapp/jsp/cadastro/cliente/table.jsp @@ -0,0 +1,20 @@ +<%@ taglib uri="http://www.mentaframework.org/tags-mtw/" prefix="mtw"%> +<%@ taglib uri="http://displaytag.sf.net" prefix="display" %> + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/webapp/lists/listaSimNao_pt_BR.i18n b/src/main/webapp/lists/listaSimNao_pt_BR.i18n new file mode 100644 index 0000000..add03b9 --- /dev/null +++ b/src/main/webapp/lists/listaSimNao_pt_BR.i18n @@ -0,0 +1,2 @@ +true=Sim +false=Nao \ No newline at end of file diff --git a/src/test/java/br/com/javacia/mentawai/displaytag/web/action/ClienteActionTest.java b/src/test/java/br/com/javacia/mentawai/displaytag/web/action/ClienteActionTest.java new file mode 100644 index 0000000..24ca2dc --- /dev/null +++ b/src/test/java/br/com/javacia/mentawai/displaytag/web/action/ClienteActionTest.java @@ -0,0 +1,97 @@ +package br.com.javacia.mentawai.displaytag.web.action; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mentawai.core.BaseAction; +import org.mentawai.filter.Pagination; +import org.mentawai.util.MockAction; +import org.mockito.Mockito; + +import br.com.javacia.mentawai.displaytag.model.pojo.Cliente; +import br.com.javacia.mentawai.displaytag.model.service.ClienteService; + +public class ClienteActionTest { + + private ClienteAction clienteAction; + private ClienteService clienteService; + + @Before + public void setUp() throws Exception { + this.clienteService = Mockito.mock(ClienteService.class); + + this.clienteAction = new ClienteAction(clienteService); + MockAction.init(clienteAction); + } + + @Test + public void testSave() { + Cliente cliente = new Cliente("Razao", "Nome", true); + + Assert.assertEquals(BaseAction.SUCCESS, clienteAction.save(cliente)); + + Mockito.verify(clienteService).save(cliente); + } + + @Test + public void testEdit() { + + //Teste com sucesso no load. + Cliente cliente = new Cliente(1); + Cliente clienteRetorno = new Cliente(1, "Razao1", "Nome1", true); + Mockito.when(clienteService.load(cliente)).thenReturn(clienteRetorno); + + Assert.assertEquals(BaseAction.SUCCESS, clienteAction.edit(cliente)); + Assert.assertEquals(clienteRetorno, clienteAction.getOutput().getValue("cliente")); + + //Teste sem sucesso no load. + cliente = new Cliente(2); + Mockito.when(clienteService.load(cliente)).thenReturn(null); + + Assert.assertEquals(BaseAction.SUCCESS, clienteAction.edit(cliente)); + Assert.assertEquals(null, clienteAction.getOutput().getValue("cliente")); + + } + + @Test + public void testList() { + + Cliente cliente = new Cliente("Razao1", "Nome1", true); + Pagination pagination = new Pagination(0, 50, "razaoSocial", false); + + //Teste com elementos na lista. + List listClientes = createListClientes(3); + Mockito.when(clienteService.listByHint(cliente, pagination)).thenReturn(listClientes); + Mockito.when(clienteService.countByHint(cliente)).thenReturn(listClientes.size()); + + Assert.assertEquals(BaseAction.SUCCESS, clienteAction.list(cliente, pagination)); + + Assert.assertEquals(listClientes, clienteAction.getOutput().getValue("clientes")); + Assert.assertEquals(3, clienteAction.getOutput().getValue("countClientes")); + + //Teste sem elementos na lista. + listClientes = createListClientes(0); + Mockito.when(clienteService.listByHint(cliente, pagination)).thenReturn(listClientes); + Mockito.when(clienteService.countByHint(cliente)).thenReturn(listClientes.size()); + + Assert.assertEquals(BaseAction.SUCCESS, clienteAction.list(cliente, pagination)); + + Assert.assertEquals(listClientes, clienteAction.getOutput().getValue("clientes")); + Assert.assertEquals(0, clienteAction.getOutput().getValue("countClientes")); + + } + + private List createListClientes(int count) { + List clientes = new ArrayList(); + + for (int i = 1; i <= count; i++) { + clientes.add(new Cliente(i, "razao" + i, "nome" + i, true)); + } + + return clientes; + } + +}