/** Add a PaymentTermLine in the xml and import it, there should be an extra line then. */
  public void testGPaymentTerm() {

    setUserContext(QA_TEST_ADMIN_USER_ID);
    setAccess();
    // make a copy of the paymentterms and their children so that the
    // original db is not updated
    final List<BaseOBObject> pts =
        DalUtil.copyAll(new ArrayList<BaseOBObject>(getPaymentTerms()), true);

    // add one at the back
    for (final BaseOBObject bob : pts) {
      final PaymentTerm pt = (PaymentTerm) bob;
      pt.setId("abc");
      if (pt.getFinancialMgmtPaymentTermLineList().isEmpty()) {
        continue;
      }
      final PaymentTermLine ptl0 = pt.getFinancialMgmtPaymentTermLineList().get(0);
      ptl0.setPaymentTerms(pt);
      final PaymentTermLine ptl = (PaymentTermLine) DalUtil.copy(ptl0);
      ptl.setId(null);
      ptl.setClient(null);
      ptl.setOrganization(null);
      ptl.setLineNo((long) NO_OF_PT_LINE);
      pt.getFinancialMgmtPaymentTermLineList().add(ptl);
      ptl.setPaymentTerms(pt);
    }

    String xml = getXML(pts);
    // log.debug(xml);
    // there is a unique constraint on name
    xml = xml.replaceAll("</name>", "t</name>");

    setUserContext(QA_TEST_ADMIN_USER_ID);
    setAccess();
    final ImportResult ir =
        DataImportService.getInstance()
            .importDataFromXML(
                OBDal.getInstance().get(Client.class, QA_TEST_CLIENT_ID),
                OBDal.getInstance().get(Organization.class, QA_TEST_ORG_ID),
                xml);
    if (ir.getException() != null) {
      ir.getException().printStackTrace(System.err);
      fail(ir.getException().getMessage());
    }

    // + 1 for the inserted line
    assertEquals(TOTAL_PT_PTL + 1, ir.getInsertedObjects().size());
    for (final Object o : ir.getInsertedObjects()) {
      assertTrue(
          o instanceof PaymentTermTrl || o instanceof PaymentTerm || o instanceof PaymentTermLine);
    }
  }
  /** Export the Payment Terms from one client and import into another client. */
  public void testBPaymentTerm() {

    setTestUserContext();
    setAccess();

    final List<PaymentTerm> pts = getPaymentTerms();
    String xml = getXML(pts);

    log.debug(xml);

    // there is a unique constraint on name
    xml = xml.replaceAll("</name>", "t</name>");

    // export to client 1000001
    setUserContext(QA_TEST_ADMIN_USER_ID);
    // don't be bothered by access checks...
    setAccess();
    final ImportResult ir =
        DataImportService.getInstance()
            .importDataFromXML(
                OBDal.getInstance().get(Client.class, QA_TEST_CLIENT_ID),
                OBDal.getInstance().get(Organization.class, QA_TEST_ORG_ID),
                xml);
    if (ir.getException() != null) {
      ir.getException().printStackTrace(System.err);
      fail(ir.getException().getMessage());
    } else if (ir.getErrorMessages() != null) {
      fail(ir.getErrorMessages());
    }

    assertEquals(TOTAL_PT_PTL, ir.getInsertedObjects().size());
    assertEquals(0, ir.getUpdatedObjects().size());
  }
  /**
   * Test removal of a PaymentTermLine from a PaymentTerm in the xml, then import. After importing
   * the PaymentTermLine should have gone.
   */
  public void testEPaymentTerm() {

    setTestUserContext();
    setAccess();
    // make a copy of the paymentterms and their children so that the
    // original db is not updated
    final List<BaseOBObject> pts =
        DalUtil.copyAll(new ArrayList<BaseOBObject>(getPaymentTerms()), false);

    for (final BaseOBObject bob : pts) {
      final PaymentTerm pt = (PaymentTerm) bob;
      final PaymentTermLine ptl = pt.getFinancialMgmtPaymentTermLineList().get(1);
      pt.getFinancialMgmtPaymentTermLineList().remove(ptl);
    }

    String xml = getXML(pts);
    // there is a unique constraint on name
    xml = xml.replaceAll("</name>", "t</name>");

    setUserContext(QA_TEST_ADMIN_USER_ID);
    // a payment term line is not deletable, but for this test it should be done anyway
    // force this by being admin
    OBContext.setAdminMode();
    try {
      final ImportResult ir =
          DataImportService.getInstance()
              .importDataFromXML(
                  OBDal.getInstance().get(Client.class, QA_TEST_CLIENT_ID),
                  OBDal.getInstance().get(Organization.class, QA_TEST_ORG_ID),
                  xml);
      if (ir.getException() != null) {
        ir.getException().printStackTrace(System.err);
        fail(ir.getException().getMessage());
      }

      assertEquals(0, ir.getInsertedObjects().size());
      // name of paymentterm has changed
      // overduepaymentrule of paymenttermline is set back to 1
      assertEquals(2, ir.getUpdatedObjects().size());
      for (final Object o : ir.getUpdatedObjects()) {
        assertTrue(o instanceof PaymentTerm || o instanceof PaymentTermLine);
      }
    } finally {
      OBContext.restorePreviousMode();
    }
  }
  /**
   * Now do the same as in {@link #testCPaymentTerm()} only now with some small changes in the xml,
   * so that some objects are updated.
   */
  public void testDPaymentTerm() {

    setTestUserContext();
    setAccess();

    // make a copy of the paymentterms and their children so that the
    // original db is not updated
    final List<BaseOBObject> pts =
        DalUtil.copyAll(new ArrayList<BaseOBObject>(getPaymentTerms()), false);

    // change some data and export
    final PaymentTerm pt = (PaymentTerm) pts.get(0);
    pt.setName("testtest");
    pt.getFinancialMgmtPaymentTermLineList().get(0).setOverduePaymentDayRule("2");

    String xml = getXML(pts);
    xml = xml.replaceAll("</name>", "t</name>");

    setUserContext(QA_TEST_ADMIN_USER_ID);
    setAccess();
    final ImportResult ir =
        DataImportService.getInstance()
            .importDataFromXML(
                OBDal.getInstance().get(Client.class, QA_TEST_CLIENT_ID),
                OBDal.getInstance().get(Organization.class, QA_TEST_ORG_ID),
                xml);
    if (ir.getException() != null) {
      ir.getException().printStackTrace(System.err);
      fail(ir.getException().getMessage());
    }

    assertEquals(0, ir.getInsertedObjects().size());
    assertEquals(2, ir.getUpdatedObjects().size());
    for (final Object o : ir.getUpdatedObjects()) {
      assertTrue(o instanceof PaymentTerm || o instanceof PaymentTermLine);
      if (o instanceof PaymentTermLine) {
        final PaymentTermLine ptl = (PaymentTermLine) o;
        assertTrue(ir.getUpdatedObjects().contains(ptl.getPaymentTerms()));
      }
    }
  }