Package bpntojava.model

Source Code of bpntojava.model.Model

package bpntojava.model;

import bpntojava.controller.Constant;
import bpntojava.controller.Controller;
import bpntojava.model.bpndata.BPN;
import bpntojava.model.bpndata.CatchData;
import bpntojava.model.bpndata.Data;
import bpntojava.model.bpndata.DataLine;
import bpntojava.model.stafftable.StaffTable;
import bpntojava.model.stafftable.Usable;
import java.util.LinkedList;
import javax.swing.table.DefaultTableModel;

/**
* Das Model.
*
* @author mschuessler
*/
public class Model {

    /**
     * BPN-Tools.
     */
    private BPN bpn;
    /**
     * Controller.
     */
    private Controller controller;
    /**
     * BPN-Daten von Artikel.
     */
    private Data data;
    /**
     * BPN-Daten der Außendienste.
     */
    private Data ad;
    /**
     * Tabelle mit den Usables.
     */
    private StaffTable stafftable;
    /**
     * Liste von Daten in den einzelnen Monaten.
     */
    private LinkedList<Data> datesdata;

    /**
     * Konstruktor.
     *
     * @param bpn BPN-Tools.
     * @param controller Controller
     */
    public Model(BPN bpn, Controller controller) {
        this.bpn = bpn;
        this.controller = controller;
    }

    /**
     * Model starten.
     */
    public void go() {
        this.login();
        this.createData();
        stafftable = new StaffTable(data);
        stafftable.fillUsables();
        reduceArticle();
        this.createDatesData();
    }

    /**
     * Getter für Stafftable.
     *
     * @return Stafftable
     */
    public StaffTable getStaffTable() {
        return stafftable;
    }

    /**
     * Login zur BPN.
     */
    private void login() {
        bpn.login(Constant.FIRMA, Constant.USER, Constant.PASSWORD, Constant.MANDANT);
    }

    /**
     * Packt die Daten in einzelne Datenpakete nach Datum sortiert.
     */
    private void createDatesData() {
        datesdata = new LinkedList();
        for (int i = 0; i < data.size(); i++) {
            String datum = data.get(i).get(0);
            boolean added = false;
            for (int j = 0; j < datesdata.size(); j++) {
                if (datum.equals(datesdata.get(j).getName())) {
                    datesdata.get(j).add(data.get(i));
                    added = true;
                }
            }
            if (!added) {
                Data tmpdata = new Data();
                tmpdata.setName(datum);
                tmpdata.add(data.get(i));
                datesdata.add(tmpdata);
            }

        }
    }

    /**
     * Erstellt die Datenbank bzw ließt sie ein.
     */
    private void createData() {
        boolean datei = false;
        controller.setInfo("Daten werden geladen, bitte warten...");
        if (SaveData.dataExists(Constant.DATA)) {
            data = SaveData.load(Constant.DATA);
            datei = true;
        } else {
            CatchData cdata = new CatchData(bpn);
            data = cdata.getData();
            data.setCreated(getTime());
            SaveData.save(data, Constant.DATA);
        }
        if (SaveData.dataExists(Constant.AD)) {
            ad = SaveData.load(Constant.AD);
        } else {
            CatchData cdata = new CatchData(bpn);
            ad = cdata.getAD();
            ad.setCreated(getTime());
            SaveData.save(ad, Constant.AD);
        }
        if (datei) {
            controller.setInfo("Daten wurden aus Datei geladen.");
        } else {
            controller.setInfo("Daten wurden aus BPN geladen.");
        }
    }

    /**
     * Artikel auf reduzieren auf ein paar bestimmte.
     */
    private void reduceArticle() {
        Usable article = stafftable.getUsables().get(2);
        LinkedList<String> toAdd = new LinkedList();
        LinkedList<String> articleList = this.getArticleList();
        for (int i = 0; i < article.size(); i++) {
            for (String firstcheck : articleList) {
                String toCheck = firstcheck.split(" ")[0];
                if (article.get(i).startsWith(toCheck)) {
                    toAdd.add(firstcheck);
                }
            }
        }
        article.setNewList(toAdd);
    }

    /**
     * Gibt eine Artikelliste zurück.
     *
     * @return Liste mit Artikeln
     */
    private LinkedList<String> getArticleList() {
        LinkedList<String> toReturn = new LinkedList();
        toReturn.add("104019 Dienstleistung Notebookreparatur  inkl. Testlauf");
        toReturn.add("103783 Fehlersuche und Diagnose (bis ca. 30 min.)");
        toReturn.add("100204 Zusammenbau und Testlauf");
        toReturn.add("100420 Betriebssystem Microsoft Neuinstallation");
        toReturn.add("100205 Endkontrolle + Abschlusstests");
        toReturn.add("103872 Dienstleistung Datensicherung nach Kundenvorgabe");
        toReturn.add("104378 Expressservice (Bearbeitung innerhalb eines Tages)");
        toReturn.add("105483 Werkstattzeittakt (15min)");
        toReturn.add("103243 Fahrtkosten inkl. Techniker");
        toReturn.add("106620 Datenrettung nach Kundenvorgabe");
        toReturn.add("103331 Virenentfernung");
        toReturn.add("DLREP Betriebssystem Reparaturinstallation");
        toReturn.add("101907 Dienstleistung Fernwartung je angefangene Viertelstunde");
        toReturn.add("106622 Servicepaket Basic");
        toReturn.add("106623 Servicepaket Medium");
        toReturn.add("106624 Servicepaket Premium");
        toReturn.add("106625 Servicepaket Ultimate");
        return toReturn;
    }

