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.Writer; import java.util.List; import javax.inject.Inject; import javax.ws.rs.NotFoundException; import org.apache.commons.io.IOUtils; import au.com.bytecode.opencsv.CSVWriter; import com.mobiaware.auction.data.DataService; import com.mobiaware.auction.model.Fund; import com.mobiaware.auction.model.Item; import com.mobiaware.auction.model.User; import com.mobiaware.auction.provider.ExportService; public class CSVExportService implements ExportService { private final DataService _dataService; @Inject public CSVExportService(final DataService dataService) { _dataService = dataService; } @Override public void exportBids(final int auctionUid, final Writer writer) throws IOException { List<Item> items = _dataService.getItemsUpdatesOnly(auctionUid); if (items == null) { throw new NotFoundException(); } CSVWriter csvwriter = null; try { csvwriter = new CSVWriter(writer, ','); String[] array = { "item_number", "bidder_number", "value" }; csvwriter.writeNext(array); for (Item item : items) { array[0] = item.getItemNumber(); array[1] = item.getWinner(); array[2] = Double.toString(item.getCurPrice()); csvwriter.writeNext(array); } } finally { IOUtils.closeQuietly(csvwriter); } } @Override public void exportFunds(final int auctionUid, final Writer writer) throws IOException { List<User> users = _dataService.getUsers(auctionUid, 0, 9999, null, null); if (users == null) { throw new NotFoundException(); } CSVWriter csvwriter = null; try { csvwriter = new CSVWriter(writer, ','); String[] array = { "bidder_number", "value" }; csvwriter.writeNext(array); for (User user : users) { List<Fund> funds = _dataService.getFundsByUser(user.getUid()); for (Fund fund : funds) { array[0] = user.getBidderNumber(); array[1] = Double.toString(fund.getBidPrice()); csvwriter.writeNext(array); } } } finally { IOUtils.closeQuietly(csvwriter); } } }