package hk.reality.stock.service;

import hk.reality.stock.model.Portfolio;
import hk.reality.stock.service.exception.StorageException;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.Vector;

import org.apache.commons.io.IOUtils;

import android.util.Log;

public class FilePortfolioService implements PortfolioService {
	private static final String STORAGE_FILE = "portfolio.xml";
	private static final String TAG = "FilePortfolioService";
	private File baseDirectory;
	private List<Portfolio> portfolios;
	
	public FilePortfolioService(File directory) {
		Log.i(TAG, "init portfolio service: " + directory.getAbsolutePath());
		this.baseDirectory = directory;
		this.portfolios = new Vector<Portfolio>();
	}

	@Override
	public void create(Portfolio p) {
		Log.i(TAG, "create portfolio: " + p.getName());
		p.setId(UUID.randomUUID().toString());
		portfolios.add(p);
		save();
	}

	@Override
	public void delete(Portfolio p) {
		Log.i(TAG, "delete portfolio: " + p.getName());
		portfolios.remove(p);
		save();
	}

	@Override
	public List<Portfolio> list() {
		Log.i(TAG, "list portfolio ");
		return Collections.unmodifiableList(this.portfolios);
	}

	@Override
	public void update(Portfolio p) {
		Log.i(TAG, "update portfolio: " + p.getName());
		
		int pos = portfolios.indexOf(p);
		if (pos > -1) {
			Portfolio orig = portfolios.get(pos);
			orig.setName(p.getName());
			orig.setStocks(p.getStocks());
		} else {
			throw new IllegalArgumentException("record not found");
		}
		save();
	}
	
	private void save() {
		File store = new File(baseDirectory, STORAGE_FILE);
		Writer writer = null;
		try {
			if (!store.exists()) {
				store.createNewFile();
			}
			
			writer = new BufferedWriter(new FileWriter(store));
			String xml = PortfolioSerializer.toXML(portfolios);
			IOUtils.write(xml, writer);
		
		} catch (IOException e) {
			throw new StorageException("failed storing datafile", e);
		
		} finally {
			IOUtils.closeQuietly(writer);
		
		}
	}
}
@@ -182,16 +182,43 @@
 o.File;%0A
+import java.io.FileReader;%0A
 import j
@@ -264,16 +264,39 @@
 eption;%0A
+import java.io.Reader;%0A
 import j
@@ -906,29 +906,23 @@
 s = 
-new Vector%3CPortfolio%3E
+loadOrCreateNew
 ();%0A
@@ -1727,21 +1727,675 @@
 ate 
-void save() %7B
+List%3CPortfolio%3E loadOrCreateNew() %7B%0A        Log.i(TAG, %22loading portfolio file ...%22);%0A        File store = new File(baseDirectory, STORAGE_FILE);%0A        Reader reader = null;%0A        try %7B%0A            if (!store.exists()) %7B%0A                return new Vector%3CPortfolio%3E();%0A            %7D%0A            %0A            reader = new FileReader(store);%0A            return PortfolioSerializer.fromXML(reader);%0A        %7D catch (IOException e) %7B%0A            throw new StorageException(%22failed storing datafile%22, e);%0A        %0A        %7D finally %7B%0A            IOUtils.closeQuietly(reader);%0A        %0A        %7D%0A%09%7D%0A%09%0A%09private void save() %7B%0A%09    Log.i(TAG, %22saving portfolio file ...%22);
 %0A%09%09F
@@ -2678,24 +2678,55 @@
  writer);%0A%09%09
+%09Log.i(TAG, %22save completed%22);%0A
 %0A%09%09%7D catch (

load saved portfolio upon startup