package DataManagement;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
public class Client extends BaseDataManagement implements Comparable<Object>{
private int nClient;
private int nCont;
private String name;
private String adress;
private int codPost1;
private int codPost2;
private String freg;
private int phoneNumber;
private int mobilePhone;
private String email;
private Date bornDate;
private ArrayList<Car> lstCar;
/**
* @param nCont
* @param name
* @param adress
* @param codPost1
* @param codPost2
* @param freg
* @param phoneNumber
* @param mobilePhone
* @param email
*/
public Client(int nClient, int nCont, String name, String adress, int codPost1,
int codPost2, String freg, int phoneNumber, int mobilePhone, String email, ArrayList<Car> lst, Date bornDate) {
super();
this.nClient = nClient;
this.nCont = nCont;
this.name = name;
this.adress = adress;
this.codPost1 = codPost1;
this.codPost2 = codPost2;
this.freg = freg;
this.phoneNumber = phoneNumber;
this.mobilePhone = mobilePhone;
this.email = email;
lstCar = lst;
this.bornDate = bornDate;
}
public Client() {
super();
}
public int getnClient() {
return nClient;
}
public void setnClient(int nClient) {
this.nClient = nClient;
}
/**
* @return the nCont
*/
public int getnCont() {
return nCont;
}
public void setnCont(int nCont){
this.nCont = nCont;
}
/**
* @return - Nome do Cliente
*
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* @return - Morada do Cliente
*
*/
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
/**
* @return - Codigo postal (4 primeiros digitos) do Cliente
*
*/
public int getCodPost1() {
return codPost1;
}
public void setCodPost1(int codPost1) {
this.codPost1 = codPost1;
}
/**
* @return - Codigo postal (3 ultimos digitos) do Cliente
*
*/
public int getCodPost2() {
return codPost2;
}
public void setCodPost2(int codPost2) {
this.codPost2 = codPost2;
}
/**
* @return - freguesia do Cliente
*
*/
public String getFreg() {
return freg;
}
public void setFreg(String freg) {
this.freg = freg;
}
/**
* @return - numero de telefone do Cliente
*
*/
public int getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(int phoneNumber) {
this.phoneNumber = phoneNumber;
}
/**
* @return - Email do Cliente
*
*/
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
/**
* @return the mobilePhone
*/
public int getMobilePhone() {
return mobilePhone;
}
/**
* @param mobilePhone the mobilePhone to set
*/
public void setMobilePhone(int mobilePhone) {
this.mobilePhone = mobilePhone;
}
public void setCars(ArrayList<Car> lst ){
lstCar = lst;
}
public ArrayList<Car> getCars() {
return lstCar;
}
public Date getDateBorn(){
return bornDate;
}
public boolean insert(int nCont, String name, String adress, int codPost1,
int codPost2, String freg, int phoneNumber, int mobilePhone, String email, ArrayList<Car> lstCar, Date bornDate){
try {
name = name.toUpperCase();
email = email.toUpperCase();
freg = freg.toUpperCase();
adress = adress.toUpperCase();
query.open();
boolean aux = query.insert("insert into client(nCont, nameClient, adressClient, phoneClient, mobileClient, codPost1, codPost2, freg, email, bornDate)values (?,?,?,?,?,?,?,?,?,?)",
new Object[]{nCont,name,adress,phoneNumber,mobilePhone,codPost1,codPost2,freg,email,bornDate});
ResultSet rs = query.select("SELECT MAX(nClient) FROM client");
int nextId = -1;
if(rs.next()){
nextId = rs.getInt(1);
}
Iterator<Car> itCar = lstCar.iterator();
while (itCar.hasNext())
query.insert("insert into carowner (matricula, nCont) values (?,?)", new Object[]{itCar.next().getMatricula(),nextId});
query.close();
return aux;
} catch (SQLException e) {
query.close();
e.printStackTrace();
}
return false;
}
public boolean update(int nClient, int nCont, String name, String adress, int codPost1,
int codPost2, String freg, int phoneNumber, Integer mobilePhone, String email, ArrayList<Car> lstCar, Date bornDate) throws UnsupportedOperationException {
query.open();
try {
query.update("UPDATE client SET nCont = ?, nameClient = ?, adressClient = ?, phoneClient = ? , mobileClient = ?, " +
"codPost1 = ?, codPost2 = ?, freg = ?, email = ?, bornDate = ? where nClient = ?",
new Object[]{nCont,name,adress,phoneNumber,mobilePhone,codPost1,codPost2,freg,email,bornDate, nClient});
query.delete("delete from carowner where nCont = " + nClient);
Iterator<Car> itCar = lstCar.iterator();
while (itCar.hasNext())
query.insert("insert into carowner (matricula, nCont) values (?,?)", new Object[]{itCar.next().getMatricula(),nClient});
query.close();
} catch (SQLException e) {
query.close();
e.printStackTrace();
return false;
}
return true;
}
/**
* @param nCont Codigo do cliente a remover
* @return Retorna o numero de linhas afectadas, retorna -1 caso exista algum erro
* */
public int remove(int nCont) {
int ret = -1;
try {
query.open();
ret = query.delete("delete from client where nCont = " + nCont);
query.close();
} catch (SQLException e) {
query.close();
}
return ret;
}
public int countRecords(int nCont) {
int count = 0;
ResultSet rs;
try {
query.open();
if (nCont > 0) {
rs = query.select("select count(*) from Client where nCont = "+nCont);
} else {
rs = query.select("select count(*) from Client");
}
if (rs.next()) {
count = rs.getInt(1);
}
query.close();
} catch (SQLException e) {
query.close();
}
return count;
}
public int lastClient(){
int ret = -1;
try {
query.open();
ResultSet rs = query.select("SELECT MAX(nClient) FROM client");
if(rs.next()){
ret = rs.getInt(1);
}
query.close();
return ret;
} catch (SQLException e) {
query.close();
e.printStackTrace();
}
return ret;
}
/**
* @param nCont Numero de Contribuinte a pesquisar
* @return Retorna um objecto do tipo Client, com o Cliente selecionado, caso n�o exista retorna NULL
* @since V2.5
* */
public Client select(int nCont) {
Client lst = null;
Mark m = new Mark();
Model mo = new Model();
ArrayList<Car> lstCar = new ArrayList<Car>();
try {
query.open();
ResultSet rs = query.select("SELECT car.* FROM carowner INNER JOIN car ON car.matricula = carowner.matricula where nCont = "+nCont);
while (rs.next()) {
lstCar.add(new Car(rs.getString("matricula"), rs.getString("VIN"), m.select(rs.getInt("idMarca")),
mo.select(rs.getInt("idModelo")), rs.getDate("dataFabrico"), rs.getInt("yearMake"), rs.getString("tipoMotor"),
rs.getInt("cc"), rs.getString("fuel"), null));
}
rs = query.select("select * from Client where nCont = "+ nCont);
if (rs.next()) {
lst = new Client(rs.getInt("nClient"),rs.getInt("nCont"), rs.getString("nameClient"), rs.getString("adressClient"),
rs.getInt("codPost1"), rs.getInt("codPost2"), rs.getString("freg"),
rs.getInt("phoneClient"), rs.getInt("mobileClient"), rs.getString("email"), lstCar, rs.getDate(("bornDate")));
}
query.close();
} catch (SQLException e) {
e.printStackTrace();
query.close();
}
return lst;
}
/**
* @return Retorna uma LinkedList com objectos do tipo Mark
* @see LinkedList
* @since V 1.5
* */
public ArrayList<Client> select() {
ArrayList<Client> lst = null;
ResultSet rs;
try {
query.open();
rs = query.selectPrepared("select * from client order by nameClient ASC", null);
if(rs.last()){ //cria a lista com o tamanho exacto!
lst = new ArrayList<Client>(rs.getRow());
rs.beforeFirst();
}
while (rs.next()) {
lst.add(new Client(rs.getInt("nClient"),rs.getInt("nCont"), rs.getString("nameClient"), rs.getString("adressClient"),
rs.getInt("codPost1"), rs.getInt("codPost2"), rs.getString("freg"), rs.getInt("phoneClient"),
rs.getInt("mobileClient"), rs.getString("email"), null, rs.getDate("bornDate")));
}
query.close();
} catch (SQLException e) {
query.close();
e.printStackTrace();
}
return lst;
}
public ArrayList<Client> select(String name) {
ArrayList<Client> lst = null;
ResultSet rs;
try {
query.open();
rs = query.select ("select * from client where nameClient like '%"+ name +"%'");
if(rs.last()){ //cria a lista com o tamanho exacto!
lst = new ArrayList<Client>(rs.getRow());
rs.beforeFirst();
}
while (rs.next()) {
lst.add(new Client(rs.getInt("nClient"),rs.getInt("nCont"), rs.getString("nameClient"), rs.getString("adressClient"),
rs.getInt("codPost1"), rs.getInt("codPost2"), rs.getString("freg"), rs.getInt("phoneClient"),
rs.getInt("mobileClient"), rs.getString("email"), null, rs.getDate("bornDate")));
}
query.close();
} catch (SQLException e) {
query.close();
e.printStackTrace();
}
return lst;
}
//verificar o uso
public ArrayList<Client> selectCont(int nCont) {
ArrayList<Client> lst = new ArrayList<Client>();
ArrayList<Car> lstCar;
Mark m = new Mark();
Model mo = new Model();
ResultSet rs, rsCar;
try {
query.open();
rs = query.select("select * from client where nCont like '"+ nCont +"%' order by nameClient ASC");
while (rs.next()) {
rsCar = query.select("SELECT car.* FROM carowner INNER JOIN car ON car.matricula = carowner.matricula WHERE nCont = "+rs.getInt("nCont") );
lstCar = new ArrayList<Car>();
while (rsCar.next()) {
lstCar.add(new Car(rsCar.getString("matricula"), rsCar.getString("VIN"), m.select(rsCar.getInt("idMarca")),
mo.select(rsCar.getInt("idModelo")), rsCar.getDate("dataFabrico"), rsCar.getInt("yearMake"), rsCar.getString("tipoMotor"),
rsCar.getInt("cc"), rsCar.getString("fuel"), null));
}
lst.add(new Client(rs.getInt("nClient"),rs.getInt("nCont"), rs.getString("nameClient"), rs.getString("adressClient"),
rs.getInt("codPost1"), rs.getInt("codPost2"), rs.getString("freg"), rs.getInt("phoneClient"),
rs.getInt("mobileClient"), rs.getString("email"), lstCar, rs.getDate("bornDate")));
}
query.close();
} catch (SQLException e) {
e.printStackTrace();
query.close();
}
return lst;
}
public Client selectByNClient(int nClient) {
Client lst = null;
Mark m = new Mark();
Model mo = new Model();
ArrayList<Car> lstCar = new ArrayList<Car>();
try {
query.open();
ResultSet rs = query.select("SELECT car.* FROM carowner INNER JOIN car ON car.matricula = carowner.matricula where nCont = "+nClient);
while (rs.next()) {
lstCar.add(new Car(rs.getString("matricula"), rs.getString("VIN"), m.select(rs.getInt("idMarca")),
mo.select(rs.getInt("idModelo")), rs.getDate("dataFabrico"), rs.getInt("yearMake"), rs.getString("tipoMotor"),
rs.getInt("cc"), rs.getString("fuel"), null));
}
rs = query.select("select * from Client where nClient = "+nClient);
if (rs.next()) {
lst = new Client(rs.getInt("nClient"),rs.getInt("nCont"), rs.getString("nameClient"), rs.getString("adressClient"),
rs.getInt("codPost1"), rs.getInt("codPost2"), rs.getString("freg"),
rs.getInt("phoneClient"), rs.getInt("mobileClient"), rs.getString("email"), lstCar, rs.getDate(("bornDate")));
}
query.close();
} catch (SQLException e) {
e.printStackTrace();
query.close();
}
return lst;
}
public ArrayList<Client> selectByCar(String matricula){
ArrayList<Client> lst = null;
try {
query.open();
ResultSet rs = query.select("SELECT client.* FROM carowner INNER JOIN client ON client.nClient = carowner.nCont where matricula = '" + matricula + "'");
if(rs.last()){
lst = new ArrayList<Client>(rs.getRow());
rs.beforeFirst();
}
while (rs.next()) {
lst.add(new Client(rs.getInt("nClient"),rs.getInt("nCont"), rs.getString("nameClient"), rs.getString("adressClient"),
rs.getInt("codPost1"), rs.getInt("codPost2"), rs.getString("freg"), rs.getInt("phoneClient"),
rs.getInt("mobileClient"), rs.getString("email"), null, rs.getDate("bornDate")));
}
query.close();
} catch (SQLException e) {
e.printStackTrace();
query.close();
}
return lst;
}
public int compareTo(Object o) {
return nCont - ((Client)o).getnCont();
}
}