1 package fr.ifremer.tutti.service.export.toconfirmreport;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import fr.ifremer.tutti.persistence.ProgressionModel;
28 import fr.ifremer.tutti.persistence.entities.data.Cruise;
29 import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch;
30 import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
31 import fr.ifremer.tutti.persistence.entities.referential.Species;
32 import fr.ifremer.tutti.persistence.entities.referential.TaxonCache;
33 import fr.ifremer.tutti.persistence.entities.referential.TaxonCaches;
34 import fr.ifremer.tutti.service.AbstractTuttiService;
35 import fr.ifremer.tutti.service.DecoratorService;
36 import fr.ifremer.tutti.service.PdfGeneratorService;
37 import fr.ifremer.tutti.service.PersistenceService;
38 import fr.ifremer.tutti.service.TuttiServiceContext;
39 import fr.ifremer.tutti.service.catches.WeightComputingService;
40 import fr.ifremer.tutti.type.WeightUnit;
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44 import java.io.File;
45 import java.io.Serializable;
46 import java.util.ArrayList;
47 import java.util.List;
48 import java.util.Locale;
49
50 import static org.nuiton.i18n.I18n.t;
51
52
53
54
55
56 public class ToConfirmReportService extends AbstractTuttiService {
57
58 private static final Log log = LogFactory.getLog(ToConfirmReportService.class);
59
60 protected PersistenceService persistenceService;
61
62 protected DecoratorService decoratorService;
63
64 protected PdfGeneratorService pdfGeneratorService;
65
66 protected WeightComputingService weightComputingService;
67
68 protected WeightUnit speciesWeightUnit;
69
70 protected WeightUnit benthosWeightUnit;
71
72 @Override
73 public void setServiceContext(TuttiServiceContext context) {
74 super.setServiceContext(context);
75 persistenceService = getService(PersistenceService.class);
76 decoratorService = getService(DecoratorService.class);
77 pdfGeneratorService = getService(PdfGeneratorService.class);
78 weightComputingService = getService(WeightComputingService.class);
79
80 speciesWeightUnit = context.getConfig().getSpeciesWeightUnit();
81 benthosWeightUnit = context.getConfig().getBenthosWeightUnit();
82 }
83
84 public int getNumberOfSteps(Integer cruiseId) {
85
86 List<Integer> allFishingOperation = persistenceService.getAllFishingOperationIds(cruiseId);
87 return allFishingOperation.size() + 3;
88
89 }
90
91 public void createToConfirmReport(File file, Integer cruiseId, ProgressionModel progressionModel) {
92
93 progressionModel.increments(t("tutti.toconfirmReport.loading.cruise", cruiseId));
94
95 Cruise cruise = persistenceService.getCruise(cruiseId);
96 if (log.isDebugEnabled()) {
97 log.debug("Cruise " + decoratorService.getDecorator(cruise).toString(cruise));
98 }
99
100 progressionModel.increments(t("tutti.toconfirmReport.loading.protocol"));
101
102 TuttiProtocol protocol = context.getDataContext().getProtocol();
103
104 TaxonCache speciesCache = TaxonCaches.createSpeciesCache(persistenceService, protocol);
105 TaxonCache benthosCache = TaxonCaches.createBenthosCache(persistenceService, protocol);
106
107 List<Integer> allFishingOperation = persistenceService.getAllFishingOperationIds(cruiseId);
108
109 ToConfirmReportBean reportBean = new ToConfirmReportBean(cruise, speciesWeightUnit, benthosWeightUnit);
110
111 int currentOperation = 1;
112 int nbOperations = allFishingOperation.size();
113
114 for (Integer operationId : allFishingOperation) {
115
116 progressionModel.increments(t("tutti.toconfirmReport.loading.operation", operationId, currentOperation++, nbOperations));
117
118 ToConfirmReportFishingOperationData fishingOperationData =
119 ToConfirmReportFishingOperationData.create(persistenceService, operationId);
120
121 if (fishingOperationData != null) {
122
123 ToConfirmReportFishingOperationBean operationBean = createOperationBean(fishingOperationData, speciesCache, benthosCache);
124 reportBean.addOperation(operationBean);
125 }
126
127 }
128
129 progressionModel.increments(t("tutti.toconfirmReport.generate.report"));
130
131 generatePdf(file, reportBean);
132
133 }
134
135 protected ToConfirmReportFishingOperationBean createOperationBean(ToConfirmReportFishingOperationData fishingOperationData,
136 TaxonCache speciesCache,
137 TaxonCache benthosCache) {
138
139
140
141 List<ToConfirmReportBatchEntryBean> speciesCatchList;
142
143 if (fishingOperationData.isWithSpeciesBatchToConfirm()) {
144
145 List<SpeciesBatch> speciesBatchEntries = fishingOperationData.getSpeciesBatchToConfirm();
146 speciesCache.loadInBatches(speciesBatchEntries);
147 speciesCatchList = createBatchBeans(speciesWeightUnit, speciesBatchEntries);
148
149 } else {
150
151 speciesCatchList = null;
152 }
153
154
155
156 List<ToConfirmReportBatchEntryBean> benthosCatchList;
157
158 if (fishingOperationData.isWithBenthosBatchToConfirm()) {
159
160 List<SpeciesBatch> benthosBatchEntries = fishingOperationData.getBenthosBatchToConfirm();
161 benthosCache.loadInBatches(benthosBatchEntries);
162 benthosCatchList = createBatchBeans(benthosWeightUnit, benthosBatchEntries);
163
164 } else {
165
166 benthosCatchList = null;
167 }
168
169 return new ToConfirmReportFishingOperationBean(fishingOperationData.getFishingOperation(),
170 speciesCatchList,
171 benthosCatchList);
172
173 }
174
175 protected List<ToConfirmReportBatchEntryBean> createBatchBeans(WeightUnit weightUnit, List<SpeciesBatch> batchEntries) {
176
177 List<ToConfirmReportBatchEntryBean> catchList = new ArrayList<>();
178
179 for (SpeciesBatch batch : batchEntries) {
180
181 Species species = batch.getSpecies();
182
183 Float sampleCategoryWeightValue = weightUnit.fromEntity(batch.getSampleCategoryWeight());
184 String sampleCategoryWeight = weightUnit.renderWeight(sampleCategoryWeightValue);
185
186 Float weightValue = weightUnit.fromEntity(batch.getWeight());
187 String weight = weightUnit.renderWeight(weightValue);
188
189 String comment = batch.getComment();
190
191 String category = getBatchDecoratedSampleCategoryValue(batch);
192
193 while (batch.getParentBatch() != null) {
194 batch = batch.getParentBatch();
195 category = getBatchDecoratedSampleCategoryValue(batch) + " / " + category;
196 }
197
198 ToConfirmReportBatchEntryBean reportEntry = new ToConfirmReportBatchEntryBean(species,
199 category,
200 sampleCategoryWeight,
201 weight,
202 comment);
203 catchList.add(reportEntry);
204 }
205
206 return catchList;
207 }
208
209 protected String getBatchDecoratedSampleCategoryValue(SpeciesBatch batch) {
210 Serializable sampleCategoryValue = batch.getSampleCategoryValue();
211 return decoratorService.getDecorator(sampleCategoryValue).toString(sampleCategoryValue);
212 }
213
214 protected void generatePdf(File targetFile, ToConfirmReportBean reportBean) {
215
216 Locale locale = context.getConfig().getI18nLocale();
217
218 pdfGeneratorService.generatePdf(targetFile, locale, "toConfirmSpeciesReport.ftl", reportBean);
219
220 }
221 }