View Javadoc
1   package fr.ifremer.dali.dao.system.rule;
2   
3   /*
4    * #%L
5    * Dali :: Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.collect.Lists;
27  import fr.ifremer.dali.config.DaliConfiguration;
28  import fr.ifremer.dali.dao.referential.DaliUnitDao;
29  import fr.ifremer.dali.dao.referential.pmfm.*;
30  import fr.ifremer.dali.dto.DaliBeanFactory;
31  import fr.ifremer.dali.dto.configuration.control.RulePmfmDTO;
32  import fr.ifremer.dali.dto.referential.pmfm.PmfmDTO;
33  import fr.ifremer.dali.service.StatusFilter;
34  import fr.ifremer.quadrige3.core.dao.referential.UnitImpl;
35  import fr.ifremer.quadrige3.core.dao.referential.pmfm.FractionImpl;
36  import fr.ifremer.quadrige3.core.dao.referential.pmfm.MatrixImpl;
37  import fr.ifremer.quadrige3.core.dao.referential.pmfm.MethodImpl;
38  import fr.ifremer.quadrige3.core.dao.referential.pmfm.ParameterImpl;
39  import fr.ifremer.quadrige3.core.dao.system.rule.*;
40  import fr.ifremer.quadrige3.core.dao.technical.Assert;
41  import fr.ifremer.quadrige3.core.dao.technical.hibernate.TemporaryDataHelper;
42  import org.hibernate.SessionFactory;
43  import org.hibernate.type.StringType;
44  import org.springframework.beans.factory.annotation.Autowired;
45  import org.springframework.stereotype.Repository;
46  
47  import javax.annotation.Resource;
48  import java.util.Arrays;
49  import java.util.Iterator;
50  import java.util.List;
51  
52  /**
53   * <p>DaliRuleListDaoImpl class.</p>
54   *
55   * @author Ludovic
56   */
57  @Repository("daliRulePmfmDao")
58  public class DaliRulePmfmDaoImpl extends RulePmfmDaoImpl implements DaliRulePmfmDao {
59  
60      @Resource
61      protected DaliConfiguration config;
62      @Resource(name = "daliPmfmDao")
63      protected DaliPmfmDao pmfmDao;
64      @Resource(name = "daliParameterDao")
65      private DaliParameterDao parameterDao;
66      @Resource(name = "daliMatrixDao")
67      private DaliMatrixDao matrixDao;
68      @Resource(name = "daliFractionDao")
69      private DaliFractionDao fractionDao;
70      @Resource(name = "daliMethodDao")
71      private DaliMethodDao methodDao;
72      @Resource(name = "daliUnitDao")
73      private DaliUnitDao unitDao;
74  
75      /**
76       * <p>Constructor for DaliRuleListDaoImpl.</p>
77       *
78       * @param sessionFactory a {@link SessionFactory} object.
79       */
80      @Autowired
81      public DaliRulePmfmDaoImpl(SessionFactory sessionFactory) {
82          super(sessionFactory);
83      }
84  
85      @Override
86      public List<RulePmfmDTO> getRulePmfmByRuleCode(String ruleCode) {
87          Assert.notBlank(ruleCode);
88  
89          Iterator<Object[]> it = queryIterator("rulePmfmByRuleCode",
90                  "ruleCode", StringType.INSTANCE, ruleCode);
91  
92          List<RulePmfmDTO> result = Lists.newArrayList();
93          while (it.hasNext()) {
94              Object[] source = it.next();
95              result.add(toRulePmfmDTO(Arrays.asList(source).iterator()));
96          }
97  
98          return result;
99  
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public RulePmfmDTO save(RulePmfmDTO source, String ruleCd) {
107         Assert.notNull(source);
108         Assert.notNull(source.getPmfm());
109         Assert.notNull(source.getPmfm().getParameter());
110         Assert.notNull(source.getPmfm().getParameter().getCode());
111         Assert.notBlank(ruleCd);
112 
113         // Parent
114         Rule parent = get(RuleImpl.class, ruleCd);
115 
116         // Load entity
117         RulePmfm target = source.getId() != null ? get(source.getId()) : null;
118         boolean isNew = false;
119         if (target == null) {
120             target = RulePmfm.Factory.newInstance();
121             target.setRulePmfmId(TemporaryDataHelper.getNewNegativeIdForTemporaryData(getSession(), RulePmfmImpl.class));
122             parent.addRulePmfms(target);
123             target.setRule(parent);
124             isNew = true;
125         }
126 
127         // DTO -> VO
128         beanToEntity(source, target);
129 
130         // Save or update
131         if (isNew) {
132             getSession().save(target);
133             // Update source
134             source.setId(target.getRulePmfmId());
135         } else {
136             getSession().update(target);
137         }
138 
139         return source;
140     }
141 
142     private void beanToEntity(RulePmfmDTO source, RulePmfm target) {
143         // Set properties
144         target.setParameter(load(ParameterImpl.class, source.getPmfm().getParameter().getCode()));
145         target.setMatrix(source.getPmfm().getMatrix() == null ? null : load(MatrixImpl.class, source.getPmfm().getMatrix().getId()));
146         target.setFraction(source.getPmfm().getFraction() == null ? null : load(FractionImpl.class, source.getPmfm().getFraction().getId()));
147         target.setMethod(source.getPmfm().getMethod() == null ? null : load(MethodImpl.class, source.getPmfm().getMethod().getId()));
148         target.setUnit(source.getPmfm().getUnit() == null ? null : load(UnitImpl.class, source.getPmfm().getUnit().getId()));
149     }
150 
151     // internal methods
152 
153     private RulePmfmDTO toRulePmfmDTO(Iterator<Object> source) {
154         RulePmfmDTO result = DaliBeanFactory.newRulePmfmDTO();
155         result.setId((Integer) source.next()); // this id is NOT the pmfm id but the rulePmfm id
156 
157         String parameterCode = (String) source.next();
158         Integer matrixId = (Integer) source.next();
159         Integer fractionId = (Integer) source.next();
160         Integer methodId = (Integer) source.next();
161         Integer unitId = (Integer) source.next();
162 
163         // get existing pmfm by the four criteria
164         List<PmfmDTO> pmfms = pmfmDao.findPmfms(parameterCode, matrixId, fractionId, methodId, unitId, null, StatusFilter.ACTIVE.toStatusCodes());
165 
166         if (pmfms.size() == 1) {
167             // corresponding pmfm found
168             result.setPmfm(pmfms.get(0));
169 
170         } else {
171 
172             PmfmDTO pmfm = DaliBeanFactory.newPmfmDTO();
173             // only parameter is mandatory
174             pmfm.setParameter(parameterDao.getParameterByCode(parameterCode));
175 
176             if (matrixId != null) {
177                 pmfm.setMatrix(matrixDao.getMatrixById(matrixId));
178             }
179             if (fractionId != null) {
180                 pmfm.setFraction(fractionDao.getFractionById(fractionId));
181             }
182             if (methodId != null) {
183                 pmfm.setMethod(methodDao.getMethodById(methodId));
184             }
185             if (unitId != null) {
186                 pmfm.setUnit(unitDao.getUnitById(unitId));
187             }
188 
189             result.setPmfm(pmfm);
190 
191         }
192         return result;
193     }
194 
195 }