View Javadoc
1   package net.sumaris.core.dao.data;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Core
6    * %%
7    * Copyright (C) 2018 SUMARiS Consortium
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 net.sumaris.core.dao.referential.location.LocationDao;
26  import net.sumaris.core.dao.technical.model.IEntity;
27  import net.sumaris.core.model.data.Landing;
28  import net.sumaris.core.model.data.ObservedLocation;
29  import net.sumaris.core.model.data.Trip;
30  import net.sumaris.core.model.referential.location.Location;
31  import net.sumaris.core.util.Beans;
32  import net.sumaris.core.vo.administration.programStrategy.ProgramVO;
33  import net.sumaris.core.vo.data.DataFetchOptions;
34  import net.sumaris.core.vo.data.LandingVO;
35  import net.sumaris.core.vo.filter.LandingFilterVO;
36  import org.apache.commons.collections4.CollectionUtils;
37  import org.apache.commons.lang3.NotImplementedException;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  import org.springframework.beans.factory.annotation.Autowired;
41  import org.springframework.data.jpa.domain.Specification;
42  
43  import javax.persistence.EntityManager;
44  import java.util.Date;
45  import java.util.List;
46  import java.util.stream.Collectors;
47  
48  public class LandingRepositoryImpl
49          extends RootDataRepositoryImpl<Landing, Integer, LandingVO, LandingFilterVO>
50          implements LandingRepositoryExtend {
51  
52  
53  
54      private static final Logger log =
55              LoggerFactory.getLogger(LandingRepositoryImpl.class);
56  
57      @Autowired
58      private LocationDao locationDao;
59  
60      public LandingRepositoryImpl(EntityManager entityManager) {
61          super(Landing.class, entityManager);
62      }
63  
64  
65      @Override
66      public LandingVO toVO(Landing source) {
67          return super.toVO(source);
68      }
69  
70      @Override
71      public Specification<Landing> toSpecification(LandingFilterVO filter) {
72          if (filter == null) return null;
73  
74          return Specification.where(and(
75                  hasObservedLocationId(filter.getObservedLocationId()),
76                  hasTripId(filter.getTripId()),
77                  betweenDate(filter.getStartDate(), filter.getEndDate()),
78                  hasLocationId(filter.getLocationId()))
79          );
80      }
81  
82      public Class<LandingVO> getVOClass() {
83          return LandingVO.class;
84      }
85  
86      //@Override
87      public List<LandingVO> saveAllByObservedLocationId(int observedLocationId, List<LandingVO> sources) {
88          // Load parent entity
89          ObservedLocation parent = get(ObservedLocation.class, observedLocationId);
90          ProgramVOprogramStrategy/ProgramVO.html#ProgramVO">ProgramVO parentProgram = new ProgramVO();
91          parentProgram.setId(parent.getProgram().getId());
92  
93          // Remember existing entities
94          final List<Integer> sourcesIdsToRemove = Beans.collectIds(Beans.getList(parent.getLandings()));
95  
96          // Save each gears
97          List<LandingVO> result = sources.stream().map(source -> {
98              source.setObservedLocationId(observedLocationId);
99              source.setProgram(parentProgram);
100 
101             if (source.getId() != null) {
102                 sourcesIdsToRemove.remove(source.getId());
103             }
104             return save(source);
105         }).collect(Collectors.toList());
106 
107         // Remove unused entities
108         if (CollectionUtils.isNotEmpty(sourcesIdsToRemove)) {
109             sourcesIdsToRemove.forEach(this::deleteById);
110         }
111 
112         return result;
113     }
114 
115     @Override
116     public void toVO(Landing source, LandingVO target, DataFetchOptions fetchOptions, boolean copyIfNull) {
117         super.toVO(source, target, fetchOptions, copyIfNull);
118 
119         // location
120         target.setLocation(locationDao.toLocationVO(source.getLocation()));
121 
122         // Parent link
123         if (source.getObservedLocation() != null) {
124             target.setObservedLocationId(source.getObservedLocation().getId());
125         }
126         if (source.getTrip() != null) {
127             target.setTripId(source.getTrip().getId());
128         }
129     }
130 
131     @Override
132     public void toEntity(LandingVO source, Landing target, boolean copyIfNull) {
133 
134         DataDaos.copyRootDataProperties(getEntityManager(), source, target, copyIfNull);
135 
136         // Vessel
137         copyVessel(source, target, copyIfNull);
138 
139         // Observers
140         copyObservers(source, target, copyIfNull);
141 
142         // Landing location
143         if (copyIfNull || source.getLocation() != null) {
144             if (source.getLocation() == null || source.getLocation().getId() == null) {
145                 target.setLocation(null);
146             } else {
147                 target.setLocation(load(Location.class, source.getLocation().getId()));
148             }
149         }
150 
151         // Observed Location
152         Integer observedLocationId = source.getObservedLocationId() != null ? source.getObservedLocationId() : (source.getObservedLocation() != null ? source.getObservedLocation().getId() : null);
153         if (copyIfNull || (observedLocationId != null)) {
154             if (observedLocationId == null) {
155                 target.setObservedLocation(null);
156             } else {
157                 target.setObservedLocation(load(ObservedLocation.class, observedLocationId));
158             }
159         }
160 
161         // Trip
162         Integer tripId = source.getTripId() != null ? source.getTripId() : (source.getTrip() != null ? source.getTrip().getId() : null);
163         if (copyIfNull || (tripId != null)) {
164             if (tripId == null) {
165                 target.setTrip(null);
166             } else {
167                 target.setTrip(load(Trip.class, tripId));
168             }
169         }
170 
171 
172     }
173 
174 }