Code pour l'ajout d'un client

Répondre
madrigo
Messages : 15
Enregistré le : ven. mai 30, 2014 1:58 pm

mar. juil. 29, 2014 12:51 pm

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 ;)
Avatar du membre
guillaume
Messages : 2434
Enregistré le : ven. févr. 11, 2011 7:15 pm

mar. juil. 29, 2014 3:54 pm

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();
        }

    }

}
Directeur technique d'OpenConcerto qui dans son temps libre s'occupe du forum.
Pour une assistance pro, nous sommes joignables à ILM Informatique contre quelques jetons.
Pensez aussi à lire le manuel !
madrigo
Messages : 15
Enregistré le : ven. mai 30, 2014 1:58 pm

mer. juil. 30, 2014 9:39 am

Merci, c'est exactement ce que je voulais :)
Par contre j'ai trouvé aucune trace de cette classe magique dans le code, c'est normal ou j'ai mal cherché ?

En tout cas merci pour la réactivité !
Avatar du membre
guillaume
Messages : 2434
Enregistré le : ven. févr. 11, 2011 7:15 pm

mer. juil. 30, 2014 5:02 pm

Bonjour,

vous avez le manuel ?

Cordialement,
Directeur technique d'OpenConcerto qui dans son temps libre s'occupe du forum.
Pour une assistance pro, nous sommes joignables à ILM Informatique contre quelques jetons.
Pensez aussi à lire le manuel !
Répondre