View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.home.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 com.google.common.base.Preconditions;
26  import com.google.common.collect.Sets;
27  import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel;
28  import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
29  import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocols;
30  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
31  import fr.ifremer.tutti.persistence.entities.referential.Species;
32  import fr.ifremer.tutti.service.protocol.ProtocolImportExportService;
33  import fr.ifremer.tutti.ui.swing.TuttiScreen;
34  import fr.ifremer.tutti.ui.swing.content.actions.AbstractChangeScreenAction;
35  import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
36  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
37  import jaxx.runtime.JAXXUtil;
38  import jaxx.runtime.context.JAXXContextEntryDef;
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  
42  import javax.swing.JOptionPane;
43  import javax.swing.UIManager;
44  import java.io.File;
45  import java.util.List;
46  import java.util.Map;
47  import java.util.Set;
48  
49  import static org.nuiton.i18n.I18n.t;
50  
51  /**
52   * Opens a file chooser and imports the protocol from the selected file.
53   *
54   * @author Tony Chemit - chemit@codelutin.com
55   * @since 1.0
56   */
57  public class ImportProtocolAction extends AbstractChangeScreenAction {
58  
59      /** Logger. */
60      private static final Log log =
61              LogFactory.getLog(ImportProtocolAction.class);
62  
63      public static final JAXXContextEntryDef<TuttiProtocol> IMPORT_PROTOCOL_ENTRY =
64              JAXXUtil.newContextEntryDef("importProtocol", TuttiProtocol.class);
65  
66      protected TuttiProtocol protocol;
67  
68      public ImportProtocolAction(MainUIHandler handler) {
69          super(handler, true, TuttiScreen.EDIT_PROTOCOL);
70      }
71  
72      @Override
73      public boolean prepareAction() throws Exception {
74  
75          IMPORT_PROTOCOL_ENTRY.removeContextValue(getContext().getMainUI());
76  
77          boolean doAction = super.prepareAction();
78  
79          File file = null;
80  
81          if (doAction) {
82              // choose file to import
83              file = chooseFile(
84                      t("tutti.selectCruise.title.choose.importProtocolFile"),
85                      t("tutti.selectCruise.action.importProtocol"),
86                      "^.+\\.tuttiProtocol$", t("tutti.common.file.protocol")
87              );
88  
89              doAction = file != null;
90          }
91  
92          if (doAction) {
93  
94              // import protocol
95              if (log.isInfoEnabled()) {
96                  log.info("Will import protocol file: " + file);
97              }
98  
99              ProtocolImportExportService service =
100                     getContext().getTuttiProtocolImportExportService();
101 
102             protocol = service.importProtocol(file);
103 
104             // remove id
105             protocol.setId((String) null);
106 
107             sendMessage(t("tutti.importProtocol.action.success", protocol.getName()));
108         }
109 
110         if (doAction) {
111 
112             // check that protocol is compatible with sample category model
113             doAction = cleanCategories(protocol);
114 
115         }
116 
117         if (doAction) {
118 
119             // replace obsolete referent taxons
120             TuttiProtocols.translateReferenceTaxonIds(protocol, getContext().getPersistenceService().getAllObsoleteReferentTaxons());
121 
122             List<Species> allReferentSpecies = getContext().getPersistenceService().getAllReferentSpecies();
123 
124             // clean species
125             doAction = cleanSpecies(allReferentSpecies, protocol);
126 
127             if (doAction) {
128 
129                 // clean benthos
130                 doAction = cleanBenthos(allReferentSpecies, protocol);
131             }
132         }
133 
134         return doAction;
135     }
136 
137     @Override
138     public void doAction() throws Exception {
139         Preconditions.checkNotNull(protocol);
140 
141         // store protocol in context
142         IMPORT_PROTOCOL_ENTRY.setContextValue(getContext().getMainUI(), protocol);
143         protocol = null;
144         createProgressionModelIfRequired(4);
145 
146         // removed selected protocol
147         getContext().setProtocolId(null);
148         super.doAction();
149     }
150 
151     protected boolean cleanCategories(TuttiProtocol protocol) {
152 
153         boolean doAction = true;
154         SampleCategoryModel sampleCategoryModel =
155                 getDataContext().getSampleCategoryModel();
156 
157         Set<Integer> badCategories = Sets.newHashSet();
158 
159         TuttiProtocols.checkSampleCategories(sampleCategoryModel,
160                                              protocol,
161                                              badCategories);
162 
163         if (!badCategories.isEmpty()) {
164 
165             if (log.isWarnEnabled()) {
166                 log.warn("There is some bad categories: " + badCategories);
167             }
168 
169             String message = TuttiProtocols.getBadCategoriesMessage(
170                     badCategories,
171                     getDecorator(Caracteristic.class, null),
172                     getContext().getPersistenceService());
173 
174             String htmlMessage = String.format(
175                     AbstractTuttiUIHandler.CONFIRMATION_FORMAT,
176                     message,
177                     t("tutti.common.askBeforeImportProtocol.help"));
178             int response = JOptionPane.showOptionDialog(
179                     getContext().getActionUI(),
180                     htmlMessage,
181                     t("tutti.common.askBeforeEditProtocol.title"),
182                     JOptionPane.YES_NO_OPTION,
183                     JOptionPane.WARNING_MESSAGE,
184                     UIManager.getIcon("warning"),
185                     new Object[]{t("tutti.option.cleanAndImport"), t("tutti.option.import"), t("tutti.option.cancel")},
186                     t("tutti.option.cancel")
187             );
188 
189             switch (response) {
190                 case 0:
191                     if (log.isInfoEnabled()) {
192                         log.info("Clean and Import");
193                     }
194                     TuttiProtocols.removeBadCategories(sampleCategoryModel, protocol);
195                     break;
196                 case 1:
197                     if (log.isInfoEnabled()) {
198                         log.info("Import with no cleaning");
199                     }
200                     break;
201                 default:
202 
203                     // cancel
204                     doAction = false;
205             }
206         }
207         return doAction;
208     }
209 
210 
211     protected boolean cleanSpecies(List<Species> allReferentSpecies, TuttiProtocol protocol) {
212 
213         boolean doAction = true;
214 
215         Map<Integer, String> missingSpecies = TuttiProtocols.detectMissingSpecies(protocol, allReferentSpecies);
216         boolean withBadSpecies = !missingSpecies.isEmpty();
217 
218         if (withBadSpecies) {
219 
220             if (log.isWarnEnabled()) {
221                 log.warn("There is some bad species to remove.");
222             }
223 
224             String message = TuttiProtocols.getBadSpeciesMessage(missingSpecies);
225 
226             String htmlMessage = String.format(
227                     AbstractTuttiUIHandler.CONFIRMATION_FORMAT,
228                     message,
229                     t("tutti.common.askBeforeImportProtocolSpecies.help"));
230 
231             int response = JOptionPane.showOptionDialog(
232                     getContext().getActionUI(),
233                     htmlMessage,
234                     t("tutti.common.askBeforeEditProtocolSpecies.title"),
235                     JOptionPane.YES_NO_OPTION,
236                     JOptionPane.WARNING_MESSAGE,
237                     UIManager.getIcon("warning"),
238                     new Object[]{t("tutti.option.cleanAndImport"), t("tutti.option.cancel")},
239                     t("tutti.option.cancel"));
240 
241             switch (response) {
242                 case 0:
243 
244                     if (log.isInfoEnabled()) {
245                         log.info("Clean species and Import");
246                     }
247                     TuttiProtocols.removeBadSpecies(protocol, missingSpecies);
248 
249                     break;
250                 default:
251 
252                     // cancel
253                     doAction = false;
254             }
255         }
256         return doAction;
257     }
258 
259     protected boolean cleanBenthos(List<Species> allReferentSpecies, TuttiProtocol protocol) {
260 
261         boolean doAction = true;
262 
263         Map<Integer, String> missingBenthos = TuttiProtocols.detectMissingBenthos(protocol, allReferentSpecies);
264         boolean withBadBenthos = !missingBenthos.isEmpty();
265         if (withBadBenthos) {
266 
267             if (log.isWarnEnabled()) {
268                 log.warn("There is some bad benthos to remove.");
269             }
270 
271             String message = TuttiProtocols.getBadBenthosMessage(missingBenthos);
272 
273             String htmlMessage = String.format(
274                     AbstractTuttiUIHandler.CONFIRMATION_FORMAT,
275                     message,
276                     t("tutti.common.askBeforeImportProtocolBenthos.help"));
277 
278             int response = JOptionPane.showOptionDialog(
279                     getContext().getActionUI(),
280                     htmlMessage,
281                     t("tutti.common.askBeforeEditProtocolBenthos.title"),
282                     JOptionPane.YES_NO_OPTION,
283                     JOptionPane.WARNING_MESSAGE,
284                     UIManager.getIcon("warning"),
285                     new Object[]{t("tutti.option.cleanAndImport"), t("tutti.option.cancel")},
286                     t("tutti.option.cancel"));
287 
288             switch (response) {
289                 case 0:
290 
291                     if (log.isInfoEnabled()) {
292                         log.info("Clean benthos and Import");
293                     }
294                     TuttiProtocols.removeBadBenthos(protocol, missingBenthos);
295 
296                     break;
297                 default:
298 
299                     // cancel
300                     doAction = false;
301             }
302         }
303         return doAction;
304     }
305 }