1 package fr.ifremer.quadrige3.ui.swing.action;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import fr.ifremer.quadrige3.ui.swing.Application;
27 import fr.ifremer.quadrige3.ui.swing.callback.DataUpdaterCallBack;
28 import fr.ifremer.quadrige3.ui.swing.content.AbstractMainUIHandler;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.nuiton.updater.ApplicationUpdater;
32
33 import java.io.File;
34
35 import static org.nuiton.i18n.I18n.t;
36
37
38
39
40
41
42
43 public class UpdateDataAction extends AbstractMainUIAction {
44
45 private static final Log LOG = LogFactory.getLog(UpdateDataAction.class);
46
47 public UpdateDataAction(AbstractMainUIHandler handler) {
48 super(handler, true);
49 setActionDescription(t("quadrige3.applicationUpdater.data.action"));
50 types = DataUpdaterCallBack.UpdateType.values();
51 }
52
53 private DataUpdaterCallBack.UpdateType[] types;
54
55 private boolean reload;
56 private boolean silent = false;
57
58
59 @Override
60 public boolean prepareAction() throws Exception {
61 boolean doAction = super.prepareAction();
62
63 if (doAction) {
64
65 doAction = getContext().checkUpdateReachable(getConfig().getUpdateDataUrl(), true);
66 }
67 return doAction;
68 }
69
70
71 @Override
72 public void releaseAction() {
73 super.releaseAction();
74 types = DataUpdaterCallBack.UpdateType.values();
75 }
76
77
78 @Override
79 public void doAction() {
80
81 reload = false;
82
83 File current = getConfig().getBaseDirectory();
84 if (current == null || !current.exists()) {
85
86
87 if (LOG.isWarnEnabled()) {
88 LOG.warn("No application base directory defined, skip updates.");
89 }
90 } else {
91
92 String url = getConfig().getUpdateDataUrl();
93 File dest = new File(getConfig().getBaseDirectory(), "NEW");
94
95 if (LOG.isInfoEnabled()) {
96 LOG.info(String.format("Try to update i18N, help or config for exploitation context (current application location: %s), using update url: %s", current, url));
97 }
98
99 createProgressionUIModel();
100 getProgressionUIModel().setMessage(t("quadrige3.applicationUpdater.checkUpdates"));
101
102 DataUpdaterCallBack callback
103 = new DataUpdaterCallBack(this, getProgressionUIModel());
104
105 callback.setTypes(types);
106
107 ApplicationUpdater up = new ApplicationUpdater();
108 up.update(url,
109 current,
110 dest,
111 false,
112 callback,
113 getProgressionUIModel());
114
115 if (callback.isDataUpdated()) {
116
117 reload = true;
118
119 } else {
120
121 sendMessage(t("quadrige3.applicationUpdater.data.noUpdate"));
122 }
123 }
124 }
125
126
127
128
129
130
131 public void setTypes(DataUpdaterCallBack.UpdateType... types) {
132 this.types = types;
133 }
134
135 public void setSilent(boolean silent) {
136 this.silent = silent;
137 }
138
139
140 @Override
141 public void postSuccessAction() {
142 super.postSuccessAction();
143
144 if (silent) return;
145
146 if (reload) {
147
148 try {
149 Thread.sleep(1000);
150 } catch (InterruptedException e) {
151 if (LOG.isWarnEnabled()) {
152 LOG.warn("Could not wait 1 second...", e);
153 }
154 }
155
156
157 getHandler().showSuccessMessage(t("quadrige3.applicationUpdater.success.title"),
158 t("quadrige3.applicationUpdater.data.success.message"));
159
160 CloseApplicationAction action = getContext().getActionFactory().createLogicAction(
161 getHandler(), CloseApplicationAction.class);
162 action.setExitCode(Application.UPDATE_EXIT_CODE);
163 getActionEngine().runAction(action);
164 }
165 }
166
167
168
169
170
171
172 public boolean isReload() {
173 return reload;
174 }
175 }