/*
 * Copyright 2007 The Kuali Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.kra.proposaldevelopment.web.struts.action;

import java.util.ArrayList;
import java.util.List;

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.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.kuali.kra.proposaldevelopment.bo.PropScienceKeyword;
import org.kuali.kra.proposaldevelopment.bo.ScienceKeyword;
import org.kuali.kra.proposaldevelopment.document.ProposalDevelopmentDocument;
import org.kuali.kra.proposaldevelopment.web.struts.form.ProposalDevelopmentForm;

public class ProposalDevelopmentAction extends KraTransactionalDocumentActionBase {
    private static final Log LOG = LogFactory.getLog(ProposalDevelopmentAction.class);
    
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // TODO : for text area pop window
     //   request.getSession().setAttribute(org.kuali.kra.infrastructure.Constants.TEXT_AREA_FIELD_NAME, "detail");
     //   request.getSession().setAttribute(org.kuali.kra.infrastructure.Constants.HTML_FORM_ACTION,"proposalDevelopment");
        return super.execute(mapping, form, request, response);
    }
    
    @Override
    public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ProposalDevelopmentForm proposalDevelopmentForm = (ProposalDevelopmentForm) form;
        return super.save(mapping, form, request, response);
    }

    public ActionForward proposal(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        return mapping.findForward("proposal");
    }
    
    public ActionForward keyPersonnel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        return mapping.findForward("keyPersonnel");
    }
    
    public ActionForward template(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        return mapping.findForward("template");
    }
    
    public ActionForward notes(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        return mapping.findForward("notes");
    }
    
    public ActionForward specialReview(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        return mapping.findForward("specialReview");
    }
    
    public ActionForward abstractPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        return mapping.findForward("abstractPage");
    }
    
    public ActionForward customData(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        return mapping.findForward("customData");
    }
    
    public ActionForward actions(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        return mapping.findForward("actions");
    }
    
    public ActionForward addScienceKeyword(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

        ProposalDevelopmentForm proposalDevelopmentForm = (ProposalDevelopmentForm)form;
        ProposalDevelopmentDocument proposalDevelopmentDocument = proposalDevelopmentForm.getProposalDevelopmentDocument();
        List<PropScienceKeyword> keywords = proposalDevelopmentDocument.getKeywords();
        String newScienceKeywordCode = proposalDevelopmentDocument.getNewScienceKeywordCode();
        String newDescription = proposalDevelopmentDocument.getNewDescription();
        String defaultNewDescription = proposalDevelopmentDocument.getDefaultNewDescription();
        if(!newDescription.equalsIgnoreCase(defaultNewDescription)) {
            PropScienceKeyword propScienceKeyword = new PropScienceKeyword();
            propScienceKeyword.setScienceKeywordCode(newScienceKeywordCode);
            
            ScienceKeyword scienceKeyword = new ScienceKeyword();
            scienceKeyword.setScienceKeywordCode(newScienceKeywordCode);
            scienceKeyword.setDescription(newDescription);
            
            propScienceKeyword.setScienceKeyword(scienceKeyword);
            keywords.add(propScienceKeyword);
            
            proposalDevelopmentDocument.setKeywords(keywords);

            /* initialize new keyword */
            proposalDevelopmentDocument.setNewScienceKeywordCode(null);
            proposalDevelopmentDocument.setNewDescription(defaultNewDescription);
        }

        return mapping.findForward("proposal");
    }
    
    public ActionForward selectAllScienceKeyword(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

        ProposalDevelopmentForm proposalDevelopmentForm = (ProposalDevelopmentForm)form;
        ProposalDevelopmentDocument proposalDevelopmentDocument = proposalDevelopmentForm.getProposalDevelopmentDocument();
        List<PropScienceKeyword> keywords = proposalDevelopmentDocument.getKeywords();
        for(int i=0; i<keywords.size(); i++) {
            PropScienceKeyword propScienceKeyword = (PropScienceKeyword)keywords.get(i);
            propScienceKeyword.setSelectKeyword(true);
        }

        return mapping.findForward("proposal");
    }

    public ActionForward deleteSelectedScienceKeyword(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

        ProposalDevelopmentForm proposalDevelopmentForm = (ProposalDevelopmentForm)form;
        ProposalDevelopmentDocument proposalDevelopmentDocument = proposalDevelopmentForm.getProposalDevelopmentDocument();
        List<PropScienceKeyword> keywords = proposalDevelopmentDocument.getKeywords();
        List<PropScienceKeyword> newKeywords = new ArrayList();
        for(int i=0; i<keywords.size(); i++) {
            PropScienceKeyword propScienceKeyword = (PropScienceKeyword)keywords.get(i);
            if(!propScienceKeyword.getSelectKeyword()) {
                newKeywords.add(propScienceKeyword);
            }
        }
        proposalDevelopmentDocument.setKeywords(newKeywords);

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

@@ -1674,291 +1674,8 @@
 n %7B%0A
-        // TODO : for text area pop window%0A     //   request.getSession().setAttribute(org.kuali.kra.infrastructure.Constants.TEXT_AREA_FIELD_NAME, %22detail%22);%0A     //   request.getSession().setAttribute(org.kuali.kra.infrastructure.Constants.HTML_FORM_ACTION,%22proposalDevelopment%22);%0A
     
@@ -3617,36 +3617,32 @@
 ctions%22);%0A    %7D%0A
-    
 %0A    public Acti
@@ -6908,10 +6908,15 @@
 ;%0A    %7D%0A
+    %0A
 %7D%0A

include proposal science keyword panel - cvs sync - update