001package jmri.jmrit.logixng.expressions;
002
003import java.beans.*;
004import java.io.*;
005import java.nio.file.Files;
006import java.util.*;
007
008import jmri.InstanceManager;
009import jmri.JmriException;
010import jmri.jmrit.logixng.*;
011import jmri.jmrit.logixng.util.*;
012
013
014/**
015 * Does a file exists? If so, delete the file and return true.
016 *
017 * @author Daniel Bergqvist Copyright 2023
018 */
019public class FileAsFlag extends AbstractDigitalExpression
020        implements PropertyChangeListener {
021
022    private final LogixNG_SelectString _selectFilename =
023            new LogixNG_SelectString(this, this);
024
025    private final LogixNG_SelectEnum<DeleteOrKeep> _selectDeleteOrKeep =
026            new LogixNG_SelectEnum<>(this, DeleteOrKeep.values(), DeleteOrKeep.Keep, this);
027
028
029    public FileAsFlag(String sys, String user)
030            throws BadUserNameException, BadSystemNameException {
031        super(sys, user);
032    }
033
034    @Override
035    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws JmriException {
036        DigitalExpressionManager manager = InstanceManager.getDefault(DigitalExpressionManager.class);
037        String sysName = systemNames.get(getSystemName());
038        String userName = userNames.get(getSystemName());
039        if (sysName == null) sysName = manager.getAutoSystemName();
040        FileAsFlag copy = new FileAsFlag(sysName, userName);
041        copy.setComment(getComment());
042        _selectFilename.copy(copy._selectFilename);
043        _selectDeleteOrKeep.copy(copy._selectDeleteOrKeep);
044        return manager.registerExpression(copy);
045    }
046
047    public LogixNG_SelectString getSelectFilename() {
048        return _selectFilename;
049    }
050
051    public LogixNG_SelectEnum<DeleteOrKeep> getSelectDeleteOrKeep() {
052        return _selectDeleteOrKeep;
053    }
054
055    /** {@inheritDoc} */
056    @Override
057    public Category getCategory() {
058        return Category.OTHER;
059    }
060
061    /** {@inheritDoc} */
062    @Override
063    public boolean evaluate() throws JmriException {
064
065        String filename = _selectFilename.evaluateValue(getConditionalNG());
066        DeleteOrKeep deleteOrKeep = _selectDeleteOrKeep.evaluateEnum(getConditionalNG());
067
068        if (filename == null) return false;
069
070        try {
071            File file = new File(jmri.util.FileUtil.getExternalFilename(filename));
072            if (file.exists()) {
073                if (deleteOrKeep == DeleteOrKeep.Delete) {
074                    Files.delete(file.toPath());
075                }
076                return true;
077            } else {
078                return false;
079            }
080        } catch (IOException e) {
081            throw new JmriException("IOException has occurred: " + e.getLocalizedMessage(), e);
082        }
083    }
084
085    @Override
086    public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException {
087        throw new UnsupportedOperationException("Not supported.");
088    }
089
090    @Override
091    public int getChildCount() {
092        return 0;
093    }
094
095    @Override
096    public String getShortDescription(Locale locale) {
097        return Bundle.getMessage(locale, "FileAsFlag_Short");
098    }
099
100    @Override
101    public String getLongDescription(Locale locale) {
102        String filename = _selectFilename.getDescription(locale);
103        String deleteOrKeep = _selectDeleteOrKeep.getDescription(locale);
104
105        return Bundle.getMessage(locale, "FileAsFlag_Long", filename, deleteOrKeep);
106    }
107
108    /** {@inheritDoc} */
109    @Override
110    public void setup() {
111        // Do nothing
112    }
113
114    /** {@inheritDoc} */
115    @Override
116    public void registerListenersForThisClass() {
117        if (!_listenersAreRegistered) {
118            _listenersAreRegistered = true;
119        }
120    }
121
122    /** {@inheritDoc} */
123    @Override
124    public void unregisterListenersForThisClass() {
125        if (_listenersAreRegistered) {
126            _listenersAreRegistered = false;
127        }
128    }
129
130    /** {@inheritDoc} */
131    @Override
132    public void propertyChange(PropertyChangeEvent evt) {
133        getConditionalNG().execute();
134    }
135
136    /** {@inheritDoc} */
137    @Override
138    public void disposeMe() {
139        // Do nothing
140    }
141
142
143    public enum DeleteOrKeep {
144        Delete(Bundle.getMessage("FileAsFlag_Delete")),
145        Keep(Bundle.getMessage("FileAsFlag_Keep"));
146
147        private final String _text;
148
149        private DeleteOrKeep(String text) {
150            this._text = text;
151        }
152
153        @Override
154        public String toString() {
155            return _text;
156        }
157
158    }
159
160
161//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(FileAsFlag.class);
162
163}