| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| HSQLImporter |
|
| 1.6666666666666667;1.667 | ||||
| HSQLImporter$1 |
|
| 1.6666666666666667;1.667 | ||||
| HSQLImporter$HSQLMatcher |
|
| 1.6666666666666667;1.667 |
| 1 | package org.jtheque.core.managers.schema; | |
| 2 | ||
| 3 | import org.jtheque.core.managers.Managers; | |
| 4 | import org.jtheque.core.managers.beans.IBeansManager; | |
| 5 | import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; | |
| 6 | ||
| 7 | import javax.annotation.Resource; | |
| 8 | import java.util.Arrays; | |
| 9 | import java.util.HashMap; | |
| 10 | import java.util.Map; | |
| 11 | ||
| 12 | /* | |
| 13 | * This file is part of JTheque. | |
| 14 | * | |
| 15 | * JTheque is free software: you can redistribute it and/or modify | |
| 16 | * it under the terms of the GNU General Public License as published by | |
| 17 | * the Free Software Foundation, either version 3 of the License. | |
| 18 | * | |
| 19 | * JTheque is distributed in the hope that it will be useful, | |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 22 | * GNU General Public License for more details. | |
| 23 | * | |
| 24 | * You should have received a copy of the GNU General Public License | |
| 25 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
| 26 | */ | |
| 27 | ||
| 28 | /** | |
| 29 | * An importer for HSQL. It make generic the process of import from HSQL File inserts with a little mapping. | |
| 30 | * | |
| 31 | * @author Baptiste Wicht | |
| 32 | */ | |
| 33 | 0 | public final class HSQLImporter { |
| 34 | @Resource | |
| 35 | private SimpleJdbcTemplate jdbcTemplate; | |
| 36 | ||
| 37 | 0 | private final Map<String, HSQLMatcher> matchers = new HashMap<String, HSQLMatcher>(20); |
| 38 | ||
| 39 | /** | |
| 40 | * Construct a new HSQLImporter. | |
| 41 | */ | |
| 42 | public HSQLImporter() { | |
| 43 | 0 | super(); |
| 44 | ||
| 45 | 0 | Managers.getManager(IBeansManager.class).inject(this); |
| 46 | 0 | } |
| 47 | ||
| 48 | /** | |
| 49 | * Import the inserts. | |
| 50 | * | |
| 51 | * @param inserts The inserts to add | |
| 52 | */ | |
| 53 | public void importInserts(Iterable<Insert> inserts) { | |
| 54 | 0 | for (Insert insert : inserts) { |
| 55 | 0 | matchers.get(insert.getTable()).exec(insert); |
| 56 | } | |
| 57 | 0 | } |
| 58 | ||
| 59 | /** | |
| 60 | * Match the table to the specific request. | |
| 61 | * | |
| 62 | * @param table The table in HSQL. | |
| 63 | * @param request The request to use to insert to current database. | |
| 64 | * @param positions The positions to use to fill the request. | |
| 65 | */ | |
| 66 | public void match(String table, String request, int... positions) { | |
| 67 | 0 | matchers.put(table, new HSQLMatcher(request, positions, null)); |
| 68 | 0 | } |
| 69 | ||
| 70 | /** | |
| 71 | * Match the table to the specific request. | |
| 72 | * | |
| 73 | * @param table The table in HSQL. | |
| 74 | * @param request The request to use to insert to current database. | |
| 75 | * @param positions The positions to use to fill the request. | |
| 76 | * @param append An object to append to the list of params. | |
| 77 | */ | |
| 78 | public void match(String table, String request, Object append, int... positions) { | |
| 79 | 0 | matchers.put(table, new HSQLMatcher(request, positions, append)); |
| 80 | 0 | } |
| 81 | ||
| 82 | /** | |
| 83 | * An object to match a request with the arguments of a HSQL insert. | |
| 84 | * | |
| 85 | * @author Baptiste Wicht | |
| 86 | */ | |
| 87 | 0 | private final class HSQLMatcher { |
| 88 | private final String request; | |
| 89 | private final int[] positions; | |
| 90 | private final Object append; | |
| 91 | ||
| 92 | /** | |
| 93 | * Construct a new <code>HSQLMatcher</code>. | |
| 94 | * | |
| 95 | * @param request The request to insert in the current database. | |
| 96 | * @param positions The positions to get the informations from the HSQL insert. | |
| 97 | * @param append The object to append to the end of the params. | |
| 98 | */ | |
| 99 | 0 | private HSQLMatcher(String request, int[] positions, Object append) { |
| 100 | 0 | super(); |
| 101 | ||
| 102 | 0 | this.request = request; |
| 103 | 0 | this.positions = Arrays.copyOf(positions, positions.length); |
| 104 | 0 | this.append = append; |
| 105 | 0 | } |
| 106 | ||
| 107 | /** | |
| 108 | * Exec the insert. | |
| 109 | * | |
| 110 | * @param insert The HSQL Insert to get the informations from. | |
| 111 | */ | |
| 112 | public void exec(Insert insert) { | |
| 113 | 0 | Object[] parameters = new Object[append == null ? positions.length : positions.length + 1]; |
| 114 | ||
| 115 | 0 | for (int i = 0; i < positions.length; i++) { |
| 116 | 0 | parameters[i] = insert.getString(positions[i]); |
| 117 | } | |
| 118 | ||
| 119 | 0 | if (append == null) { |
| 120 | 0 | parameters[parameters.length - 1] = append; |
| 121 | } | |
| 122 | ||
| 123 | 0 | jdbcTemplate.update(request, parameters); |
| 124 | 0 | } |
| 125 | } | |
| 126 | } |