View Javadoc
1   // license-header java merge-point
2   //
3   // Attention: Generated code! Do not modify by hand!
4   // Generated by: SpringHibernateDaoImpl.vsl in andromda-spring-cartridge.
5   //
6   package fr.ifremer.quadrige2.core.dao.administration.user;
7   
8   /*-
9    * #%L
10   * Quadrige2 Core :: Quadrige2 Server Core
11   * $Id:$
12   * $HeadURL:$
13   * %%
14   * Copyright (C) 2017 Ifremer
15   * %%
16   * This program is free software: you can redistribute it and/or modify
17   * it under the terms of the GNU Affero General Public License as published by
18   * the Free Software Foundation, either version 3 of the License, or
19   * (at your option) any later version.
20   * 
21   * This program is distributed in the hope that it will be useful,
22   * but WITHOUT ANY WARRANTY; without even the implied warranty of
23   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24   * GNU General Public License for more details.
25   * 
26   * You should have received a copy of the GNU Affero General Public License
27   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28   * #L%
29   */
30  
31  import com.google.common.base.Preconditions;
32  import fr.ifremer.quadrige2.core.exception.Quadrige2TechnicalException;
33  import fr.ifremer.quadrige2.core.dao.referential.StatusImpl;
34  import fr.ifremer.quadrige2.core.vo.administration.user.DepartmentVO;
35  import org.hibernate.SessionFactory;
36  import org.springframework.beans.factory.annotation.Autowired;
37  import org.springframework.context.annotation.Lazy;
38  import org.springframework.stereotype.Repository;
39  
40  /**
41   * <p>
42   * DepartmentDaoImpl class.
43   * </p>
44   * 
45   * @see Department
46   */
47  @Repository("departmentDao")
48  @Lazy
49  public class DepartmentDaoImpl
50  		extends DepartmentDaoBase
51  		implements DepartmentExtendDao {
52  
53  	/**
54  	 * Constructor used by Spring
55  	 * 
56  	 * @param sessionFactory
57  	 *            a {@link org.hibernate.SessionFactory} object.
58  	 */
59  	@Autowired
60  	public DepartmentDaoImpl(SessionFactory sessionFactory) {
61  		super();
62  		setSessionFactory(sessionFactory);
63  	}
64  
65  	/** {@inheritDoc} */
66  	@Override
67  	public DepartmentVO save(DepartmentVO bean) {
68  		Preconditions.checkNotNull(bean);
69  
70  		Department entity = null;
71  		if (bean.getDepId() != null) {
72  			entity = load(bean.getDepId());
73  		}
74  
75  		// not exists on DB: create it
76  		if (entity == null) {
77  			entity = Department.Factory.newInstance();
78  
79  			// Convert to entity
80  			toEntity(bean, entity);
81  
82  			// Update
83  			entity = create(entity);
84  		} else {
85  			entity = load(bean.getDepId());
86  
87  			// Convert to entity
88  			toEntity(bean, entity);
89  
90  			// Update
91  			update(entity);
92  		}
93  
94  		// Refresh the id
95  		bean.setDepId(entity.getDepId());
96  
97  		// Refresh the parent department id (could have been reseted, if not exist yet in local DB)
98  		if (entity.getParentDepartment() == null) {
99  			bean.setParentDepartmentId(null);
100 		} else {
101 			bean.setParentDepartmentId(entity.getParentDepartment().getDepId());
102 		}
103 
104 		return bean;
105 	}
106 
107 	/** {@inheritDoc} */
108 	@Override
109 	public Department departmentVOToEntity(DepartmentVO source) {
110 		Department target;
111 		if (source.getDepId() != null && source.getDepId() > 0) {
112 			target = load(source.getDepId());
113 		}
114 		else {
115 			target = Department.Factory.newInstance();
116 		}
117 		toEntity(source, target);
118 
119 		return target;
120 	}
121 
122 	/* -- Internal methods -- */
123 
124 	/**
125 	 * <p>
126 	 * toEntity.
127 	 * </p>
128 	 * 
129 	 * @param source
130 	 *            a {@link fr.ifremer.quadrige2.core.vo.administration.user.DepartmentVO} object.
131 	 * @param target
132 	 *            a {@link fr.ifremer.quadrige2.core.dao.administration.user.Department} object.
133 	 */
134 	protected void toEntity(DepartmentVO source, Department target) {
135 		try {
136 			target.setDepId(source.getDepId());
137 			target.setDepCd(source.getDepCd());
138 			target.setDepNm(source.getDepNm());
139 			target.setDepLdapPresent(source.getDepLdapPresent());
140 			target.setDepCreationDt(source.getDepCreationDt());
141 			target.setUpdateDt(source.getUpdateDt());
142 			target.setStatus(load(StatusImpl.class, source.getStatusCd()));
143 			target.setDepEMail(source.getDepEMail());
144 			target.setDepAddress(source.getDepAddress());
145 			target.setDepPhone(source.getDepPhone());
146 
147 			// Parent department
148 			if (source.getParentDepartmentId() == null) {
149 				target.setParentDepartment(null);
150 			} else {
151 				// use a get (and NOT a load), to avoid error if parent not exists yet in local database
152 				Department parentDepartment = load(source.getParentDepartmentId());
153 				target.setParentDepartment(parentDepartment);
154 			}
155 
156 		} catch (Exception e) {
157 			throw new Quadrige2TechnicalException("Error while transform DepartmentVO bean into Department entity", e);
158 		}
159 	}
160 }