001/*
002 * Copyright 2002-2013 the original author or authors.
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005 * the License. You may obtain a copy of the License at
006 * 
007 * https://www.apache.org/licenses/LICENSE-2.0
008 * 
009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011 * specific language governing permissions and limitations under the License.
012 */
013package org.springframework.batch.item.data;
014
015import org.springframework.batch.item.ItemWriter;
016import org.springframework.batch.item.KeyValueItemWriter;
017import org.springframework.data.gemfire.GemfireOperations;
018import org.springframework.data.gemfire.GemfireTemplate;
019import org.springframework.util.Assert;
020
021/**
022 * An {@link ItemWriter} that stores items in GemFire
023 * 
024 * @author David Turanski
025 * @since 2.2
026 *
027 */
028public class GemfireItemWriter<K,V> extends KeyValueItemWriter<K,V> {
029        private GemfireOperations gemfireTemplate;
030        /**
031         * @param gemfireTemplate the {@link GemfireTemplate} to set
032         */
033        public void setTemplate(GemfireTemplate gemfireTemplate) {
034                this.gemfireTemplate = gemfireTemplate;
035        }
036
037        /* (non-Javadoc)
038         * @see org.springframework.batch.item.KeyValueItemWriter#writeKeyValue(java.lang.Object, java.lang.Object)
039         */
040        @Override
041        protected void writeKeyValue(K key, V value) {
042                if (delete) {
043                        gemfireTemplate.remove(key);
044                } else {
045                        gemfireTemplate.put(key, value);
046                }
047        }
048
049        /* (non-Javadoc)
050         * @see org.springframework.batch.item.KeyValueItemWriter#init()
051         */
052        @Override
053        protected void init() {
054                Assert.notNull(gemfireTemplate, "A GemfireTemplate is required.");
055        }
056
057}