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.ui.swing.TuttiScreen;
32  import fr.ifremer.tutti.ui.swing.content.actions.AbstractChangeScreenAction;
33  import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
34  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
35  import jaxx.runtime.context.JAXXContextEntryDef;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  import javax.swing.JOptionPane;
40  import javax.swing.UIManager;
41  import java.util.Set;
42  
43  import static org.nuiton.i18n.I18n.t;
44  
45  /**
46   * Opens the protocol creation screen with the data of an already existing protocol.
47   *
48   * @author Tony Chemit - chemit@codelutin.com
49   * @since 1.0
50   */
51  public class CloneProtocolAction extends AbstractChangeScreenAction {
52  
53      /** Logger. */
54      private static final Log log = LogFactory.getLog(CloneProtocolAction.class);
55  
56      public static final JAXXContextEntryDef<TuttiProtocol> CLONE_PROTOCOL_ENTRY = new JAXXContextEntryDef<>("cloneProtocol", TuttiProtocol.class);
57  
58      protected TuttiProtocol protocol;
59  
60      public CloneProtocolAction(MainUIHandler handler) {
61          super(handler, true, TuttiScreen.EDIT_PROTOCOL);
62      }
63  
64      @Override
65      public boolean prepareAction() throws Exception {
66  
67          CLONE_PROTOCOL_ENTRY.removeContextValue(getContext().getMainUI());
68  
69          boolean doAction = super.prepareAction();
70          if (doAction) {
71  
72              // check that protocol is compatible with sample category model
73              SampleCategoryModel sampleCategoryModel =
74                      getDataContext().getSampleCategoryModel();
75  
76              if (log.isInfoEnabled()) {
77                  log.info("Clone protocol: " + getContext().getProtocolId());
78              }
79  
80              // get protocol
81              protocol = getContext().getPersistenceService().getProtocol(getContext().getProtocolId());
82  
83              // remove id
84              protocol.setId((String) null);
85  
86              Set<Integer> badCategories = Sets.newHashSet();
87  
88              TuttiProtocols.checkSampleCategories(sampleCategoryModel,
89                                                   protocol,
90                                                   badCategories);
91  
92              if (!badCategories.isEmpty()) {
93  
94                  // detect some bad categories
95                  if (log.isWarnEnabled()) {
96                      log.warn("There is some bad categories: " + badCategories);
97                  }
98  
99                  String message = TuttiProtocols.getBadCategoriesMessage(
100                         badCategories,
101                         getDecorator(Caracteristic.class, null),
102                         getContext().getPersistenceService());
103                 String htmlMessage = String.format(
104                         AbstractTuttiUIHandler.CONFIRMATION_FORMAT,
105                         message,
106                         t("tutti.common.askBeforeCloneProtocol.help"));
107                 int response = JOptionPane.showOptionDialog(
108                         getContext().getActionUI(),
109                         htmlMessage,
110                         t("tutti.common.askBeforeEditProtocol.title"),
111                         JOptionPane.YES_NO_OPTION,
112                         JOptionPane.WARNING_MESSAGE,
113                         UIManager.getIcon("warning"),
114                         new Object[]{t("tutti.option.cleanAndClone"), t("tutti.option.clone"), t("tutti.option.cancel")},
115                         t("tutti.option.cancel")
116                 );
117 
118                 switch (response) {
119                     case 0:
120                         // clean and clone
121                         if (log.isInfoEnabled()) {
122                             log.info("Clean and Clone");
123                         }
124                         TuttiProtocols.removeBadCategories(sampleCategoryModel,
125                                                            protocol);
126                         break;
127 
128                     case 1:
129                         // clone with no clean
130                         if (log.isInfoEnabled()) {
131                             log.info("Clone with no cleaning");
132                         }
133                         break;
134                     default:
135 
136                         // cancel
137                         doAction = false;
138                 }
139             }
140         }
141         return doAction;
142     }
143 
144     @Override
145     public void doAction() throws Exception {
146         Preconditions.checkNotNull(protocol);
147 
148         // store protocol in context
149         CLONE_PROTOCOL_ENTRY.setContextValue(getContext().getMainUI(), protocol);
150         protocol = null;
151 
152         createProgressionModelIfRequired(4);
153 
154         // removed selected protocol
155         getContext().setProtocolId(null);
156 
157         super.doAction();
158     }
159 }