Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Caio Oliveira committed Oct 27, 2011
0 parents commit 4f7f7fc
Show file tree
Hide file tree
Showing 17 changed files with 751 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Exemplos de como paginacao sobe demanda com displaytag.
47 changes: 47 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.javacia.exemplos</groupId>
<artifactId>mentawai-displaytag</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<dependencies>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>


<!-- Mockito -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.8.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>me.soliveirajr</groupId>
<artifactId>mentawai</artifactId>
<version>2.2.3-SNAPSHOT</version>
</dependency>

<!-- Jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- DisplayTag -->
<dependency>
<groupId>displaytag</groupId>
<artifactId>displaytag</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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<Cliente> clientes = new ArrayList<Cliente>();

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<Cliente> listByHint(Cliente cliente, Pagination pagination) {
if(clientes.size() == 0){
return clientes;
}

List<Cliente> listResult = filterByHint(cliente);

int limitRecord = pagination.getInitRecord() + pagination.getLimitRecord();

if(limitRecord > listResult.size() ){
limitRecord = listResult.size();
}

List<Cliente> subList = listResult.subList(pagination.getInitRecord(), limitRecord);

Collections.sort(subList, new ClienteComparator(pagination.getSortAttribute(), pagination.isDesc()));

return subList;
}

private class ClienteComparator implements Comparator<Cliente>{

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<Cliente> filterByHint(Cliente clienteHint) {
List<Cliente> clientesResult = new ArrayList<Cliente>(clientes);

Iterator<Cliente> 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<Cliente> listResult = filterByHint(cliente);

return listResult.size();
}

}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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;

}

}
2 changes: 2 additions & 0 deletions src/main/resources/displaytag.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
locale.resolver=org.mentawai.i18n.I18nDisplaytagAdapter
locale.provider=org.mentawai.i18n.I18nDisplaytagAdapter
Loading

0 comments on commit 4f7f7fc

Please sign in to comment.