Exemplo n.º 1
0
  /* (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;
    }
  }