|
| 1 | +/* |
| 2 | + * Copyright (C) 2009-2018 Slava Semushin <[email protected]> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | + */ |
| 18 | +package ru.mystamps.web.feature.series.importing.extractor; |
| 19 | + |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.apache.commons.lang3.Validate; |
| 25 | + |
| 26 | +import org.springframework.beans.factory.annotation.Value; |
| 27 | +import org.springframework.dao.EmptyResultDataAccessException; |
| 28 | +import org.springframework.jdbc.core.ResultSetExtractor; |
| 29 | +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; |
| 30 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
| 31 | +import org.springframework.jdbc.support.GeneratedKeyHolder; |
| 32 | +import org.springframework.jdbc.support.KeyHolder; |
| 33 | + |
| 34 | +import lombok.RequiredArgsConstructor; |
| 35 | + |
| 36 | +@RequiredArgsConstructor |
| 37 | +public class JdbcSiteParserDao implements SiteParserDao { |
| 38 | + |
| 39 | + private static final ResultSetExtractor<Map<String, String>> PARAMS_EXTRACTOR = |
| 40 | + new MapResultSetExtractor("name", "value"); |
| 41 | + |
| 42 | + private final NamedParameterJdbcTemplate jdbcTemplate; |
| 43 | + |
| 44 | + @Value("${site_parser.create}") |
| 45 | + private String addParserSql; |
| 46 | + |
| 47 | + @Value("${site_parser_param.create}") |
| 48 | + private String addParserParameterSql; |
| 49 | + |
| 50 | + @Value("${site_parser.find_like_matched_url}") |
| 51 | + private String findParserIdByMatchedUrl; |
| 52 | + |
| 53 | + @SuppressWarnings("PMD.LongVariable") |
| 54 | + @Value("${site_parser_param.find_all_with_parser_name}") |
| 55 | + private String findParametersWithParserNameSql; |
| 56 | + |
| 57 | + @Override |
| 58 | + public Integer addParser(String name) { |
| 59 | + KeyHolder holder = new GeneratedKeyHolder(); |
| 60 | + |
| 61 | + int affected = jdbcTemplate.update( |
| 62 | + addParserSql, |
| 63 | + new MapSqlParameterSource("name", name), |
| 64 | + holder |
| 65 | + ); |
| 66 | + |
| 67 | + Validate.validState( |
| 68 | + affected == 1, |
| 69 | + "Unexpected number of affected rows after creation of site parser: %d", |
| 70 | + affected |
| 71 | + ); |
| 72 | + |
| 73 | + return holder.getKey().intValue(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void addParserParameter(AddParserParameterDbDto param) { |
| 78 | + Map<String, Object> params = new HashMap<>(); |
| 79 | + params.put("parser_id", param.getParserId()); |
| 80 | + params.put("name", param.getName()); |
| 81 | + params.put("value", param.getValue()); |
| 82 | + |
| 83 | + int affected = jdbcTemplate.update(addParserParameterSql, params); |
| 84 | + |
| 85 | + Validate.validState( |
| 86 | + affected == 1, |
| 87 | + "Unexpected number of affected rows after adding parser parameter: %d", |
| 88 | + affected |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public Integer findParserIdForUrl(String url) { |
| 94 | + try { |
| 95 | + return jdbcTemplate.queryForObject( |
| 96 | + findParserIdByMatchedUrl, |
| 97 | + Collections.singletonMap("url", url), |
| 98 | + Integer.class |
| 99 | + ); |
| 100 | + } catch (EmptyResultDataAccessException ignored) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public SiteParserConfiguration findConfigurationForParser(Integer parserId) { |
| 107 | + Map<String, String> params = jdbcTemplate.query( |
| 108 | + findParametersWithParserNameSql, |
| 109 | + Collections.singletonMap("parser_id", parserId), |
| 110 | + PARAMS_EXTRACTOR |
| 111 | + ); |
| 112 | + |
| 113 | + return new SiteParserConfiguration(params); |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments