View Javadoc
1   package net.sumaris.core.extraction.dao.technical;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Core Extraction
6    * %%
7    * Copyright (C) 2018 - 2019 SUMARiS Consortium
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 com.google.common.base.Joiner;
26  import net.sumaris.core.util.Dates;
27  
28  import java.util.Collection;
29  import java.util.Date;
30  import java.util.Objects;
31  import java.util.stream.Collectors;
32  
33  /**
34   * Useful method around DAO and entities.
35   *
36   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
37   */
38  public class Daos extends net.sumaris.core.dao.technical.Daos {
39  
40  
41      private final static String SQL_TO_DATE = "TO_DATE('%s', '%s')";
42  
43      /**
44       * Concat single quoted strings with ',' character, without parenthesis
45       *
46       * @param strings a {@link Collection} object.
47       * @return concatenated strings
48       */
49      public static String getSqlInValueFromStringCollection(Collection<String> strings) {
50          if (strings == null) return "";
51          return Joiner.on(',').skipNulls().join(strings.stream().filter(Objects::nonNull).map(s -> "'" + s + "'").collect(Collectors.toSet()));
52      }
53  
54      /**
55       * Concat integers with ',' character, without parenthesis
56       *
57       * @param integers a {@link Collection} object.
58       * @return concatenated integers
59       */
60      public static String getSqlInValueFromIntegerCollection(Collection<Integer> integers) {
61          if (integers == null) return "";
62          return Joiner.on(',').skipNulls().join(integers.stream().filter(Objects::nonNull).collect(Collectors.toSet()));
63      }
64  
65  
66      public static String getSqlToDate(Date date) {
67          if (date == null) return null;
68          return String.format(SQL_TO_DATE,
69                  Dates.formatDate(date, "yyyy-MM-dd HH:mm:ss"),
70                  "YYYY-MM-DD HH24:MI:SS");
71      }
72  }