View Javadoc
1   package fr.ifremer.quadrige2.core.dao.administration.strategy;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 Server 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.quadrige2.core.dao.administration.user.DepartmentImpl;
26  import fr.ifremer.quadrige2.core.dao.referential.AnalysisInstrumentImpl;
27  import fr.ifremer.quadrige2.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  			} else {
88  				target.setPmfmStratId(source.getPmfmStrategy().getPmfmStratId());
89  			}
90  
91  			// Applied strategy
92  			if (source.getAppliedStrategy() == null) {
93  				target.setAppliedStratId(null);
94  			} else {
95  				target.setAppliedStratId(source.getAppliedStrategy().getAppliedStratId());
96  			}
97  		}
98  
99  		// Department
100 		if (source.getDepartment() == null) {
101 			target.setDepId(null);
102 		}
103 		else {
104 			target.setDepId(source.getDepartment().getDepId());
105 		}
106 
107 		// Analysis instrument
108 		if (source.getAnalysisInstrument() == null) {
109 			target.setAnalInstId(null);
110 		}
111 		else {
112 			target.setAnalInstId(source.getAnalysisInstrument().getAnalInstId());
113 		}
114 	}
115 
116 	/**
117 	 * Retrieves the entity object that is associated with the specified value object
118 	 * from the object store. If no such entity object exists in the object store,
119 	 * a new, blank entity is created
120 	 */
121 	private PmfmAppliedStrategy loadPmfmAppliedStrategyFromPmfmAppliedStrategyVO(PmfmAppliedStrategyVO source)
122 	{
123 		PmfmAppliedStrategyPK pk = new PmfmAppliedStrategyPK();
124 
125 		// Pmfm strat id
126 		Integer pmfmStratId = source.getPmfmStratId();
127 		if (pmfmStratId == null && source.getPmfmStrategyVO() != null) {
128 			pmfmStratId = source.getPmfmStrategyVO().getPmfmStratId();
129 		}
130 		pk.setPmfmStrategy(load(PmfmStrategyImpl.class, pmfmStratId));
131 
132 		// Applied strategy
133 		Integer appliedStratId = source.getAppliedStratId();
134 		if (appliedStratId == null && source.getAppliedStrategyVO() != null) {
135 			appliedStratId = source.getAppliedStrategyVO().getAppliedStratId();
136 		}
137 		pk.setAppliedStrategy(load(AppliedStrategyImpl.class, appliedStratId));
138 
139 		PmfmAppliedStrategy pmfmAppliedStrategy = this.get(pk);
140 		if (pmfmAppliedStrategy == null)
141 		{
142 			pmfmAppliedStrategy = PmfmAppliedStrategy.Factory.newInstance();
143 			pmfmAppliedStrategy.setPmfmAppliedStrategyPk(pk);
144 		}
145 		return pmfmAppliedStrategy;
146 	}
147 
148 	/** {@inheritDoc} */
149 	public PmfmAppliedStrategy pmfmAppliedStrategyVOToEntity(PmfmAppliedStrategyVO pmfmAppliedStrategyVO)
150 	{
151 		PmfmAppliedStrategy entity = this.loadPmfmAppliedStrategyFromPmfmAppliedStrategyVO(pmfmAppliedStrategyVO);
152 		this.pmfmAppliedStrategyVOToEntity(pmfmAppliedStrategyVO, entity, true);
153 		return entity;
154 	}
155 
156 	/** {@inheritDoc} */
157 	@Override
158 	public void pmfmAppliedStrategyVOToEntity(
159 			PmfmAppliedStrategyVO source,
160 			PmfmAppliedStrategy target,
161 			boolean copyIfNull)
162 	{
163 		super.pmfmAppliedStrategyVOToEntity(source, target, copyIfNull);
164 
165 		// Applied strategy
166 		if (copyIfNull || source.getAppliedStrategyVO() != null || source.getAppliedStratId() != null) {
167 			if (source.getAppliedStrategyVO() == null && source.getAppliedStratId() == null) {
168 				target.setAppliedStrategy(null);
169 			}
170 			else {
171 				Integer appliedStratId = source.getAppliedStratId();
172 				if (appliedStratId == null && source.getAppliedStrategyVO() != null) {
173 					appliedStratId = source.getAppliedStrategyVO().getAppliedStratId();
174 				}
175 				target.setAppliedStrategy(load(AppliedStrategyImpl.class, appliedStratId));
176 			}
177 		}
178 
179 		// Pmfm strategy
180 		if (copyIfNull || source.getPmfmStrategyVO() != null || source.getPmfmStratId() != null) {
181 			if (source.getPmfmStrategyVO() == null && source.getPmfmStratId() == null) {
182 				target.setPmfmStrategy(null);
183 			}
184 			else {
185 				Integer pmfmStratId = source.getPmfmStratId();
186 				if (pmfmStratId == null && source.getPmfmStrategyVO() != null) {
187 					pmfmStratId = source.getPmfmStrategyVO().getPmfmStratId();
188 				}
189 				target.setPmfmStrategy(load(PmfmStrategyImpl.class, pmfmStratId));
190 			}
191 		}
192 
193 		// Department
194 		if (copyIfNull || source.getDepId() != null) {
195 			if (source.getDepId() == null) {
196 				target.setDepartment(null);
197 			}
198 			else {
199 				target.setDepartment(load(DepartmentImpl.class, source.getDepId()));
200 			}
201 		}
202 
203 		// Analysis instrument
204 		if (copyIfNull || source.getAnalInstId() != null) {
205 			if (source.getAnalInstId() == null) {
206 				target.setAnalysisInstrument(null);
207 			} else {
208 				target.setAnalysisInstrument(load(AnalysisInstrumentImpl.class, source.getAnalInstId()));
209 			}
210 		}
211 	}
212 }