View Javadoc
1   package fr.ifremer.reefdb.dao.system.rule;
2   
3   /*-
4    * #%L
5    * Reef DB :: Core
6    * %%
7    * Copyright (C) 2014 - 2018 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (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 Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import com.google.common.collect.Lists;
25  import fr.ifremer.quadrige3.core.dao.referential.UnitImpl;
26  import fr.ifremer.quadrige3.core.dao.referential.pmfm.FractionImpl;
27  import fr.ifremer.quadrige3.core.dao.referential.pmfm.MatrixImpl;
28  import fr.ifremer.quadrige3.core.dao.referential.pmfm.MethodImpl;
29  import fr.ifremer.quadrige3.core.dao.referential.pmfm.ParameterImpl;
30  import fr.ifremer.quadrige3.core.dao.system.rule.*;
31  import fr.ifremer.quadrige3.core.dao.technical.Assert;
32  import fr.ifremer.quadrige3.core.dao.technical.hibernate.TemporaryDataHelper;
33  import fr.ifremer.reefdb.dao.referential.ReefDbUnitDao;
34  import fr.ifremer.reefdb.dao.referential.pmfm.*;
35  import fr.ifremer.reefdb.dto.ReefDbBeanFactory;
36  import fr.ifremer.reefdb.dto.configuration.control.RulePmfmDTO;
37  import fr.ifremer.reefdb.dto.referential.pmfm.PmfmDTO;
38  import fr.ifremer.reefdb.service.StatusFilter;
39  import org.hibernate.SessionFactory;
40  import org.hibernate.type.StringType;
41  import org.springframework.beans.factory.annotation.Autowired;
42  import org.springframework.stereotype.Repository;
43  
44  import javax.annotation.Resource;
45  import java.util.Arrays;
46  import java.util.Iterator;
47  import java.util.List;
48  
49  /**
50   * @author peck7 on 03/07/2018.
51   */
52  @Repository("reefDbRulePmfmDao")
53  public class ReefDbRulePmfmDaoImpl extends RulePmfmDaoImpl implements ReefDbRulePmfmDao {
54  
55      @Resource(name = "reefDbPmfmDao")
56      protected ReefDbPmfmDao pmfmDao;
57  
58      @Resource(name = "reefDbParameterDao")
59      protected ReefDbParameterDao parameterDao;
60  
61      @Resource(name = "reefDbMatrixDao")
62      protected ReefDbMatrixDao matrixDao;
63  
64      @Resource(name = "reefDbFractionDao")
65      protected ReefDbFractionDao fractionDao;
66  
67      @Resource(name = "reefDbMethodDao")
68      protected ReefDbMethodDao methodDao;
69  
70      @Resource(name = "reefDbUnitDao")
71      protected ReefDbUnitDao unitDao;
72  
73      /**
74       * Constructor used by Spring
75       *
76       * @param sessionFactory
77       */
78      @Autowired
79      public ReefDbRulePmfmDaoImpl(SessionFactory sessionFactory) {
80          super(sessionFactory);
81      }
82  
83      @Override
84      public List<RulePmfmDTO> getRulePmfmByRuleCode(String ruleCode) {
85          Assert.notBlank(ruleCode);
86  
87          Iterator<Object[]> it = queryIterator("rulePmfmByRuleCode",
88                  "ruleCode", StringType.INSTANCE, ruleCode);
89  
90          List<RulePmfmDTO> result = Lists.newArrayList();
91          while (it.hasNext()) {
92              Object[] source = it.next();
93              result.add(toRulePmfmDTO(Arrays.asList(source).iterator()));
94          }
95  
96          return result;
97      }
98  
99      @Override
100     public RulePmfmDTO save(RulePmfmDTO source, String ruleCd) {
101         Assert.notNull(source);
102         Assert.notNull(source.getPmfm());
103         Assert.notNull(source.getPmfm().getParameter());
104         Assert.notBlank(source.getPmfm().getParameter().getCode());
105         Assert.notBlank(ruleCd);
106 
107         // Parent
108         Rule parent = get(RuleImpl.class, ruleCd);
109 
110         // Load entity
111         RulePmfm target = source.getId() != null ? get(source.getId()) : null;
112         boolean isNew = false;
113         if (target == null) {
114             target = RulePmfm.Factory.newInstance();
115             target.setRulePmfmId(TemporaryDataHelper.getNewNegativeIdForTemporaryData(getSession(), RulePmfmImpl.class));
116             parent.addRulePmfms(target);
117             target.setRule(parent);
118             isNew = true;
119         }
120 
121         // DTO -> VO
122         beanToEntity(source, target);
123 
124         // Save or update
125         if (isNew) {
126             getSession().save(target);
127             // Update source
128             source.setId(target.getRulePmfmId());
129         } else {
130             getSession().update(target);
131         }
132 
133         return source;
134     }
135 
136     private void beanToEntity(RulePmfmDTO source, RulePmfm target) {
137         // Set properties
138         target.setParameter(load(ParameterImpl.class, source.getPmfm().getParameter().getCode()));
139         target.setMatrix(source.getPmfm().getMatrix() == null ? null : load(MatrixImpl.class, source.getPmfm().getMatrix().getId()));
140         target.setFraction(source.getPmfm().getFraction() == null ? null : load(FractionImpl.class, source.getPmfm().getFraction().getId()));
141         target.setMethod(source.getPmfm().getMethod() == null ? null : load(MethodImpl.class, source.getPmfm().getMethod().getId()));
142         target.setUnit(source.getPmfm().getUnit() == null ? null : load(UnitImpl.class, source.getPmfm().getUnit().getId()));
143     }
144 
145     // internal methods
146 
147     private RulePmfmDTO toRulePmfmDTO(Iterator<Object> source) {
148         RulePmfmDTO result = ReefDbBeanFactory.newRulePmfmDTO();
149         result.setId((Integer) source.next());
150 
151         String parameterCode = (String) source.next();
152         Integer matrixId = (Integer) source.next();
153         Integer fractionId = (Integer) source.next();
154         Integer methodId = (Integer) source.next();
155         Integer unitId = (Integer) source.next();
156 
157         // get existing pmfm by the four criteria
158         List<PmfmDTO> pmfms = pmfmDao.findPmfms(parameterCode, matrixId, fractionId, methodId, unitId, null, StatusFilter.ACTIVE.toStatusCodes());
159 
160         if (pmfms.size() == 1) {
161             // corresponding pmfm found
162             result.setPmfm(pmfms.get(0));
163 
164         } else {
165 
166             PmfmDTO pmfm = ReefDbBeanFactory.newPmfmDTO();
167             // only parameter is mandatory
168             pmfm.setParameter(parameterDao.getParameterByCode(parameterCode));
169 
170             if (matrixId != null) {
171                 pmfm.setMatrix(matrixDao.getMatrixById(matrixId));
172             }
173             if (fractionId != null) {
174                 pmfm.setFraction(fractionDao.getFractionById(fractionId));
175             }
176             if (methodId != null) {
177                 pmfm.setMethod(methodDao.getMethodById(methodId));
178             }
179             if (unitId != null) {
180                 pmfm.setUnit(unitDao.getUnitById(unitId));
181             }
182 
183             result.setPmfm(pmfm);
184 
185         }
186         return result;
187     }
188 }