1 package fr.ifremer.tutti.ui.swing.content.actions;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import fr.ifremer.tutti.ui.swing.TuttiUIContext;
26 import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
27 import fr.ifremer.tutti.ui.swing.content.db.actions.ImportDbAction;
28 import fr.ifremer.tutti.ui.swing.content.db.actions.InstallDbAction;
29 import fr.ifremer.tutti.ui.swing.content.db.actions.OpenDbAction;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.nuiton.jaxx.application.ApplicationIOUtil;
33
34 import java.io.File;
35
36 import static org.nuiton.i18n.I18n.t;
37
38
39
40
41
42
43
44
45
46
47 public class StartAction extends AbstractMainUITuttiAction {
48
49
50 private static final Log log = LogFactory.getLog(StartAction.class);
51
52 protected AbstractMainUITuttiAction delegateAction;
53
54 public StartAction(MainUIHandler handler) {
55 super(handler, true);
56 }
57
58 @Override
59 public boolean prepareAction() throws Exception {
60
61 File actionfile = getConfig().getStartActionFile();
62 boolean doAction = actionfile.exists();
63
64 if (doAction) {
65
66 try {
67
68 String content = ApplicationIOUtil.readContent(actionfile, t("tutti.error.read.startActionFile", actionfile));
69 content = content.trim();
70
71 if (InstallDbAction.class.getName().equals(content)) {
72
73
74 delegateAction = getContext().getActionFactory().createLogicAction(handler, InstallDbAction.class);
75
76 if (log.isInfoEnabled()) {
77 log.info("Found install db action");
78 }
79 deleteOldDatabaseDirectory();
80
81 } else if (content.startsWith(ImportDbAction.class.getName())) {
82
83
84 ImportDbAction action = getContext().getActionFactory().createLogicAction(handler, ImportDbAction.class);
85 File importFile = new File(content.substring(ImportDbAction.class.getName().length() + 1));
86 action.setImportFile(importFile);
87 delegateAction = action;
88 if (log.isInfoEnabled()) {
89 log.info("Found import db action (with file " + importFile + ")");
90 }
91 deleteOldDatabaseDirectory();
92 } else {
93 if (log.isWarnEnabled()) {
94 log.warn("Unknown start action: " + content);
95 }
96 doAction = false;
97 }
98
99 } finally {
100
101
102 ApplicationIOUtil.deleteFile(
103 actionfile,
104 t("tutti.error.delete.startActionFile", actionfile));
105 }
106 }
107
108 if (!doAction) {
109
110
111
112 TuttiUIContext context = getContext();
113
114 if (context.isDbLoaded()) {
115
116
117
118 OpenHomeScreenAction action = getContext().getActionFactory().createLogicAction(handler, OpenHomeScreenAction.class);
119 action.setSkipCheckCurrentScreen(true);
120 action.setActionDescription(getUI().getMenuActionSelectCruise().getToolTipText());
121 delegateAction = action;
122
123 } else {
124
125 if (context.isDbExist()) {
126
127
128 OpenDbAction action = getContext().getActionFactory().createLogicAction(handler, OpenDbAction.class);
129 action.setSkipCheckCurrentScreen(true);
130 action.setUpdateReferentiel(true);
131 delegateAction = action;
132
133 } else {
134
135
136 context.clearDbContext();
137
138
139 OpenDbScreenAction action = getContext().getActionFactory().createLogicAction(handler, OpenDbScreenAction.class);
140 action.setSkipCheckCurrentScreen(true);
141 delegateAction = action;
142 }
143 }
144 }
145
146 setActionDescription(delegateAction.getActionDescription());
147 doAction = delegateAction.prepareAction();
148 return doAction;
149 }
150
151 @Override
152 public void doAction() throws Exception {
153
154 getActionEngine().runInternalAction(delegateAction);
155 }
156
157 @Override
158 protected void releaseAction() {
159 delegateAction = null;
160 super.releaseAction();
161 }
162
163 protected void deleteOldDatabaseDirectory() {
164 File dbDirectory = getConfig().getDbDirectory();
165 if (dbDirectory.exists()) {
166
167
168 if (log.isInfoEnabled()) {
169 log.info("Delete previous database directory: " + dbDirectory);
170 }
171 ApplicationIOUtil.deleteDirectory(dbDirectory, "Could not delete old db directory");
172 }
173 }
174 }