View Javadoc
1   package fr.ifremer.quadrige3.core.dao.administration.user;
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  
26  import fr.ifremer.quadrige3.core.dao.referential.StatusImpl;
27  import fr.ifremer.quadrige3.core.dao.technical.Assert;
28  import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
29  import fr.ifremer.quadrige3.core.vo.administration.user.DepartmentVO;
30  import org.hibernate.SessionFactory;
31  import org.springframework.beans.factory.annotation.Autowired;
32  import org.springframework.context.annotation.Lazy;
33  import org.springframework.stereotype.Repository;
34  
35  import javax.annotation.Resource;
36  import java.util.Date;
37  import java.util.List;
38  
39  /**
40   * <p>
41   * DepartmentDaoImpl class.
42   * </p>
43   * 
44   * @see fr.ifremer.quadrige3.core.dao.administration.user.Department
45   */
46  @Repository("departmentDao")
47  @Lazy
48  public class DepartmentDaoImpl
49  		extends fr.ifremer.quadrige3.core.dao.administration.user.DepartmentDaoBase
50  		implements DepartmentExtendDao {
51  
52  	@Resource(name = "departmentJdbcDao")
53  	private DepartmentJdbcDao departmentJdbcDao;
54  
55  	/**
56  	 * Constructor used by Spring
57  	 * 
58  	 * @param sessionFactory
59  	 *            a {@link org.hibernate.SessionFactory} object.
60  	 */
61  	@Autowired
62  	public DepartmentDaoImpl(SessionFactory sessionFactory) {
63  		super();
64  		setSessionFactory(sessionFactory);
65  	}
66  
67  	/** {@inheritDoc} */
68  	@Override
69  	public DepartmentVO save(DepartmentVO bean) {
70  		Assert.notNull(bean, "no bean to save");
71  
72  		Department entity = null;
73  		if (bean.getDepId() != null) {
74  			entity = load(bean.getDepId());
75  		}
76  
77  		// not exists on DB: create it
78  		if (entity == null) {
79  			entity = Department.Factory.newInstance();
80  
81  			// Convert to entity
82  			toEntity(bean, entity);
83  
84  			// Update
85  			entity = create(entity);
86  		} else {
87  			entity = load(bean.getDepId());
88  
89  			// Convert to entity
90  			toEntity(bean, entity);
91  
92  			// Update
93  			update(entity);
94  		}
95  
96  		// Refresh the id
97  		bean.setDepId(entity.getDepId());
98  
99  		// Refresh the parent department id (could have been reset, if not exist yet in local DB)
100 		if (entity.getParentDepartment() == null) {
101 			bean.setParentDepartmentId(null);
102 		} else {
103 			bean.setParentDepartmentId(entity.getParentDepartment().getDepId());
104 		}
105 
106 		return bean;
107 	}
108 
109 	@Override
110 	public List<Integer> getInheritedRecorderDepartmentIds(int departmentId) {
111 		return departmentJdbcDao.getInheritedRecorderDepartmentIdsFrom(departmentId, new Date());
112 	}
113 
114 	/** {@inheritDoc} */
115 	@Override
116 	public Department departmentVOToEntity(DepartmentVO source) {
117 		Department target;
118 		if (source.getDepId() != null) {
119 			target = load(source.getDepId());
120 		}
121 		else {
122 			target = Department.Factory.newInstance();
123 		}
124 
125 		toEntity(source, target);
126 
127 		return target;
128 	}
129 
130 	/* -- Internal methods -- */
131 
132 	/**
133 	 * <p>
134 	 * toEntity.
135 	 * </p>
136 	 * 
137 	 * @param source
138 	 *            a {@link fr.ifremer.quadrige3.core.vo.administration.user.DepartmentVO} object.
139 	 * @param target
140 	 *            a {@link fr.ifremer.quadrige3.core.dao.administration.user.Department} object.
141 	 */
142 	protected void toEntity(DepartmentVO source, Department target) {
143 		try {
144 			target.setDepId(source.getDepId());
145 			target.setDepCd(source.getDepCd());
146 			target.setDepNm(source.getDepNm());
147 			target.setDepLdapPresent(source.getDepLdapPresent());
148 			target.setDepCreationDt(source.getDepCreationDt());
149 			target.setUpdateDt(source.getUpdateDt());
150 			target.setStatus(load(StatusImpl.class, source.getStatusCd()));
151 			target.setDepEMail(source.getDepEMail());
152 			target.setDepAddress(source.getDepAddress());
153 			target.setDepPhone(source.getDepPhone());
154 
155 			// Parent department
156 			if (source.getParentDepartmentId() == null) {
157 				target.setParentDepartment(null);
158 			} else {
159 				// use a get (and NOT a load), to avoid error if parent not exists yet in local database
160 				Department parentDepartment = load(source.getParentDepartmentId());
161 				target.setParentDepartment(parentDepartment);
162 			}
163 
164 		} catch (Exception e) {
165 			throw new QuadrigeTechnicalException("Error while transform DepartmentVO bean into Department entity", e);
166 		}
167 	}
168 }