Agregada busqueda a vista de autor list

Busca entre todos los campos de nombre, apellido paterno y apellido
materno
This commit is contained in:
Daniel Cortés
2019-05-07 19:02:41 -04:00
parent 202f13c70d
commit 64017090b3
5 changed files with 82 additions and 6 deletions

View File

@@ -14,18 +14,32 @@ public class AutorListController {
public AutorListController(AutorListPanel view) {
this.view = view;
this.autorRepository = new AutorRepository();
this.setupListeners();
}
public void reload() {
this.loadAutorTable();
}
private void setupListeners() {
this.view.getBuscarButton().addActionListener(e -> {
String term = this.view.getSearchField().getText();
List<Autor> autores = this.autorRepository.search(term);
this.loadAutorTable(autores);
});
}
private void loadAutorTable() {
List<Autor> autores = this.autorRepository.getAll();
loadAutorTable(autores);
}
private void loadAutorTable(List<Autor> autores) {
BaseTableModel<Autor> model = this.view.getAutorModel();
model.addRows(autores);
}
public AutorListPanel getView() {
return view;
}

View File

@@ -1,5 +1,6 @@
package xyz.danielcortes.models;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
@@ -63,6 +64,9 @@ public class Autor {
}
public Set<Libro> getLibros() {
if (this.libros == null) {
this.libros = new HashSet<>();
}
return libros;
}

View File

@@ -3,19 +3,39 @@ package xyz.danielcortes.repository;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import xyz.danielcortes.framework.PersistenceManager;
import xyz.danielcortes.models.Autor;
public class AutorRepository {
private EntityManager em;
public AutorRepository() { this.em = PersistenceManager.getEntityManager(); }
public AutorRepository() {
this.em = PersistenceManager.getEntityManager();
}
public List<Autor> getAll() {
TypedQuery<Autor> query = em.createQuery("SELECT a FROM Autor a", Autor.class);
return query.getResultList();
}
public List<Autor> search(String term) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Autor> query = cb.createQuery(Autor.class);
Root<Autor> r = query.from(Autor.class);
query.where(
cb.or(
cb.like(cb.lower(r.get("nombre")), "%" + term.toLowerCase() + "%"),
cb.like(cb.lower(r.get("apellidoPaterno")), "%" + term.toLowerCase() + "%"),
cb.like(cb.lower(r.get("apellidoMaterno")), "%" + term.toLowerCase() + "%")
)
);
return em.createQuery(query).getResultList();
}
public void save(Autor autor) {
em.getTransaction().begin();
em.persist(autor);

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="xyz.danielcortes.views.autor.AutorListPanel">
<grid id="27dc6" binding="contentPane" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="20" left="20" bottom="20" right="20"/>
<constraints>
<xy x="20" y="20" width="501" height="443"/>
@@ -10,7 +10,7 @@
<children>
<scrollpane id="4cd2">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false">
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="400" height="-1"/>
</grid>
</constraints>
@@ -23,6 +23,22 @@
</component>
</children>
</scrollpane>
<component id="ced19" class="javax.swing.JTextField" binding="searchField" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="d89c0" class="javax.swing.JButton" binding="buscarButton" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Buscar"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@@ -4,10 +4,12 @@ import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import java.awt.Dimension;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import xyz.danielcortes.framework.BaseTableModel;
import xyz.danielcortes.models.Autor;
@@ -15,6 +17,8 @@ public class AutorListPanel {
private JPanel contentPane;
private JTable autorTable;
private JTextField searchField;
private JButton buscarButton;
private BaseTableModel<Autor> autorModel;
public JPanel getContentPane() {
@@ -29,6 +33,14 @@ public class AutorListPanel {
return autorModel;
}
public JButton getBuscarButton() {
return buscarButton;
}
public JTextField getSearchField() {
return searchField;
}
private void createUIComponents() {
this.createAutorTable();
}
@@ -67,14 +79,24 @@ public class AutorListPanel {
private void $$$setupUI$$$() {
createUIComponents();
contentPane = new JPanel();
contentPane.setLayout(new GridLayoutManager(1, 1, new Insets(20, 20, 20, 20), -1, -1));
contentPane.setLayout(new GridLayoutManager(2, 2, new Insets(20, 20, 20, 20), -1, -1));
final JScrollPane scrollPane1 = new JScrollPane();
contentPane.add(scrollPane1,
new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
new GridConstraints(1, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW,
GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null,
new Dimension(400, -1), null, 0, false));
scrollPane1.setViewportView(autorTable);
searchField = new JTextField();
contentPane.add(searchField, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST,
GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW,
GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
buscarButton = new JButton();
buscarButton.setText("Buscar");
contentPane.add(buscarButton, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER,
GridConstraints.FILL_HORIZONTAL,
GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
}
/**