/*
 * Created on Dec 27, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package gov.va.med.esr.ui.signature.action;

//Java classes
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.mlw.vlh.ValueList;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import gov.va.med.fw.model.lookup.Lookup;
import gov.va.med.fw.ui.struts.ValueListActionUtils;

import gov.va.med.esr.common.model.person.Person;
import gov.va.med.esr.common.model.person.SignatureImage;

import gov.va.med.esr.ui.common.action.PersonAbstractAction;
import gov.va.med.esr.ui.common.service.LookupCacheService;

/**
 * @author DNS   CHENB
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SignatureViewAction extends PersonAbstractAction {

	/**
	 * 
	 */
	public SignatureViewAction() {
		super();
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub

	}
	private List getSignatures(Person person)
	{
        if (person == null || person.getSignatureImages() == null || person.getSignatureImages().size() == 0)
        	return null;
        
        Set signatureImages = person.getSignatureImages();
    	List signatures = new ArrayList();
        	
    	LookupCacheService cacheService = getLookupCacheService();
    	
    	//loop through and replace username with user's full name
    	for (Iterator i=signatureImages.iterator(); i.hasNext();){
    		SignatureImage image = (SignatureImage)i.next();
    		SignatureImage newImage = (SignatureImage)image.clone();
    		try {
    			Lookup lookup = cacheService.getByCodeFromCache(
    				gov.va.med.esr.common.model.security.ESRUserPrincipalImpl.class,newImage.getUser());
    			if (lookup != null) {
    				newImage.setUser(lookup.getDescription());    				
    			}
    		}catch (Exception e) {
    			//Ignore the lookup exceptions
    		}
    		signatures.add(newImage);
    	}
    	Collections.sort(signatures);
		return signatures;
	}
	
	public ActionForward display( ActionMapping mapping, 
			ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response )
	throws Exception {
		SignatureViewForm signatureForm = (form instanceof SignatureViewForm) ? (SignatureViewForm)form : null;
		
		Person person = getSelectedPerson( request );
		
	    ValueList valueList = ValueListActionUtils.getValueList(signatureForm,
	            request, "signatureTableId", "signatureAdapter", getSignatures(person));

	    // Backup the ValueListInfo into session and store the resultant ValueList on the request
	    ValueListActionUtils.setValueList(request, valueList, "vlhlist");
		
		return mapping.findForward("success");
	}
}
