de.burrotinto.jKabel.config.ConfigReader.java Source code

Java tutorial

Introduction

Here is the source code for de.burrotinto.jKabel.config.ConfigReader.java

Source

/*
 * jKabel - Ein hochperfomantes, extremstanpassungsfhiges Mehrbenutzersystem zur erfassung von Kabelstrecken
 *
 * Copyright (C) 2016 Florian Klinger
 *
 * This program 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.burrotinto.jKabel.config;

import de.burrotinto.jKabel.JKabelS;
import de.burrotinto.jKabel.config.trommelSort.AbstractTrommelSort;
import de.burrotinto.jKabel.config.trommelSort.Richtung;
import de.burrotinto.jKabel.config.trommelSort.TrommelIntelligentSort;
import de.burrotinto.jKabel.config.typSort.AbstractTypeSort;
import de.burrotinto.jKabel.config.typSort.TypNameSort;
import de.burrotinto.jKabel.config.typSort.TypeFrequenzSort;
import de.burrotinto.jKabel.config.typSort.TypeMatNrSort;
import de.burrotinto.jKabel.dbauswahlAS.enitys.IKabeltypE;
import de.burrotinto.jKabel.dbauswahlAS.enitys.ITrommelE;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

/**
 * Einfacher Propertysreader. ISt ein Singelton
 * Created by derduke on 01.06.16.
 */
public class ConfigReader {
    public static final String SWNPFAD = "O:\\KFM-Verwaltung\\Materialwirtschaft\\Lager\\jKabelDB\\";
    private static final String DBPFADPROP = "DB.PFAD";
    private static final String SORTTYP = "SORT.TYP";
    private static final String SORTTYPINORDER = "SORT.TYP.INORDER";
    private static final String SORTTROMMELINORDER = "SORT.TROMMEL.INORDER";
    private static final String SORTTROMMEL = "SORT.TROMMEL";
    private static final String ZEIGEALLE = "SORT.TROMMEL.ZEIGEALLE";

    private static ConfigReader instance = new ConfigReader();
    private static Logger log = Logger.getLogger(ConfigReader.class);
    private final File propFile = new File(System.getProperty("user.home") + File.separator + "jKabel.prop");

    private Properties prop = null;

    private AnnotationConfigApplicationContext context = JKabelS.getSpringContext();

    private HashMap<String, AbstractTypeSort> alleTypSortierer = null;

    private ConfigReader() {
    }

    /**
     * Gibt die ConfigReader Instanz zurck
     *
     * @return den ConfigReader
     */
    public static ConfigReader getInstance() {
        return instance;
    }

    /**
     * Gibt den Pfad zur DB zurck
     *
     * @return Pfad zu der zu benutzenden DB ODER NULL  wenn nicht gestetzt
     */
    public String getPath() {
        String path = null;
        try {
            Properties prop = getProperties();
            if (prop.getProperty(DBPFADPROP) != null) {
                path = prop.getProperty(DBPFADPROP);
            }
        } catch (IOException e) {
            return null;
        }
        return path;
    }

    private Properties getProperties() throws IOException {
        // lazy implementation
        if (prop == null) {
            prop = new Properties();
            if (!propFile.exists())
                propFile.createNewFile();
            prop.load(new FileReader(propFile));
        }
        return prop;
    }

    /**
     * Zum Speichern des Pfades
     *
     * @param path wo die DB liegt
     * @throws IOException
     */
    public void savePath(String path) throws IOException {
        prop.setProperty(DBPFADPROP, path);
        save();
    }

    private void save() throws IOException {
        prop.store(new FileWriter(propFile), "Konfigurationsdatei des JKabel");
        log.info("save:");
        for (Map.Entry<Object, Object> set : getProperties().entrySet()) {
            log.info(set.getKey() + " " + set.getValue());
        }
    }

    public void setTypSort(String typSort) throws IOException {
        prop.setProperty(SORTTYP, typSort);
        save();
    }

    public void setTypeInOrder(boolean inOrder) throws IOException {
        prop.setProperty(SORTTYPINORDER, Boolean.toString(inOrder));
        save();
    }

    public Comparator<? super IKabeltypE> getKabeltypSort() {
        try {
            //Default Wert speichern
            if (getProperties().getProperty(SORTTYP) == null) {
                try {
                    setTypSort("matnr");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            AbstractTypeSort c = alleTypSortierer.get(getProperties().getProperty(SORTTYP));
            if (c == null)
                c = new TypeMatNrSort();
            c.setInOrder(isTypeInOrder());
            return c;

        } catch (Exception e) {
            return new TypeMatNrSort();
        }
    }

    public Boolean isTypeInOrder() {
        try {
            return getProperties().getProperty(SORTTYPINORDER) == null
                    || Boolean.parseBoolean(getProperties().getProperty(SORTTYPINORDER));
        } catch (IOException e) {
            e.printStackTrace();
            return true;
        }
    }

    public Comparator<ITrommelE> getTrommelSort() {
        AbstractTrommelSort c;
        try {
            //Default Wert speichern
            if (getProperties().getProperty(SORTTROMMEL) == null) {
                try {
                    setTrommelSort("TrommelIntelligentSort.class");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            c = (AbstractTrommelSort) context.getBean(Class.forName(prop.getProperty(SORTTROMMEL)));

        } catch (Exception e) {
            c = context.getBean(TrommelIntelligentSort.class);
        }
        context.getBean(Richtung.class).setAufsteigend(isTrommelInOrder());
        return c;
    }

    public void setTrommelSort(String typSort) throws IOException {
        prop.setProperty(SORTTROMMEL, typSort);
        save();
    }

    private boolean isTrommelInOrder() {
        try {
            return getProperties().getProperty(SORTTROMMELINORDER) == null
                    || Boolean.parseBoolean(getProperties().getProperty(SORTTROMMELINORDER));
        } catch (IOException e) {
            e.printStackTrace();
            return true;
        }
    }

    public void setTrommelInOrder(boolean inOrder) throws IOException {
        prop.setProperty(SORTTROMMELINORDER, Boolean.toString(inOrder));
        save();
    }

    public Collection<AbstractTypeSort> getAllTypSort() {
        if (alleTypSortierer == null) {
            alleTypSortierer = new HashMap<>();
            alleTypSortierer.put(TypeFrequenzSort.class.getName(), new TypeFrequenzSort());
            alleTypSortierer.put(TypeMatNrSort.class.getName(), new TypeMatNrSort());
            alleTypSortierer.put(TypNameSort.class.getName(), new TypNameSort());
        }
        return alleTypSortierer.values();
    }

    public boolean isZeigeAlle() {
        try {
            return getProperties().getProperty(ZEIGEALLE) != null
                    && Boolean.parseBoolean(getProperties().getProperty(ZEIGEALLE));
        } catch (IOException e) {
            return false;
        }
    }

    public void setZeigeAlle(boolean zeigeAlle) throws IOException {
        prop.setProperty(ZEIGEALLE, Boolean.toString(zeigeAlle));
        save();
    }

    public List<AbstractTrommelSort> getAllTrommelSort() {
        return (List<AbstractTrommelSort>) context.getBean("getAllTrommelSort");
    }

    @Autowired(required = true)
    public void setAnnotationConfigApplicationContext(AnnotationConfigApplicationContext context) {
        this.context = context;
    }
}