001package jmri.jmrix.can.cbus.node;
002
003import java.io.File;
004import java.io.IOException;
005import java.nio.file.Files;
006import java.nio.file.Paths;
007
008import javax.annotation.CheckForNull;
009import javax.annotation.Nonnull;
010
011import jmri.InstanceManager;
012import jmri.jmrit.XmlFile;
013import jmri.jmrix.can.CanSystemConnectionMemo;
014
015import jmri.util.FileUtil;
016
017/**
018 * Class to define location for a CbusNodeBackup File.
019 * @author Steve Young Copyright (C) 2019
020 */
021public class CbusNodeBackupFile extends XmlFile {
022
023    final CanSystemConnectionMemo memo;
024
025    public CbusNodeBackupFile( CanSystemConnectionMemo memo ) {
026        this.memo = memo;
027    }
028
029    private CanSystemConnectionMemo getMemo() {
030        return( memo != null ? memo : InstanceManager.getDefault(CanSystemConnectionMemo.class) );
031    }
032
033    /**
034     * Get Backup FileName for a given Node Number.
035     * Includes full directory path and filename.
036     *
037     * @param nodeNum Node Number
038     * @return the Backup File location within user directory.
039     */
040    @Nonnull
041    public String getDefaultFileName(int nodeNum) {
042        return getFileLocation() + File.separator + getFileName(nodeNum);
043    }
044
045    /**
046     * Get Backup File for a given Node Number.
047     *
048     * @param nodeNum Node Number
049     * @param store True to make a new file if does not exist
050     * @return the Backup File
051     */
052    @CheckForNull
053    public File getFile(int nodeNum, boolean store) {
054        // Verify that cbus/M/node/ directory exists
055        FileUtil.createDirectory(getFileLocation());
056        migrateFileLocation();
057
058        File file = findFile(getDefaultFileName(nodeNum));
059        if (file == null && store) {
060            file = new File(getDefaultFileName(nodeNum));
061        }
062        return file;
063    }
064
065    /**
066     * Get Backup FileName for a given Node Number.
067     *
068     * @param nodeNum Node Number
069     * @return the Backup FileName
070     */
071    public String getFileName(int nodeNum) {
072        return nodeNum + ".xml";  // NOI18N
073    }
074
075    /**
076     * Path to location of files.
077     *
078     * @return path to location
079     */
080    public String getFileLocation() {
081        return FileUtil.getUserFilesPath() 
082        + "cbus" + File.separator + getMemo().getSystemPrefix() + File.separator + "nodes";  // NOI18N
083    }
084
085    /**
086     * Delete Backup File for a given Node Number.
087     *
088     * @param nodeNum Node Number
089     * @return true if no file to delete, or delete success. Else false.
090     */
091    public boolean deleteFile(int nodeNum) {
092        File toDelete = getFile(nodeNum,false);
093        if (toDelete != null ) {
094            return toDelete.delete();
095        }
096        return true;
097    }
098
099    protected final String oldFileLocation = FileUtil.getUserFilesPath() + "cbus" + File.separator + "nodes";
100
101    private void migrateFileLocation(){
102        if ( findFile(oldFileLocation ) == null ){
103            return;
104        }
105        try {
106            jmri.jmrix.can.cbus.eventtable.CbusEventTableXmlFile.migrate(Paths.get(oldFileLocation), getFileLocation(), getMemo().getSystemPrefix() );
107            Files.delete(Paths.get(oldFileLocation));
108            log.warn("Migrated existing CBUS Node Data to {}", getMemo().getUserName());
109        } catch(IOException e){
110            log.error("Unable to migrate CBUS Data ",e);
111        }
112    }
113
114    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CbusNodeBackupFile.class);
115}