Agregada busqueda a vista de autor list
Busca entre todos los campos de nombre, apellido paterno y apellido materno
This commit is contained in:
@@ -14,18 +14,32 @@ public class AutorListController {
|
|||||||
public AutorListController(AutorListPanel view) {
|
public AutorListController(AutorListPanel view) {
|
||||||
this.view = view;
|
this.view = view;
|
||||||
this.autorRepository = new AutorRepository();
|
this.autorRepository = new AutorRepository();
|
||||||
|
this.setupListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reload() {
|
public void reload() {
|
||||||
this.loadAutorTable();
|
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() {
|
private void loadAutorTable() {
|
||||||
List<Autor> autores = this.autorRepository.getAll();
|
List<Autor> autores = this.autorRepository.getAll();
|
||||||
|
loadAutorTable(autores);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadAutorTable(List<Autor> autores) {
|
||||||
BaseTableModel<Autor> model = this.view.getAutorModel();
|
BaseTableModel<Autor> model = this.view.getAutorModel();
|
||||||
model.addRows(autores);
|
model.addRows(autores);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public AutorListPanel getView() {
|
public AutorListPanel getView() {
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package xyz.danielcortes.models;
|
package xyz.danielcortes.models;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
@@ -14,7 +15,7 @@ import javax.persistence.Table;
|
|||||||
public class Autor {
|
public class Autor {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy= GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
@Column(name = "id", nullable = false)
|
@Column(name = "id", nullable = false)
|
||||||
private Integer id;
|
private Integer id;
|
||||||
|
|
||||||
@@ -63,6 +64,9 @@ public class Autor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Set<Libro> getLibros() {
|
public Set<Libro> getLibros() {
|
||||||
|
if (this.libros == null) {
|
||||||
|
this.libros = new HashSet<>();
|
||||||
|
}
|
||||||
return libros;
|
return libros;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,19 +3,39 @@ package xyz.danielcortes.repository;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.TypedQuery;
|
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.framework.PersistenceManager;
|
||||||
import xyz.danielcortes.models.Autor;
|
import xyz.danielcortes.models.Autor;
|
||||||
|
|
||||||
public class AutorRepository {
|
public class AutorRepository {
|
||||||
|
|
||||||
private EntityManager em;
|
private EntityManager em;
|
||||||
|
|
||||||
public AutorRepository() { this.em = PersistenceManager.getEntityManager(); }
|
public AutorRepository() {
|
||||||
|
this.em = PersistenceManager.getEntityManager();
|
||||||
|
}
|
||||||
|
|
||||||
public List<Autor> getAll() {
|
public List<Autor> getAll() {
|
||||||
TypedQuery<Autor> query = em.createQuery("SELECT a FROM Autor a", Autor.class);
|
TypedQuery<Autor> query = em.createQuery("SELECT a FROM Autor a", Autor.class);
|
||||||
return query.getResultList();
|
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) {
|
public void save(Autor autor) {
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
em.persist(autor);
|
em.persist(autor);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?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">
|
<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"/>
|
<margin top="20" left="20" bottom="20" right="20"/>
|
||||||
<constraints>
|
<constraints>
|
||||||
<xy x="20" y="20" width="501" height="443"/>
|
<xy x="20" y="20" width="501" height="443"/>
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
<children>
|
<children>
|
||||||
<scrollpane id="4cd2">
|
<scrollpane id="4cd2">
|
||||||
<constraints>
|
<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"/>
|
<preferred-size width="400" height="-1"/>
|
||||||
</grid>
|
</grid>
|
||||||
</constraints>
|
</constraints>
|
||||||
@@ -23,6 +23,22 @@
|
|||||||
</component>
|
</component>
|
||||||
</children>
|
</children>
|
||||||
</scrollpane>
|
</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>
|
</children>
|
||||||
</grid>
|
</grid>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
@@ -4,10 +4,12 @@ import com.intellij.uiDesigner.core.GridConstraints;
|
|||||||
import com.intellij.uiDesigner.core.GridLayoutManager;
|
import com.intellij.uiDesigner.core.GridLayoutManager;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
|
import javax.swing.JButton;
|
||||||
import javax.swing.JComponent;
|
import javax.swing.JComponent;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.JScrollPane;
|
import javax.swing.JScrollPane;
|
||||||
import javax.swing.JTable;
|
import javax.swing.JTable;
|
||||||
|
import javax.swing.JTextField;
|
||||||
import xyz.danielcortes.framework.BaseTableModel;
|
import xyz.danielcortes.framework.BaseTableModel;
|
||||||
import xyz.danielcortes.models.Autor;
|
import xyz.danielcortes.models.Autor;
|
||||||
|
|
||||||
@@ -15,6 +17,8 @@ public class AutorListPanel {
|
|||||||
|
|
||||||
private JPanel contentPane;
|
private JPanel contentPane;
|
||||||
private JTable autorTable;
|
private JTable autorTable;
|
||||||
|
private JTextField searchField;
|
||||||
|
private JButton buscarButton;
|
||||||
private BaseTableModel<Autor> autorModel;
|
private BaseTableModel<Autor> autorModel;
|
||||||
|
|
||||||
public JPanel getContentPane() {
|
public JPanel getContentPane() {
|
||||||
@@ -29,6 +33,14 @@ public class AutorListPanel {
|
|||||||
return autorModel;
|
return autorModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JButton getBuscarButton() {
|
||||||
|
return buscarButton;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JTextField getSearchField() {
|
||||||
|
return searchField;
|
||||||
|
}
|
||||||
|
|
||||||
private void createUIComponents() {
|
private void createUIComponents() {
|
||||||
this.createAutorTable();
|
this.createAutorTable();
|
||||||
}
|
}
|
||||||
@@ -67,14 +79,24 @@ public class AutorListPanel {
|
|||||||
private void $$$setupUI$$$() {
|
private void $$$setupUI$$$() {
|
||||||
createUIComponents();
|
createUIComponents();
|
||||||
contentPane = new JPanel();
|
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();
|
final JScrollPane scrollPane1 = new JScrollPane();
|
||||||
contentPane.add(scrollPane1,
|
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,
|
||||||
GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null,
|
GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null,
|
||||||
new Dimension(400, -1), null, 0, false));
|
new Dimension(400, -1), null, 0, false));
|
||||||
scrollPane1.setViewportView(autorTable);
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user