/**
* Copyright (C) 2007 Julien Revault d'Allonnes
*
* This file is part of DruideCave.
*
* DruideCave is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* DruideCave is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DruideCave; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.ledruide.druidecave.business;
import com.ledruide.druidecave.business.lists.CountryProxy;
import com.ledruide.druidecave.business.lists.RegionProxy;
import com.ledruide.druidecave.dao.Vigne;
import com.ledruide.druidecave.dao.VignePeer;
import com.ledruide.druidecave.dao.Vigneron;
import com.ledruide.druidecave.dao.VigneronPeer;
import com.ledruide.druidecave.gui.Home;
import com.ledruide.druidegui.SwapFrame;
import com.ledruide.druidegui.helpers.KeyValueBean;
import com.ledruide.druidegui.helpers.Session;
import com.ledruide.druidegui.helpers.TextTools;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
/**
* @author Julien
* @since 14 oct. 2006
*/
public class VigneronProxy {
/**
* Cette m�thode permet de sauvegarder les r�gions li�es � un r�coltant
*
* @param recoltant_id L'id du recoltant en ajout
* @param regions la liste des r�gions � ajouter
* @return true si tout se passe correctement, false sinon.
*/
public static boolean ajoutRegions(String recoltant_id, Enumeration<?> regions) {
try {
// on supprime les vigne existantes de ce viticulteur.
Vigne lavigne = new Vigne();
lavigne.setVigneron_id(recoltant_id);
// on ne commit pas les donn�e maintenant....
VignePeer.deleteSome(VignePeer.find(lavigne), false);
while (regions.hasMoreElements()) {
KeyValueBean countryRegion = (KeyValueBean) regions.nextElement();
// kloug avoir une clef en 2 (pays,region) et l'extraire.
String countryKey = CountryProxy.extract(countryRegion.getKey());
String regionKey = RegionProxy.extract(countryRegion.getKey());
//Region region = RegionPeer.findByPK(countryRegion.getKey(),"");
Vigne vigne = new Vigne();
vigne.setVigneron_id(recoltant_id);
vigne.setCountry_id(countryKey);
vigne.setRegion_id(regionKey);
if (regions.hasMoreElements()) {
vigne.save(false);
}
else {
// On commit sur le dernier �l�ment pour tout enregistrer d'un coup...
vigne.save(true);
}
}
}
catch (Exception e) {
e.printStackTrace();
try {
VignePeer.rollback();
} catch (Exception e1) {
// Pas tr�s grave �a sera fait tout seul au prochain acc�s � la base...
e1.printStackTrace();
}
return false;
}
return true;
}
/**
* Cette m�thode permet de retrouver la totalit� des r�gions de
* vigne de ce r�coltant
* @param recoltant_id l'ID du r�coltant concern�
* @return La liste des r�gions de ses vignes.
*/
public static List getRegions(String recoltant_id) {
List vignes = null;
try {
// On r�cupere les r�gion de vigne qu'ils travaillent
Vigne vigne = new Vigne();
vigne.setVigneron_id(recoltant_id);
vignes = VignePeer.find(vigne);
}
catch (Exception e) {
System.out.println("Impossible de trouver les vignes de ce r�coltant : ");
e.printStackTrace();
}
if(vignes == null) {
vignes = new ArrayList();
}
return vignes;
}
public static List<Vigneron> trouver(String nom, String pays_id, String region_id) {
List<Vigneron> resultats = new ArrayList<Vigneron>();
try {
// On r�cup�re les recoltants
List results = VigneronPeer.findAll();
if (results != null) {
// Pour chaque r�coltant
for (int i = 0; i < results.size(); i++) {
Vigneron recoltant1 = (Vigneron) results.get(i);
// On r�cup�re leur ID
String recoltant_id = recoltant1.getId();
// On v�rifie que leurs vignes sont dans le bon pays region.
if (!TextTools.isNullOrEmpty(pays_id)) {
// On r�cupere les r�gion de vigne qu'ils travaillent
Vigne vigne = new Vigne();
vigne.setVigneron_id(recoltant_id);
vigne.setCountry_id(pays_id);
if (!TextTools.isNullOrEmpty(region_id)) vigne.setRegion_id(region_id);
List vignes = VignePeer.find(vigne);
if (vignes != null && vignes.size() > 0) {
resultats.add(recoltant1);
}
else {
// si la r�gion n'est pas renseign�e on ne regarde que le pays
if (TextTools.isNullOrEmpty(region_id)) {
if (pays_id.equals(recoltant1.getCountry_id())) {
resultats.add(recoltant1);
}
}
}
}
else {
resultats.add(recoltant1);
}
}
// Si le nom pass� en argument n'est pas nul ou vide
if (!TextTools.isNullOrEmpty(nom)) {
// Nous faisons une recherche sur chaque �l�ment qui peut contenir
// une partie du nom pass� en argument
Iterator<Vigneron> liste = resultats.iterator();
while (liste.hasNext()) {
Vigneron bout = liste.next();
if (bout != null && bout.getLastname() != null) {
if (bout.getLastname().indexOf(nom) == -1) {
liste.remove();
}
}
}
}
}
}
catch (Exception e) {
System.out.println("Probl�me lors de la recherche d'un r�coltant");
e.printStackTrace();
resultats = new ArrayList<Vigneron>(0);
}
return resultats;
}
/**
* Permet de filtrer les r�coltant en fonction de la note.<br>
* Si la note n'est pas renseign�e : On affiche tous les r�coltants.
* @param recoltants
* @param laNote
* @return la liste des r�coltant r�sultant de ce filtre.
*/
public static List filtrerParNoteMin(List recoltants, String laNote) {
List resultats = new ArrayList();
try {
int noteMini = 0;
try {
noteMini = Integer.parseInt(laNote);
} catch (NumberFormatException e) {
// Si la note n'est pas renseign�e on affiche tout.
return recoltants;
}
if (recoltants != null) {
for (int i = 0 ; i < recoltants.size() ; i++) {
Vigneron recoltant = (Vigneron) recoltants.get(i);
if (recoltant != null) {
String note = recoltant.getNote();
if (!TextTools.isNullOrEmpty(note)) {
int noteI = 0;
try {
noteI = Integer.parseInt(note);
} catch (NumberFormatException e) {
noteI = Integer.MAX_VALUE;
}
if (noteMini <= noteI) {
resultats.add(recoltant);
}
}
}
}
}
}
catch (Exception e) {
System.out.println("Probl�me lors de la recherche d'un r�coltant");
e.printStackTrace();
}
return resultats;
}
public static Vigneron getSessionRecoltantFromID() throws Exception {
String recoltant_pk = (String) Session.getInstance().getAttribute("RECOLTANT_ID");
Vigneron recoltant = VigneronPeer.findByPK(recoltant_pk);
if (recoltant == null) {
// On repart vers la page d'accueil !
// ERREUR
SwapFrame.getInstance().init(Home.class);
}
return recoltant;
}
public static List<KeyValueBean> getKeyValueList() {
return getKeyValueList(null, null, null);
}
/**
* Retunrs a list containing KeyValueBean with Vignron ID as a key
* and Lastname, Firstname
* @param country the country id
* @return a list containing KeyValueBean with Vignron ID as a key
*/
public static List<KeyValueBean> getKeyValueList(String country) {
return getKeyValueList(country, null,null);
}
/**
* Retunrs a list containing KeyValueBean with Vignron ID as a key
* and Lastname, Firstname
* @param country the country id
* @param region the Region id
* @return a list containing KeyValueBean with Vignron ID as a key
*/
public static List<KeyValueBean> getKeyValueList(String country, String region) {
return getKeyValueList(country, region, null);
}
/**
* Retunrs a list containing KeyValueBean with Vignron ID as a key
* and Lastname, Firstname
* @param country the country id
* @param region the Region id
* @param denomination the denomination ID
* @return a list containing KeyValueBean with Vignron ID as a key
*/
public static List<KeyValueBean> getKeyValueList(String country, String region, String denomination) {
List<KeyValueBean> results = new ArrayList<KeyValueBean>();
try {
List<Vigneron> vignerons = VigneronPeer.findAll();
// For each Vigneron we're going to filter with the country/region
for (int i = 0; i < vignerons.size(); i++) {
Vigneron vigneron = vignerons.get(i);
Vigne vigne = new Vigne();
vigne.setVigneron_id(vigneron.getId());
if (! TextTools.isNullOrEmpty(country)) {
vigne.setCountry_id(country);
if (TextTools.isNullOrEmpty(region)) {
vigne.setVigneron_id(region);
if (TextTools.isNullOrEmpty(denomination)) {
vigne.setVigneron_id(denomination);
}
}
}
List vignes = VignePeer.find(vigne);
if (vignes != null && vignes.size() > 0) {
results.add(new KeyValueBean(vigneron.getId(), vigneron.getLastname() + ", " + vigneron.getFirstname()));
}
}
}
catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return results;
}
}