// Copyright 2008 by Basil Vandegriend.  All rights reserved.

package com.basilv.resumecalculator;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.*;

import org.joda.time.LocalDate;

import com.basilv.core.Assert;

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "resume")
public class Resume
{
	@XmlAttribute(required = true)
	private String name;
	
	// TODO: Lesson: need to return underlying list for modification by XML parser.
	// nice documentation on key annotations: http://www.caucho.com/resin-3.1/doc/jaxb-annotations.xtp
	// full documentation: http://java.sun.com/javaee/5/docs/tutorial/backup/update3/doc/JAXB5.html
	@XmlElementWrapper(name="entries")
	@XmlElements({ @XmlElement(name="entry",type=ResumeEntry.class) })
	private List<ResumeEntry> entries = new ArrayList<ResumeEntry>();
	
	private LocalDate lastUpdateDate;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public LocalDate getLastUpdateDate() {
		return lastUpdateDate;
	}
	public void setLastUpdateDate(LocalDate lastUpdateDate) {
		this.lastUpdateDate = lastUpdateDate;
	}
	
	public List<ResumeEntry> getEntries() {
		// Return the underlying list and not a wrapper so generation from XML by JAXB works correctly.
		return entries;
	}

	/**
	 * Return the entry for the specified id or null if no such entry is found.
	 * @param entryId 
	 * @return
	 */
	public ResumeEntry getEntry(String entryId) {
		for (ResumeEntry entry : entries) {
			if (entry.getId().equals(entryId)) {
				return entry;
			}
		}
		return null;
	}
	
	/**
	 * Add the specified entry to this resume.
	 * @param entry Cannot be null. Cannot have the same id as an existing entry in this resume.
	 */
	public void addEntry(ResumeEntry entry) {
		Assert.notNull("entry", entry);
		Assert.notNull("entry.id", entry.getId());
		
		if (getEntry(entry.getId()) != null) {
			throw new IllegalArgumentException("This resume already contains an entry with id [" + entry.getId() + "].");
		}
		
		entries.add(entry);
	}

}
