private ProductPublicationStatus publishWorkProduct(
      EDXLDistribution edxl, DigestType digest, WorkProductDocument currentWorkProduct) {
    log.debug("Creating work product for HAVE message");
    ValidationUtil.validate(edxl, true);
    ValidationUtil.validate(digest, true);

    // Create a work product
    WorkProduct workProduct = new WorkProduct();
    workProduct.setProductType(HAVE_PRODUCT_TYPE);

    // If updating set the work product identification
    if (currentWorkProduct != null && currentWorkProduct.getWorkProduct() != null) {
      IdentificationType id =
          WorkProductHelper.getIdentificationElement(currentWorkProduct.getWorkProduct());
      if (id != null) {
        try {
          Integer version = Integer.parseInt(id.getVersion().getStringValue());
          workProduct.setProductVersion(version);
          workProduct.setChecksum(id.getChecksum().getStringValue());
          workProduct.setProductID(id.getIdentifier().getStringValue());
          workProduct.setProductType(id.getType().getStringValue());
          workProduct.setActive(id.getState() == StateType.ACTIVE);
        } catch (NumberFormatException e) {
          log.error("Error parsing version number to update HAVE message: " + e.getMessage());
        }
      } else {
        log.error("Cannot find WorkProductIdentification in matching HAVE message");
      }
    }

    //        workProduct.setProductID(wpIDBuffer.toString());
    EDXLDistributionDocument edxlDoc = EDXLDistributionDocument.Factory.newInstance();
    edxlDoc.addNewEDXLDistribution().set(edxl);
    workProduct.setProduct(edxlDoc.xmlText().getBytes());

    DigestDocument digestDoc = DigestDocument.Factory.newInstance();
    digestDoc.setDigest((DigestType) digest.copy());
    workProduct.setDigest(digestDoc);

    if (edxl.sizeOfContentObjectArray() > 0
        && edxl.getContentObjectArray(0).getIncidentID() != null) {
      workProduct
          .getAssociatedInterestGroupIDs()
          .add(edxl.getContentObjectArray(0).getIncidentID());
    }
    //        System.out.println(WorkProductHelper.toWorkProductXmlDocument(workProduct));

    // publish the work product
    ProductPublicationStatus status = workProductService.publishProduct(workProduct);

    return status;
  }
  private WorkProductList createWorkProductList(List<WorkProduct> workProducts, String incidentID) {
    WorkProductList productList = WorkProductList.Factory.newInstance();

    if (workProducts != null && workProducts.size() > 0) {
      for (WorkProduct product : workProducts) {
        if (incidentID == null
            || (product.getFirstAssociatedInterestGroupID() != null
                && product.getFirstAssociatedInterestGroupID().equals(incidentID))) {
          productList.addNewWorkProduct().set(WorkProductHelper.toWorkProductSummary(product));
        }
      }
    }

    return productList;
  }
  @Override
  public WorkProductDocument findHAVEMessage(String distributionReference) {
    WorkProductDocument workProduct = WorkProductDocument.Factory.newInstance();
    workProduct.addNewWorkProduct();

    String[] elements = distributionReference.split(",");
    if (elements.length == 3) {
      StringBuffer sb = new StringBuffer();
      String DID = "/de:EDXLDistribution[de:distributionID/text()='";
      // /urn:EdxlDeRequest/de:EDXLDistribution[de:distributionID='DE_DISTRIBUTION_ID']
      sb.append(DID);
      sb.append(elements[0]);
      sb.append("']");
      // and /urn:EdxlDeRequest/de:EDXLDistribution[de:senderID='RMApplication1@core1']
      sb.append("and /de:EDXLDistribution[de:senderID='");
      sb.append(elements[1]);
      sb.append("']");
      // /de:EDXLDistribution[de:dateTimeSent='2010-11-11T13:42:36.890-05:00']
      sb.append("and /de:EDXLDistribution[de:dateTimeSent='");
      sb.append(elements[2]);
      sb.append("']");
      Map<String, String> namespaceMap = new HashMap<String, String>();
      namespaceMap.put("de", "urn:oasis:names:tc:emergency:EDXL:DE:1.0");
      try {
        List<WorkProduct> list =
            workProductService.getProductByTypeAndXQuery(
                HAVE_PRODUCT_TYPE, sb.toString(), namespaceMap);
        if (list.size() == 1) {
          WorkProduct wp = list.get(0);
          workProduct.setWorkProduct(WorkProductHelper.toWorkProduct(wp));
        }
      } catch (InvalidXpathException e) {
        log.error("Invalid XPath expression finding HAVE message: " + e.getMessage());
      }
    }

    return workProduct;
  }