nel:
Algo importante douglasfborba. O teu insert me parece a nível DAO.
O ideal é que ele receba como parâmetro um objeto e não vários parâmetros, conforme você fez.
A camada de serviço, que vai usufruir do DAO, é que deve ter a preocupação em verificar quais campos são obrigatórios e lançar uma exceção, caso algum não esteja preenchido.
Infelizmente, tratativas de tela normalmente são maçantes e nos obrigam a verificações desse tipo.
O que tu pode fazer é criar um Map ou List com os campos obrigatórios e fazer a validação da lista.
Podes talvez partir para algum pattern, mas o que me vem a cabeça agora é o Strategy e não sei se é aplicável a sua necessidade, é de se pensar.
nel esta é a primeira vez que trabalho com Design Patterns, então não sei muito ao seu respeito e desculpe-me se eu não entendimuito bem sua colocação, mas pretendo futuramente dar uma estudada mais a fundo sobre o assunto. Já em relação ao método, a ideia é que ele faça parte da camada de Controller, então imagino que é ele quem deve fazer as verificações. Para ficar mais claro, seguem abaixo as classes relacionadas, exceto a do formulário que ainda não está pronta:
package br.unicentro.lynx.model.pojo;
public class Publisher {
private Integer id_publisher;
private String name_publisher;
private Contact contact_publisher;
public Publisher() {
}
public Publisher(Integer id_publisher, String name_publisher) {
this.id_publisher = id_publisher;
this.name_publisher = name_publisher;
}
public Publisher(String name_publisher, Contact contact_publisher) {
this.name_publisher = name_publisher;
this.contact_publisher = contact_publisher;
}
public Publisher(Integer id_publisher, String name_publisher,
Contact contact_publisher) {
this.id_publisher = id_publisher;
this.name_publisher = name_publisher;
this.contact_publisher = contact_publisher;
}
public Integer getIdPublisher() {
return id_publisher;
}
public void setIdPublisher(Integer id_publisher) {
this.id_publisher = id_publisher;
}
public String getNamePublisher() {
return name_publisher;
}
public void setNamePublisher(String name_publisher) {
this.name_publisher = name_publisher;
}
public Contact getContactPublisher() {
return contact_publisher;
}
public void setContactPublisher(Contact contact_publisher) {
this.contact_publisher = contact_publisher;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("PublisherController [id_publisher=")
.append(id_publisher).append(", name_publisher=")
.append(name_publisher).append(", id_contact_publisher=")
.append(contact_publisher).append("]");
return builder.toString();
}
}
package br.unicentro.lynx.model.pojo;
public class Contact {
private Integer id_contact;
private String street_contact;
private String number_contact;
private String zip_code_contact;
private String complement_contact;
private String city_contact;
private String state_contact;
private String phone_contact;
private String email_contact;
public Contact() {
}
public Contact(String street_contact, String number_contact,
String zip_code_contact, String complement_contact,
String city_contact, String state_contact, String phone_contact,
String email_contact) {
this.street_contact = street_contact;
this.number_contact = number_contact;
this.zip_code_contact = zip_code_contact;
this.complement_contact = complement_contact;
this.city_contact = city_contact;
this.state_contact = state_contact;
this.phone_contact = phone_contact;
this.email_contact = email_contact;
}
public Contact(Integer id_contact, String street_contact,
String number_contact, String zip_code_contact,
String complement_contact, String city_contact,
String state_contact, String phone_contact, String email_contact) {
this.id_contact = id_contact;
this.street_contact = street_contact;
this.number_contact = number_contact;
this.zip_code_contact = zip_code_contact;
this.complement_contact = complement_contact;
this.city_contact = city_contact;
this.state_contact = state_contact;
this.phone_contact = phone_contact;
this.email_contact = email_contact;
}
public Integer getIdContact() {
return id_contact;
}
public void setIdContact(Integer id_contact) {
this.id_contact = id_contact;
}
public String getStreetContact() {
return street_contact;
}
public void setStreetContact(String street_contact) {
this.street_contact = street_contact;
}
public String getNumberContact() {
return number_contact;
}
public void setNumberContact(String number_contact) {
this.number_contact = number_contact;
}
public String getZipCodeContact() {
return zip_code_contact;
}
public void setZipCodeContact(String zip_code_contact) {
this.zip_code_contact = zip_code_contact;
}
public String getComplementContact() {
return complement_contact;
}
public void setComplementContact(String complement_contact) {
this.complement_contact = complement_contact;
}
public String getCityContact() {
return city_contact;
}
public void setCityContact(String city_contact) {
this.city_contact = city_contact;
}
public String getStateContact() {
return state_contact;
}
public void setStateContact(String state_contact) {
this.state_contact = state_contact;
}
public String getPhoneContact() {
return phone_contact;
}
public void setPhoneContact(String phone_contact) {
this.phone_contact = phone_contact;
}
public String getEmailContact() {
return email_contact;
}
public void setEmailContact(String email_contact) {
this.email_contact = email_contact;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("ContactController [id_contact=").append(id_contact)
.append(", street_contact=").append(street_contact)
.append(", number_contact=").append(number_contact)
.append(", zip_code_contact=").append(zip_code_contact)
.append(", complement_contact=").append(complement_contact)
.append(", city_contact=").append(city_contact)
.append(", state_contact=").append(state_contact)
.append(", phone_contact=").append(phone_contact)
.append(", email_contact=").append(email_contact).append("]");
return builder.toString();
}
}
package br.unicentro.lynx.model.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import br.unicentro.lynx.model.factory.ConnectionFactory;
import br.unicentro.lynx.model.pojo.Publisher;
public class PublisherDao {
private String sql;
private Connection connection;
private PreparedStatement statement;
private ResultSet result;
public void insert(Publisher publisher) {
open();
new ContactDao().insert(publisher.getContactPublisher());
this.sql = "INSERT INTO Publishers (name_publisher, id_contact_publisher) VALUES (?, ?)";
try {
this.statement = connection.prepareStatement(sql);
this.statement.setString(1, publisher.getNamePublisher());
this.statement.setInt(2, new ContactDao().searchLastRegister()
.getIdContact());
this.statement.execute();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close();
}
}
public void update(Publisher publisher) {
open();
new ContactDao().update(publisher.getContactPublisher());
this.sql = "UPDATE Publishers SET name_publisher = ? WHERE id_publisher = ?";
try {
this.statement = connection.prepareStatement(sql);
this.statement.setString(1, publisher.getNamePublisher());
this.statement.setInt(2, publisher.getIdPublisher());
this.statement.execute();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close();
}
}
public void delete(Integer id_publisher) {
open();
this.sql = "DELETE FROM Publishers WHERE id_publisher = ?";
try {
this.statement = connection.prepareStatement(sql);
this.statement.setInt(1, id_publisher);
this.statement.execute();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close();
}
}
public Publisher searchId(Integer id_publisher) {
open();
this.sql = "SELECT * FROM Publishers WHERE id_publisher = ?";
try {
this.statement = connection.prepareStatement(sql);
this.statement.setInt(1, id_publisher);
this.result = this.statement.executeQuery();
if (this.result.next()) {
return new Publisher(result.getInt("id_publisher"),
result.getString("name_publisher"),
new ContactDao().searchId(result
.getInt("id_contact_publisher")));
} else {
return null;
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close();
}
}
public ArrayList<Publisher> listAll() {
open();
this.sql = "SELECT * FROM Publishers";
ArrayList<Publisher> publishers = new ArrayList<Publisher>();
try {
this.statement = connection.prepareStatement(sql);
this.result = this.statement.executeQuery();
while (this.result.next()) {
publishers.add(new Publisher(result.getInt("id_publisher"),
result.getString("name_publisher"),
new ContactDao().searchId(result
.getInt("id_contact_publisher"))));
}
return publishers;
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
this.result.close();
close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
private void open() {
this.connection = ConnectionFactory.getConnection(
ConnectionFactory.URL, ConnectionFactory.USER,
ConnectionFactory.PASSWORD);
}
private void close() {
try {
this.statement.close();
this.connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
package br.unicentro.lynx.model.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import br.unicentro.lynx.model.factory.ConnectionFactory;
import br.unicentro.lynx.model.pojo.Contact;
public class ContactDao {
private String sql;
private Connection connection;
private PreparedStatement statement;
private ResultSet result;
public void insert(Contact contact) {
open();
this.sql = "INSERT INTO Contacts (street_contact, number_contact, zip_code_contact, complement_contact, city_contact, state_contact, phone_contact, email_contact) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
try {
this.statement = connection.prepareStatement(sql);
this.statement.setString(1, contact.getStreetContact());
this.statement.setString(2, contact.getNumberContact());
this.statement.setString(3, contact.getZipCodeContact());
this.statement.setString(4, contact.getComplementContact());
this.statement.setString(5, contact.getCityContact());
this.statement.setString(6, contact.getStateContact());
this.statement.setString(7, contact.getPhoneContact());
this.statement.setString(8, contact.getEmailContact());
this.statement.execute();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close();
}
}
public void update(Contact contact) {
open();
this.sql = "UPDATE Contacts SET street_contact = ?, number_contact = ?, zip_code_contact = ?, complement_contact = ?, city_contact = ?, state_contact = ?, phone_contact = ?, email_contact = ? WHERE id_contact = ?";
try {
this.statement = connection.prepareStatement(sql);
this.statement.setString(1, contact.getStreetContact());
this.statement.setString(2, contact.getNumberContact());
this.statement.setString(3, contact.getZipCodeContact());
this.statement.setString(4, contact.getComplementContact());
this.statement.setString(5, contact.getCityContact());
this.statement.setString(6, contact.getStateContact());
this.statement.setString(7, contact.getPhoneContact());
this.statement.setString(8, contact.getEmailContact());
this.statement.setInt(9, contact.getIdContact());
this.statement.execute();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close();
}
}
public void delete(Integer id_contact) {
open();
this.sql = "DELETE FROM Contacts WHERE id_contact = ?";
try {
this.statement = connection.prepareStatement(sql);
this.statement.setInt(1, id_contact);
this.statement.execute();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close();
}
}
public Contact searchId(Integer id_contact) {
open();
this.sql = "SELECT * FROM Contacts WHERE id_contact = ?";
try {
this.statement = connection.prepareStatement(sql);
this.statement.setInt(1, id_contact);
this.result = this.statement.executeQuery();
if (this.result.next()) {
return new Contact(result.getInt("id_contact"),
result.getString("street_contact"),
result.getString("number_contact"),
result.getString("zip_code_contact"),
result.getString("complement_contact"),
result.getString("city_contact"),
result.getString("state_contact"),
result.getString("phone_contact"),
result.getString("email_contact"));
} else {
return null;
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close();
}
}
public Contact searchLastRegister() {
open();
this.sql = "SELECT * FROM Contacts WHERE id_contact = (SELECT MAX(id_contact) FROM Contacts)";
try {
this.statement = connection.prepareStatement(sql);
this.result = this.statement.executeQuery();
if (this.result.next()) {
return new Contact(result.getInt("id_contact"),
result.getString("street_contact"),
result.getString("number_contact"),
result.getString("zip_code_contact"),
result.getString("complement_contact"),
result.getString("city_contact"),
result.getString("state_contact"),
result.getString("phone_contact"),
result.getString("email_contact"));
} else {
return null;
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close();
}
}
public ArrayList<Contact> listAll() {
open();
this.sql = "SELECT * FROM Contacts";
ArrayList<Contact> contacts = new ArrayList<Contact>();
try {
this.statement = connection.prepareStatement(sql);
this.result = this.statement.executeQuery();
while (this.result.next()) {
contacts.add(new Contact(result.getInt("id_contact"), result
.getString("street_contact"), result
.getString("number_contact"), result
.getString("zip_code_contact"), result
.getString("complement_contact"), result
.getString("city_contact"), result
.getString("state_contact"), result
.getString("phone_contact"), result
.getString("email_contact")));
}
return contacts;
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
this.result.close();
close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
private void open() {
this.connection = ConnectionFactory.getConnection(
ConnectionFactory.URL, ConnectionFactory.USER,
ConnectionFactory.PASSWORD);
}
private void close() {
try {
this.statement.close();
this.connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}