com.jpcasati.loar.beans.LoadFile.java Source code

Java tutorial

Introduction

Here is the source code for com.jpcasati.loar.beans.LoadFile.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jpcasati.loar.beans;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.servlet.http.Part;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author jpcasati
 */
@ManagedBean
@RequestScoped
public class LoadFile {

    private boolean showLoad = true;

    public boolean isLoaded() {
        return showLoad;
    }

    public void setLoaded(boolean showLoad) {
        this.showLoad = showLoad;
    }

    private String status = "Not Initiated";

    List<String> registers;

    public List<String> getRegisters() {
        return registers;
    }

    public void setRegisters(List<String> registers) {
        this.registers = registers;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    private javax.servlet.http.Part log;

    public Part getLog() {
        return log;
    }

    public void setLog(Part log) {
        this.log = log;
    }

    /**
     * Creates a new instance of LoadFile
     */
    public LoadFile() {
    }

    public void load() throws Exception {
        this.status = "Not Loaded Yet.";
        try {
            InputStream is = log.getInputStream();
            byte[] bytes = IOUtils.toByteArray(is);
            String logFile = new String(bytes);
            String lines[] = logFile.split("\\r?\\n");

            registers = new ArrayList<>(Arrays.asList(lines));

            this.showLoad = false;
            this.status = "Loaded Successfully.";
        } catch (IOException ex) {
            this.status = "Error on Load File.";
        }
    }

}