bogdanrechi.lansator.updater.App.java Source code

Java tutorial

Introduction

Here is the source code for bogdanrechi.lansator.updater.App.java

Source

/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2017 Bogdan Rechi
 * 
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

package bogdanrechi.lansator.updater;

import org.apache.commons.io.FilenameUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;

import bogdanrechi.commons.Console;
import bogdanrechi.commons.Platform;
import bogdanrechi.commons.exceptions.StringException;
import bogdanrechi.commons.file.FileContent;
import bogdanrechi.commons.file.Files;
import bogdanrechi.commons.text.Text;
import bogdanrechi.lansator.properties.Configuration;
import bogdanrechi.lansator.properties.Item;
import bogdanrechi.lansator.properties.ProgramItem;
import bogdanrechi.lansator.properties.ProgramsGroupItem;

/**
 * Lansator updater
 *
 * @author Bogdan Rechi
 */
public class App {
    public static void main(String[] args) {
        if (args.length != 1) {
            Console.println("Usage: LansatorUpdater path_to_lansator.json");
            return;
        }

        Configuration _config;

        GsonBuilder builder = new GsonBuilder();
        Gson _gson = builder.serializeNulls().setPrettyPrinting().create();

        try {
            _config = _gson.fromJson(FileContent.readTextFile(args[0]), Configuration.class);

            String imagesFolder = Files.getParent(args[0]) + Platform.FILE_SEPARATOR + "images";

            for (ProgramsGroupItem groupItem : _config.programsGroups) {
                setItemImageFile(imagesFolder, groupItem.imageFile, groupItem);

                for (ProgramItem programItem : groupItem.programs) {
                    setItemImageFile(imagesFolder, programItem.imageFile, programItem);
                }
            }

            Console.println(_gson.toJson(_config) + "\r\n");

            FileContent.writeTextFile(_gson.toJson(_config) + "\r\n", args[0]);
        } catch (JsonSyntaxException | StringException e) {
            Console.println(e.getMessage());
        }
    }

    /**
     * Set item image file
     * 
     * @param imagesPath
     *          Images path
     * @param newImageFile
     *          Newly selected image file
     * @param item
     *          Item to set the image for
     */
    public static void setItemImageFile(String imagesPath, String newImageFile, Item item) throws StringException {
        String imageFileNewName = Text.newGuid() + "." + FilenameUtils.getExtension(newImageFile);

        if (!Files.exists(imagesPath)) {
            Files.createFolder(imagesPath);
        }

        Files.copyFileToFolder(newImageFile, imagesPath, imageFileNewName);

        item.imageFile = imageFileNewName;
    }
}