View Javadoc
1   package fr.ifremer.quadrige3.core.dao.administration.strategy;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Client Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 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  import fr.ifremer.quadrige3.core.dao.administration.user.DepartmentImpl;
26  import fr.ifremer.quadrige3.core.dao.referential.AnalysisInstrumentImpl;
27  import fr.ifremer.quadrige3.core.vo.administration.strategy.PmfmAppliedStrategyVO;
28  import org.hibernate.SessionFactory;
29  import org.springframework.beans.factory.annotation.Autowired;
30  import org.springframework.context.annotation.Lazy;
31  import org.springframework.stereotype.Repository;
32  
33  /**
34   * <p>
35   * PmfmAppliedStrategyDaoImpl class.
36   * </p>
37   * 
38   * @see PmfmAppliedStrategy
39   */
40  @Repository("pmfmAppliedStrategyDao")
41  @Lazy
42  public class PmfmAppliedStrategyDaoImpl
43  		extends PmfmAppliedStrategyDaoBase
44  {
45  	/**
46  	 * Constructor used by Spring
47  	 * 
48  	 * @param sessionFactory
49  	 *            a {@link org.hibernate.SessionFactory} object.
50  	 */
51  	@Autowired
52  	public PmfmAppliedStrategyDaoImpl(SessionFactory sessionFactory) {
53  		super();
54  		setSessionFactory(sessionFactory);
55  	}
56  
57  	/** {@inheritDoc} */
58  	public void toPmfmAppliedStrategyVO(
59  			PmfmAppliedStrategy source,
60  			PmfmAppliedStrategyVO target)
61  	{
62  		super.toPmfmAppliedStrategyVO(source, target);
63  
64  		if (source.getPmfmAppliedStrategyPk() != null) {
65  			PmfmAppliedStrategyPK pk = source.getPmfmAppliedStrategyPk();
66  
67  			// Pmfm strategy
68  			if (pk.getPmfmStrategy() == null) {
69  				target.setPmfmStratId(null);
70  			}
71  			else {
72  				target.setPmfmStratId(pk.getPmfmStrategy().getPmfmStratId());
73  			}
74  
75  			// Applied strategy
76  			if (pk.getAppliedStrategy() == null) {
77  				target.setAppliedStratId(null);
78  			}
79  			else {
80  				target.setAppliedStratId(pk.getAppliedStrategy().getAppliedStratId());
81  			}
82  		}
83  		else {
84  			// Pmfm strategy
85  			if (source.getPmfmStrategy() == null) {
86  				target.setPmfmStratId(null);
87  			}
88  			else {
89  				target.setPmfmStratId(source.getPmfmStrategy().getPmfmStratId());
90  			}
91  
92  			// Applied strategy
93  			if (source.getAppliedStrategy() == null) {
94  				target.setAppliedStratId(null);
95  			}
96  			else {
97  				target.setAppliedStratId(source.getAppliedStrategy().getAppliedStratId());
98  			}
99  		}
100 
101 		// Department
102 		if (source.getDepartment() == null) {
103 			target.setDepId(null);
104 		}
105 		else {
106 			target.setDepId(source.getDepartment().getDepId());
107 		}
108 
109 		// Analysis instrument
110 		if (source.getAnalysisInstrument() == null) {
111 			target.setAnalInstId(null);
112 		}
113 		else {
114 			target.setAnalInstId(source.getAnalysisInstrument().getAnalInstId());
115 		}
116 	}
117 
118 	/**
119 	 * Retrieves the entity object that is associated with the specified value object
120 	 * from the object store. If no such entity object exists in the object store,
121 	 * a new, blank entity is created
122 	 */
123 	private PmfmAppliedStrategy loadPmfmAppliedStrategyFromPmfmAppliedStrategyVO(PmfmAppliedStrategyVO source)
124 	{
125 		PmfmAppliedStrategyPK pk = new PmfmAppliedStrategyPK();
126 
127 		// Pmfm strat id
128 		Integer pmfmStratId = source.getPmfmStratId();
129 		if (pmfmStratId == null && source.getPmfmStrategyVO() != null) {
130 			pmfmStratId = source.getPmfmStrategyVO().getPmfmStratId();
131 		}
132 		pk.setPmfmStrategy(load(PmfmStrategyImpl.class, pmfmStratId));
133 
134 		// Applied strategy
135 		Integer appliedStratId = source.getAppliedStratId();
136 		if (appliedStratId == null && source.getAppliedStrategyVO() != null) {
137 			appliedStratId = source.getAppliedStrategyVO().getAppliedStratId();
138 		}
139 		pk.setAppliedStrategy(load(AppliedStrategyImpl.class, appliedStratId));
140 
141 		PmfmAppliedStrategy pmfmAppliedStrategy = this.get(pk);
142 		if (pmfmAppliedStrategy == null)
143 		{
144 			pmfmAppliedStrategy = PmfmAppliedStrategy.Factory.newInstance();
145 			pmfmAppliedStrategy.setPmfmAppliedStrategyPk(pk);
146 		}
147 		return pmfmAppliedStrategy;
148 	}
149 
150 	/** {@inheritDoc} */
151 	public PmfmAppliedStrategy pmfmAppliedStrategyVOToEntity(PmfmAppliedStrategyVO pmfmAppliedStrategyVO)
152 	{
153 		PmfmAppliedStrategy entity = this.loadPmfmAppliedStrategyFromPmfmAppliedStrategyVO(pmfmAppliedStrategyVO);
154 		this.pmfmAppliedStrategyVOToEntity(pmfmAppliedStrategyVO, entity, true);
155 		return entity;
156 	}
157 
158 	/** {@inheritDoc} */
159 	@Override
160 	public void pmfmAppliedStrategyVOToEntity(
161 			PmfmAppliedStrategyVO source,
162 			PmfmAppliedStrategy target,
163 			boolean copyIfNull)
164 	{
165 		super.pmfmAppliedStrategyVOToEntity(source, target, copyIfNull);
166 
167 		// Applied strategy
168 		if (copyIfNull || source.getAppliedStrategyVO() != null || source.getAppliedStratId() != null) {
169 			if (source.getAppliedStrategyVO() == null && source.getAppliedStratId() == null) {
170 				target.setAppliedStrategy(null);
171 			}
172 			else {
173 				Integer appliedStratId = source.getAppliedStratId();
174 				if (appliedStratId == null && source.getAppliedStrategyVO() != null) {
175 					appliedStratId = source.getAppliedStrategyVO().getAppliedStratId();
176 				}
177 				target.setAppliedStrategy(load(AppliedStrategyImpl.class, appliedStratId));
178 			}
179 		}
180 
181 		// Pmfm strategy
182 		if (copyIfNull || source.getPmfmStrategyVO() != null || source.getPmfmStratId() != null) {
183 			if (source.getPmfmStrategyVO() == null && source.getPmfmStratId() == null) {
184 				target.setPmfmStrategy(null);
185 			}
186 			else {
187 				Integer pmfmStratId = source.getPmfmStratId();
188 				if (pmfmStratId == null && source.getPmfmStrategyVO() != null) {
189 					pmfmStratId = source.getPmfmStrategyVO().getPmfmStratId();
190 				}
191 				target.setPmfmStrategy(load(PmfmStrategyImpl.class, pmfmStratId));
192 			}
193 		}
194 
195 		// Department
196 		if (copyIfNull || source.getDepId() != null) {
197 			if (source.getDepId() == null) {
198 				target.setDepartment(null);
199 			}
200 			else {
201 				target.setDepartment(load(DepartmentImpl.class, source.getDepId()));
202 			}
203 		}
204 
205 		// Analysis instrument
206 		if (copyIfNull || source.getAnalInstId() != null) {
207 			if (source.getAnalInstId() == null) {
208 				target.setAnalysisInstrument(null);
209 			} else {
210 				target.setAnalysisInstrument(load(AnalysisInstrumentImpl.class, source.getAnalInstId()));
211 			}
212 		}
213 	}
214 }