View Javadoc
1   package fr.ifremer.tutti.persistence.test;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
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.Lists;
26  import fr.ifremer.tutti.persistence.entities.CaracteristicMap;
27  import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch;
28  import fr.ifremer.tutti.persistence.entities.data.AccidentalBatchs;
29  import fr.ifremer.tutti.persistence.entities.data.CatchBatch;
30  import fr.ifremer.tutti.persistence.entities.data.CatchBatchs;
31  import fr.ifremer.tutti.persistence.entities.data.Cruise;
32  import fr.ifremer.tutti.persistence.entities.data.Cruises;
33  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
34  import fr.ifremer.tutti.persistence.entities.data.FishingOperations;
35  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
36  import fr.ifremer.tutti.persistence.entities.referential.CaracteristicType;
37  import fr.ifremer.tutti.persistence.entities.referential.Gear;
38  import fr.ifremer.tutti.persistence.entities.referential.GearWithOriginalRankOrder;
39  import fr.ifremer.tutti.persistence.entities.referential.GearWithOriginalRankOrders;
40  import fr.ifremer.tutti.persistence.entities.referential.Person;
41  import fr.ifremer.tutti.persistence.entities.referential.TuttiLocation;
42  import fr.ifremer.tutti.persistence.entities.referential.TuttiLocations;
43  import fr.ifremer.tutti.persistence.entities.referential.Vessel;
44  import fr.ifremer.tutti.persistence.entities.referential.Vessels;
45  import fr.ifremer.tutti.persistence.service.AccidentalBatchPersistenceService;
46  import fr.ifremer.tutti.persistence.service.CatchBatchPersistenceService;
47  import fr.ifremer.tutti.persistence.service.CruisePersistenceService;
48  import fr.ifremer.tutti.persistence.service.FishingOperationPersistenceService;
49  import fr.ifremer.tutti.persistence.service.IndividualObservationBatchPersistenceService;
50  import fr.ifremer.tutti.persistence.service.ProgramPersistenceService;
51  import fr.ifremer.tutti.persistence.service.TuttiPersistenceServiceLocator;
52  import fr.ifremer.tutti.persistence.service.referential.CaracteristicPersistenceService;
53  import fr.ifremer.tutti.persistence.service.referential.GearPersistenceService;
54  import fr.ifremer.tutti.persistence.service.referential.LocationPersistenceService;
55  import fr.ifremer.tutti.persistence.service.referential.PersonPersistenceService;
56  import fr.ifremer.tutti.persistence.service.referential.SpeciesPersistenceService;
57  import org.junit.Assert;
58  
59  import java.io.Serializable;
60  import java.util.Calendar;
61  import java.util.Date;
62  import java.util.GregorianCalendar;
63  import java.util.List;
64  
65  /**
66   * Fixtures for the allegro db.
67   *
68   * @author Tony Chemit - chemit@codelutin.com
69   * @since 0.3
70   */
71  public class DatabaseFixtures {
72  
73      public String programCode() {
74          // campaign CGFS
75          return "CAM-CGFS";
76      }
77  
78      public Integer cruiseId() {
79          // cruise CGFS2010
80          return 100000;
81      }
82  
83      public String fishingVesselCode() {
84          return "851751";
85      }
86  
87      public String zoneId() {
88          // zone CGFS
89          return "61979";
90      }
91  
92      public String strataId() {
93          // strate 6M
94          return "57377";
95      }
96  
97      public String subStrataId() {
98          // substrata 61995
99          return "61995";
100     }
101 
102     public String localite() {
103         // Localite 8Q3
104         return "57774";
105     }
106 
107     public int refNbCaracteristic() {
108         return 485;
109     }
110 
111     public int refNbSpecies() {
112         return 16961;
113     }
114 
115     public int refNbReferentSpecies() {
116         return 8649;
117     }
118 
119     public int refNbScientificGear() {
120         return 9;
121     }
122 
123     public int refNbFishingGear() {
124         return 75;
125     }
126 
127     public int refNbCountry() {
128         return 239;
129     }
130 
131     public int refNbHarbour() {
132         return 1898;
133     }
134 
135     public int refNbProgramZone() {
136         return 14;
137     }
138 
139     public int refNbStrata() {
140         return 76;
141     }
142 
143     public int refNbSubStrata() {
144         return 16;
145     }
146 
147     public int refNbLocalite() {
148         return 129;
149     }
150 
151     public int refNbFishingVessel() {
152         return 24837;
153     }
154 
155     public int refNbScientificVessel() {
156         return 3;
157     }
158 
159     public int refNbPerson() {
160         return 128;
161     }
162 
163     public int refNbObjectType() {
164         return 57;
165     }
166 
167     public String refSpeciesId() {
168         return "15923";
169     }
170 
171     public Integer refSpeciesTaxonId() {
172         return 467;
173     }
174 
175     public Integer refNumericalPmfmId() {
176         return 113;
177     }
178 
179     public Integer refAlphanumericPmfmId() {
180         return 1424;
181     }
182 
183     public String refSpeciesRefTaxCode() {
184         return "SEPIOFF";
185     }
186 
187     public String refSpeciesVernacularCode() {
188         return "Seiche";
189     }
190 
191     public Integer refBadSpeciesTaxonId() {
192         return 7632;
193     }
194 
195     public Integer refBad2SpeciesTaxonId() {
196         return 2320;
197     }
198 
199     public String objectTypeCode() {
200         return "BATCH";
201     }
202 
203     public Integer caracteristicWithNullQualitativeValue() {
204         return 114;
205     }
206 
207     protected CruisePersistenceService cruiseService;
208 
209     protected ProgramPersistenceService programService;
210 
211     protected FishingOperationPersistenceService fishingOperationService;
212 
213     protected CatchBatchPersistenceService catchBatchService;
214 
215     private CaracteristicPersistenceService caracteristicService;
216 
217     private GearPersistenceService gearService;
218 
219     private LocationPersistenceService locationService;
220 
221     private PersonPersistenceService personService;
222 
223     private SpeciesPersistenceService speciesService;
224 
225     protected AccidentalBatchPersistenceService accidentalBatchService;
226 
227     protected IndividualObservationBatchPersistenceService individualObservationBatchService;
228 
229     public void setUp() {
230         if (caracteristicService == null) {
231             caracteristicService = TuttiPersistenceServiceLocator.getCaracteristicPersistenceService();
232             gearService = TuttiPersistenceServiceLocator.getGearPersistenceService();
233             locationService = TuttiPersistenceServiceLocator.getLocationPersistenceService();
234             personService = TuttiPersistenceServiceLocator.getPersonPersistenceService();
235             speciesService = TuttiPersistenceServiceLocator.getSpeciesPersistenceService();
236             programService = TuttiPersistenceServiceLocator.getProgramPersistenceService();
237             cruiseService = TuttiPersistenceServiceLocator.getCruisePersistenceService();
238             fishingOperationService = TuttiPersistenceServiceLocator.getFishingOperationPersistenceService();
239             catchBatchService = TuttiPersistenceServiceLocator.getCatchBatchPersistenceService();
240             accidentalBatchService = TuttiPersistenceServiceLocator.getAccidentalBatchPersistenceService();
241             individualObservationBatchService = TuttiPersistenceServiceLocator.getIndividualObservationBatchPersistenceService();
242         }
243     }
244 
245     public Cruise createCruise() {
246 
247         setUp();
248 
249         String programCode = programCode();
250         Cruise cruise = Cruises.newCruise();
251 
252         // -----------------------------------------------------------------------------
253         // 1. Test with all properties filled
254         // -----------------------------------------------------------------------------
255         cruise.setId((String) null);
256 
257         cruise.setName("Unit-test-" + System.currentTimeMillis());
258 
259         cruise.setProgram(programService.getProgram(programCode));
260 
261         Calendar calendar = new GregorianCalendar();
262         cruise.setBeginDate(calendar.getTime());
263 
264         calendar.add(Calendar.MONTH, 1); // add one month
265         cruise.setEndDate(calendar.getTime());
266 
267         List<TuttiLocation> allHarbour = locationService.getAllHarbour();
268         Assert.assertNotNull(allHarbour);
269         Assert.assertTrue(allHarbour.size() > 1);
270         cruise.setDepartureLocation(allHarbour.get(0));
271         cruise.setReturnLocation(allHarbour.get(1));
272 
273         List<Gear> gears = gearService.getAllFishingGear();
274         List<GearWithOriginalRankOrder> gearsWithOrder = Lists.newArrayListWithCapacity(gears.size());
275         short rankOrder = (short) 0;
276         for (Gear gear : gears) {
277             GearWithOriginalRankOrder gearWithOriginalRankOrder = GearWithOriginalRankOrders.newGearWithOriginalRankOrder(gear);
278             gearWithOriginalRankOrder.setOriginalRankOrder(rankOrder);
279             gearWithOriginalRankOrder.setRankOrder(rankOrder);
280             rankOrder++;
281             gearsWithOrder.add(gearWithOriginalRankOrder);
282         }
283         cruise.setGear(Lists.newArrayList(gearsWithOrder));
284 
285         cruise.setComment("My comments on cruise");
286         cruise.setSurveyPart("SurveyPart");
287 
288         Person managerPerson = personService.getAllPerson().get(0);
289         cruise.setHeadOfMission(Lists.newArrayList(managerPerson));
290 
291         Vessel fishingVessel = Vessels.newVessel();
292         fishingVessel.setId(fishingVesselCode());
293 
294         cruise.setVessel(fishingVessel);
295 
296         cruise.setMultirigNumber(2);
297 
298         return cruiseService.createCruise(cruise);
299     }
300 
301     public FishingOperation createFishingOperation(Cruise cruise) {
302         setUp();
303 
304         FishingOperation fishingOperation = FishingOperations.newFishingOperation();
305         fishingOperation.setCruise(cruise);
306         fishingOperation.setComment("newFishingOperation");
307         fishingOperation.setFishingOperationNumber(1);
308 
309         // Retrieve some environment caracteristics
310         List<Caracteristic> allEnvironmentCaracteristics = caracteristicService.getAllCaracteristic();
311         CaracteristicMap environmentCaracteristics = new CaracteristicMap();
312         CaracteristicMap environmentValuesOneEntry = new CaracteristicMap();
313         int count = 0;
314         for (Caracteristic caracteristic : allEnvironmentCaracteristics) {
315             if (count == 3) {
316                 break;
317             }
318             Serializable value = null;
319             if (caracteristic.getCaracteristicType() == CaracteristicType.NUMBER) {
320                 value = 1.0f;
321                 count++;
322             } else if (caracteristic.getCaracteristicType() == CaracteristicType.TEXT) {
323                 value = "some text";
324                 count++;
325             } else if (caracteristic.getCaracteristicType() == CaracteristicType.QUALITATIVE
326                        && caracteristic.getQualitativeValue(0) != null) {
327                 // Choose the first qualitative value
328                 value = caracteristic.getQualitativeValue(0);
329                 count++;
330             }
331             if (value != null) {
332                 environmentCaracteristics.put(caracteristic, value);
333                 if (environmentValuesOneEntry.size() == 0) {
334                     environmentValuesOneEntry.put(caracteristic, value);
335                 }
336             }
337         }
338 
339         // Retrieve some gear use caracteristics
340         List<Caracteristic> allGearShootingCaracteristics = caracteristicService.getAllCaracteristic();
341         CaracteristicMap gearShootingCaracteristics = new CaracteristicMap();
342         CaracteristicMap gearShootingCaracteristicsOneEntry = new CaracteristicMap();
343         count = 0;
344         for (Caracteristic caracteristic : allGearShootingCaracteristics) {
345             if (count == 3) {
346                 break;
347             }
348             Serializable value = null;
349             if (caracteristic.getCaracteristicType() == CaracteristicType.NUMBER) {
350                 value = 1.0f;
351                 count++;
352             } else if (caracteristic.getCaracteristicType() == CaracteristicType.TEXT) {
353                 value = "some text";
354                 count++;
355             } else if (caracteristic.getCaracteristicType() == CaracteristicType.QUALITATIVE
356                        && caracteristic.getQualitativeValue(0) != null) {
357                 // Choose the first qualitative value
358                 value = caracteristic.getQualitativeValue(0);
359                 count++;
360             }
361             if (value != null) {
362                 gearShootingCaracteristics.put(caracteristic, value);
363                 if (gearShootingCaracteristicsOneEntry.size() == 0) {
364                     gearShootingCaracteristicsOneEntry.put(caracteristic, value);
365                 }
366             }
367         }
368 
369         Calendar calendar = Calendar.getInstance();
370         fishingOperation.setId((String) null);
371         fishingOperation.setStationNumber("STA2");
372         fishingOperation.setFishingOperationNumber(2);
373         fishingOperation.setMultirigAggregation("1");
374         calendar.setTime(new Date());
375         calendar.set(Calendar.HOUR_OF_DAY, 1);
376         calendar.set(Calendar.MILLISECOND, 99);
377         fishingOperation.setGearShootingStartDate(calendar.getTime());
378         calendar.setTime(new Date());
379         calendar.set(Calendar.HOUR_OF_DAY, 10);
380         calendar.set(Calendar.MILLISECOND, 99);
381         fishingOperation.setGearShootingEndDate(calendar.getTime());
382         fishingOperation.setGearShootingStartLatitude(47.6f);
383         fishingOperation.setGearShootingStartLongitude(-5.05f);
384         fishingOperation.setGearShootingEndLatitude(47.9854f);
385         fishingOperation.setGearShootingEndLongitude(-5.597f);
386 
387         fishingOperation.setTrawlDistance(100);
388         fishingOperation.setFishingOperationRectiligne(true);
389         fishingOperation.setFishingOperationValid(Boolean.TRUE);
390         fishingOperation.setComment("Unit test createFishingOperation() - Part n°2 : All properties set");
391         fishingOperation.setGear(cruise.getGear(0));
392         fishingOperation.setVessel(cruise.getVessel());
393         fishingOperation.setVesselUseFeatures(environmentCaracteristics);
394         fishingOperation.setGearUseFeatures(gearShootingCaracteristics);
395 
396         TuttiLocation strata = TuttiLocations.newTuttiLocation();
397         strata.setId(strataId());
398         fishingOperation.setStrata(strata);
399         TuttiLocation subStrata = TuttiLocations.newTuttiLocation();
400         subStrata.setId(subStrataId());
401         fishingOperation.setSubStrata(subStrata);
402         TuttiLocation localite = TuttiLocations.newTuttiLocation();
403         localite.setId(localite());
404         fishingOperation.setLocation(localite);
405 
406         return fishingOperationService.createFishingOperation(fishingOperation);
407     }
408 
409     public CatchBatch createMinimalCatchBatch(FishingOperation fishingOperation) {
410 
411         setUp();
412 
413         CatchBatch catchBatch = CatchBatchs.newCatchBatch();
414         catchBatch.setFishingOperation(fishingOperation);
415         return catchBatchService.createCatchBatch(catchBatch);
416     }
417 
418     public AccidentalBatch createMinimalAccidentalBatch(FishingOperation fishingOperation) {
419 
420         setUp();
421 
422         AccidentalBatch minimalAccidentalBatch = AccidentalBatchs.newAccidentalBatch();
423         minimalAccidentalBatch.setFishingOperation(fishingOperation);
424         minimalAccidentalBatch.setSpecies(speciesService.getSpeciesByReferenceTaxonId(refSpeciesTaxonId()));
425         minimalAccidentalBatch.setCaracteristics(new CaracteristicMap());
426         return accidentalBatchService.createAccidentalBatch(minimalAccidentalBatch);
427     }
428 
429 }