001package org.intellimate.izou.system.context;
002
003import org.intellimate.izou.identification.Identification;
004import org.intellimate.izou.identification.IllegalIDException;
005import org.intellimate.izou.system.file.FileSubscriber;
006import org.intellimate.izou.system.file.ReloadableFile;
007
008import java.io.File;
009import java.io.IOException;
010import java.nio.file.Path;
011
012/**
013 * @author Leander Kurscheidt
014 * @version 1.0
015 */
016public interface Files {
017    /**
018     * Use this method to register a file with the watcherService
019     *
020     * @param dir directory of file
021     * @param fileType the name/extension of the file
022     *                 IMPORTANT: Please try to always enter the full name with extension of the file (Ex: "test.txt"),
023     *                 it would be best if the fileType is the full file name, and that the file name is clearly
024     *                 distinguishable from other files.
025     *                 For example, the property files are stored with the ID of the addon they belong too. That way
026     *                 every property file is easily distinguishable.
027     * @param reloadableFile object of interface that file belongs to
028     * @throws java.io.IOException exception thrown by watcher service
029     */
030    void registerFileDir(Path dir, String fileType, ReloadableFile reloadableFile) throws IOException;
031
032    /**
033     * Writes default file to real file
034     * The default file would be a file that can be packaged along with the code, from which a real file (say a
035     * properties file for example) can be loaded. This is useful because there are files (like property files0 that
036     * cannot be shipped with the package and have to be created at runtime. To still be able to fill these files, you
037     * can create a default file (usually txt) from which the content, as mentioned above, can then be loaded into the
038     * real file.
039     *
040     * @param defaultFilePath path to default file (or where it should be created)
041     * @param realFilePath path to real file (that should be filled with content of default file)
042     * @return true if operation has succeeded, else false
043     */
044    boolean writeToFile(String defaultFilePath, String realFilePath);
045
046    /**
047     * Creates a default File in case it does not exist yet. Default files can be used to load other files that are
048     * created at runtime (like properties file)
049     *
050     * @param defaultFilePath path to default file.txt (or where it should be created)
051     * @param initMessage the string to write in default file
052     * @throws java.io.IOException is thrown by bufferedWriter
053     */
054    void createDefaultFile(String defaultFilePath, String initMessage) throws IOException;
055
056    /**
057     * Registers a {@link FileSubscriber} with a {@link ReloadableFile}. So when the {@code reloadableFile} is
058     * reloaded, the fileSubscriber will be notified. Multiple file subscribers can be registered with the same
059     * reloadable file.
060     *
061     * @param reloadableFile the reloadable file that should be observed
062     * @param fileSubscriber the fileSubscriber that should be notified when the reloadable file is reloaded
063     * @param identification the Identification of the requesting instance
064     * @throws IllegalIDException not yet implemented
065     */
066    void register(ReloadableFile reloadableFile, FileSubscriber fileSubscriber, Identification identification) throws IllegalIDException;
067
068    /**
069     * Registers a {@link FileSubscriber} so that whenever any file is reloaded, the fileSubscriber is notified.
070     *
071     * @param fileSubscriber the fileSubscriber that should be notified when the reloadable file is reloaded
072     * @param identification the Identification of the requesting instance
073     * @throws IllegalIDException not yet implemented
074     */
075    void register(FileSubscriber fileSubscriber, Identification identification) throws IllegalIDException;
076
077    /**
078     * Unregisters all instances of fileSubscriber found.
079     *
080     * @param fileSubscriber the fileSubscriber to unregister
081     */
082    void unregister(FileSubscriber fileSubscriber);
083
084    /**
085     * gets the File pointing towards the location of the lib-folder
086     * @return the File
087     */
088    File getLibLocation();
089
090    /**
091     * gets the File pointing towards the location of the resource-folder
092     * @return the File
093     */
094    File getResourceLocation();
095
096    /**
097     * gets the File pointing towards the location of the properties-folder
098     * @return the File
099     */
100    File getPropertiesLocation();
101
102    /**
103     * gets the File pointing towards the location of the logs-folder
104     * @return the File
105     */
106    File getLogsLocation();
107}