package uk.org.adamretter.webexample.view.bean;

import java.util.Set;
import uk.org.adamretter.webexample.model.Category;
import uk.org.adamretter.webexample.model.Conference;
import uk.org.adamretter.webexample.service.CategoryService;
import uk.org.adamretter.webexample.service.ConferenceService;

/**
 *
 * @author aretter
 */
public class ConferenceBean {
    
    private ConferenceService service;
    private CategoryService categoryService;
    private Conference conference;

    private ConferenceService getService() {
        return service;
    }

    public void setService(ConferenceService service) {
        this.service = service;
    }

    private CategoryService getCategoryService() {
        return categoryService;
    }

    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }
    
    public Conference getConference() {
        if(conference == null) {
            conference = new Conference();
        }
        return conference;
    }
    
    public void addCategory() {
        Category defaultCategory = getCategoryService().getDefaultCategory();
        
        getConference().getCategories().add(defaultCategory);
    }
    
    public void deleteLastCategory() {
        
        Set<Category> categories = getConference().getCategories();
        if(categories != null && categories.size() > 0) {
        
            Category catsArry[] = categories.toArray(new Category[categories.size()]);
        
            getConference().getCategories().remove(catsArry[catsArry.length - 1]);
        }
    }
    
    public void submit() {
        getService().save(getConference());
    }
}
