View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.fishing.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 fr.ifremer.tutti.persistence.entities.CaracteristicMap;
26  import fr.ifremer.tutti.persistence.entities.TuttiEntities;
27  import fr.ifremer.tutti.persistence.entities.data.CatchBatch;
28  import fr.ifremer.tutti.persistence.entities.data.CatchBatchs;
29  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
30  import fr.ifremer.tutti.service.PersistenceService;
31  import fr.ifremer.tutti.service.ValidationService;
32  import fr.ifremer.tutti.ui.swing.content.operation.EditFishingOperationUI;
33  import fr.ifremer.tutti.ui.swing.content.operation.EditFishingOperationUIHandler;
34  import fr.ifremer.tutti.ui.swing.content.operation.EditFishingOperationUIModel;
35  import fr.ifremer.tutti.ui.swing.content.operation.FishingOperationsUIModel;
36  import fr.ifremer.tutti.ui.swing.content.operation.fishing.AbstractCaracteristicTabUIModel;
37  import fr.ifremer.tutti.ui.swing.content.operation.fishing.GearUseFeatureTabUIModel;
38  import fr.ifremer.tutti.ui.swing.content.operation.fishing.VesselUseFeatureTabUIModel;
39  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  /**
44   * Saves a fishing operation and potentially sets another screen or switch to another tab.
45   *
46   * @author Kevin Morin - kmorin@codelutin.com
47   * @since 1.0
48   */
49  public class SaveFishingOperationAction extends LongActionSupport<EditFishingOperationUIModel, EditFishingOperationUI, EditFishingOperationUIHandler> {
50  
51      /** Logger. */
52      private static final Log log = LogFactory.getLog(SaveFishingOperationAction.class);
53  
54      /**
55       * A flag to update ui after create or save the edit fishing operation.
56       *
57       * @since 1.0
58       */
59      protected boolean updateUI;
60  
61      protected boolean moveTab;
62  
63      public SaveFishingOperationAction(EditFishingOperationUIHandler handler) {
64          super(handler, true);
65      }
66  
67      public void setUpdateUI(boolean updateUI) {
68          this.updateUI = updateUI;
69      }
70  
71      @Override
72      public void releaseAction() {
73          updateUI = true;
74          super.releaseAction();
75      }
76  
77      @Override
78      public void doAction() throws Exception {
79  
80          // previous fishingOperation was modified, let's save it
81          EditFishingOperationUIModel beanToSave = handler.getFishingOperationMonitor().getBean();
82  
83          moveTab = true;
84  
85          // must save when bean is new or was modifiy and is valid
86          boolean mustSave = beanToSave.getFishingOperation() != null && beanToSave.isValid();
87  
88          if (mustSave) {
89  
90              // prepare model
91              beanToSave.convertGearShootingCoordinatesToDD();
92  
93              // save modified fishing operation
94              FishingOperation toSave = beanToSave.toEntity();
95  
96              AbstractCaracteristicTabUIModel[] subModels = handler.getSubModels();
97              for (AbstractCaracteristicTabUIModel subModel : subModels) {
98                  Class<?> modelClass = subModel.getClass();
99                  CaracteristicMap caracteristics = subModel.getCaracteristicMap();
100                 if (subModel.isModify()) {
101                     // see http://forge.codelutin.com/issues/4717
102                     moveTab = false;
103                 }
104                 if (modelClass.isAssignableFrom(VesselUseFeatureTabUIModel.class)) {
105                     toSave.setVesselUseFeatures(caracteristics);
106                 } else if (modelClass.isAssignableFrom(GearUseFeatureTabUIModel.class)) {
107                     toSave.setGearUseFeatures(caracteristics);
108                 }
109                 subModel.setModify(false);
110             }
111 
112             sendMessage("[ Trait - Caractéristiques générales ] Sauvegarde des modifications de " + decorate(toSave) + ".");
113 
114             if (log.isInfoEnabled()) {
115                 log.info("FishingOperation " + toSave.getId() + " was modified, will save it.");
116             }
117 
118             saveFishingOperation(toSave);
119 
120         }
121 
122     }
123 
124     @Override
125     public void postSuccessAction() {
126         super.postSuccessAction();
127         handler.getFishingOperationMonitor().clearModified();
128         getModel().setModify(false);
129         if (moveTab && ValidationService.VALIDATION_CONTEXT_EDIT.equals(getContext().getValidationContext())) {
130             handler.getParentUi().getTabPane().setSelectedIndex(1);
131             handler.getParentUi().getCatchesTabContent().getTabPane().setSelectedIndex(1);
132         }
133     }
134 
135     protected void saveFishingOperation(FishingOperation toSave) {
136 
137         boolean create = TuttiEntities.isNew(toSave);
138 
139         if (create) {
140 
141             createFishingOperation(toSave);
142 
143         } else {
144 
145             FishingOperationsUIModel model = getHandler().getParentUi().getModel();
146             model.setEditionAdjusting(true);
147 
148             try {
149 
150                 persistFishingOperation(toSave);
151 
152             } finally {
153 
154                 model.setEditionAdjusting(false);
155 
156             }
157 
158         }
159 
160     }
161 
162     private void createFishingOperation(FishingOperation toSave) {
163 
164         FishingOperationsUIModel model = getHandler().getParentUi().getModel();
165         PersistenceService service = getContext().getPersistenceService();
166 
167         // create fishing operation
168         FishingOperation savedFishingOperation = service.createFishingOperation(toSave);
169 
170         // create then the CatchBatch
171         CatchBatch catchBatch = CatchBatchs.newCatchBatch();
172         catchBatch.setFishingOperation(savedFishingOperation);
173         service.createCatchBatch(catchBatch);
174 
175         // add new created fishing operation to list
176         model.addFishingOperation(savedFishingOperation);
177 
178         // select it (will reload editing fishing operation)
179         model.setSelectedFishingOperation(savedFishingOperation);
180 
181     }
182 
183     private void persistFishingOperation(FishingOperation toSave) {
184 
185 
186         PersistenceService service = getContext().getPersistenceService();
187 
188         FishingOperationsUIModel model = getHandler().getParentUi().getModel();
189 
190         // save fishing operation
191         FishingOperation savedFishingOperation = service.saveFishingOperation(toSave);
192 
193         model.setSelectedFishingOperation(null);
194 
195         // reinject it in model
196         model.updateFishingOperation(savedFishingOperation);
197 
198         model.setSelectedFishingOperation(savedFishingOperation);
199 
200         getDataContext().reloadFishingOperation();
201 
202     }
203 
204 }