001package jmri.jmrit.operations; 002 003import java.io.File; 004import java.io.IOException; 005 006import org.slf4j.Logger; 007import org.slf4j.LoggerFactory; 008 009import jmri.*; 010import jmri.implementation.AbstractShutDownTask; 011import jmri.jmrit.operations.locations.LocationManager; 012import jmri.jmrit.operations.locations.schedules.ScheduleManager; 013import jmri.jmrit.operations.rollingstock.cars.CarManager; 014import jmri.jmrit.operations.rollingstock.engines.EngineManager; 015import jmri.jmrit.operations.routes.RouteManager; 016import jmri.jmrit.operations.setup.AutoBackup; 017import jmri.jmrit.operations.setup.Setup; 018import jmri.jmrit.operations.trains.TrainManager; 019import jmri.jmrit.operations.trains.schedules.TrainScheduleManager; 020 021/** 022 * A manager for Operations. This manager controls the Operations ShutDownTask. 023 * 024 * @author Randall Wood 2014 025 */ 026public final class OperationsManager implements InstanceManagerAutoDefault, InstanceManagerAutoInitialize { 027 028 private ShutDownTask shutDownTask = null; 029 030 static private final Logger log = LoggerFactory.getLogger(OperationsManager.class); 031 032 public OperationsManager() { 033 } 034 035 /** 036 * Get the path to the Operations folder, rooted in the User's file path, as 037 * a String. 038 * 039 * @return A path 040 */ 041 public String getPath() { 042 return OperationsXml.getFileLocation() + OperationsXml.getOperationsDirectoryName() + File.separator; 043 } 044 045 /** 046 * Get the path to a file rooted in the Operations path. 047 * 048 * @param name The name of the file 049 * @return A path 050 * @see #getPath() 051 */ 052 public String getPath(String name) { 053 if (name != null) { 054 return this.getPath() + name; 055 } 056 return this.getPath(); 057 } 058 059 /** 060 * Get a {@link java.io.File} rooted in the Operations path. 061 * 062 * @param name The name of the file 063 * @return A file 064 * @see #getPath() 065 */ 066 public File getFile(String name) { 067 return new File(this.getPath(name)); 068 } 069 070 /** 071 * Register the non-default {@link jmri.ShutDownTask}. 072 * <p> 073 * Replaces the existing operations ShutDownTask with the new task. Use a 074 * null value to prevent an operations ShutDownTask from being run when JMRI 075 * shuts down. Use {@link #getDefaultShutDownTask() } to use the default 076 * operations ShutDownTask. 077 * 078 * @param shutDownTask The new ShutDownTask or null 079 */ 080 public void setShutDownTask(ShutDownTask shutDownTask) { 081 ShutDownManager manager = InstanceManager.getDefault(ShutDownManager.class); 082 if (this.shutDownTask != null) { 083 manager.deregister(this.shutDownTask); 084 } 085 this.shutDownTask = shutDownTask; 086 if (this.shutDownTask != null) { 087 manager.register(this.shutDownTask); 088 } 089 } 090 091 /** 092 * Get a copy of the default operations {@link jmri.ShutDownTask}. The 093 * default ShutDownTask saves the operations state at shutdown without 094 * prompting. 095 * 096 * @return A new ShutDownTask 097 */ 098 public static ShutDownTask getDefaultShutDownTask() { 099 return new AbstractShutDownTask("Save Operations State") { // NOI18N 100 @Override 101 public void run() { 102 try { 103 OperationsXml.save(); 104 } catch (Exception ex) { 105 log.warn("Error saving operations state: {}", ex.getMessage()); 106 log.debug("Details follow: ", ex); 107 } 108 } 109 }; 110 } 111 112 @Override 113 public void initialize() { 114 // ensure the default instance of all operations managers 115 // are initialized by calling their instance() methods 116 // Is there a different, more optimal order for this? 117 InstanceManager.getDefault(CarManager.class); 118 InstanceManager.getDefault(EngineManager.class); 119 InstanceManager.getDefault(TrainManager.class); 120 InstanceManager.getDefault(LocationManager.class); 121 InstanceManager.getDefault(RouteManager.class); 122 InstanceManager.getDefault(ScheduleManager.class); 123 InstanceManager.getDefault(TrainScheduleManager.class); 124 this.setShutDownTask(OperationsManager.getDefaultShutDownTask()); 125 // auto backup? 126 if (Setup.isAutoBackupEnabled()) { 127 try { 128 AutoBackup backup = new AutoBackup(); 129 backup.autoBackup(); 130 } catch (IOException ex) { 131 log.debug("Auto backup after enabling Auto Backup flag.", ex); 132 } 133 } 134 } 135}