package org.jerrymouse.cc.site.bean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;

@Embeddable
public class Storage {
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "user")
	private List<Resource> resources;

	public Resource getResourceByName(String name) {
		return getResourcesMap().get(name);
	}

	private Map<String, Resource> getResourcesMap() {
		Map<String, Resource> map = new HashMap<String, Resource>();
		for (Resource resource : getResources()) {
			map.put(resource.getFilename(), resource);
		}
		return map;
	}

	public List<Resource> getResources() {
		return resources;
	}

	public void setResources(List<Resource> resources) {
		this.resources = resources;
	}

	public void addResource(Resource resource) {
		resources.add(resource);
	}

}
