streamme.data.FolderStructure.java Source code

Java tutorial

Introduction

Here is the source code for streamme.data.FolderStructure.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 streamme.data;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONObject;
import org.json.JSONArray;

/**
 *
 * @author Scop
 */
public class FolderStructure {
    private List<Folder> structure;
    private HashMap<Integer, FileNode> files;
    private HashMap<Integer, Folder> folders;

    public FolderStructure() {
        this.structure = new ArrayList<>();
        this.files = new HashMap<>();
        this.folders = new HashMap<>();
    }

    public void addFolder(String path) {
        Folder f = new Folder(folders.size(), path);
        folders.put(f.getId(), f);
        structure.add(f);
        fillFolder(f);
        cleanEmptyFolder(f);
    }

    private void fillFolder(Folder folder) {
        String path = folder.getPath();

        File f = new File(path);
        File[] listOfFiles = f.listFiles();
        for (File file : listOfFiles) {
            String filename = file.getName();
            String nPath = path + "\\" + filename;
            if (file.isFile()) {
                if (filename.matches("^.*\\.(mp3|m4a)$") && !hasFile(nPath)) {
                    FileNode fn = new FileNode(files.size(), nPath);
                    folder.addFile(fn);
                    files.put(fn.getId(), fn);
                }
            } else if (file.isDirectory()) {
                if (!hasFolder(nPath)) {
                    Folder newFolder = new Folder(folders.size(), nPath);
                    folder.addFolder(newFolder);
                    fillFolder(newFolder);
                    folders.put(newFolder.getId(), newFolder);
                }
            }
        }
    }

    private boolean cleanEmptyFolder(Folder folder) {
        List<Folder> toRemove = new ArrayList<>();
        for (Folder f : folder.getFolders()) {
            if (cleanEmptyFolder(f))
                toRemove.add(f);
        }
        for (Folder f : toRemove)
            folder.getFolders().remove(f);
        return folder.getFolders().isEmpty() && folder.getFiles().isEmpty();
    }

    public FileNode getFile(int id) {
        return files.get(id);
    }

    public Folder getFolder(int id) {
        return folders.get(id);
    }

    public boolean hasFile(String path) {
        //for (Folder f : folders.values())
        //    if (f.hasFile(path))
        //        return true;
        return false;
    }

    public boolean hasFolder(String path) {
        //for (Folder f : folders.values())
        //    if (f.hasFolder(path))
        //        return true;
        return false;
    }

    public List<Folder> getStructure() {
        return structure;
    }

    public HashMap<Integer, FileNode> getFiles() {
        return files;
    }

    public HashMap<Integer, Folder> getFolders() {
        return folders;
    }

    @Override
    public String toString() {
        String str = "";
        for (Folder f : structure) {
            str += "+ " + f.getPath() + "\n";
            str += f.depthPrint(1);
        }
        return str;
    }

    public String getJSON() {
        JSONArray main = new JSONArray();
        for (Folder f : structure) {
            main.put(getJSON(f));
        }
        return main.toString();
    }

    private JSONObject getJSON(Folder f) {
        JSONObject node = new JSONObject();
        JSONArray jsonFile = new JSONArray();
        JSONArray jsonFolder = new JSONArray();
        List<Folder> childs = f.getFolders();
        List<FileNode> files = f.getFiles();

        for (FileNode fn : files) {
            JSONObject filenode = new JSONObject();
            filenode.put("id", fn.getId());
            filenode.put("name", fn.getFilename());
            jsonFile.put(filenode);
        }
        for (Folder child : childs) {
            jsonFolder.put(getJSON(child));
        }
        node.put("id", f.getId());
        node.put("name", f.getFolderName());
        node.put("files", jsonFile);
        node.put("folders", jsonFolder);
        return node;
    }
}