/*
   * if questionnaire is associated with Amendment/renewal submodulecode.
   * if this protocol is normal protocol, then it has to check whether the A/R of this
   * questionnaire has merged to this protocol yet.
   */
  private boolean isCurrentAorRQn(AnswerHeader answerHeader) {
    boolean isCurrentQn = false;
    if (getProtocol().isAmendment() || getProtocol().isRenewal()) {
      // if this is A/R, then just match sequencenumber and modulesubitemkey
      isCurrentQn =
          answerHeader.getModuleSubItemKey().equals(getProtocol().getSequenceNumber().toString());
    } else {
      // if this is a regular protocol, then get this A/R associated this this Qn and see if
      // A/R has been merged to this version of protocol
      Map keyValues = new HashMap();
      keyValues.put("protocolNumber", answerHeader.getModuleItemKey());

      ProtocolBase protocol =
          ((List<ProtocolBase>)
                  getBusinessObjectService()
                      .findMatchingOrderBy(
                          getProtocolBOClassHook(), keyValues, "sequenceNumber", false))
              .get(0);

      isCurrentQn =
          answerHeader.getModuleSubItemKey().equals(protocol.getSequenceNumber().toString())
              && !CollectionUtils.isEmpty(protocol.getProtocolSubmissions())
              && isMergedToProtocol(getProtocol(), protocol);
    }
    return isCurrentQn;
  }
  protected void buildAndAttachProtocolCorrespondence(
      ProtocolBase protocol, byte[] data, String correspTypeCode) {

    ProtocolCorrespondence protocolCorrespondence = getNewProtocolCorrespondenceHook();
    protocolCorrespondence.setProtocol(protocol);
    protocolCorrespondence.setProtocolId(protocol.getProtocolId());
    protocolCorrespondence.setProtocolNumber(protocol.getProtocolNumber());
    protocolCorrespondence.setSequenceNumber(protocol.getSequenceNumber());
    protocolCorrespondence.setProtoCorrespTypeCode(correspTypeCode);

    ProtocolActionBase lastAction = protocol.getLastProtocolAction();
    protocolCorrespondence.setProtocolAction(lastAction);
    protocolCorrespondence.setActionIdFk(lastAction.getProtocolActionId());
    protocolCorrespondence.setCorrespondence(data);
    protocolCorrespondence.setActionId(lastAction.getActionId());

    // What is Final flag used for? ANSWER: the final flag is used by the IRB admin to denote
    // correspondences
    // that are ready to be sent/published to the PI etc.
    protocolCorrespondence.setFinalFlag(false);
    protocolCorrespondence.setCreateUser(GlobalVariables.getUserSession().getPrincipalName());
    protocolCorrespondence.setCreateTimestamp(dateTimeService.getCurrentTimestamp());

    if (lastAction.getProtocolCorrespondences() == null) {
      List<ProtocolCorrespondence> correspondences = new ArrayList<ProtocolCorrespondence>();
      correspondences.add(protocolCorrespondence);
      lastAction.setProtocolCorrespondences(correspondences);
      protocol.refreshReferenceObject("protocolSubmissions");
    } else {
      lastAction.getProtocolCorrespondences().add(protocolCorrespondence);
    }

    getBusinessObjectService().save(protocolCorrespondence);

    getBusinessObjectService().save(protocol);
  }
 /*
  * This is Questionnaire answer is with submodulecode of "0".
  * Then if this protocol is A/R, then it has to check whether this is the first version
  * A/R merged into.
  */
 private boolean isCurrentRegularQn(AnswerHeader answerHeader) {
   boolean isCurrentQn = false;
   if ((getProtocol().isAmendment() || getProtocol().isRenewal())
       && !answerHeader.getModuleItemKey().equals(getProtocol().getProtocolNumber())) {
     Map keyValues = new HashMap();
     keyValues.put("protocolNumber", answerHeader.getModuleItemKey());
     ProtocolBase prevProtocol = null;
     // if this is an A/R protocol, then need to find the original protocol that the A/R first
     // merged into.
     for (ProtocolBase protocol :
         ((List<ProtocolBase>)
             getBusinessObjectService()
                 .findMatchingOrderBy(
                     getProtocolBOClassHook(), keyValues, "sequenceNumber", true))) {
       isCurrentQn =
           answerHeader.getModuleSubItemKey().equals(protocol.getSequenceNumber().toString())
               && !CollectionUtils.isEmpty(getProtocol().getProtocolSubmissions())
               && isMergedToProtocol(protocol, getProtocol());
       if (isCurrentQn) {
         if (prevProtocol == null || !isMergedToProtocol(prevProtocol, getProtocol())) {
           // this is the protocol this A/R merged into. so, use this questionnaire.
           break;
         } else {
           // prevProtocol is the initial ProtocolBase that this A/R merged into.
           isCurrentQn = false;
         }
       }
       prevProtocol = protocol;
     }
   } else {
     // if this is a regular protocol, then check if sequencenumber & modulesubitemkey match
     isCurrentQn =
         answerHeader.getModuleSubItemKey().equals(getProtocol().getSequenceNumber().toString());
   }
   return isCurrentQn;
 }