View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.cruise.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.collect.Sets;
26  import fr.ifremer.tutti.persistence.entities.TuttiEntities;
27  import fr.ifremer.tutti.persistence.entities.data.Cruise;
28  import fr.ifremer.tutti.persistence.entities.referential.Gear;
29  import fr.ifremer.tutti.persistence.entities.referential.GearWithOriginalRankOrder;
30  import fr.ifremer.tutti.persistence.entities.referential.GearWithOriginalRankOrders;
31  import fr.ifremer.tutti.service.PersistenceService;
32  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
33  import fr.ifremer.tutti.ui.swing.content.cruise.EditCruiseUI;
34  import fr.ifremer.tutti.ui.swing.content.cruise.EditCruiseUIHandler;
35  import fr.ifremer.tutti.ui.swing.content.cruise.EditCruiseUIModel;
36  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
37  import fr.ifremer.tutti.ui.swing.util.TuttiUIUtil;
38  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
39  import org.apache.commons.lang3.ObjectUtils;
40  
41  import javax.swing.ImageIcon;
42  import javax.swing.JLabel;
43  import javax.swing.JOptionPane;
44  import java.util.Set;
45  
46  import static org.nuiton.i18n.I18n.t;
47  
48  /**
49   * Saves a cruise
50   *
51   * @author Kevin Morin - kmorin@codelutin.com
52   * @since 1.0
53   */
54  public class SaveCruiseAction extends LongActionSupport<EditCruiseUIModel, EditCruiseUI, EditCruiseUIHandler> {
55  
56      protected PersistenceService persistenceService;
57  
58      protected boolean updateVessel;
59  
60      protected boolean updateGear;
61  
62      public SaveCruiseAction(EditCruiseUIHandler handler) {
63          super(handler, true);
64          persistenceService = getContext().getPersistenceService();
65      }
66  
67      @Override
68      public boolean prepareAction() throws Exception {
69          boolean result = super.prepareAction();
70  
71          Cruise bean = getModel().toEntity();
72          if (!TuttiEntities.isNew(bean)) {
73              Cruise oldCruise = persistenceService.getCruise(bean.getIdAsInt());
74  
75              // check vessel has not changed
76              if (ObjectUtils.notEqual(oldCruise.getVessel(), bean.getVessel())) {
77  
78                  // if so ask user confirmation
79                  String htmlMessage = String.format(
80                          AbstractTuttiUIHandler.CONFIRMATION_FORMAT,
81                          t("tutti.editCruise.action.save.vesselChanged.message"),
82                          t("tutti.editCruise.action.save.vesselChanged.help"));
83  
84                  int answer = JOptionPane.showConfirmDialog(getContext().getActionUI(),
85                                                             htmlMessage,
86                                                             t("tutti.editCruise.action.save.vesselChanged.title"),
87                                                             JOptionPane.OK_CANCEL_OPTION,
88                                                             JOptionPane.WARNING_MESSAGE);
89  
90                  result = updateVessel = answer == JOptionPane.OK_OPTION;
91              }
92  
93              if (result) {
94  
95                  if (ObjectUtils.notEqual(oldCruise.getGear(), bean.getGear())) {
96  
97                      // get all gears to remove
98  
99                      Set<Gear> gearsToRemove =
100                             Sets.<Gear>newHashSet(oldCruise.getGear());
101 
102                     for (GearWithOriginalRankOrder gearWithOriginalRankOrder : bean.getGear()) {
103                         GearWithOriginalRankOrder g =
104                                 GearWithOriginalRankOrders.newGearWithOriginalRankOrder(
105                                         gearWithOriginalRankOrder.getIdAsInt(),
106                                         gearWithOriginalRankOrder.getOriginalRankOrder()
107                                 );
108                         gearsToRemove.remove(g);
109                     }
110 
111                     // check they are not used by any operation of the cruise
112                     boolean obsoleteGearIsUsed = persistenceService.isOperationUseGears(
113                             bean.getIdAsInt(), gearsToRemove);
114 
115                     if (obsoleteGearIsUsed) {
116 
117                         // not possible, can't removed used gears
118 
119                         String message = t("tutti.persistence.cruise.gearUsedInOperations.error");
120                         displayWarningMessage(
121                                 t("tutti.editCruise.action.save.gearChanged.title"),
122                                 "<html><body>" + message + "</body></html>"
123                         );
124                         result = false;
125                     } else {
126 
127                         // if so ask user confirmation
128                         String htmlMessage = String.format(
129                                 AbstractTuttiUIHandler.CONFIRMATION_FORMAT,
130                                 t("tutti.editCruise.action.save.gearChanged.message"),
131                                 t("tutti.editCruise.action.save.gearChanged.help"));
132 
133                         int answer = JOptionPane.showConfirmDialog(getContext().getActionUI(),
134                                                                    htmlMessage,
135                                                                    t("tutti.editCruise.action.save.gearChanged.title"),
136                                                                    JOptionPane.OK_CANCEL_OPTION,
137                                                                    JOptionPane.WARNING_MESSAGE);
138 
139                         result = updateGear = answer == JOptionPane.OK_OPTION;
140                     }
141                 }
142             }
143         }
144 
145         return result;
146     }
147 
148     @Override
149     public void doAction() throws Exception {
150         TuttiUIContext context = getContext();
151         EditCruiseUIModel model = getModel();
152 
153         Cruise bean = model.toEntity();
154 
155         Cruise saved;
156         boolean createCruise = TuttiEntities.isNew(bean);
157         if (createCruise) {
158 
159             saved = persistenceService.createCruise(bean);
160             model.setId(saved.getId());
161             sendMessage(t("tutti.flash.info.cruiseCreated", bean.getName()));
162         } else {
163             saved = persistenceService.saveCruise(bean, updateVessel, updateGear);
164             sendMessage(t("tutti.flash.info.cruiseSaved", bean.getName()));
165         }
166 
167         context.setProgramId(saved.getProgram().getId());
168         context.setCruiseId(saved.getIdAsInt());
169 
170         if (!createCruise) {
171 
172             // reload cruise
173             getDataContext().reloadCruise();
174         }
175 
176         getDataContext().closeCruiseCache();
177 
178         // update originalRankOrder for all gears of the cruise
179         for (GearWithOriginalRankOrder gear : model.getGear()) {
180             gear.setOriginalRankOrder(gear.getRankOrder());
181         }
182         // update SynchronizationStatus
183         model.setSynchronizationStatus(saved.getSynchronizationStatus());
184         model.setModify(false);
185     }
186 
187     @Override
188     public void postSuccessAction() {
189 
190         getContext().getMainUI().getHandler().setBodyTitle(EditCruiseUIHandler.getTitle(true));
191 
192         ImageIcon icon = TuttiUIUtil.getCruiseIcon(getModel());
193         getContext().getMainUI().getBody().setLeftDecoration(new JLabel(icon));
194     }
195 }