1 package net.sumaris.core.extraction.dao.technical;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
120
121
122 protected XMLQuery createXMLQuery() {
123 return applicationContext.getBean("xmlQuery", XMLQuery.class);
124 }
125
126
127 }