    /**
     * Gibt das Datum der Datenbankerstellung zurück.
     *
     * @return Datum
     */
    public String getCreated() {
        return data.getCreated();
    }

    /**
     * Summe der Preise erhalten in Abhängigkeit.
     *
     * @param date Datum (MM.YYYY)
     * @param staff Mitarbeiter
     * @param article Artikel
     * @return Betrag
     */
    public float getData(String date, String staff, String article) {
        article = article.split(" ")[0];
        if (article.equals("Außendienst")) {
            return getADDATA(date, staff);
        }
        float ret = (float) 0;

        Data tmpdata = null;
        for (int i = 0; i < datesdata.size(); i++) {
            if (date.equals(datesdata.get(i).getName())) {
                tmpdata = datesdata.get(i);
            }
        }

        for (int i = 0; i < tmpdata.size(); i++) {
            DataLine dataline = tmpdata.get(i);
            if (dataline.get(1).equals(staff)
                    && article.startsWith(dataline.get(2))
                    && dataline.get(0).equals(date)) {
                ret += Float.parseFloat(dataline.get(4));
            }
        }
        return ret;
    }

    /**
     * Summe der Außendienste erhalten in Abhängigkeit.
     *
     * @param date Datum (MM.YYYY)
     * @param staff Mitarbeiter
     * @return Betrag
     */
    private float getADDATA(String date, String staff) {
        float ret = (float) 0;
        if (staff.equals("Marc Schüßler")) {
            for (int i = 0; i < ad.size(); i++) {
                DataLine dataline = ad.get(i);
                if (dataline.get(0).equals(date) && dataline.get(1).equals("10044001")) {
                    ret += Float.parseFloat(dataline.get(3));
                }
            }
        }
        if (staff.equals("Gregor Rabczuk")) {
            for (int i = 0; i < ad.size(); i++) {
                DataLine dataline = ad.get(i);
                if (dataline.get(0).equals(date) && dataline.get(1).equals("10044002")) {
                    ret += Float.parseFloat(dataline.get(3));
                }
            }
        }
        if (staff.equals("Stefan Baumgartner")) {
            for (int i = 0; i < ad.size(); i++) {
                DataLine dataline = ad.get(i);
                if (dataline.get(0).equals(date) && dataline.get(1).equals("10044003")) {
                    ret += Float.parseFloat(dataline.get(3));
                }
            }
        }
        if (staff.equals("Dominik Hipper")) {
            for (int i = 0; i < ad.size(); i++) {
                DataLine dataline = ad.get(i);
                if (dataline.get(0).equals(date) && dataline.get(1).equals("10044004")) {
                    ret += Float.parseFloat(dataline.get(3));
                }
            }
        }
        if (staff.equals("Steven Frerking")) {
            for (int i = 0; i < ad.size(); i++) {
                DataLine dataline = ad.get(i);
                if (dataline.get(0).equals(date) && dataline.get(1).equals("10044005")) {
                    ret += Float.parseFloat(dataline.get(3));
                }
            }
        }
        if (staff.equals("Philipp Brinkoff")) {
            for (int i = 0; i < ad.size(); i++) {
                DataLine dataline = ad.get(i);
                if (dataline.get(0).equals(date) && dataline.get(1).equals("10044006")) {
                    ret += Float.parseFloat(dataline.get(3));
                }
            }
        }
        if (staff.equals("Kevin Schmengler")) {
            for (int i = 0; i < ad.size(); i++) {
                DataLine dataline = ad.get(i);
                if (dataline.get(0).equals(date) && dataline.get(1).equals("10044007")) {
                    ret += Float.parseFloat(dataline.get(3));
                }
            }
        }
        if (staff.equals("Julian Görres")) {
            for (int i = 0; i < ad.size(); i++) {
                DataLine dataline = ad.get(i);
                if (dataline.get(0).equals(date) && dataline.get(1).equals("10044008")) {
                    ret += Float.parseFloat(dataline.get(3));
                }
            }
        }
        if (staff.equals("Oliver Passler")) {
            for (int i = 0; i < ad.size(); i++) {
                DataLine dataline = ad.get(i);
                if (dataline.get(0).equals(date) && dataline.get(1).equals("10044009")) {
                    ret += Float.parseFloat(dataline.get(3));
                }
            }
        }
        if (staff.equals("Florian Schröder")) {
            for (int i = 0; i < ad.size(); i++) {
                DataLine dataline = ad.get(i);
                if (dataline.get(0).equals(date) && dataline.get(1).equals("10044010")) {
                    ret += Float.parseFloat(dataline.get(3));
                }
            }
        }
        if (staff.equals("Tarik Luca Badidi")) {
            for (int i = 0; i < ad.size(); i++) {
                DataLine dataline = ad.get(i);
                if (dataline.get(0).equals(date) && dataline.get(1).equals("10044011")) {
                    ret += Float.parseFloat(dataline.get(3));
                }
            }
        }
        if (staff.equals("Dirk Schneider")) {
            for (int i = 0; i < ad.size(); i++) {
                DataLine dataline = ad.get(i);
                if (dataline.get(0).equals(date) && dataline.get(1).equals("10044012")) {
                    ret += Float.parseFloat(dataline.get(3));
                }
            }
        }
        return ret;
    }

