package cz.hsrs.rest.util;

import cz.hsrs.db.model.NoItemFoundException;
import cz.hsrs.db.model.vgi.VgiCategory;
import cz.hsrs.db.util.UserUtil;
import cz.hsrs.db.vgi.util.VgiCategoryUtil;

import java.sql.SQLException;
import java.util.List;

/**
 * Utility class for VgiCategoryRest services
 * @author mkepka
 *
 */
public class VgiCategoryRestUtil {

    private final UserUtil userUt;
    private final VgiCategoryUtil cUtil;

    public VgiCategoryRestUtil(){
        this.userUt = new UserUtil();
        this.cUtil = new VgiCategoryUtil();
    }
    
    /**
     * Method processes GetVgiCategory by given categoryId
     * @param categoryId - ID of category
     * @param userName - name of user
     * @return VgiCategory object if there is one in the DB
     */
    public VgiCategory processGetVgiCategory(int categoryId, String userName) throws SQLException{
        try{
            userUt.getUserId(userName);
            return cUtil.getVgiCategory(categoryId);
        } catch(NoItemFoundException e){
            throw new SQLException(e.getMessage());
        }
    }
    
    /**
     * Method processes GetVgiCategory by given categoryId
     * @param categoryId - ID of category
     * @param userName - name of user
     * @return VgiCategory object if there is one in the DB
     */
    public List<VgiCategory> processGetVgiCategoryDescendants(int categoryId, String userName) throws SQLException{
        try{
            userUt.getUserId(userName);
            return cUtil.getVgiCategoryDescendants(categoryId);
        } catch(NoItemFoundException e){
            throw new SQLException(e.getMessage());
        }
    }
    
    /**
     * Method processes SelectVgiCategories by given user
     * @return LinkedList of VgiCategory objects associated to given user
     */
    public List<VgiCategory> processGetVgiCategories(String userName) throws SQLException{
        try{
            userUt.getUserId(userName);
            return cUtil.getCategoriesList();
        } catch(NoItemFoundException e){
            throw new SQLException(e.getMessage());
        }
    }
}