package org.appfuse.webapp.action;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.appfuse.Constants;
import org.appfuse.model.Experience;
import org.appfuse.service.ResumeManager;
import org.appfuse.webapp.form.ExperienceForm;


/**
 * Implementation of <strong>Action</strong> that interacts with the {@link
 * ExperienceForm} and retrieves values. It interacts with the {@link
 * ResumeManager} to retrieve/persist values to the database.
 *
 * <p>
 * <a href="ExperienceAction.java.html"><i>View Source</i></a>
 * </p>
 *
 * @author <a href="mailto:[email protected]">Matt Raible</a>
 * @version $Revision: 1.1 $ $Date: 2004/03/31 13:04:24 $
 *
 * @struts.action name="experienceForm" path="/editExperience"
 *       scope="request" validate="false" parameter="action" input="edit"
 * @struts.action name="experienceForm" path="/saveExperience"
 *      scope="request" validate="true" parameter="action" input="edit"
 *
 * @struts.action-forward name="edit" path=".experience"
 */
public final class ExperienceAction extends BaseAction {
    // ========================================================
    private Log log = LogFactory.getLog(ExperienceAction.class);

    // ================================================================
    public ActionForward edit(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'edit' method");
        }

        ExperienceForm experienceForm = (ExperienceForm) form;

        if ((experienceForm.getId() == null) ||
                "".equals(experienceForm.getId())) {
            ActionErrors errors = new ActionErrors();
            errors.add(ActionErrors.GLOBAL_ERROR,
                       new ActionError("errors.required", "experience.id"));
            saveErrors(request, errors);

            return mapping.getInputForward();
        }

        ResumeManager mgr = (ResumeManager) getBean("resumeManager");
        Experience ex = (Experience) mgr.getExperience(experienceForm.getId());
        experienceForm = (ExperienceForm) convert(ex);
        experienceForm = (ExperienceForm) convertDates(ex, experienceForm);
        request.setAttribute(Constants.EXPERIENCE_KEY, experienceForm);
        request.setAttribute(Constants.EXPERIENCES,
                             mgr.getWorkHistory(experienceForm.getResumeId()));

        request.setAttribute("menuUrl", "experiences.do");

        // return a forward to edit forward
        return mapping.findForward("edit");
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'delete' method");
        }

        ExperienceForm experienceForm = (ExperienceForm) form;

        if ((experienceForm.getId() == null) ||
                "".equals(experienceForm.getId())) {
            ActionErrors errors = new ActionErrors();
            errors.add(ActionErrors.GLOBAL_ERROR,
                       new ActionError("errors.required", "experience.id"));
            saveErrors(request, errors);

            return mapping.getInputForward();
        }

        ResumeManager mgr = (ResumeManager) getBean("resumeManager");

        mgr.removeObject(convert(experienceForm));

        ActionMessages messages = new ActionMessages();
        messages.add(ActionMessages.GLOBAL_MESSAGE,
                     new ActionMessage("experience.deleted"));
        saveMessages(request, messages);

        return setupList(mapping, request, experienceForm.getResumeId());
    }

    public ActionForward save(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'save' method");
        }

        ExperienceForm experienceForm = (ExperienceForm) form;
        boolean isNew = ("".equals(experienceForm.getId()));

        ResumeManager mgr = (ResumeManager) getBean("resumeManager");

        mgr.saveObject(convert(experienceForm));

        ActionMessages messages = new ActionMessages();
        String msg = (isNew) ? "experience.added" : "experience.updated";
        messages.add(ActionMessages.GLOBAL_MESSAGE,
                     new ActionMessage(msg, experienceForm.getOrganizationName()));
        saveMessages(request, messages);

        removeFormBean(mapping, request);

        return setupList(mapping, request, experienceForm.getResumeId());
    }

    public ActionForward search(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'search' method");
        }

        ExperienceForm experience = (ExperienceForm) form;
        String resumeId = experience.getResumeId();

        return setupList(mapping, request, resumeId);
    }

    public ActionForward setupList(ActionMapping mapping,
                                   HttpServletRequest request, String resumeId)
    throws Exception {
        if (resumeId == null) {
            resumeId = getResumeId(request);
        }

        ResumeManager mgr = (ResumeManager) getBean("resumeManager");

        request.setAttribute(Constants.EXPERIENCES, mgr.getWorkHistory(resumeId));

        ExperienceForm experienceForm = new ExperienceForm();
        experienceForm.setResumeId(resumeId);
        request.setAttribute(Constants.EXPERIENCE_KEY, experienceForm);

        // lookup the resume and stuff it in the request too
        request.setAttribute(Constants.RESUME_KEY, mgr.getResume(resumeId));

        request.setAttribute("menuUrl", "experiences.do");

        return mapping.findForward("edit");
    }
}