View Javadoc
1   package fr.ifremer.tutti.persistence.dao;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2016 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (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 General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import com.google.common.base.Function;
28  import com.google.common.collect.Multimap;
29  import com.google.common.collect.Multimaps;
30  import fr.ifremer.adagio.core.dao.referential.taxon.TaxonNameDaoImpl;
31  import fr.ifremer.adagio.core.dao.referential.taxon.TaxonRefVO;
32  import org.hibernate.Query;
33  import org.hibernate.Session;
34  import org.hibernate.SessionFactory;
35  import org.springframework.beans.factory.annotation.Autowired;
36  import org.springframework.context.annotation.Lazy;
37  import org.springframework.stereotype.Repository;
38  
39  import java.util.ArrayList;
40  import java.util.Collection;
41  import java.util.Iterator;
42  import java.util.List;
43  
44  /**
45   * Created on 16/01/16.
46   *
47   * @author Tony Chemit - chemit@codelutin.com
48   */
49  @Repository("taxonNameDaoTutti")
50  @Lazy
51  public class TaxonNameDaoTuttiImpl extends TaxonNameDaoImpl implements TaxonNameDaoTutti {
52  
53      @Autowired
54      public TaxonNameDaoTuttiImpl(SessionFactory sessionFactory) {
55          super(sessionFactory);
56      }
57  
58      @Override
59      public TaxonRefVO[] getAllTaxonNamesWithObsoletes(boolean withSynonyms, Integer transcribingId) {
60  
61          Session session = getSession();
62  
63          Query query;
64  
65          boolean withTranscribing = transcribingId != null;
66  
67          if (withSynonyms) {
68              query = session.getNamedQuery("allTaxonNamesWithObsoletes");
69          } else {
70              query = session.getNamedQuery("allTaxonNamesIsReferentWithObsoletes");
71          }
72  
73          List<TaxonRefVO> results = new ArrayList<>();
74          for (Iterator<Object[]> iterator = query.iterate(); iterator.hasNext(); ) {
75              Object[] cols = iterator.next();
76              TaxonRefVO taxonNameRefTaxVO = loadTaxon(cols, false);
77              results.add(taxonNameRefTaxVO);
78          }
79          if (results.size() == 0) {
80              return null;
81          }
82  
83          if (withTranscribing) {
84              query = session.getNamedQuery("allTranscribingForAType");
85              query.setInteger("transcribingTypeId", transcribingId);
86  
87              Multimap<Integer, TaxonRefVO> r = Multimaps.index(results, TaxonRefVO::getReferenceTaxonId);
88  
89              for (Iterator<Object[]> iterator = query.iterate(); iterator.hasNext(); ) {
90                  Object[] cols = iterator.next();
91                  Integer referencetaxonId = (Integer) cols[0];
92                  String externalCode = (String) cols[1];
93                  Collection<TaxonRefVO> taxonRefVOs = r.get(referencetaxonId);
94                  if (taxonRefVOs != null)
95                      for (TaxonRefVO taxonRefVO : taxonRefVOs) {
96                          taxonRefVO.setExternalCode(externalCode);
97                      }
98              }
99          }
100 
101         return results.toArray(new TaxonRefVO[results.size()]);
102 
103     }
104 }