View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
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   * Start action.
40   *
41   * If there is a start action file, then load it and run inside actions, otherwise
42   * just starts normal ui action (open db if exists, or go to manage db screen).
43   *
44   * @author Tony Chemit - chemit@codelutin.com
45   * @since 2.4
46   */
47  public class StartAction extends AbstractMainUITuttiAction {
48  
49      /** Logger. */
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                  // get action to execute
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                      // install db
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                      // import db
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                 // delete start action file
102                 ApplicationIOUtil.deleteFile(
103                         actionfile,
104                         t("tutti.error.delete.startActionFile", actionfile));
105             }
106         }
107 
108         if (!doAction) {
109 
110             // no start action, normal start
111 
112             TuttiUIContext context = getContext();
113 
114             if (context.isDbLoaded()) {
115 
116                 // db already opened (happens when reloading ui)
117                 // just go to select cruise screen
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                     // open tutti db (using a fake button to have simple api)
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                     // clean db context
136                     context.clearDbContext();
137 
138                     // go to manage db screen (to install db)
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             // delete it before install
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 }