| 1 | /* |
| 2 | * Copyright 2002-2013 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | * the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | * specific language governing permissions and limitations under the License. |
| 12 | */ |
| 13 | package org.springframework.batch.item; |
| 14 | |
| 15 | import java.util.List; |
| 16 | |
| 17 | import org.springframework.beans.factory.InitializingBean; |
| 18 | import org.springframework.core.convert.converter.Converter; |
| 19 | import org.springframework.util.Assert; |
| 20 | |
| 21 | /** |
| 22 | * A base class to implement any {@link ItemWriter} that writes to a key value store |
| 23 | * using a {@link Converter} to derive a key from an item |
| 24 | * |
| 25 | * @author David Turanski |
| 26 | * @since 2.2 |
| 27 | * |
| 28 | */ |
| 29 | public abstract class KeyValueItemWriter<K, V> implements ItemWriter<V>, InitializingBean { |
| 30 | |
| 31 | protected Converter<V, K> itemKeyMapper; |
| 32 | protected boolean delete; |
| 33 | |
| 34 | /* (non-Javadoc) |
| 35 | * @see org.springframework.batch.item.ItemWriter#write(java.util.List) |
| 36 | */ |
| 37 | @Override |
| 38 | public void write(List<? extends V> items) throws Exception { |
| 39 | if (items == null) { |
| 40 | return; |
| 41 | } |
| 42 | for (V item : items) { |
| 43 | K key = itemKeyMapper.convert(item); |
| 44 | writeKeyValue(key, item); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Subclasses implement this method to write each item to key value store |
| 50 | * @param key the key |
| 51 | * @param value the item |
| 52 | */ |
| 53 | protected abstract void writeKeyValue(K key, V value); |
| 54 | |
| 55 | /** |
| 56 | * afterPropertiesSet() hook |
| 57 | */ |
| 58 | protected abstract void init(); |
| 59 | |
| 60 | /** |
| 61 | * Set the {@link Converter} to use to derive the key from the item |
| 62 | * @param itemKeyMapper |
| 63 | */ |
| 64 | public void setItemKeyMapper(Converter<V, K> itemKeyMapper) { |
| 65 | this.itemKeyMapper = itemKeyMapper; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Sets the delete flag to have the item writer perform deletes |
| 70 | * @param delete |
| 71 | */ |
| 72 | public void setDelete(boolean delete) { |
| 73 | this.delete = delete; |
| 74 | } |
| 75 | |
| 76 | /* (non-Javadoc) |
| 77 | * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() |
| 78 | */ |
| 79 | @Override |
| 80 | public void afterPropertiesSet() throws Exception { |
| 81 | Assert.notNull(itemKeyMapper, "itemKeyMapper requires a Converter type."); |
| 82 | init(); |
| 83 | } |
| 84 | } |