package nl.hsleiden.components.catalog;

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

import javax.jcr.RepositoryException;

import nl.hsleiden.componentsinfo.ContactPersonsInfo;
import nl.hsleiden.utils.HslUtils;
import nl.hsleiden.utils.Constants.Attributes;
import nl.hsleiden.utils.Constants.WidgetConstants;

import org.hippoecm.hst.content.beans.standard.HippoBean;
import org.hippoecm.hst.core.component.HstComponentException;
import org.hippoecm.hst.core.component.HstRequest;
import org.hippoecm.hst.core.component.HstResponse;
import org.hippoecm.hst.core.parameters.ParametersInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tdclighthouse.prototype.components.AjaxEnabledComponent;

@ParametersInfo(type = ContactPersonsInfo.class)
public class ContactPersons extends AjaxEnabledComponent {

    private static final Logger LOG = LoggerFactory.getLogger(ContactPersons.class);

    public Map<String, Object> getModel(HstRequest request, HstResponse response) {
        try {
            ContactPersonsInfo parametersInfo = getConfiguration(request);    
            return populateModel(request, parametersInfo);
        } catch (RepositoryException e) {
            LOG.error(e.getMessage(), e);
            throw new HstComponentException(e.getMessage(), e);
        }
    }

    protected Map<String, Object> populateModel(HstRequest request, ContactPersonsInfo parametersInfo) {
        Map<String, Object> model = new HashMap<String, Object>();

        model.put("info", parametersInfo);
        addItemsToModel(request, model, parametersInfo);

        return model;
    }
    
    private void addItemsToModel(HstRequest request, Map<String, Object> model, ContactPersonsInfo parametersInfo) {
        List<HippoBean> items = new ArrayList<HippoBean>();
        
        //items should be added only if not null
        items.add(HslUtils.getSelectedBean(request, parametersInfo.getFirstContact()));
        items.add(HslUtils.getSelectedBean(request, parametersInfo.getSecondContact()));
        items.add(HslUtils.getSelectedBean(request, parametersInfo.getThirdContact()));
        
        if (!items.isEmpty()) {
            model.put(Attributes.ITEMS, items);
        } else {
            request.setAttribute(WidgetConstants.WEB_MASTER_MESSAGE, "webmaster.nocontacts.message");
        }
    }

    private ContactPersonsInfo getConfiguration(HstRequest request) throws RepositoryException {
        ContactPersonsInfo paramInfo = this.<ContactPersonsInfo> getComponentParametersInfo(request);
        return paramInfo;
    }

    
}

@@ -231,43 +231,8 @@
 fo;%0A
-import nl.hsleiden.utils.HslUtils;%0A
 impo
@@ -731,16 +731,68 @@
 mponent;
+%0Aimport com.tdclighthouse.prototype.utils.BeanUtils;
 %0A%0A@Param
@@ -1933,35 +1933,36 @@
       items.add(
-Hsl
+Bean
 Utils.getSelecte
@@ -1954,38 +1954,38 @@
 tils.get
-SelectedBean(request, 
+BeanViaAbsolutionPath(
 paramete
@@ -2000,32 +2000,41 @@
 etFirstContact()
+, request
 ));%0A        item
@@ -2031,35 +2031,36 @@
       items.add(
-Hsl
+Bean
 Utils.getSelecte
@@ -2052,38 +2052,38 @@
 tils.get
-SelectedBean(request, 
+BeanViaAbsolutionPath(
 paramete
@@ -2099,32 +2099,41 @@
 tSecondContact()
+, request
 ));%0A        item
@@ -2142,11 +2142,12 @@
 add(
-Hsl
+Bean
 Util
@@ -2155,30 +2155,30 @@
 .get
-SelectedBean(request, 
+BeanViaAbsolutionPath(
 para
@@ -2205,16 +2205,25 @@
 ontact()
+, request
 ));%0A    

fix compilation failure after merge