Je cherche à ajouter un client en base mais je ne trouve pas le code pour le faire.
L'idée est de créer un nouveau client donc je pourrais renseigner les infos "à la main" sans passer par l'interface graphique.
L'algorithme ressemblerait à quelque chose comme cela :
Client c = new Client();
c.put("NOM", "Client Test");
c.create();
Ca fait 2 jours que je bute là dessus et j'ai l'impression que ce n'est pas faisable, mais je me trompe forcément
Code pour l'ajout d'un client
Ca ira mieux avec ceci :
Code : Tout sélectionner
public class SimpleCustomer {
private String name;
private String email;
private String street;
private String postalCode;
private String city;
public SimpleCustomer(String name, String email, String street, String postalCode, String city) {
this.name = name;
this.email = email;
this.street = street;
this.postalCode = postalCode;
this.city = city;
}
public int createIfNotExists(DBRoot root) throws Exception {
final SQLTable tClient = root.getTable("CLIENT");
final SQLTable tAdresse = root.getTable("ADRESSE");
// retrieve all the customers and check
final SQLRowValues graph_address = new SQLRowValues(tAdresse);
graph_address.put("VILLE", null);
final SQLRowValues graph = new SQLRowValues(tClient);
graph.put("NOM", null);
graph.put("ID_ADRESSE", graph_address);
final SQLRowValuesListFetcher f = new SQLRowValuesListFetcher(graph);
// use setSelTransf() on the fetcher to add a where if needed
final List<SQLRowValues> rowsClient = f.fetch();
SQLRowValues client = null;
for (SQLRowValues rClient : rowsClient) {
String name = rClient.getString("NOM").toLowerCase();
String city = rClient.getForeign("ID_ADRESSE").getString("VILLE");
if (name.equalsIgnoreCase(this.name) && city.equalsIgnoreCase(this.city)) {
// put other test here for duplicate customer criteria
client = rClient;
break;
}
}
// exit if customer already exists
if (client != null) {
return client.getID();
}
// Customer
final SQLRowValues r = new SQLRowValues(tClient);
r.put("NOM", this.name);
r.put("MAIL", this.email);
// Customer address
final SQLRowValues rAddr = new SQLRowValues(root.getTable("ADRESSE"));
rAddr.put("RUE", this.street);
rAddr.put("CODE_POSTAL", this.postalCode);
rAddr.put("VILLE", this.city);
r.put("ID_ADRESSE", rAddr);
// Customer default payment method
final SQLRow rMode = ModeReglementDefautPrefPanel.getDefaultRow(true);
final SQLElement eltModeReglement = ComptaPropsConfiguration.getInstance().getDirectory().getElement("MODE_REGLEMENT");
final SQLRowValues rowVals = eltModeReglement.createCopy(rMode, null);
r.put("ID_MODE_REGLEMENT", rowVals);
// Customer default account
r.put("ID_COMPTE_PCE", ComptePCESQLElement.getIdComptePceDefault("Clients"));
// store the new customer
return r.commit().getID();
}
public static void main(String[] args) {
final HeadlessOpenConcerto c = new HeadlessOpenConcerto(2, 42);
final SimpleCustomer customer = new SimpleCustomer("MegaCorp", "contact@megacorp.eu", "3 rue de Boston", "80000", "Amiens");
int customerId;
try {
customerId = customer.createIfNotExists(c.getCompanyRoot());
System.out.println("Customer successfully created (id:" + customerId + ")");
} catch (Exception e) {
System.err.println("Unable to create customer");
e.printStackTrace();
}
}
}