View Javadoc
1   package fr.ifremer.quadrige3.core.dao.referential.transcribing;
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 org.apache.commons.lang3.StringUtils;
27  import org.hibernate.SessionFactory;
28  import org.hibernate.type.IntegerType;
29  import org.hibernate.type.StringType;
30  import org.springframework.beans.factory.annotation.Autowired;
31  import org.springframework.context.annotation.Lazy;
32  import org.springframework.stereotype.Repository;
33  
34  import java.util.HashMap;
35  import java.util.Iterator;
36  import java.util.Map;
37  
38  /**
39   * <p>
40   * TranscribingItemDaoImpl class.
41   * </p>
42   * 
43   * @see fr.ifremer.quadrige3.core.dao.referential.transcribing.TranscribingItem
44   */
45  @Repository("transcribingItemDao")
46  @Lazy
47  public class TranscribingItemDaoImpl
48  		extends TranscribingItemDaoBase
49  		implements TranscribingItemExtendDao
50  {
51  	/**
52  	 * Constructor used by Spring
53  	 * 
54  	 * @param sessionFactory
55  	 *            a {@link org.hibernate.SessionFactory} object.
56  	 */
57  	@Autowired
58  	public TranscribingItemDaoImpl(SessionFactory sessionFactory) {
59  		super();
60  		setSessionFactory(sessionFactory);
61  	}
62  
63  	/** {@inheritDoc} */
64  	@Override
65  	public Map<Integer, String> getAllTranscribingItemsById(String transcribingItemTypeLabel) {
66  		Map<Integer, String> result = new HashMap<>();
67  
68  		if (StringUtils.isBlank(transcribingItemTypeLabel)) return result;
69  
70  		Iterator<Object[]> rows = queryIterator("allTranscribingById",
71  				"transcItemTypeLb", StringType.INSTANCE, transcribingItemTypeLabel);
72  
73  		while (rows.hasNext()) {
74  			Object[] cols = rows.next();
75  			Integer objectId = (Integer) cols[0];
76  			String externalCode = (String) cols[1];
77  			result.put(objectId, externalCode);
78  		}
79  
80  		return result;
81  	}
82  
83  	@Override
84  	public Map<String, String> getAllTranscribingItemsByCode(String transcribingItemTypeLabel) {
85  		Map<String, String> result = new HashMap<>();
86  
87  		if (StringUtils.isBlank(transcribingItemTypeLabel)) return result;
88  
89  		Iterator<Object[]> rows = queryIterator("allTranscribingByCode",
90  				"transcItemTypeLb", StringType.INSTANCE, transcribingItemTypeLabel);
91  
92  		while (rows.hasNext()) {
93  			Object[] cols = rows.next();
94  			String objectCode = (String) cols[0];
95  			String externalCode = (String) cols[1];
96  			result.put(objectCode, externalCode);
97  		}
98  
99  		return result;
100 	}
101 
102 	/** {@inheritDoc} */
103 	@Override
104 	public String getTranscribingItemById(String transcribingItemTypeLabel, int objectId) {
105 
106 		if (StringUtils.isBlank(transcribingItemTypeLabel)) return null;
107 
108 		Iterator<String> rows = queryIteratorTyped("transcribingByTypeAndObjectId",
109 				"transcItemTypeLb", StringType.INSTANCE, transcribingItemTypeLabel,
110 				"objectId", IntegerType.INSTANCE, objectId);
111 
112 		if (rows.hasNext()) {
113 			return rows.next();
114 		}
115 
116 		return null;
117 	}
118 
119 	@Override
120 	public String getTranscribingItemByCode(String transcribingItemTypeLabel, String objectCode) {
121 
122 		if (StringUtils.isBlank(transcribingItemTypeLabel) || StringUtils.isBlank(objectCode)) return null;
123 
124 		Iterator<String> rows = queryIteratorTyped("transcribingByTypeAndObjectCode",
125 				"transcItemTypeLb", StringType.INSTANCE, transcribingItemTypeLabel,
126 				"objectCode", StringType.INSTANCE, objectCode);
127 
128 		if (rows.hasNext()) {
129 			return rows.next();
130 		}
131 
132 		return null;
133 	}
134 }