private SubAwardDocument getSubAwardDocument() throws WorkflowException {
    SubAwardDocument document =
        (SubAwardDocument)
            KRADServiceLocatorWeb.getDocumentService().getNewDocument(SubAwardDocument.class);
    document.getDocumentHeader().setDocumentDescription("SubcontractDocumentTest test doc");
    SubAward subAward = new SubAward();
    subAward.setOrganizationId("000001");
    subAward.setSubAwardTypeCode(229);
    subAward.setSubAwardCode("7687");
    subAward.setStatusCode(123);
    subAward.setPurchaseOrderNum("111");
    subAward.setRequisitionerId("1");

    document.setSubAward(subAward);
    KRADServiceLocatorWeb.getDocumentService().saveDocument(document);
    return document;
  }
 protected void loadDocumentInForm(
     HttpServletRequest request, InstitutionalProposalForm institutionalProposalForm)
     throws WorkflowException {
   String docIdRequestParameter = request.getParameter(KRADConstants.PARAMETER_DOC_ID);
   InstitutionalProposalDocument retrievedDocument =
       (InstitutionalProposalDocument)
           KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(docIdRequestParameter);
   institutionalProposalForm.setDocument(retrievedDocument);
   request.setAttribute(KRADConstants.PARAMETER_DOC_ID, docIdRequestParameter);
 }
  /**
   * Create a proposal development document. For testing purposes, we only need its proposal number
   * to be set.
   */
  private ProposalDevelopmentDocument createProposal(
      String documentDescription, String leadUnitNumber) throws Exception {
    ProposalDevelopmentDocument document =
        (ProposalDevelopmentDocument)
            KRADServiceLocatorWeb.getDocumentService()
                .getNewDocument("ProposalDevelopmentDocument");

    Date requestedStartDateInitial = new Date(System.currentTimeMillis());
    Date requestedEndDateInitial = new Date(System.currentTimeMillis());

    document.getDocumentHeader().setDocumentDescription(documentDescription);
    document.getDevelopmentProposal().setSponsorCode("000162");
    document.getDevelopmentProposal().setTitle("project title");
    document.getDevelopmentProposal().setRequestedStartDateInitial(requestedStartDateInitial);
    document.getDevelopmentProposal().setRequestedEndDateInitial(requestedEndDateInitial);
    document.getDevelopmentProposal().setActivityTypeCode("1");
    document.getDevelopmentProposal().setProposalTypeCode("1");
    document.getDevelopmentProposal().setOwnedByUnitNumber(leadUnitNumber);
    document.getDevelopmentProposal().setPrimeSponsorCode("000120");

    proposalDevelopmentService.initializeUnitOrganizationLocation(document);
    proposalDevelopmentService.initializeProposalSiteNumbers(document);

    document =
        (ProposalDevelopmentDocument)
            KRADServiceLocatorWeb.getDocumentService().saveDocument(document);
    initializeProposalUsers(document);

    document =
        (ProposalDevelopmentDocument)
            KRADServiceLocatorWeb.getDocumentService().saveDocument(document);
    ProposalDevelopmentDocument savedDocument =
        (ProposalDevelopmentDocument)
            KRADServiceLocatorWeb.getDocumentService()
                .getByDocumentHeaderId(document.getDocumentNumber());
    assertNotNull(savedDocument);

    return savedDocument;
  }
  @Override
  public List<Map<String, String>> resolveMultipleRoleQualifiers(
      @WebParam(name = "kewTypeId") String kewTypeId,
      @WebParam(name = "roleId") String roleId,
      @WebParam(name = "document") Document document,
      @WebParam(name = "documentContent") DocumentContent documentContent) {
    List<Map<String, String>> deptQualifiers = new ArrayList<Map<String, String>>();
    // try to get values from maintainable object if instance of position
    try {
      org.kuali.rice.krad.document.Document doc =
          KRADServiceLocatorWeb.getDocumentService()
              .getByDocumentHeaderId(document.getDocumentId());
      if (doc instanceof MaintenanceDocument) {
        MaintenanceDocument md = (MaintenanceDocument) doc;
        if (md.getNewMaintainableObject().getDataObject() instanceof PositionBo) {
          PositionBo position = (PositionBo) (md.getNewMaintainableObject().getDataObject());

          for (PositionDepartmentBo positionDepartment : position.getDepartmentList()) {
            if (!positionDepartment.getDeptAfflObj().isPrimaryIndicator()) {
              Map<String, String> qualifiers = new HashMap<String, String>();
              qualifiers.put(
                  KPMERoleMemberAttribute.DEPARTMENT.getRoleMemberAttributeName(),
                  String.valueOf(positionDepartment.getDepartment()));
              qualifiers.put(
                  KPMERoleMemberAttribute.GROUP_KEY_CODE.getRoleMemberAttributeName(),
                  String.valueOf(positionDepartment.getGroupKeyCode()));
              deptQualifiers.add(qualifiers);
            }
          }
        }
      }
    } catch (WorkflowException e) {
      LOG.error("Unable to retrieve document with documemnt ID: " + document.getDocumentId());
    }

    if (deptQualifiers.isEmpty()) {
      Map<String, String> qualifiers = new HashMap<String, String>();
      qualifiers.put(
          KPMERoleMemberAttribute.DEPARTMENT.getRoleMemberAttributeName(), StringUtils.EMPTY);
      qualifiers.put(
          KPMERoleMemberAttribute.GROUP_KEY_CODE.getRoleMemberAttributeName(), StringUtils.EMPTY);
      deptQualifiers.add(qualifiers);
    }
    return deptQualifiers;
  }
  @Override
  public ActionForward docHandler(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {

    ActionForward forward = null;

    ProtocolFormBase protocolForm = (ProtocolFormBase) form;
    String command = protocolForm.getCommand();
    String detailId;

    if (command.startsWith(KewApiConstants.DOCSEARCH_COMMAND + "detailId")) {}

    if (KewApiConstants.ACTIONLIST_INLINE_COMMAND.equals(command)) {

    } else if (getProtocolActionsMappingNameHoook().equals(command)
        || getProtocolOnlineReviewMappingNameHoook().equals(command)) {
      String docIdRequestParameter = request.getParameter(KRADConstants.PARAMETER_DOC_ID);
      Document retrievedDocument =
          KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(docIdRequestParameter);
      protocolForm.setDocument(retrievedDocument);
      request.setAttribute(KRADConstants.PARAMETER_DOC_ID, docIdRequestParameter);
      loadDocument(protocolForm);
    } else {
      forward = super.docHandler(mapping, form, request, response);
    }

    if (KewApiConstants.INITIATE_COMMAND.equals(protocolForm.getCommand())) {
      protocolForm.getProtocolDocument().initialize();
    } else {
      protocolForm.initialize();
    }

    if (getProtocolActionsMappingNameHoook().equals(command)) {
      forward = protocolActions(mapping, protocolForm, request, response);
    }
    if (getProtocolOnlineReviewMappingNameHoook().equals(command)) {
      forward = onlineReview(mapping, protocolForm, request, response);
    }

    return forward;
  }
  /**
   * Test the approval of an amendment. Verify that the sequence number has been incremented for the
   * protocol version and that it's status has been set to approved.
   *
   * @throws Exception
   */
  @Test
  public void runAmendmentTest() throws Exception {
    ProtocolDocument protocolDocument = ProtocolFactory.createProtocolDocument("0906000003");
    ProtocolSubmitAction submitAction = getMockSubmitAction();

    protocolSubmitActionService.submitToIrbForReview(protocolDocument.getProtocol(), submitAction);

    documentService.routeDocument(protocolDocument, null, null);
    documentService.blanketApproveDocument(protocolDocument, null, null);

    String docNbr =
        protocolAmendRenewService.createAmendment(protocolDocument, getMockProtocolAmendmentBean());

    ProtocolDocument amendmentDocument =
        (ProtocolDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(docNbr);
    protocolSubmitActionService.submitToIrbForReview(amendmentDocument.getProtocol(), submitAction);

    documentService.routeDocument(amendmentDocument, null, null);
    // temporarily disable unit test
    //        documentService.blanketApproveDocument(amendmentDocument, null, null);
    //
    //        assertTrue(getWorkflowDocument(amendmentDocument).isFinal());
    //
    //        Protocol newProtocol =
    // protocolFinder.findCurrentProtocolByNumber(protocolDocument.getProtocol().getProtocolNumber());
    //        assertTrue(newProtocol.getSequenceNumber() ==
    // protocolDocument.getProtocol().getSequenceNumber() + 1);
    //
    //        /*
    //         * Must read the protocol document again in order to obtain the most recent changes.
    //         */
    //        protocolDocument = (ProtocolDocument)
    // documentService.getByDocumentHeaderId(protocolDocument.getDocumentNumber());
    //
    //        assertFalse(protocolDocument.getProtocol().isActive());
    //        assertFalse(amendmentDocument.getProtocol().isActive());
    //        assertTrue(newProtocol.isActive());
    //
    //        // TODO: This test can be re-added once we can route the new protocol through
    // workflow.
    //        //assertEquals(getWorkflowDocument(newProtocol.getProtocolDocument).isFinal());
    //
    //        verifyProtocolAction(newProtocol, ProtocolActionType.APPROVED);
  }
  /**
   * This method gets called upon navigation to ProtocolBase Actions tab.
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return
   */
  public ActionForward protocolActions(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    // for protocol lookup copy link - rice 1.1 need this
    ProtocolFormBase protocolForm = (ProtocolFormBase) form;
    String command = request.getParameter("command");
    if (KewApiConstants.DOCSEARCH_COMMAND.equals(command)) {
      String docIdRequestParameter = request.getParameter(KRADConstants.PARAMETER_DOC_ID);
      Document retrievedDocument =
          KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(docIdRequestParameter);
      protocolForm.setDocument(retrievedDocument);
      request.setAttribute(KRADConstants.PARAMETER_DOC_ID, docIdRequestParameter);
    }
    // make sure current submission is displayed when navigate to action page.
    protocolForm.getActionHelper().setCurrentSubmissionNumber(-1);
    protocolForm.getActionHelper().prepareView();
    protocolForm.getActionHelper().prepareCommentsView();

    // When a user selects the Questionnaires tab, empty answerHeaders are generated and saved to
    // the database so that subsequent methods relying
    // on that persisted data have it available to render panels.  Make Protocol Actions tab work in
    // this same manner so it's sub-tab
    // Print ==> Questionnaires will render when a user enters a protocol but does not select the
    // Questionnaire tab to answer the questions.
    protocolForm.getQuestionnaireHelper().prepareView();
    protocolForm.getQuestionnaireHelper().populateAnswers();
    protocolForm.getQuestionnaireHelper().setQuestionnaireActiveStatuses();
    Document document = protocolForm.getDocument();
    List<AnswerHeader> answerHeaders = protocolForm.getQuestionnaireHelper().getAnswerHeaders();
    if (applyRules(new SaveQuestionnaireAnswerEvent(document, answerHeaders))
        && applyRules(new SaveProtocolQuestionnaireEvent(document, answerHeaders))) {
      protocolForm.getQuestionnaireHelper().preSave();
      getBusinessObjectService().save(answerHeaders);
    }

    return branchToPanelOrNotificationEditor(
        mapping, protocolForm, getProtocolActionsForwardNameHook());
  }
  @Override
  public ProposalDevelopmentDocument createProposalDevelopmentDocument(ProtocolForm protocolForm)
      throws Exception {
    ProposalDevelopmentDocument proposalDevelopmentDocument = null;
    Protocol protocol = protocolForm.getProtocolDocument().getProtocol();
    if (isAuthorizedCreateProposal(protocolForm.getProtocolHelper())) {
      DocumentService docService = KRADServiceLocatorWeb.getDocumentService();
      proposalDevelopmentDocument =
          (ProposalDevelopmentDocument)
              docService.getNewDocument(ProposalDevelopmentDocument.class);
      ProposalDevelopmentService proposalDevelopmentService =
          KraServiceLocator.getService(ProposalDevelopmentService.class);
      populateDocumentOverview(protocol, proposalDevelopmentDocument);
      populateRequiredFields(protocol, proposalDevelopmentDocument);
      proposalDevelopmentService.initializeUnitOrganizationLocation(proposalDevelopmentDocument);
      proposalDevelopmentService.initializeProposalSiteNumbers(proposalDevelopmentDocument);
      populateProposalPerson_Investigator(protocol, proposalDevelopmentDocument);
      populateProposalSpecialReview(protocol, proposalDevelopmentDocument);

      docService.saveDocument(proposalDevelopmentDocument);
      initializeAuthorization(proposalDevelopmentDocument);
    }
    return proposalDevelopmentDocument;
  }
  @Override
  public Document recallDocument(Document document, String annotation, boolean cancel)
      throws WorkflowException {
    checkForNulls(document);
    WorkflowDocument workflowDocument =
        KRADServiceLocatorWeb.getDocumentService()
            .getByDocumentHeaderId(document.getDocumentNumber())
            .getDocumentHeader()
            .getWorkflowDocument();

    if (!workflowDocument.isFinal() && !workflowDocument.isProcessed()) {
      Note note = createNoteFromDocument(document, annotation);
      document.addNote(note);
      getNoteService().save(note);
    }

    prepareWorkflowDocument(document);
    getWorkflowDocumentService()
        .recall(document.getDocumentHeader().getWorkflowDocument(), annotation, cancel);
    UserSessionUtils.addWorkflowDocument(
        GlobalVariables.getUserSession(), document.getDocumentHeader().getWorkflowDocument());
    removeAdHocPersonsAndWorkgroups(document);
    return document;
  }
  @Override
  public List<DocumentAttribute> extractDocumentAttributes(
      ExtensionDefinition extensionDefinition, DocumentWithContent documentWithContent) {
    List<DocumentAttribute> attributes = new ArrayList<DocumentAttribute>();

    String docId = documentWithContent.getDocument().getDocumentId();

    DocumentService docService = KRADServiceLocatorWeb.getDocumentService();
    Document doc = null;
    try {
      doc = docService.getByDocumentHeaderIdSessionless(docId);
    } catch (WorkflowException we) {
      LOG.error("Unable to retrieve document " + docId + " in getSearchStorageValues()", we);
    }

    String attributeValue = "";
    if (doc != null) {
      if (doc.getDocumentHeader() != null) {
        attributeValue = doc.getDocumentHeader().getDocumentDescription();
      } else {
        attributeValue = "null document header";
      }
    } else {
      attributeValue = "null document";
    }
    DocumentAttributeString attribute =
        DocumentAttributeFactory.createStringAttribute("documentDescription", attributeValue);
    attributes.add(attribute);

    attributeValue = "";
    if (doc != null) {
      if (doc.getDocumentHeader() != null) {
        attributeValue = doc.getDocumentHeader().getOrganizationDocumentNumber();
      } else {
        attributeValue = "null document header";
      }
    } else {
      attributeValue = "null document";
    }
    attribute =
        DocumentAttributeFactory.createStringAttribute(
            "organizationDocumentNumber", attributeValue);
    attributes.add(attribute);

    if (doc != null && doc instanceof MaintenanceDocument) {
      final Class<? extends BusinessObject> businessObjectClass =
          getBusinessObjectClass(documentWithContent.getDocument().getDocumentTypeName());
      if (businessObjectClass != null) {
        if (GlobalBusinessObject.class.isAssignableFrom(businessObjectClass)) {
          final GlobalBusinessObject globalBO =
              retrieveGlobalBusinessObject(docId, businessObjectClass);

          if (globalBO != null) {
            attributes.addAll(findAllDocumentAttributesForGlobalBusinessObject(globalBO));
          }
        } else {
          attributes.addAll(
              parsePrimaryKeyValuesFromDocument(businessObjectClass, (MaintenanceDocument) doc));
        }
      }
    }
    if (doc != null) {
      DocumentEntry docEntry =
          KRADServiceLocatorWeb.getDataDictionaryService()
              .getDataDictionary()
              .getDocumentEntry(documentWithContent.getDocument().getDocumentTypeName());
      if (docEntry != null) {
        WorkflowAttributes workflowAttributes = docEntry.getWorkflowAttributes();
        WorkflowAttributePropertyResolutionService waprs =
            KNSServiceLocator.getWorkflowAttributePropertyResolutionService();
        attributes.addAll(waprs.resolveSearchableAttributeValues(doc, workflowAttributes));
      } else {
        LOG.error(
            "Unable to find DD document entry for document type: "
                + documentWithContent.getDocument().getDocumentTypeName());
      }
    }
    return attributes;
  }