gt:         if (!caldb.isCapitalAccountingLinesExist()) {
context: /*
   * The Kuali Financial System, a comprehensive financial management system for higher education.
   * 
   * Copyright 2005-2014 The Kuali Foundation
   * 
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Affero General Public License as
   * published by the Free Software Foundation, either version 3 of the
   * License, or (at your option) any later version.
   * 
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU Affero General Public License for more details.
   * 
   * You should have received a copy of the GNU Affero General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  package org.kuali.kfs.fp.document.web.struts;
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.List;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
  import org.kuali.kfs.fp.businessobject.CapitalAccountingLines;
  import org.kuali.kfs.fp.businessobject.CapitalAssetAccountsGroupDetails;
  import org.kuali.kfs.fp.businessobject.CapitalAssetInformation;
  import org.kuali.kfs.fp.businessobject.CapitalAssetInformationDetail;
  import org.kuali.kfs.fp.businessobject.options.CapitalAccountingLinesComparator;
  import org.kuali.kfs.fp.document.CapitalAccountingLinesDocumentBase;
  import org.kuali.kfs.fp.document.validation.event.CapitalAccountingLinesSameObjectCodeSubTypeEvent;
  import org.kuali.kfs.fp.document.validation.impl.CapitalAssetAccountingLineUniquenessEnforcementValidation;
  import org.kuali.kfs.integration.cab.CapitalAssetBuilderModuleService;
  import org.kuali.kfs.sys.KFSConstants;
  import org.kuali.kfs.sys.KFSKeyConstants;
  import org.kuali.kfs.sys.businessobject.AccountingLine;
  import org.kuali.kfs.sys.businessobject.SourceAccountingLine;
  import org.kuali.kfs.sys.businessobject.TargetAccountingLine;
  import org.kuali.kfs.sys.context.SpringContext;
  import org.kuali.kfs.sys.document.AccountingDocument;
  import org.kuali.kfs.sys.document.validation.event.AttributedRouteDocumentEvent;
  import org.kuali.kfs.sys.web.struts.KualiAccountingDocumentFormBase;
  import org.kuali.rice.core.api.config.property.ConfigurationService;
  import org.kuali.rice.core.api.util.RiceConstants;
  import org.kuali.rice.kew.api.exception.WorkflowException;
  import org.kuali.rice.kns.question.ConfirmationQuestion;
  import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
  import org.kuali.rice.kns.web.struts.form.KualiForm;
  import org.kuali.rice.krad.document.Document;
  import org.kuali.rice.krad.rules.rule.event.SaveDocumentEvent;
  import org.kuali.rice.krad.service.KualiRuleService;
  import org.kuali.rice.krad.util.GlobalVariables;
  import org.kuali.rice.krad.util.KRADConstants;
  
  /**
   * This is the action class for the CapitalAccountingLinesActionBase.
   */
  public abstract class CapitalAccountingLinesActionBase extends CapitalAssetInformationActionBase {
      private CapitalAssetBuilderModuleService capitalAssetBuilderModuleService = SpringContext.getBean(CapitalAssetBuilderModuleService.class);
  
      /**
       * Upon entry we need to set the capitalAccountingLinesExist boolean and check the tab states
       * @see org.kuali.kfs.sys.web.struts.KualiAccountingDocumentActionBase#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
       */
      @Override
      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
          KualiDocumentFormBase kualiDocumentFormBase = (KualiDocumentFormBase) form;
          AccountingDocument tdoc = (AccountingDocument) kualiDocumentFormBase.getDocument();
  
          initializeCapitalAccountingLinesExist(tdoc);
          setTabStatesForCapitalAssets(form);
  
          return super.execute(mapping, form, request, response);
      }
  
      /**
       * Upon entry we need to set the capitalAccountingLinesExist boolean and check the tab states
       * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#docHandler(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
       */
      @Override
      public ActionForward docHandler(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
          KualiDocumentFormBase kualiDocumentFormBase = (KualiDocumentFormBase) form;
          AccountingDocument tdoc = (AccountingDocument) kualiDocumentFormBase.getDocument();
  
          initializeCapitalAccountingLinesExist(tdoc);
          setTabStatesForCapitalAssets(form);
  
          return super.docHandler(mapping, form, request, response);
      }
  
      /**
       * removes capitalaccountinglines which is a transient bo.. and call the super method to
       * create a error correction document.
       * @see org.kuali.kfs.sys.document.web.struts.FinancialSystemTransactionalDocumentActionBase#correct(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
       */
      @Override
      public ActionForward correct(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
          CapitalAccountingLinesFormBase capitalAccountingLinesFormBase = (CapitalAccountingLinesFormBase) form;
          CapitalAccountingLinesDocumentBase caldb = (CapitalAccountingLinesDocumentBase) capitalAccountingLinesFormBase.getFinancialDocument();
          caldb.getCapitalAccountingLines().clear();
  
          super.correct(mapping, capitalAccountingLinesFormBase, request, response);
  
          KualiAccountingDocumentFormBase kadfb = (KualiAccountingDocumentFormBase) form;
          List<CapitalAssetInformation> currentCapitalAssetInformation =  this.getCurrentCapitalAssetInformationObject(kadfb);
          for (CapitalAssetInformation capitalAsset : currentCapitalAssetInformation) {
              capitalAsset.setCapitalAssetProcessedIndicator(false);
              capitalAsset.setCapitalAssetLineAmount(capitalAsset.getCapitalAssetLineAmount().negated());
              //remove capital asset tag/location asset tag number and serial number as
              //they will fail because these values will be duplicates.
              List<CapitalAssetInformationDetail> tagLocationDetails = capitalAsset.getCapitalAssetInformationDetails();
              for (CapitalAssetInformationDetail tagLocationDetail : tagLocationDetails) {
                  tagLocationDetail.setCapitalAssetTagNumber(null);
                  tagLocationDetail.setCapitalAssetSerialNumber(null);
              }
  
              List<CapitalAssetAccountsGroupDetails> groupAccountLines = capitalAsset.getCapitalAssetAccountsGroupDetails();
              for (CapitalAssetAccountsGroupDetails groupAccountLine : groupAccountLines) {
                  groupAccountLine.setAmount(groupAccountLine.getAmount().negated());
              }
          }
  
          String distributionAmountCode = capitalAccountingLinesFormBase.getCapitalAccountingLine().getDistributionCode();
  
          AccountingDocument tdoc = (AccountingDocument) capitalAccountingLinesFormBase.getDocument();
  
          List<CapitalAccountingLines> capitalAccountingLines = new ArrayList();
          //for every source/target accounting line that has an object code that requires a
          //capital asset, creates a capital accounting line that the user can select to
          //perform create or modify asset functions.
          createCapitalAccountingLines(capitalAccountingLines, tdoc, distributionAmountCode);
          sortCaptitalAccountingLines(capitalAccountingLines);
  
          caldb.setCapitalAccountingLines(capitalAccountingLines);
          //checks capital accounting lines for capital assets and if so checks the select box
          checkSelectForCapitalAccountingLines(capitalAccountingLinesFormBase);
  
          checkCapitalAccountingLinesSelected(capitalAccountingLinesFormBase);
          calculatePercentsForSelectedCapitalAccountingLines(capitalAccountingLinesFormBase);
  
          //setup the initial next sequence number column..
          setupIntialNextCapitalAssetLineNumber(capitalAccountingLinesFormBase);
  
          return mapping.findForward(RiceConstants.MAPPING_BASIC);
      }
  
      /**
       * All document-load operations get routed through here
       *
       * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#loadDocument(org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase)
       */
      @Override
      protected void loadDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
          super.loadDocument(kualiDocumentFormBase);
  
          CapitalAccountingLinesFormBase capitalAccountingLinesFormBase = (CapitalAccountingLinesFormBase) kualiDocumentFormBase;
          AccountingDocument tdoc = (AccountingDocument) kualiDocumentFormBase.getDocument();
  
          initializeCapitalAccountingLinesExist(tdoc);
          setTabStatesForCapitalAssets(kualiDocumentFormBase);
  
          // Since the UI display depends on whether or not capitalAccountingLines is empty to display asset information, we
          // need to be careful of only generating them if there actually is asset information. Otherwise the user is forced to
          // select the generate button when they are ready
          if (checkCreateAssetsExist(capitalAccountingLinesFormBase) || checkModifyAssetsExist(capitalAccountingLinesFormBase)) {
              String distributionAmountCode = capitalAccountingLinesFormBase.getCapitalAccountingLine().getDistributionCode();
  
              List<CapitalAccountingLines> capitalAccountingLines = new ArrayList();
              //for every source/target accounting line that has an object code that requires a
              //capital asset, creates a capital accounting line that the user can select to
              //perform create or modify asset functions.
              createCapitalAccountingLines(capitalAccountingLines, tdoc, distributionAmountCode);
              sortCaptitalAccountingLines(capitalAccountingLines);
  
              CapitalAccountingLinesDocumentBase caldb = (CapitalAccountingLinesDocumentBase) tdoc;
              caldb.setCapitalAccountingLines(capitalAccountingLines);
              //checks capital accounting lines for capital assets and if so checks the select box
              checkSelectForCapitalAccountingLines(capitalAccountingLinesFormBase);
  
              checkCapitalAccountingLinesSelected(capitalAccountingLinesFormBase);
              calculatePercentsForSelectedCapitalAccountingLines(capitalAccountingLinesFormBase);
  
              //setup the initial next sequence number column..
              setupIntialNextCapitalAssetLineNumber(kualiDocumentFormBase);
  
              KualiForm kualiForm = kualiDocumentFormBase;
              //based on the records in capital accounting lines, capital asset information lists
              //set the tabs to open if lists not empty else set to close
              setTabStatesForCapitalAssets(kualiForm);
          }
      }
  
      /**
       * checks if the document has any capital accounting lines associated with it and sets
       * CapitalAccountingLinesDocumentBase.capitalAccountingLinesExist if necessary
       *
       * @param tdoc
       */
      protected void initializeCapitalAccountingLinesExist(AccountingDocument tdoc) {
          CapitalAccountingLinesDocumentBase caldb = (CapitalAccountingLinesDocumentBase) tdoc;
  
          List<SourceAccountingLine> sourceAccountLines = tdoc.getSourceAccountingLines();
          for (SourceAccountingLine line : sourceAccountLines) {
              if (capitalAssetBuilderModuleService.hasCapitalAssetObjectSubType(line)) {
                  caldb.setCapitalAccountingLinesExist(true);
                  break;
              }
          }
  
          // If it isn't true already, check the target lines
  