mysynopsis.SystemInitializer.java Source code

Java tutorial

Introduction

Here is the source code for mysynopsis.SystemInitializer.java

Source

/*
 * The MIT License
 *
 * Copyright 2016 Tonmoay Deb <tonmoay.deb@northsouth.edu>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package mysynopsis;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
import org.apache.commons.io.FileUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 *
 * @author Tonmoay Deb
 * 
 */

public abstract class SystemInitializer {

    //This Class initializes system & creates files with default value ( if none )
    /**
     * @throws IOException
     */
    public static void initSystem() throws IOException {
        try {
            FileInputStream fstream;
            fstream = new FileInputStream("data.json");
            fstream.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            setDefault();

        }
        try {
            FileInputStream fstream;
            fstream = new FileInputStream("template.html");
            fstream.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            setDefaultHTML();

        }
    }

    private static void setDefault() throws IOException {
        JSONObject bio = new JSONObject();
        bio.put("Name", "");
        bio.put("Initial", "");
        bio.put("Bio",
                "<p><font face=\"Arial\" size=\"4\"><b>Biography goes here</b></font></p><p><br></p><p></p><p><font face=\"Segoe UI\" size=\"4\"><br></font></p>");
        bio.put("Designation", "");
        bio.put("Department", "");
        bio.put("University", "");
        bio.put("University_Address", "");
        bio.put("Office", "");
        bio.put("Ohours", "");
        bio.put("Phone", "");
        bio.put("Email", "");
        bio.put("Resume_Link", "");
        bio.put("Educational",
                "<p><font face=\"Arial\" size=\"4\"><b>Educational Information</b></font></p><p><br></p><p></p><p><font face=\"Segoe UI\" size=\"4\"><br></font></p>");
        bio.put("Professional",
                "<p><font face=\"Arial\" size=\"4\"><b>Professional Experience</b></font></p><p><br></p><p></p><p><font face=\"Segoe UI\" size=\"4\"><br></font></p>");
        bio.put("Awards",
                "<p><font face=\"Arial\" size=\"4\"><b>Awards</b></font></p><p><br></p><p></p><p><font face=\"Segoe UI\" size=\"4\"><br></font></p>");

        bio.put("Image_Ext", "png");
        bio.put("Image_String",
                "iVBORw0KGgoAAAANSUhEUgAAAHgAAAB4CAYAAAA5ZDbSAAAKxklEQVR42u1de2wcRx0eQEAqQQVCAhFaSiv+QfwBqQoECAgJCbUChCoKCJXm2QYngdA0Aadp1IY4zbNJaePWcR7Ow44fiRO/HdmxYyf3ct2QPpI4e3uk0JBU/EMLTbx7O7vsMt+2xCFOfA/v7s3szk/65Lvz3t3sfDczv9f8hoRBXhqxP5XKmN9LpK35CdVcm1RpA3s8kExbp9njt+IK/XdSoTr7+x8Aj/Ga+z/Veh3Xssf1eG88bT2Cz8JnEinBS7dqfzSept9JqNbyhGK1M2IuJVXL8QNxlV5k5Lex7ypPKXQGvptI8V7i5+yprJMXJhWrg4280VzE+Ej4FfyoUqq14PiI/VkipXiJvel80iVVtU4wUm10ME9Am+KKNcgelw381fkEkZJbHMf5QCJDv5VM031YJ7khNDfZGhvde2Mq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r+h2twLd6D965ho3NTOy1bd0T/AgmB/Bd/W0C3K+5ULAAAAABJRU5ErkJggg==");

        // JSONArray education = new JSONArray();

        /*for(int i=0;i<8;i++) {
            
        education.add("");
        }
        bio.put("Education", education);*/

        JSONArray ftpinfo = new JSONArray();

        ftpinfo.add("");
        ftpinfo.add("");
        ftpinfo.add("");

        bio.put("Server_Information", ftpinfo);

        /* JSONArray professional = new JSONArray();
        professional.add("Profession 1: ");
        professional.add("Profession 2: ");
        bio.put("Professional Experience:", professional);
            
        JSONArray awards = new JSONArray();
        awards.add("Award 1: ");
        awards.add("Award 2: ");
        bio.put("Honors & Awards:", awards);*/

        try (FileWriter file = new FileWriter("data.json")) {

            file.write(bio.toJSONString());
        } catch (IOException e) {

            JOptionPane.showMessageDialog(null,
                    "Can't Create Data.json File. Please Check Your Directory Permission");
        }
    }

    private static void setDefaultHTML() {

        String htmlstr = "<html>\n" + "<head>\n" + "<title> $title </title>\n" + "</head>\n" + "<body>\n"
                + "<h2>$name $initial</h2>\n" + "<h3>$desig</h3>\n" + "<h4>$department<br>$university</h4>\n" + "\n"
                + "<p align=\"center\"><img src=\"data:image/$type;base64,$imgStr\" ></p>\n"
                + "<h4>Office Room: <i>$officeroom </i><br>Office Hours: <i>$officehours</i><br>Phone Number: <i>$phone</i><br>Email: <i>$email</i><br>Resume: <i>$resume</i></h4>\n"
                + "\n" + "\n" + "<center><p><h2>Biography</h2></p></center>\n" + "<p><h4>$biograph</h4></p>\n"
                + "<hr>\n" + "\n" + "$educational\n" + "\n" + "$professional\n" + "\n" + "$awards\n" + "\n"
                + "</body>\n" + "</html>";

        File newhtml;
        try {
            newhtml = new File("template.html");
            FileUtils.writeStringToFile(newhtml, htmlstr, "UTF-8");

        } catch (IOException e) {

            JOptionPane.showMessageDialog(null,
                    "Can't Create template.html File. Please Check Your Directory Permission");

        }
    }

}