1 package fr.ifremer.tutti.ui.swing.content.db.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.TuttiConfiguration;
26 import fr.ifremer.tutti.persistence.ProgressionModel;
27 import fr.ifremer.tutti.ui.swing.TuttiUIContext;
28 import fr.ifremer.tutti.ui.swing.content.actions.AbstractMainUITuttiAction;
29 import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
30 import fr.ifremer.tutti.ui.swing.update.TuttiDbUpdaterCallBack;
31 import fr.ifremer.tutti.ui.swing.update.Updates;
32 import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.nuiton.updater.ApplicationInfo;
36
37 import javax.swing.JOptionPane;
38 import java.io.File;
39
40 import static org.nuiton.i18n.I18n.t;
41
42
43
44
45
46
47
48 public class UpdateDbAction extends AbstractMainUITuttiAction {
49
50
51 private static final Log log = LogFactory.getLog(UpdateDbAction.class);
52
53 protected ApplicationInfo updateDbVersion;
54
55 public UpdateDbAction(MainUIHandler handler) {
56 super(handler, true);
57 setActionDescription(t("tutti.dbManager.action.upgradeDb.tip"));
58 }
59
60 @Override
61 public boolean prepareAction() throws Exception {
62 boolean doAction = super.prepareAction();
63
64 updateDbVersion = null;
65
66 if (doAction) {
67
68 doAction = getContext().checkUpdateDataReachable(true);
69 }
70
71 if (doAction) {
72
73
74 updateDbVersion = Updates.getDatabaseUpdateVersion(getConfig());
75
76 if (getContext().isDbExist() &&
77 updateDbVersion != null &&
78 updateDbVersion.newVersion != null) {
79
80
81 String htmlMessage = String.format(
82 AbstractTuttiUIHandler.CONFIRMATION_FORMAT,
83 t("tutti.dbManager.updatedb.found", updateDbVersion.newVersion),
84 t("tutti.common.askBeforeUpdate.help"));
85 int i = JOptionPane.showConfirmDialog(
86 getHandler().getUI(),
87 htmlMessage,
88 t("tutti.dbManager.title.confirm.updatedb"),
89 JOptionPane.OK_CANCEL_OPTION,
90 JOptionPane.QUESTION_MESSAGE);
91
92 doAction = i == JOptionPane.OK_OPTION;
93 }
94 }
95 return doAction;
96 }
97
98 @Override
99 public void doAction() {
100 TuttiUIContext context = getContext();
101 TuttiConfiguration config = getConfig();
102
103 File current = config.getDataDirectory();
104 String url = config.getUpdateDataUrl();
105
106 if (log.isInfoEnabled()) {
107 log.info(String.format("Try to install / update db (current data location: %s), using update url: %s", current, url));
108 }
109
110
111 ProgressionModel progressionModel = new ProgressionModel();
112 context.getActionUI().getModel().setProgressionModel(progressionModel);
113 progressionModel.setMessage(t("tutti.dbManager.action.upgradeDb.check"));
114
115 TuttiDbUpdaterCallBack callback = new TuttiDbUpdaterCallBack(url, this, progressionModel);
116
117 Updates.doUpdate(config, callback, current);
118
119
120
121
122
123
124
125
126
127
128 if (callback.isDbUpdated()) {
129
130 sendMessage(t("tutti.dbManager.action.upgradeDb.done", updateDbVersion.newVersion));
131
132 } else {
133 sendMessage(t("tutti.dbManager.action.upgradeDb.upToDate"));
134 }
135 }
136
137 @Override
138 public void postSuccessAction() {
139 handler.reloadDbManagerText();
140 super.postSuccessAction();
141 }
142
143 @Override
144 public void postFailedAction(Throwable error) {
145 handler.reloadDbManagerText();
146 super.postFailedAction(error);
147 }
148 }