Java tutorial
/* * Copyright (c) 2010 mobiaware.com. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. */ package com.mobiaware.auction.provider.impl; import java.io.IOException; import java.io.Reader; import java.util.Arrays; import java.util.List; import javax.inject.Inject; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import au.com.bytecode.opencsv.CSVParser; import au.com.bytecode.opencsv.CSVReader; import com.google.common.base.Throwables; import com.mobiaware.auction.auth.PasswordService; import com.mobiaware.auction.auth.Roles; import com.mobiaware.auction.data.DataService; import com.mobiaware.auction.model.Item; import com.mobiaware.auction.model.User; import com.mobiaware.auction.provider.ImportService; public class CSVImportService implements ImportService { private static final String NAME = CSVImportService.class.getSimpleName(); private static final Logger LOG = LoggerFactory.getLogger(NAME); private final DataService _dataService; private final PasswordService _passwordService; @Inject public CSVImportService(final DataService dataService, final PasswordService passwordService) { _dataService = dataService; _passwordService = passwordService; } @Override public void importItems(final int auctionUid, final Reader reader) throws IOException { CSVReader cvsreader = null; try { cvsreader = new CSVReader(reader, CSVParser.DEFAULT_SEPARATOR, CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER, 0, false, false); List<String> headers = Arrays.asList(cvsreader.readNext()); String[] nextLine; while ((nextLine = cvsreader.readNext()) != null) { _dataService.editItem(Item.newBuilder().setAuctionUid(auctionUid) .setItemNumber(nextLine[headers.indexOf("number")]) .setName(nextLine[headers.indexOf("name")]) .setDescription(nextLine[headers.indexOf("description")]) .setCategory(nextLine[headers.indexOf("category")]) .setSeller(nextLine[headers.indexOf("seller")]) .setValPrice(NumberUtils.toDouble(nextLine[headers.indexOf("value")])) .setMinPrice(NumberUtils.toDouble(nextLine[headers.indexOf("minimum")])) .setIncPrice(NumberUtils.toDouble(nextLine[headers.indexOf("increment")])) .setUrl(nextLine[headers.indexOf("image")]).build()); } } finally { IOUtils.closeQuietly(cvsreader); } } @Override public void importUsers(final int auctionUid, final Reader reader) { CSVReader cvsreader = null; try { cvsreader = new CSVReader(reader, CSVParser.DEFAULT_SEPARATOR, CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER, 0, false, false); List<String> headers = Arrays.asList(cvsreader.readNext()); String[] nextLine; while ((nextLine = cvsreader.readNext()) != null) { User user = User.newBuilder().setAuctionUid(auctionUid) .setBidderNumber(nextLine[headers.indexOf("bidder_number")]) .setFirstName(nextLine[headers.indexOf("first_name")]) .setLastName(nextLine[headers.indexOf("last_name")]).setRole(Roles.GUEST).build(); // Last name is used the password. String passwordHash = _passwordService.encryptPassword(user.getLastName()); user.setPasswordHash(passwordHash); _dataService.editUser(user); } } catch (IOException e) { LOG.error(Throwables.getStackTraceAsString(e)); } finally { IOUtils.closeQuietly(cvsreader); } } }