View Javadoc
1   /*
2    * Created on 29 juil. 2004
3    */
4   package net.sumaris.core.extraction.dao.technical;
5   
6   /*-
7    * #%L
8    * Quadrige3 Core :: Shared
9    * %%
10   * Copyright (C) 2017 - 2018 Ifremer
11   * %%
12   * This program is free software: you can redistribute it and/or modify
13   * it under the terms of the GNU General Public License as
14   * published by the Free Software Foundation, either version 3 of the
15   * License, or (at your option) any later version.
16   * 
17   * This program is distributed in the hope that it will be useful,
18   * but WITHOUT ANY WARRANTY; without even the implied warranty of
19   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   * GNU General Public License for more details.
21   * 
22   * You should have received a copy of the GNU General Public
23   * License along with this program.  If not, see
24   * <http://www.gnu.org/licenses/gpl-3.0.html>.
25   * #L%
26   */
27  
28  import com.google.common.base.Preconditions;
29  import fr.ifremer.common.xmlquery.HSQLDBSingleXMLQuery;
30  import net.sumaris.core.exception.SumarisTechnicalException;
31  import net.sumaris.core.extraction.dao.technical.xml.XPaths;
32  import org.apache.commons.collections4.CollectionUtils;
33  import org.apache.commons.collections4.Predicate;
34  import org.jdom2.Attribute;
35  import org.jdom2.Element;
36  import org.jdom2.filter.Filters;
37  import org.jdom2.xpath.XPathFactory;
38  import org.springframework.context.annotation.Scope;
39  import org.springframework.stereotype.Component;
40  
41  import java.util.LinkedHashSet;
42  import java.util.List;
43  import java.util.Objects;
44  import java.util.Set;
45  import java.util.stream.Collectors;
46  
47  /**
48   * @author ludovic.pecquot@e-is.pro
49   */
50  @Component("xmlQuery")
51  @Scope("prototype")
52  public class XMLQuery extends HSQLDBSingleXMLQuery {
53  
54      // let default values here for HSQLDB
55  
56      /**
57       * Get column names, with type="hidden"
58       *
59       * @return
60       */
61      public Set<String> getHiddenColumnNames() {
62  
63          try {
64              List<Element> selectElements = XPaths.compile("//query/select[contains(@type, 'hidden')]", Filters.element())
65                      .evaluate(getDocument());
66              if (CollectionUtils.isEmpty(selectElements)) return null;
67  
68              return selectElements.stream()
69                      .map(element -> element.getAttribute("alias"))
70                      .filter(Objects::nonNull)
71                      .map(attribute -> attribute.getValue())
72                      .filter(Objects::nonNull)
73                      .map(String::toLowerCase)
74                      .collect(Collectors.toCollection(LinkedHashSet::new));
75          } catch (Exception e) {
76              throw new SumarisTechnicalException(e);
77          }
78      }
79  
80      public Set<String> getVisibleColumnNames() {
81          return getColumnNames(element -> {
82              Attribute typeAttr = element.getAttribute("type");
83              return typeAttr == null || !"hidden".equalsIgnoreCase(typeAttr.getValue());
84          });
85      }
86  
87      public Set<String> getNotNumericColumnNames() {
88          return getColumnNames(element -> {
89              Attribute typeAttr = element.getAttribute("type");
90              return typeAttr == null || !"number".equalsIgnoreCase(typeAttr.getValue());
91          });
92      }
93  
94      public Set<String> getColumnNames(final Predicate<Element> filter) {
95          Preconditions.checkNotNull(filter);
96  
97          try {
98              List<Element> selectElements = XPaths.compile("//query/select", Filters.element())
99                      .evaluate(getDocument());
100             if (CollectionUtils.isEmpty(selectElements)) return null;
101 
102             return selectElements.stream()
103                     // Apply filter
104                     .filter(filter::evaluate)
105                     // Get alias
106                     .map(element -> element.getAttribute("alias"))
107                     .filter(Objects::nonNull)
108                     .map(attribute -> attribute.getValue())
109                     .filter(Objects::nonNull)
110                     .map(String::toLowerCase)
111                     .collect(Collectors.toCollection(LinkedHashSet::new));
112         } catch (Exception e) {
113             throw new SumarisTechnicalException(e);
114         }
115     }
116 
117     /**
118      * Return if option="DISTINCT" has been set on the query
119      *
120      * @return
121      */
122     public boolean hasDistinctOption() {
123         Attribute optionAtr = getFirstQueryTag().getAttribute("option");
124         return optionAtr != null && "distinct".equalsIgnoreCase(optionAtr.getValue());
125     }
126 }