View Javadoc
1   package fr.ifremer.tutti.persistence.dao;
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.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   * Created on 9/28/13.
40   *
41   * @author Tony Chemit - chemit@codelutin.com
42   * @since 2.6
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          // Retrieve entities : Gear Physical Features
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  }