1 package fr.ifremer.tutti.persistence.dao;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import com.google.common.collect.Lists;
26 import fr.ifremer.adagio.core.dao.data.survey.fishingTrip.FishingTrip;
27 import fr.ifremer.adagio.core.dao.data.vessel.feature.physical.GearPhysicalFeatures;
28 import fr.ifremer.adagio.core.dao.data.vessel.feature.physical.GearPhysicalFeaturesDaoImpl;
29 import fr.ifremer.adagio.core.dao.referential.gear.Gear;
30 import fr.ifremer.adagio.core.dao.referential.gear.GearImpl;
31 import org.hibernate.SessionFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.context.annotation.Lazy;
34 import org.springframework.stereotype.Repository;
35
36 import java.util.Objects;
37
38
39
40
41
42
43
44 @Repository("gearPhysicalFeaturesDaoTutti")
45 @Lazy
46 public class GearPhysicalFeaturesDaoImplTutti extends GearPhysicalFeaturesDaoImpl implements GearPhysicalFeaturesDaoTutti {
47
48 @Autowired
49 public GearPhysicalFeaturesDaoImplTutti(SessionFactory sessionFactory) {
50 super(sessionFactory);
51 }
52
53 @Override
54 public GearPhysicalFeatures getGearPhysicalfeatures(FishingTrip fishingTrip,
55 Integer gearId,
56 Short rankOrder,
57 boolean createIfNotExists) {
58
59 if (rankOrder == null) {
60 rankOrder = 0;
61 }
62
63 if (fishingTrip.getGearPhysicalFeatures() != null && fishingTrip.getGearPhysicalFeatures().size() >= 0) {
64 for (GearPhysicalFeatures guf : fishingTrip.getGearPhysicalFeatures()) {
65 if (Objects.equals(rankOrder, guf.getRankOrder()) &&
66 Objects.equals(gearId, guf.getGear().getId())) {
67 return guf;
68 }
69 }
70 }
71 if (!createIfNotExists) {
72 return null;
73 }
74
75 GearPhysicalFeatures gearPhysicalFeature = GearPhysicalFeatures.Factory.newInstance();
76 gearPhysicalFeature.setFishingTrip(fishingTrip);
77 gearPhysicalFeature.setRankOrder(rankOrder);
78
79 Gear gear = load(GearImpl.class, gearId);
80 gearPhysicalFeature.setGear(gear);
81 if (fishingTrip.getGearPhysicalFeatures() == null) {
82 fishingTrip.setGearPhysicalFeatures(Lists.newArrayList(gearPhysicalFeature));
83 } else {
84 fishingTrip.getGearPhysicalFeatures().add(gearPhysicalFeature);
85 }
86
87 return gearPhysicalFeature;
88 }
89 }