1 package fr.ifremer.reefdb.ui.swing.content.manage.referential.replace;
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.core.dao.technical.Assert;
27 import fr.ifremer.quadrige3.ui.core.dto.CodeOnly;
28 import fr.ifremer.quadrige3.ui.core.dto.referential.BaseReferentialDTO;
29 import fr.ifremer.reefdb.service.referential.ReferentialService;
30 import fr.ifremer.reefdb.ui.swing.action.AbstractReefDbAction;
31 import fr.ifremer.reefdb.ui.swing.util.AbstractReefDbUIHandler;
32 import fr.ifremer.reefdb.ui.swing.util.ReefDbUI;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 import java.util.Objects;
37
38 import static org.nuiton.i18n.I18n.n;
39 import static org.nuiton.i18n.I18n.t;
40
41
42
43
44
45
46
47
48
49
50 public abstract class AbstractReplaceAction<
51 E extends BaseReferentialDTO,
52 M extends AbstractReplaceUIModel<E>,
53 UI extends ReefDbUI<M, ?>,
54 H extends AbstractReefDbUIHandler<M, UI>> extends AbstractReefDbAction<M, UI, H> {
55
56 static {
57 n("reefdb.replaceTemporaryAndDelete.done");
58 n("reefdb.replaceTemporary.done");
59 }
60
61
62
63
64 private static final Log log =
65 LogFactory.getLog(AbstractReplaceAction.class);
66
67
68
69
70
71
72 protected abstract String getReferentialLabel();
73
74 private E source;
75
76 private E target;
77
78 private boolean delete;
79
80 private boolean replaced;
81
82
83
84
85
86
87
88
89
90 protected abstract boolean prepareReplaceReferential(ReferentialService service, E source, E target);
91
92
93
94
95
96
97
98
99
100 protected abstract void replaceReferential(ReferentialService service, E source, E target, boolean delete);
101
102
103
104
105 protected abstract void resetCaches();
106
107
108
109
110
111
112 protected AbstractReplaceAction(H handler) {
113 super(handler, false);
114 }
115
116
117 @Override
118 public boolean prepareAction() throws Exception {
119
120 replaced = false;
121 boolean doAction = super.prepareAction();
122
123 if (doAction) {
124
125 M model = getModel();
126 doAction = model.isValid();
127
128 if (doAction) {
129 source = model.getSelectedSource();
130 target = model.getSelectedTarget();
131 delete = model.isDelete();
132 }
133
134 if (Objects.equals(source, target)) {
135 displayErrorMessage(getReplaceUI().getTitle(), t("reefdb.replaceReferential.error.sameReferential"));
136 doAction = false;
137 }
138
139
140 if (!(source instanceof CodeOnly)) {
141
142 if (source.getId() == null) {
143 displayErrorMessage(getReplaceUI().getTitle(), t("reefdb.replaceReferential.error.source.invalid"));
144 doAction = false;
145 }
146
147 if (target.getId() == null) {
148 displayErrorMessage(getReplaceUI().getTitle(), t("reefdb.replaceReferential.error.target.invalid"));
149 doAction = false;
150 }
151 }
152 }
153
154 return doAction;
155 }
156
157
158
159
160
161
162 public E getSource() {
163 return source;
164 }
165
166
167
168
169
170
171 public E getTarget() {
172 return target;
173 }
174
175
176
177
178
179
180 public boolean isDelete() {
181 return delete;
182 }
183
184
185
186
187
188
189 public void setDelete(boolean delete) {
190 this.delete = delete;
191 }
192
193
194
195
196
197
198 protected AbstractReplaceUI getReplaceUI() {
199 return (AbstractReplaceUI) getUI();
200 }
201
202
203 @Override
204 protected String decorate(Object object) {
205 return super.decorate(object, getModel().getDecoratorContext());
206 }
207
208
209 @Override
210 public void doAction() {
211
212 Assert.notNull(source);
213 Assert.notNull(target);
214
215 if (!prepareReplaceReferential(getContext().getReferentialService(), source, target)) {
216 return;
217 }
218
219 if (log.isInfoEnabled()) {
220 log.info(String.format("Will replace %s temporary: %s by: %s",
221 getReferentialLabel(), decorate(source), decorate(target)));
222 }
223
224 replaceReferential(getContext().getReferentialService(), source, target, delete);
225 replaced = true;
226
227
228 resetCaches();
229
230 }
231
232
233 @Override
234 public void releaseAction() {
235 source = target = null;
236 delete = replaced = false;
237 super.releaseAction();
238 }
239
240
241 @Override
242 public void postSuccessAction() {
243 super.postSuccessAction();
244
245 getUI().getHandler().onCloseUI();
246
247 if (replaced) {
248 displayInfoMessage(getReplaceUI().getTitle(),
249 t(delete ? "reefdb.replaceTemporaryAndDelete.done" : "reefdb.replaceTemporary.done", getReferentialLabel(), decorate(source), decorate(target)));
250 }
251 }
252
253 }