    /**
     * Gibt die aktuelle Zeit und Datum als String zurück.
     *
     * @return Time
     */
    private String getTime() {
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd.MM.yyyy HH.mm.ss");
        return sdf.format(new java.util.Date());
    }

    /**
     * Tabelle erhalten in Abhängigkeit vom Datum.
     *
     * @param date Datumsstring (MM.YYYY)
     * @return Tabelle
     */
    public DefaultTableModel getTable(String date) {

        //Tabellenkopf
        String[] colnames = new String[stafftable.getUsables().get(1).size() + 2];
        colnames[0] = "Artikel";
        for (int i = 0; i < stafftable.getUsables().get(1).size(); i++) {
            colnames[i + 1] = stafftable.getUsables().get(1).get(i);
        }
        colnames[colnames.length - 1] = "Summe";

        //Tabellengröße
        int colwidth = stafftable.getUsables().get(1).size() + 2;
        int colheight = stafftable.getUsables().get(2).size() + 3;
        String[][] coldata = new String[colheight][colwidth];


        //Datenzeilen erstellen
        LinkedList<DataLine> rows = new LinkedList();
        for (int i = 0; i < stafftable.getUsables().get(2).size(); i++) {
            DataLine dataline = new DataLine();
            dataline.add(stafftable.getUsables().get(2).get(i));
            for (int j = 0; j < stafftable.getUsables().get(1).size(); j++) {
                String staff = stafftable.getUsables().get(1).get(j);
                String article = stafftable.getUsables().get(2).get(i);
                String prize = String.valueOf(getData(date, staff, article));
                dataline.add(prize);
            }

            rows.add(sumDataLine(dataline));
        }

        //Datenzeile Außendienste
        DataLine ads = new DataLine();
        ads.add("Außendienste");
        for (int j = 0; j < stafftable.getUsables().get(1).size(); j++) {
            String staff = stafftable.getUsables().get(1).get(j);
            String prize = String.valueOf(getData(date, staff, "Außendienst"));
            ads.add(prize);
        }
        rows.add(sumDataLine(ads));

        //Datenzeile Spaltensumme
        DataLine sum = new DataLine();
        sum.add("Summe");
        for (int i = 1; i < colwidth; i++) {
            float flsum = 0;
            for (int j = 0; j < rows.size(); j++) {
                flsum += Float.valueOf(rows.get(j).get(i).replace(',', '.'));
            }
            sum.add(floatToString(String.valueOf(flsum)).replace('.', ','));
        }
        rows.add(sum);

        //Überschrift
        coldata[0][0] = "Artikel";
        for (int i = 0; i < stafftable.getUsables().get(1).size(); i++) {
            coldata[0][i + 1] = stafftable.getUsables().get(1).get(i);
        }
        coldata[0][coldata[0].length - 1] = "Summe";

        //Zeilen in die Tabelle schreiben
        for (int y = 1; y < coldata.length; y++) {
            DataLine toput = rows.get(y - 1);
            for (int i = 0; i < toput.size(); i++) {
                coldata[y][i] = toput.get(i);
            }
        }
        DefaultTableModel toReturn = new DefaultTableModel(coldata, colnames);
        return toReturn;
    }

    /**
     * Summe der Datenzeile berechnen.
     *
     * @param dataline Datenzeile.
     * @return neue Datenzeile inkls Summe.
     */
    private DataLine sumDataLine(DataLine dataline) {
        float sum = 0;
        DataLine toReturn = new DataLine();
        toReturn.add(dataline.get(0));
        for (int i = 1; i < dataline.size(); i++) {
            sum += Float.parseFloat(dataline.get(i));
            toReturn.add(floatToString(dataline.get(i)).replace('.', ','));
        }
        toReturn.add(floatToString(String.valueOf(sum)).replace('.', ','));
        return toReturn;
    }

    /**
     * Einen String-Float auf 2 Nachkommastellen kürzen.
     *
     * @param value Vorher
     * @return Nachher
     */
    private String floatToString(String value) {
        final int hundert = 100;
        int malhundert = (int) (Float.parseFloat(value) * hundert);
        float durchhundert = ((float) malhundert) / hundert;
        return String.valueOf(durchhundert);
    }
}
TOP

Related Classes of bpntojava.model.Model

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.