net.visualillusionsent.tellplugin.xml.TellSaveHandler.java Source code

Java tutorial

Introduction

Here is the source code for net.visualillusionsent.tellplugin.xml.TellSaveHandler.java

Source

/*
 * This file is part of TellPlugin.
 *
 * Copyright  2014-2015 Visual Illusions Entertainment
 *
 * TellPlugin 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/gpl.html.
 */
/*
 * This file is part of WeatherBot.
 *
 * Copyright  2014-2015 Visual Illusions Entertainment
 *
 * WeatherBot 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/gpl.html.
 */
/*
 * This file is part of WeatherBot.
 *
 * Copyright  2014-2014 Visual Illusions Entertainment
 *
 * WeatherBot 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/gpl.html.
 */
/*
 * This file is part of WeatherBot.
 *
 * Copyright  2014 Visual Illusions Entertainment
 *
 * WeatherBot 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/gpl.html.
 */
package net.visualillusionsent.tellplugin.xml;

import net.visualillusionsent.tellplugin.Tell;
import net.visualillusionsent.vibotx.VIBotX;
import org.jdom2.Element;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Aaron on 9/23/2014.
 */
public class TellSaveHandler<T> extends XMLSaveHandler<T> {

    private static TellSaveHandler<Tell> instance;

    public TellSaveHandler() {
        super("tells");
    }

    public static TellSaveHandler get() {
        if (instance == null) {
            instance = new TellSaveHandler<Tell>();
        }
        return instance;
    }

    @Override
    public void save(T... data) {
        for (T d : data) {
            this.insertData((Tell) d);
        }
    }

    @Override
    public List<T> loadAll() {
        return (List<T>) this.load();
    }

    @Override
    public void delete(T... data) {
        for (T d : data) {
            this.deleteData((Tell) d);
        }
    }

    public void insertData(Tell tell) {
        Element set = new Element("entry");

        /* Add Element Data */
        Element receiverCol = new Element("receiver");
        receiverCol.setText(tell.getReceiver());
        set.addContent(receiverCol);

        Element senderCol = new Element("sender");
        senderCol.setText(tell.getSender());
        set.addContent(senderCol);

        Element messageCol = new Element("message");
        messageCol.setText(tell.getMessage());
        set.addContent(messageCol);

        Element dateCol = new Element("date");
        dateCol.setText(tell.getDateString());
        set.addContent(dateCol);

        getDocument().getRootElement().addContent(set);
        try {
            write();
        } catch (IOException e) {
            VIBotX.log.error("Error writng to TellPlugin Xml File", e);
        }
    }

    public void deleteData(Tell tell) {
        ArrayList<Element> toremove = new ArrayList<Element>();

        for (Element element : getDocument().getRootElement().getChildren()) {
            VIBotX.log.info(element.getChildText("receiver"));
            VIBotX.log.info(element.getChildText("sender"));
            VIBotX.log.info(element.getChildText("message"));
            VIBotX.log.info(element.getChildText("date"));
            if (!tell.getReceiver().equals(element.getChildText("receiver")))
                continue;
            if (!tell.getSender().equals(element.getChildText("sender")))
                continue;
            if (!tell.getMessage().equals(element.getChildText("message")))
                continue;
            if (!tell.getDateString().equals(element.getChildText("date")))
                continue;

            toremove.add(element);
        }
        for (Element e : toremove) {
            e.detach();
        }
        try {
            write();
        } catch (IOException e) {
            VIBotX.log.error("Error deleting from TellPlugin Xml File", e);
        }
    }

    public List<Tell> load() {
        ArrayList<Tell> toRet = new ArrayList<Tell>();
        for (Element element : getDocument().getRootElement().getChildren()) {
            String receiver, sender, message, date;

            receiver = element.getChild("receiver").getText();
            sender = element.getChild("sender").getText();
            message = element.getChild("message").getText();
            date = element.getChild("date").getText();

            Tell tell = new Tell(receiver, sender, message, date);
            toRet.add(tell);
        }
        return toRet;
    }

    private void write() throws IOException {
        RandomAccessFile f = new RandomAccessFile(getFile().getPath(), "rw");
        f.getChannel().lock();
        f.setLength(0);
        f.write(getXmlSerializer().outputString(getDocument()).getBytes(Charset.forName("UTF-8")));
        f.close();
    }
}