View Javadoc
1   /*
2    * #%L
3    * SUMARiS
4    * %%
5    * Copyright (C) 2019 SUMARiS Consortium
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as
9    * published by the Free Software Foundation, either version 3 of the
10   * License, or (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/gpl-3.0.html>.
20   * #L%
21   */
22  
23  package net.sumaris.rdf.dao;
24  
25  import com.google.common.collect.Maps;
26  import net.sumaris.core.dao.referential.ReferentialDaoImpl;
27  import net.sumaris.core.dao.technical.hibernate.HibernateDaoSupport;
28  import net.sumaris.core.util.StringUtils;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
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.PostConstruct;
36  import javax.persistence.TypedQuery;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.stream.Stream;
40  
41  @Repository("rdfModelDao")
42  @Lazy
43  public class RdfModelDaoImpl extends HibernateDaoSupport implements RdfModelDao {
44  
45      private static final Logger log = LoggerFactory.getLogger(RdfModelDaoImpl.class);
46  
47      protected Map<String, String> selectQueriesByName = Maps.newHashMap();
48  
49      @Autowired
50      protected ReferentialDaoImpl referentialDao;
51  
52      @Override
53      public <T> Stream<T> streamAll(String entityName, Class<T> aClass) {
54  
55          String hql = getSelectHqlQuery(entityName);
56          TypedQuery<T> typedQuery = entityManager.createQuery(hql, aClass)
57                  .setMaxResults(20000);
58          boolean queryHasFetchJoins = hql.indexOf(" fetch ") != -1;
59  
60          // When using fetch join, stream are not supported, so use a list
61          if (queryHasFetchJoins) {
62              return typedQuery.getResultList().stream();
63          }
64  
65          return typedQuery.getResultStream();
66      }
67  
68      @Override
69      public <T> List<T> loadAll(String entityName, Class<T> aClass) {
70  
71          String hql = getSelectHqlQuery(entityName);
72          return entityManager.createQuery(hql, aClass)
73                  .setMaxResults(20000)
74                  .getResultList();
75      }
76  
77      /* -- protected methods -- */
78  
79      protected String getSelectHqlQuery(String entityName) {
80          String hqlQuery = selectQueriesByName.get(entityName.toLowerCase());
81          if (StringUtils.isBlank(hqlQuery)) {
82              throw new IllegalArgumentException(String.format("Referential with name %s not exists", entityName));
83          }
84          return hqlQuery;
85      }
86  
87      @PostConstruct
88      protected void fillNamedQueries() {
89          selectQueriesByName.put("taxon", "select tn from TaxonName as tn" +
90                  " left join fetch tn.taxonomicLevel as tl" +
91                  " join fetch tn.referenceTaxon as rt" +
92                  " join fetch tn.status st" +
93                  " where tn.updateDate > '2015-01-01 23:59:50'");
94          selectQueriesByName.put("transcription", "select ti from TranscribingItem ti" +
95                  " join fetch ti.type tit " +
96                  " join fetch ti.status st");
97          selectQueriesByName.put("location", "select l from Location l " +
98                  " join fetch l.locationLevel ll " +
99                  " join fetch l.validityStatus vs " +
100                 " join fetch l.status st");
101         selectQueriesByName.put("gear", "select g from Gear g " +
102                 " join fetch g.gearClassification gl " +
103                 " join fetch g.strategies s " +
104                 " join fetch g.status st");
105 
106 
107         // Add missing query, from referential entity names
108         referentialDao.getAllTypes().forEach(type -> {
109             String entityName = type.getId();
110             selectQueriesByName.putIfAbsent(entityName.toLowerCase(), "from " + entityName);
111         });
112 
113     }
114 
115 
116 }