View Javadoc
1   package net.sumaris.core.dao.referential;
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.util.Beans;
26  import net.sumaris.core.dao.technical.hibernate.HibernateDaoSupport;
27  import net.sumaris.core.model.referential.pmfm.Method;
28  import net.sumaris.core.model.referential.pmfm.Parameter;
29  import net.sumaris.core.model.referential.pmfm.Pmfm;
30  import net.sumaris.core.vo.referential.ParameterValueType;
31  import net.sumaris.core.vo.referential.PmfmVO;
32  import net.sumaris.core.vo.referential.ReferentialVO;
33  import org.apache.commons.collections4.CollectionUtils;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  import org.springframework.beans.factory.annotation.Autowired;
37  import org.springframework.stereotype.Repository;
38  
39  import javax.annotation.PostConstruct;
40  import javax.persistence.TypedQuery;
41  import javax.persistence.criteria.*;
42  import java.util.*;
43  import java.util.stream.Collectors;
44  
45  @Repository("pmfmDao")
46  public class PmfmDaoImpl extends HibernateDaoSupport implements PmfmDao {
47  
48      /** Logger. */
49      private static final Logger log =
50              LoggerFactory.getLogger(PmfmDaoImpl.class);
51  
52      @Autowired
53      private ReferentialDao referentialDao;
54  
55      public int unitIdNone;
56  
57      @PostConstruct
58      protected void init() {
59          this.unitIdNone = config.getUnitIdNone();
60      }
61  
62      @Override
63      public PmfmVO getByLabel(final String label) {
64          CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
65          CriteriaQuery<Pmfm> query = builder.createQuery(Pmfm.class);
66          Root<Pmfm> root = query.from(Pmfm.class);
67  
68          ParameterExpression<String> labelParam = builder.parameter(String.class);
69  
70          query.select(root)
71                  .where(builder.equal(root.get(Pmfm.Fields.LABEL), labelParam));
72  
73          TypedQuery<Pmfm> q = getEntityManager().createQuery(query)
74                  .setParameter(labelParam, label);
75          return toPmfmVO(q.getSingleResult());
76      }
77  
78      @Override
79      public PmfmVO get(final int pmfmId) {
80          return toPmfmVO(get(Pmfm.class, pmfmId));
81      }
82  
83      @Override
84      public PmfmVO toPmfmVO(Pmfm source) {
85          if (source == null) return null;
86  
87          PmfmVOential/PmfmVO.html#PmfmVO">PmfmVO target = new PmfmVO();
88  
89          Beans.copyProperties(source, target);
90  
91          // Parameter name
92          Parameter parameter = source.getParameter();
93          target.setName(parameter.getName());
94  
95          // Parameter type
96          ParameterValueType type = ParameterValueType.fromPmfm(source);
97          target.setType(type.name().toLowerCase());
98  
99          // Method: copy isEstimated, isCalculated
100         Method method = source.getMethod();
101         if (method != null) {
102             target.setIsCalculated(method.getIsCalculated());
103             target.setIsEstimated(method.getIsEstimated());
104         }
105 
106         // Unit symbol
107         if (source.getUnit() != null && source.getUnit().getId().intValue() != unitIdNone) {
108             target.setUnit(source.getUnit().getLabel());
109         }
110 
111         // Qualitative values: from pmfm first, or (if empty) from parameter
112         if (CollectionUtils.isNotEmpty(source.getQualitativeValues())) {
113             List<ReferentialVO> qualitativeValues = source.getQualitativeValues()
114                     .stream()
115                     .map(referentialDao::toReferentialVO)
116                     .collect(Collectors.toList());
117             target.setQualitativeValues(qualitativeValues);
118         }
119         else if (CollectionUtils.isNotEmpty(parameter.getQualitativeValues())) {
120             List<ReferentialVO> qualitativeValues = parameter.getQualitativeValues()
121                     .stream()
122                     .map(referentialDao::toReferentialVO)
123                     .collect(Collectors.toList());
124             target.setQualitativeValues(qualitativeValues);
125         }
126 
127         return target;
128     }
129 }