View Javadoc
1   package net.sumaris.core.extraction.dao.technical;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Client API
6    * %%
7    * Copyright (C) 2017 - 2018 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import net.sumaris.core.config.SumarisConfiguration;
26  import net.sumaris.core.dao.technical.hibernate.HibernateDaoSupport;
27  import net.sumaris.core.dao.technical.schema.SumarisDatabaseMetadata;
28  import net.sumaris.core.exception.SumarisTechnicalException;
29  import net.sumaris.core.model.referential.IItemReferentialEntity;
30  import net.sumaris.core.service.ServiceLocator;
31  import net.sumaris.core.service.referential.ReferentialService;
32  import org.apache.commons.collections4.CollectionUtils;
33  import org.jdom2.Element;
34  import org.jdom2.filter.Filters;
35  import org.jdom2.xpath.XPathFactory;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  import org.springframework.beans.factory.annotation.Autowired;
39  import org.springframework.context.ApplicationContext;
40  import org.springframework.dao.DataRetrievalFailureException;
41  
42  import javax.persistence.Query;
43  import java.util.List;
44  import java.util.Objects;
45  import java.util.Set;
46  import java.util.function.Function;
47  import java.util.stream.Collectors;
48  import java.util.stream.Stream;
49  
50  /**
51   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
52   */
53  public abstract class ExtractionBaseDaoImpl extends HibernateDaoSupport {
54  
55      private static final Logger log = LoggerFactory.getLogger(ExtractionBaseDaoImpl.class);
56  
57      protected static final String XML_QUERY_PATH = "xmlQuery";
58  
59      @Autowired
60      protected SumarisConfiguration configuration;
61  
62      @Autowired
63      protected ReferentialService referentialService;
64  
65      @Autowired
66      ApplicationContext applicationContext;
67  
68      @Autowired
69      public ExtractionBaseDaoImpl() {
70          super();
71      }
72  
73      @SuppressWarnings("unchecked")
74      protected <R> List<R> query(String query, Class<R> jdbcClass) {
75          Query nativeQuery = getEntityManager().createNativeQuery(query);
76          Stream<R> resultStream = (Stream<R>) nativeQuery.getResultStream().map(jdbcClass::cast);
77          return resultStream.collect(Collectors.toList());
78      }
79  
80      protected <R> List<R> query(String query, Function<Object[], R> rowMapper) {
81          Query nativeQuery = getEntityManager().createNativeQuery(query);
82          Stream<Object[]> resultStream = (Stream<Object[]>) nativeQuery.getResultStream();
83          return resultStream.map(rowMapper).collect(Collectors.toList());
84      }
85  
86      protected <R> List<R> query(String query, Function<Object[], R> rowMapper, int offset, int size) {
87          Query nativeQuery = getEntityManager().createNativeQuery(query)
88                  .setFirstResult(offset)
89                  .setMaxResults(size);
90          Stream<Object[]> resultStream = (Stream<Object[]>) nativeQuery.getResultStream();
91          return resultStream.map(rowMapper).collect(Collectors.toList());
92      }
93  
94  
95      protected int queryUpdate(String query) {
96          if (log.isDebugEnabled()) log.debug("aggregate: " + query);
97          Query nativeQuery = getEntityManager().createNativeQuery(query);
98          return nativeQuery.executeUpdate();
99      }
100 
101     protected long queryCount(String query) {
102         if (log.isDebugEnabled()) log.debug("aggregate: " + query);
103         Query nativeQuery = getEntityManager().createNativeQuery(query);
104         Object result = nativeQuery.getSingleResult();
105         if (result == null)
106             throw new DataRetrievalFailureException(String.format("query count result is null.\nquery: %s", query));
107         if (result instanceof Number) {
108             return ((Number) result).longValue();
109         } else {
110             throw new DataRetrievalFailureException(String.format("query count result is not a number: %s \nquery: %s", result, query));
111         }
112     }
113 
114     protected Integer getReferentialIdByUniqueLabel(Class<? extends IItemReferentialEntity> entityClass, String label) {
115         return referentialService.getIdByUniqueLabel(entityClass, label);
116     }
117 
118     /**
119      * Create a new XML Query
120      * @return
121      */
122     protected XMLQuery createXMLQuery() {
123         return applicationContext.getBean("xmlQuery", XMLQuery.class);
124     }
125 
126 
127 }