package de.dass_it.vanhelsing.gui;

import java.util.ArrayList;

import de.dass_it.vanhelsing.gui.items.ConcreteUserObjectItem;
import de.dass_it.vanhelsing.gui.items.UserObjectItem;
import de.dass_it.www.vanhelsing.VanHelsingStub.ResourceAttributeType;
import de.dass_it.www.vanhelsing.VanHelsingStub.ResourceInfo;
/**
 * create a ConcreteUserObject out of a ResourceInfo oder ResourceAttributeType()
 * @author tgoecke
 *
 */
public class UserObjectItemFactory {
	/**
	 * creates a UserObjectItem based on a ResourceInfo object
	 * @param ri	ResourceInfo object to create the UserObjectItem
	 * @return	a new ConcreteUserObjectItem object
	 */
	public UserObjectItem createUserObjectItem(ResourceInfo ri){
		ConcreteUserObjectItem obj = new ConcreteUserObjectItem();
		obj.setResId(ri.getResId());
		obj.setResName(ri.getResName());
		obj.setResType(ri.getResType());
		obj.setDirector(ri.getDirector());
		return obj;
	}
	/**
	 * creates a UserObjectItem based on a ResourceAttributeType array and a ResourceInfo object
	 * @param ra	key value pair object 
	 * @param ri
	 * @return the created UserObjectItem
	 */
	public UserObjectItem createUserObjectItem(ResourceAttributeType ra[], ResourceInfo ri){
		ConcreteUserObjectItem obj = (ConcreteUserObjectItem)createUserObjectItem(ri);
		ArrayList<ViewItem> viewItemList = new ArrayList<ViewItem>();
		ViewItem vi;
		String s;
		boolean b;
		for (ResourceAttributeType attr : ra){
			vi = new ViewItem();
			
			s = BeanUtil.getDataType(BeanUtil.getAccessType(obj.getResType().toLowerCase()), attr.getKey());
			if (s == null){
				s = "String";
			}
			vi.setDataType(s);
			
			s = BeanUtil.getRenderer(BeanUtil.getAccessType(obj.getResType().toLowerCase()), attr.getKey());
			if (s == null){
				s = "inputText";
			}
			vi.setRenderer(s);
			
			b = false;
			b = BeanUtil.getRequired(BeanUtil.getAccessType(obj.getResType().toLowerCase()), attr.getKey());
			vi.setRequired(b);
			
			vi.setKey(attr.getKey());
			vi.setKeyValue(attr.getValue());
			viewItemList.add(vi);
		}
		obj.setViewItemList(viewItemList);
		return obj;
	}
	/**
	 * create an empty object of a given type 
	 * @param type	type of resource to be created
	 * @return newResource 	new resource object to be edited by the user 
	 */
	public UserObjectItem createUserObjectItem(String type){
		ConcreteUserObjectItem newResource = new ConcreteUserObjectItem();
		newResource.setResType(type);
		newResource.setDirector(BeanUtil.getProperty("defaultdirector"));
		newResource.setViewItemList(BeanUtil.getTypeProperties(type.toLowerCase()));
		return newResource;
	}
}
