View Javadoc
1   // Generated by: hibernate/SpringHibernateDaoImpl.vsl in andromda-spring-cartridge.
2   // license-header java merge-point
3   //
4   // Attention: Generated code! Do not modify by hand!
5   // Generated by: SpringHibernateDaoImpl.vsl in andromda-spring-cartridge.
6   //
7   package fr.ifremer.quadrige3.core.dao.system.rule;
8   
9   /*-
10   * #%L
11   * Quadrige3 Core :: Client API
12   * %%
13   * Copyright (C) 2017 - 2018 Ifremer
14   * %%
15   * This program is free software: you can redistribute it and/or modify
16   * it under the terms of the GNU Affero General Public License as published by
17   * the Free Software Foundation, either version 3 of the License, or
18   * (at your option) any later version.
19   * 
20   * This program is distributed in the hope that it will be useful,
21   * but WITHOUT ANY WARRANTY; without even the implied warranty of
22   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   * GNU General Public License for more details.
24   * 
25   * You should have received a copy of the GNU Affero General Public License
26   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27   * #L%
28   */
29  import fr.ifremer.quadrige3.core.dao.technical.Assert;
30  import fr.ifremer.quadrige3.core.vo.system.rule.RulePreconditionVO;
31  import org.hibernate.SessionFactory;
32  import org.hibernate.type.StringType;
33  import org.springframework.beans.factory.annotation.Autowired;
34  import org.springframework.context.annotation.Lazy;
35  import org.springframework.stereotype.Repository;
36  
37  import java.sql.Timestamp;
38  import java.util.Collection;
39  import java.util.List;
40  
41  /**
42   * @see RulePrecondition
43   */
44  @Repository("rulePreconditionDao")
45  @Lazy
46  public class RulePreconditionDaoImpl
47      extends RulePreconditionDaoBase
48  	implements RulePreconditionExtendDao
49  {
50      /**
51       * Constructor used by Spring
52       */
53  	@Autowired
54  	public RulePreconditionDaoImpl(SessionFactory sessionFactory) {
55  		super();
56  		setSessionFactory(sessionFactory);
57  	}
58  
59  	@Override
60  	protected RulePreconditionVO handleSave(RulePreconditionVO source, Timestamp updateDt) {
61  		Assert.notNull(source);
62  		Assert.notNull(source.getRuleCd());
63  
64  		// Load parent
65  		Rule parent = get(RuleImpl.class, source.getRuleCd());
66  
67  		// Load entity
68  		RulePrecondition entity = null;
69  		boolean isNew = false;
70  		if (source.getRulePrecondId() != null) {
71  			entity = get(source.getRulePrecondId());
72  		}
73  		if (entity == null) {
74  			entity = RulePrecondition.Factory.newInstance();
75  			entity.setRule(parent);
76  			parent.getRulePreconditions().clear();
77  			parent.addRulePreconditions(entity);
78  			isNew = true;
79  		}
80  
81  		// Check update_dt :
82  		// NOT NEED on a client database
83  
84  		// Update update_dt
85  		// NOT NEED on a client database
86  
87  		// VO -> Entity
88  		rulePreconditionVOToEntity(source, entity, true);
89  
90  		// Save entity
91  		if (isNew) {
92  			getSession().save(entity);
93  			source.setRulePrecondId(entity.getRulePrecondId());
94  		} else {
95  			getSession().update(entity);
96  		}
97  
98  		return source;
99  	}
100 
101 	@Override
102 	public void toRulePreconditionVO(RulePrecondition source, RulePreconditionVO target) {
103 		super.toRulePreconditionVO(source, target);
104 
105 		// Base rule
106 		if (source.getRule() == null) {
107 			target.setRuleCd(null);
108 		} else {
109 			target.setRuleCd(source.getRule().getRuleCd());
110 		}
111 
112 		// Used rule
113 		if (source.getUsedRule() == null) {
114 			target.setUsedRuleCd(null);
115 		} else {
116 			target.setUsedRuleCd(source.getUsedRule().getRuleCd());
117 		}
118 	}
119 
120 	@Override
121 	protected void handleRemoveByIds(Collection<Integer> rulePreconditionIds) {
122 		rulePreconditionIds.forEach(this::remove);
123 	}
124 
125     @Override
126     public RulePrecondition rulePreconditionVOToEntity(RulePreconditionVO rulePreconditionVO) {
127         RulePrecondition entity = this.loadRulePreconditionFromRulePreconditionVO(rulePreconditionVO);
128         this.rulePreconditionVOToEntity(rulePreconditionVO, entity, true);
129         return entity;
130     }
131 
132 	@Override
133 	public void rulePreconditionVOToEntity(RulePreconditionVO source, RulePrecondition target, boolean copyIfNull) {
134 		super.rulePreconditionVOToEntity(source, target, copyIfNull);
135 
136 		// Id
137 		if (copyIfNull || source.getRulePrecondId() != null) {
138 			target.setRulePrecondId(source.getRulePrecondId());
139 		}
140 
141 		// Base rule
142 		if (copyIfNull || source.getRuleCd() != null) {
143 			if (source.getRuleCd() == null) {
144 				target.setRule(null);
145 			} else {
146 				target.setRule(load(RuleImpl.class, source.getRuleCd()));
147 			}
148 		}
149 
150 		// Used rule
151 		if (copyIfNull || source.getUsedRuleCd() != null) {
152 			if (source.getUsedRuleCd() == null) {
153 				target.setUsedRule(null);
154 			} else {
155 				target.setUsedRule(load(RuleImpl.class, source.getUsedRuleCd()));
156 			}
157 		}
158 	}
159 
160 	/**
161 	 * Retrieves the entity object that is associated with the specified value object
162 	 * from the object store. If no such entity object exists in the object store,
163 	 * a new, blank entity is created
164 	 */
165 	private RulePrecondition loadRulePreconditionFromRulePreconditionVO(RulePreconditionVO rulePreconditionVO) {
166 		RulePrecondition rulePrecondition = null;
167 		if (rulePreconditionVO.getRulePrecondId() != null) {
168 			rulePrecondition = this.get(rulePreconditionVO.getRulePrecondId());
169 		}
170 		if (rulePrecondition == null) {
171 			rulePrecondition = RulePrecondition.Factory.newInstance();
172 		}
173 		return rulePrecondition;
174 	}
175 
176 	@Override
177 	public List<RulePrecondition> getByCode(String code) {
178 		return queryListTyped("rulePreconditionsByCode", "code", StringType.INSTANCE, code);
179 	}
180 }