001/*
002 * Copyright 2006-2007 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.batch.sample.domain.order.internal.mapper;
018
019import org.springframework.batch.item.file.mapping.FieldSetMapper;
020import org.springframework.batch.item.file.transform.FieldSet;
021import org.springframework.batch.sample.domain.order.Customer;
022
023public class CustomerFieldSetMapper implements FieldSetMapper<Customer> {
024
025        public static final String LINE_ID_COLUMN = "LINE_ID";
026        public static final String COMPANY_NAME_COLUMN = "COMPANY_NAME";
027        public static final String LAST_NAME_COLUMN = "LAST_NAME";
028        public static final String FIRST_NAME_COLUMN = "FIRST_NAME";
029        public static final String MIDDLE_NAME_COLUMN = "MIDDLE_NAME";
030        public static final String TRUE_SYMBOL = "T";
031        public static final String REGISTERED_COLUMN = "REGISTERED";
032        public static final String REG_ID_COLUMN = "REG_ID";
033        public static final String VIP_COLUMN = "VIP";
034
035        @Override
036        public Customer mapFieldSet(FieldSet fieldSet) {
037                Customer customer = new Customer();
038
039                if (Customer.LINE_ID_BUSINESS_CUST.equals(fieldSet.readString(LINE_ID_COLUMN))) {
040                        customer.setCompanyName(fieldSet.readString(COMPANY_NAME_COLUMN));
041                        // business customer must be always registered
042                        customer.setRegistered(true);
043                }
044
045                if (Customer.LINE_ID_NON_BUSINESS_CUST.equals(fieldSet.readString(LINE_ID_COLUMN))) {
046                        customer.setLastName(fieldSet.readString(LAST_NAME_COLUMN));
047                        customer.setFirstName(fieldSet.readString(FIRST_NAME_COLUMN));
048                        customer.setMiddleName(fieldSet.readString(MIDDLE_NAME_COLUMN));
049                        customer.setRegistered(TRUE_SYMBOL.equals(fieldSet.readString(REGISTERED_COLUMN)));
050                }
051
052                customer.setRegistrationId(fieldSet.readLong(REG_ID_COLUMN));
053                customer.setVip(TRUE_SYMBOL.equals(fieldSet.readString(VIP_COLUMN)));
054
055                return customer;
056        }
057}