This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository tutti. See https://gitlab.nuiton.org/codelutin/tutti.git commit 76a3dcb83f44f9f01c902f796ff4966704a668bb Author: Tony CHEMIT <chemit@codelutin.com> Date: Wed Apr 13 11:52:44 2016 +0200 Revue de l'action d'élévation des poids en utilisant les exceptions typées --- .../catches/actions/ComputeBatchWeightsAction.java | 221 +++++++++------------ 1 file changed, 94 insertions(+), 127 deletions(-) diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/actions/ComputeBatchWeightsAction.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/actions/ComputeBatchWeightsAction.java index a976727..0432426 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/actions/ComputeBatchWeightsAction.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/actions/ComputeBatchWeightsAction.java @@ -30,6 +30,10 @@ import fr.ifremer.tutti.persistence.entities.data.MarineLitterBatch; import fr.ifremer.tutti.persistence.entities.data.SampleCategory; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; import fr.ifremer.tutti.service.PersistenceService; +import fr.ifremer.tutti.service.catches.BenthosWeightComputingException; +import fr.ifremer.tutti.service.catches.CatchWeightComputingException; +import fr.ifremer.tutti.service.catches.MarineLitterWeightComputingException; +import fr.ifremer.tutti.service.catches.SpeciesWeightComputingException; import fr.ifremer.tutti.service.catches.TuttiWeightComputingException; import fr.ifremer.tutti.service.catches.WeightCleaningService; import fr.ifremer.tutti.service.catches.WeightComputingService; @@ -55,6 +59,7 @@ import javax.swing.JOptionPane; import javax.swing.table.TableColumn; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.Set; import static org.nuiton.i18n.I18n.t; @@ -64,22 +69,22 @@ import static org.nuiton.i18n.I18n.t; * @since 1.0 */ public class ComputeBatchWeightsAction extends LongActionSupport<EditCatchesUIModel, EditCatchesUI, EditCatchesUIHandler> { - + protected final WeightCleaningService cleaningService; + protected final PersistenceService persistenceService; protected List<SpeciesBatchRowModel> speciesBatchRows; - protected List<SpeciesBatchRowModel> benthosBatchRows; protected CatchBatch catchBatch; protected Boolean modified; - protected Integer tabInError; public ComputeBatchWeightsAction(EditCatchesUIHandler handler) { super(handler, true); this.cleaningService = getContext().getWeightCleaningService(); + this.persistenceService = getContext().getPersistenceService(); } @Override @@ -125,9 +130,8 @@ public class ComputeBatchWeightsAction extends LongActionSupport<EditCatchesUIMo @Override public void doAction() throws Exception { - PersistenceService persistenceService = getContext().getPersistenceService(); - WeightComputingService weightComputingService = getContext().getWeightComputingService(); + WeightComputingService computingService = getContext().getWeightComputingService(); EditCatchesUIModel model = getModel(); Integer operationId = model.getFishingOperation().getIdAsInt(); @@ -136,61 +140,33 @@ public class ComputeBatchWeightsAction extends LongActionSupport<EditCatchesUIMo // Compute species batches // --------- - Float totalSpeciesSortedWeight; - BatchContainer<SpeciesBatch> computedSpeciesBatches; - - try { - computedSpeciesBatches = weightComputingService.getComputedSpeciesBatches(operationId); - if (computedSpeciesBatches == null) { - totalSpeciesSortedWeight = 0f; - } else { - totalSpeciesSortedWeight = computeSpeciesBatches(persistenceService, computedSpeciesBatches, speciesBatchRows = new ArrayList<>()); - } - - } catch (TuttiWeightComputingException e) { - - tabInError = 1; - throw e; + BatchContainer<SpeciesBatch> computedSpeciesBatches = computingService.getComputedSpeciesBatches(operationId); + Float totalSpeciesSortedWeight; + if (computedSpeciesBatches == null) { + totalSpeciesSortedWeight = 0f; + } else { + totalSpeciesSortedWeight = computeSpeciesBatches(getSpeciesBatchUI(), computedSpeciesBatches, speciesBatchRows = new ArrayList<>()); } // --------- // Compute benthos batches // --------- - Float totalBenthosSortedWeight; - BatchContainer<SpeciesBatch> computedBenthosBatches; - - try { - computedBenthosBatches = weightComputingService.getComputedBenthosBatches(operationId); - if (computedBenthosBatches == null) { - totalBenthosSortedWeight = 0f; - } else { - totalBenthosSortedWeight = computeSpeciesBatches(persistenceService, computedBenthosBatches, benthosBatchRows = new ArrayList<>()); - } - - } catch (TuttiWeightComputingException e) { - - tabInError = 2; - throw e; + BatchContainer<SpeciesBatch> computedBenthosBatches = computingService.getComputedBenthosBatches(operationId); + Float totalBenthosSortedWeight; + if (computedBenthosBatches == null) { + totalBenthosSortedWeight = 0f; + } else { + totalBenthosSortedWeight = computeSpeciesBatches(getBenthosBatchUI(), computedBenthosBatches, benthosBatchRows = new ArrayList<>()); } // --------- // Compute marine litter batches // --------- - BatchContainer<MarineLitterBatch> computedMarineLitterBatches; - try { - computedMarineLitterBatches = - weightComputingService.getComputedMarineLitterBatches(operationId, model.getMarineLitterTotalWeight()); - - } catch (TuttiWeightComputingException e) { - - tabInError = 3; - throw e; - - } + BatchContainer<MarineLitterBatch> computedMarineLitterBatches = computingService.getComputedMarineLitterBatches(operationId, model.getMarineLitterTotalWeight()); modified = model.isModify(); catchBatch = model.toEntity(); @@ -271,10 +247,10 @@ public class ComputeBatchWeightsAction extends LongActionSupport<EditCatchesUIMo } } - weightComputingService.computeCatchBatchWeights(catchBatch, - computedSpeciesBatches, - computedBenthosBatches, - computedMarineLitterBatches); + computingService.computeCatchBatchWeights(catchBatch, + computedSpeciesBatches, + computedBenthosBatches, + computedMarineLitterBatches); } @@ -285,16 +261,6 @@ public class ComputeBatchWeightsAction extends LongActionSupport<EditCatchesUIMo afterAction(); getUI().repaint(); - //TCHEMIT-2015-04-27 Plus besoin car le champs qui posait problème n'existe plus -// // see http://forge.codelutin.com/issues/3853 -// SwingUtilities.invokeLater( -// new Runnable() { -// @Override -// public void run() { -// getUI().getComputeSpeciesBatchButton().requestFocus(); -// } -// } -// ); } protected void afterAction() { @@ -372,31 +338,43 @@ public class ComputeBatchWeightsAction extends LongActionSupport<EditCatchesUIMo afterAction(); - if (error instanceof TuttiWeightComputingException) { + if (error instanceof CatchWeightComputingException) { - // elevation error - TuttiWeightComputingException e = (TuttiWeightComputingException) error; + getUI().getTabPane().setSelectedIndex(0); - getUI().getTabPane().setSelectedIndex(tabInError); + } - switch (tabInError) { + if (error instanceof SpeciesWeightComputingException) { - case 1: - // species error - treatSpeciesBatchError(e, getSpeciesBatchUI().getModel()); + SpeciesWeightComputingException e = (SpeciesWeightComputingException) error; - break; + getUI().getTabPane().setSelectedIndex(1); - case 2: + treatSpeciesBatchError(getSpeciesBatchUI(), e); - // benthos error - treatSpeciesBatchError(e, getBenthosBatchUI().getModel()); + } - break; + if (error instanceof BenthosWeightComputingException) { + + BenthosWeightComputingException e = (BenthosWeightComputingException) error; + + getUI().getTabPane().setSelectedIndex(2); + + treatSpeciesBatchError(getBenthosBatchUI(), e); + + } - case 3: - // marine litter error - JTables.doSelectCell(getUI().getMarineLitterTabContent().getTable(), e.getIndex(), 3); + if (error instanceof MarineLitterWeightComputingException) { + + MarineLitterWeightComputingException e = (MarineLitterWeightComputingException) error; + + getUI().getTabPane().setSelectedIndex(3); + + Optional<Integer> optionalRowIndex = e.getIndex(); + + if (optionalRowIndex.isPresent()) { + + JTables.doSelectCell(getUI().getMarineLitterTabContent().getTable(), optionalRowIndex.get(), 3); } @@ -408,13 +386,13 @@ public class ComputeBatchWeightsAction extends LongActionSupport<EditCatchesUIMo //-- Internal methods --// //------------------------------------------------------------------------// - protected Float computeSpeciesBatches(PersistenceService persistenceService, BatchContainer<SpeciesBatch> computedSpeciesBatches, List<SpeciesBatchRowModel> speciesBatchRows) { + protected Float computeSpeciesBatches(SpeciesBatchUI ui, BatchContainer<SpeciesBatch> computedSpeciesBatches, List<SpeciesBatchRowModel> speciesBatchRows) { + Float totalSortedWeight = 0f; - List<SpeciesBatch> children = computedSpeciesBatches.getChildren(); - for (SpeciesBatch batch : children) { - SpeciesBatchRowModel row = getSpeciesBatchUI().getHandler().loadBatch(batch, null, speciesBatchRows); - if (persistenceService.isVracBatch(row)) { + for (SpeciesBatch speciesBatch : computedSpeciesBatches.getChildren()) { + SpeciesBatchRowModel row = ui.getHandler().loadBatch(speciesBatch, null, speciesBatchRows); + if (persistenceService.isVracBatch(speciesBatch)) { SampleCategory<?> sampleCategory = row.getFirstSampleCategory(); Float weight = Numbers.getValueOrComputedValue(sampleCategory.getCategoryWeight(), sampleCategory.getComputedWeight()); totalSortedWeight += weight; @@ -422,58 +400,47 @@ public class ComputeBatchWeightsAction extends LongActionSupport<EditCatchesUIMo } return totalSortedWeight; + } -// protected Float computeBenthosBatches(PersistenceService persistenceService, BatchContainer<SpeciesBatch> computedBenthosBatches) { -// Float totalSortedWeight = 0f; -// -// if (computedBenthosBatches != null) { -// benthosBatchRows = new ArrayList<>(); -// List<SpeciesBatch> children = computedBenthosBatches.getChildren(); -// for (SpeciesBatch batch : children) { -// SpeciesBatchRowModel row = getBenthosBatchUI().getHandler().loadBatch(batch, null, benthosBatchRows); -// -// if (persistenceService.isVracBatch(row)) { -// SampleCategory<?> sampleCategory = row.getFirstSampleCategory(); -// Float weight = Numbers.getValueOrComputedValue(sampleCategory.getCategoryWeight(), -// sampleCategory.getComputedWeight()); -// totalSortedWeight += weight; -// } -// } -//// getUI().getBenthosTabContent().getModel().setRows(benthosBatchRows); -// -// } -// return totalSortedWeight; -// } - - protected void treatSpeciesBatchError(TuttiWeightComputingException e, SpeciesBatchUIModel speciesBatchUIModel) { - - int index = e.getIndex(); - - SpeciesBatchRowModel row; - - if (speciesBatchUIModel.getSpeciesSortMode() != SpeciesSortMode.NONE) { - - // must resort rows in natural order (in service we use this order) - List<SpeciesBatchRowModel> rows = new ArrayList<>(speciesBatchUIModel.getRows()); - SpeciesBatchNaturalOrderComparator.sort(rows); - // get the row - row = rows.get(index); - // get the correct rowIndex in the sorted list - index = speciesBatchUIModel.getRows().indexOf(row); + protected void treatSpeciesBatchError(SpeciesBatchUI ui, TuttiWeightComputingException e) { - } else { + Optional<Integer> optionalRowIndex = e.getIndex(); - // correct order can directly get row from list - List<SpeciesBatchRowModel> rows = speciesBatchUIModel.getRows(); - row = rows.get(index); + Optional<String> optionalProperty = e.getProperty(); - } + if (optionalRowIndex.isPresent() && optionalProperty.isPresent()) { + + SpeciesBatchUIModel speciesBatchUIModel = ui.getModel(); + + Integer index = optionalRowIndex.get(); + + SpeciesBatchRowModel row; + + if (SpeciesSortMode.NONE != speciesBatchUIModel.getSpeciesSortMode()) { + + // must resort rows in natural order (in service we use this order) + List<SpeciesBatchRowModel> rows = new ArrayList<>(speciesBatchUIModel.getRows()); + SpeciesBatchNaturalOrderComparator.sort(rows); + // get the row + row = rows.get(index); + // get the correct rowIndex in the sorted list + index = speciesBatchUIModel.getRows().indexOf(row); - JXTable table = getSpeciesBatchUI().getTable(); - SpeciesBatchTableModel tableModel = (SpeciesBatchTableModel) table.getModel(); - Set<SampleCategoryColumnIdentifier> sampleCols = tableModel.getSampleCols(); - selectBatchCell(table, index, row, e.getProperty(), sampleCols, SpeciesBatchTableModel.WEIGHT); + } else { + + // correct order can directly get row from list + List<SpeciesBatchRowModel> rows = speciesBatchUIModel.getRows(); + row = rows.get(index); + + } + + JXTable table = ui.getTable(); + SpeciesBatchTableModel tableModel = (SpeciesBatchTableModel) table.getModel(); + Set<SampleCategoryColumnIdentifier> sampleCols = tableModel.getSampleCols(); + selectBatchCell(table, index, row, optionalProperty.get(), sampleCols, SpeciesBatchTableModel.WEIGHT); + + } } -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.