View Javadoc
1   package fr.ifremer.tutti.service.genericformat;
2   
3   /*
4    * #%L
5    * Tutti :: Service
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import com.google.common.base.Preconditions;
28  import fr.ifremer.tutti.persistence.ProgressionModel;
29  import fr.ifremer.tutti.persistence.entities.data.BatchContainer;
30  import fr.ifremer.tutti.persistence.entities.data.CatchBatch;
31  import fr.ifremer.tutti.persistence.entities.data.Cruise;
32  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
33  import fr.ifremer.tutti.persistence.entities.data.MarineLitterBatch;
34  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch;
35  import fr.ifremer.tutti.service.AbstractTuttiService;
36  import fr.ifremer.tutti.service.DecoratorService;
37  import fr.ifremer.tutti.service.PersistenceService;
38  import fr.ifremer.tutti.service.TuttiServiceContext;
39  import fr.ifremer.tutti.service.catches.WeightComputingService;
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  import org.nuiton.decorator.Decorator;
43  import org.nuiton.jaxx.application.ApplicationBusinessException;
44  
45  import java.util.LinkedHashMap;
46  import java.util.LinkedHashSet;
47  import java.util.Map;
48  import java.util.Set;
49  
50  import static org.nuiton.i18n.I18n.t;
51  
52  /**
53   * Created on 3/28/15.
54   *
55   * @author Tony Chemit - chemit@codelutin.com
56   * @since 3.14.3
57   */
58  public class GenericFormatCheckDataService extends AbstractTuttiService {
59  
60      /** Logger. */
61      private static final Log log = LogFactory.getLog(GenericFormatCheckDataService.class);
62  
63      protected PersistenceService persistenceService;
64  
65      protected WeightComputingService weightComputingService;
66  
67      protected Decorator<FishingOperation> fishingOperationDecorator;
68  
69      protected Decorator<Cruise> cruiseDecorator;
70  
71      @Override
72      public void setServiceContext(TuttiServiceContext context) {
73  
74          super.setServiceContext(context);
75          persistenceService = getService(PersistenceService.class);
76          weightComputingService = getService(WeightComputingService.class);
77  
78          DecoratorService decoratorService = getService(DecoratorService.class);
79          cruiseDecorator = decoratorService.getDecoratorByType(Cruise.class);
80          fishingOperationDecorator = decoratorService.getDecoratorByType(FishingOperation.class);
81  
82      }
83  
84      public String getCruiseErrors(Cruise cruise, Set<FishingOperation> operations, ProgressionModel progressionModel) {
85  
86          Map<Integer, String> errors = getFishingOperationsErrors(cruise, operations, progressionModel);
87  
88          String result;
89  
90          if (!errors.isEmpty()) {
91  
92              StringBuilder errorMessageBuilder = new StringBuilder();
93  
94              for (String error : errors.values()) {
95                  errorMessageBuilder.append("<li>").append(error).append("</li>");
96              }
97  
98              String cruiseStr = cruiseDecorator.toString(cruise);
99              result = t("tutti.service.genericFormat.invalid.cruise", cruiseStr, errorMessageBuilder.toString());
100 
101         } else {
102 
103             result = null;
104 
105         }
106 
107         return result;
108 
109     }
110 
111     protected Map<Integer, String> getFishingOperationsErrors(Cruise cruise,
112                                                               Set<FishingOperation> operations,
113                                                               ProgressionModel progressionModel) {
114 
115         Preconditions.checkNotNull(cruise);
116         Preconditions.checkNotNull(progressionModel);
117 
118         progressionModel.increments(t("tutti.service.genericFormat.checkCruise", cruise.getName()));
119 
120         Map<Integer, String> errors = new LinkedHashMap<>();
121 
122         for (FishingOperation fishingOperation : operations) {
123 
124             Integer fishingOperationId = fishingOperation.getIdAsInt();
125 
126             progressionModel.increments(t("tutti.service.genericFormat.checkCruiseFishingOperation", cruise.getName(), fishingOperationDecorator.toString(fishingOperation)));
127 
128             Set<String> errorsForOperation = checkFishingOperation(fishingOperationId);
129             if (!errorsForOperation.isEmpty()) {
130 
131                 String fishingOperationStr = fishingOperationDecorator.toString(fishingOperation);
132 
133                 StringBuilder sb = new StringBuilder();
134                 for (String error : errorsForOperation) {
135                     sb.append("<li>").append(error).append("</li>");
136                 }
137                 String errorMessage = t("tutti.service.genericFormat.invalid.fishingOperation", fishingOperationStr, sb.toString());
138                 errors.put(fishingOperationId, errorMessage);
139 
140             }
141 
142         }
143 
144         return errors;
145 
146     }
147 
148     public Set<String> checkFishingOperation(Integer fishingOperationId) {
149 
150         if (log.isDebugEnabled()) {
151             log.debug("Will check operation: " + fishingOperationId);
152         }
153 
154         Set<String> errors = new LinkedHashSet<>();
155 
156         boolean withCatchBatch = persistenceService.isFishingOperationWithCatchBatch(fishingOperationId);
157 
158         if (!withCatchBatch) {
159 
160             if (log.isWarnEnabled()) {
161                 log.warn("Skip fishing operation " + fishingOperationId + " since no catchBatch associated.");
162             }
163 
164         } else {
165 
166             BatchContainer<SpeciesBatch> rootSpeciesBatch;
167             try {
168                 rootSpeciesBatch = weightComputingService.getComputedSpeciesBatches(fishingOperationId);
169 
170             } catch (ApplicationBusinessException e) {
171                 errors.add(e.getMessage());
172                 rootSpeciesBatch = persistenceService.getRootSpeciesBatch(fishingOperationId, false);
173             }
174 
175             BatchContainer<SpeciesBatch> rootBenthosBatch;
176             try {
177                 rootBenthosBatch = weightComputingService.getComputedBenthosBatches(fishingOperationId);
178 
179             } catch (ApplicationBusinessException e) {
180                 errors.add(e.getMessage());
181                 rootBenthosBatch = persistenceService.getRootBenthosBatch(fishingOperationId, false);
182             }
183 
184             CatchBatch catchBatch = persistenceService.getCatchBatchFromFishingOperation(fishingOperationId);
185 
186             BatchContainer<MarineLitterBatch> rootMarineLitterBatch;
187             try {
188                 Float weight = catchBatch == null ? null : catchBatch.getMarineLitterTotalWeight();
189                 rootMarineLitterBatch = weightComputingService.getComputedMarineLitterBatches(fishingOperationId, weight);
190 
191             } catch (ApplicationBusinessException e) {
192                 errors.add(e.getMessage());
193                 rootMarineLitterBatch = persistenceService.getRootMarineLitterBatch(fishingOperationId);
194             }
195 
196             try {
197                 if (catchBatch != null) {
198                     weightComputingService.computeCatchBatchWeights(catchBatch,
199                                                                     rootSpeciesBatch,
200                                                                     rootBenthosBatch,
201                                                                     rootMarineLitterBatch);
202                 }
203             } catch (ApplicationBusinessException e) {
204                 errors.add(e.getMessage());
205             }
206         }
207 
208         return errors;
209 
210     }
211 
212 }