/* (non-Javadoc)
   * @see gov.nih.nci.cadsr.cdecart.CdeCartUtilInterface#deleteCartNodes(javax.servlet.http.HttpSession, java.lang.String, java.lang.String[])
   */
  @Override
  public void deleteCartNodes(
      final HttpSession mySession, final String principalName, final String[] ids)
      throws Exception {
    if ((ids == null) || (ids.length == 0)) {
      log.warn("Nothing to delete no ID received");
      return;
    }

    if (mySession == null) {
      // this shall never happen; controller provides the session
      log.warn("Session is not found");
      return;
    }

    // we shall be after login here, and principalName is never null
    if (principalName == null) {
      log.error("........No user found in session in findCartNodes");
      throw new AutheticationFailureException("Authenticated user not found in the session");
    }
    try {
      if (ocURL == null) { // try to get it again for a chance fixed in DB
        ToolOptionsModel cdeCartOptionsModel =
            toolOptionsDAO.getToolOptionsByToolNameAndProperty("ObjectCartAPI", "URL");
        if (cdeCartOptionsModel != null) {
          ocURL = cdeCartOptionsModel.getValue();
        }
        if (ocURL == null) {
          log.warn("Cannot get a value of ObjectCart URL from the system configuration");
        }
      }

      ObjectCartClient ocClient = null;

      if (!ocURL.equals("")) ocClient = new ObjectCartClient(ocURL);
      else ocClient = new ObjectCartClient();

      // Get the cart in the session
      CDECart sessionCart = (CDECart) mySession.getAttribute(CaDSRConstants.CDE_CART);

      CDECart userCart = new CDECartOCImpl(ocClient, principalName, CaDSRConstants.CDE_CART);

      Collection<String> items = Arrays.asList(ids);

      sessionCart.removeDataElements(items);
      userCart.removeDataElements(items);
    } catch (ObjectCartException oce) {
      log.error("Exception on cdeCart.getDataElements", oce);
      throw oce;
    }
  }
  protected CDECart findCdeCart(final HttpSession mySession, final String principalName)
      throws ObjectCartException, AutheticationFailureException {

    CDECart cdeCart = (CDECart) mySession.getAttribute(CaDSRConstants.CDE_CART);
    String uid = null;
    if (cdeCart == null) {
      if (ocURL == null) { // try to get it again for a chance fixed in DB
        ToolOptionsModel cdeCartOptionsModel =
            toolOptionsDAO.getToolOptionsByToolNameAndProperty("ObjectCartAPI", "URL");
        if (cdeCartOptionsModel != null) {
          ocURL = cdeCartOptionsModel.getValue();
        }
        if (ocURL == null) {
          log.warn("Cannot get a valid value of ObjectCart URL from the system configuration");
        } else {
          log.debug("Found Object Cart URL:" + ocURL);
        }
      }
      ObjectCartClient ocClient = null;

      if (ocURL != null) ocClient = new ObjectCartClient(ocURL);
      else ocClient = new ObjectCartClient();

      uid = principalName;

      // we shall be after login here, and uid is never null
      if (uid == null) {
        log.error("........No user found in session in findCdeCart");
        throw new AutheticationFailureException("Authenticated user not found in the session");
      }

      cdeCart = getCDECartOCImpl(ocClient, uid);
      // we need to set up this session attribute not to make the remote call again
      mySession.setAttribute(CaDSRConstants.CDE_CART, cdeCart);
      log.info("Object Cart cdeCart attribute is added to session of: " + principalName);
    }

    return cdeCart;
  }
  /* (non-Javadoc)
   * @see gov.nih.nci.cadsr.cdecart.CdeCartUtilInterface#addToCart(javax.servlet.http.HttpSession, java.lang.String, java.util.List)
   */
  @Override
  public void addToCart(HttpSession mySession, String principalName, List<String> cdeIds)
      throws ObjectCartException, AutheticationFailureException {
    if ((cdeIds == null) || (mySession == null)) {
      log.debug("Nothing to save");
      return;
    }

    CDECart sessionCart = (CDECart) mySession.getAttribute(CaDSRConstants.CDE_CART);

    if (sessionCart == null) {
      sessionCart = findCdeCart(mySession, principalName);
    }

    String userName = principalName;

    // we shall be after login here, and userName is never null
    if (userName == null) {
      log.error("........No user found in session in addToCart");
      throw new AutheticationFailureException("Authenticated user not found in the session");
    }

    try {
      if (ocURL == null) { // try to get it again for a chance fixed in DB
        ToolOptionsModel cdeCartOptionsModel =
            toolOptionsDAO.getToolOptionsByToolNameAndProperty("ObjectCartAPI", "URL");
        if (cdeCartOptionsModel != null) {
          ocURL = cdeCartOptionsModel.getValue();
        }
        if (ocURL == null) {
          log.warn("Cannot get a value of ObjectCart URL from the system configuration");
        }
      }

      ObjectCartClient ocClient = null;

      if (!ocURL.equals("")) ocClient = new ObjectCartClient(ocURL);
      else ocClient = new ObjectCartClient();

      CDECart userCart = new CDECartOCImpl(ocClient, userName, CaDSRConstants.CDE_CART);

      List<DataElementModel> deModelList = dataElementDAO.getCdeByDeIdseqList(cdeIds);

      Collection<DataElementTransferObject> addCandidates = buildCartTransferObjects(deModelList);
      Collection<CDECartItem> items = new ArrayList<CDECartItem>();

      for (DataElementTransferObject deto : addCandidates) {
        CDECartItem cartItem = sessionCart.findDataElement(deto.getIdseq());
        if (cartItem == null) {
          CDECartItem cdeItem = new CDECartItemTransferObject();
          cdeItem.setPersistedInd(false); // we have IDs only for items to save
          cdeItem.setItem(deto);
          items.add(cdeItem);
        } else {
          cartItem.setPersistedInd(true);
          items.add(cartItem);
        }
      }

      userCart.mergeDataElements(items);
    } catch (ObjectCartException oce) {
      log.error("Exception on cdeCart.getDataElements", oce);
      throw oce;
    }
  }