/** @see org.jpublish.ErrorHandler#handleError(JPublishError) */
  public boolean handleError(Throwable error, WebPageRequest context) {
    if (context.getResponse() != null
        && context.getResponse().getContentType() != null
        && !context.getResponse().getContentType().contains("json")) {
      return false;
    }

    OpenEditException exception = null;
    if (context != null) {
      try {
        if (!(error instanceof OpenEditException)) {
          exception = new OpenEditException(error); // we need the toStacktrace method
        } else {
          exception = (OpenEditException) error;
        }
        if (!context.hasRedirected() && context.getResponse() != null) {
          try {
            context.getResponse().setStatus(500);
          } catch (Exception ex) {
            // ignored
            log.debug("Ignored:" + ex);
          }
        }
        error.printStackTrace();
        String pathWithError = exception.getPathWithError();
        if (pathWithError == null) {
          pathWithError = context.getPage().getPath();
          exception.setPathWithError(pathWithError);
        }
        context.putPageValue("editPath", exception.getPathWithError());
        context.putPageValue(
            "oe-exception", exception); // must be a top level thing since we create a new context
        PageStreamer pages = (PageStreamer) context.getPageValue(PageRequestKeys.PAGES);

        Writer out = context.getWriter();
        out.append("{ \"reponse\": {\n");
        out.append(" \"status\":\"error\",");
        out.append("{ \"path\":\"" + pathWithError + "\",");
        out.append("{ \"details\":\"" + error + "\"");
        out.append("\n}");
        // error.printStackTrace( new PrintWriter( writer ) );
        out.flush();
      } catch (Exception ex) {
        // Do not throw an error here is it will be infinite
        log.error(ex);
        ex.printStackTrace();
        try {
          context.getWriter().write("Check error logs: " + ex);
          // throw new OpenEditRuntimeException(ex);
        } catch (Throwable ex1) {
          log.error(ex1);
        }
      }
      return true;
    }
    return false;
  }
  public HitTracker findOrdersForUser(WebPageRequest req) {
    String catalogid = req.findValue("catalogid");
    User owner = (User) req.getPageValue("owner");
    if (owner == null) {
      owner = req.getUser();
    }
    HitTracker orders = getOrderManager().findOrdersForUser(catalogid, owner);
    req.putPageValue("orders", orders);
    req.putPageValue("searcher", getSearcherManager().getSearcher(catalogid, "order"));

    return orders;
  }
  public Order loadOrder(WebPageRequest inReq) {
    Order order = (Order) inReq.getPageValue("order");
    if (order != null) {
      return order;
    }

    String catalogid = inReq.findValue("catalogid");
    String orderid = inReq.findValue("orderid");
    if (orderid == null) {
      orderid = inReq.getRequestParameter("id");
    }
    if (orderid == null) {
      return null;
    }
    order = getOrderManager().loadOrder(catalogid, orderid);
    inReq.putPageValue("order", order);
    return order;
  }
 public void filterOrderItems(WebPageRequest req) {
   ArrayList<String> list = new ArrayList<String>(); // add omitted orders to a list
   String publishtype = req.getRequestParameter("publishdestination.value");
   String catalogid = req.findValue("catalogid");
   HitTracker items = (HitTracker) req.getPageValue("orderitems");
   if (items == null) {
     Order order = loadOrder(req);
     if (order != null) {
       String orderid = order.getId();
       if (orderid == null) {
         orderid = req.getRequestParameter("orderid");
       }
       items = getOrderManager().findOrderItems(req, catalogid, order);
     }
   }
   if (items != null) {
     // get searchers
     Searcher publishdestsearcher =
         getMediaArchive(req).getSearcherManager().getSearcher(catalogid, "publishdestination");
     Data publisher = (Data) publishdestsearcher.searchById(publishtype);
     String publishername = publisher.getName();
     Searcher convertpresetsearcher =
         getMediaArchive(req).getSearcherManager().getSearcher(catalogid, "convertpreset");
     // see if convertpreset has the appropriate field
     String publishtofield = "publishto" + publishername.replace(" ", "").toLowerCase();
     if (convertpresetsearcher.getDetail(publishtofield) != null) // field is present
     {
       for (int i = 0; i < items.size(); i++) {
         Data data = items.get(i);
         Asset asset = getMediaArchive(req).getAsset(data.get("assetid"));
         String fileformat = asset.get("fileformat");
         String rendertype = getMediaArchive(req).getMediaRenderType(fileformat);
         // build query
         SearchQuery presetquery = convertpresetsearcher.createSearchQuery();
         presetquery.append(publishtofield, "true").append("inputtype", rendertype);
         // execute query
         HitTracker hits = convertpresetsearcher.search(presetquery);
         if (hits.size() > 0) continue;
         list.add(asset.getId());
       }
     }
   }
   req.putPageValue("invaliditems", list); // process this in step2
 }
  public Order loadOrderBasket(WebPageRequest inReq) {
    Order basket = null;
    try {
      MediaArchive archive = getMediaArchive(inReq);
      basket = (Order) inReq.getPageValue("orderbasket");

      if (basket == null) {
        String id = inReq.getUserName() + "_orderbasket";
        String appid = inReq.findValue("applicationid");
        Searcher searcher = getSearcherManager().getSearcher(archive.getCatalogId(), "order");
        basket = (Order) searcher.searchById(id);
        if (basket == null) {
          basket =
              getOrderManager().createNewOrder(appid, archive.getCatalogId(), inReq.getUserName());
          basket.setId(id);
          basket.setProperty("ordertype", "basket");
          getOrderManager().saveOrder(archive.getCatalogId(), inReq.getUser(), basket);
        }
        basket.setProperty("basket", "true");
        basket.setProperty("ordertype", "basket");

        inReq.putSessionValue("orderbasket", basket);
      }
      inReq.putPageValue("order", basket);

      HitTracker items =
          loadOrderManager(inReq).findOrderItems(inReq, archive.getCatalogId(), basket);
      inReq.putPageValue("orderitems", items);

      String check = inReq.findValue("clearmissing");
      if (Boolean.parseBoolean(check)) {
        // Make sure these have the same number of assets found
        getOrderManager().removeMissingAssets(inReq, archive, basket, items);
      }
    } catch (Throwable ex) {
      log.error(ex);
    }

    return basket;
  }
  public boolean checkItemApproval(WebPageRequest inReq) throws Exception {

    if (inReq.getUser() == null) {
      return false;
    }
    MediaArchive archive = getMediaArchive(inReq);

    // Searcher ordersearcher =
    // archive.getSearcherManager().getSearcher(archive.getCatalogId(),
    // "order");
    // SearchQuery search = ordersearcinKeyher.createSearchQuery();
    // search.addExact("userid", inReq.getUser().getId());
    // search.addExact("orderstatus", "processed");
    // search.addSortBy("date");
    // HitTracker hits = ordersearcher.search(search);
    // look for the most recent order for an approved asset
    Asset asset = (Asset) inReq.getPageValue("asset");
    String sourcepath = null;
    if (asset != null) {
      sourcepath = asset.getSourcePath();
    } else {
      sourcepath = archive.getSourcePathForPage(inReq);
    }
    if (sourcepath == null) {
      return false;
    }
    Searcher itemsearcher =
        archive.getSearcherManager().getSearcher(archive.getCatalogId(), "orderitem");
    SearchQuery search = itemsearcher.createSearchQuery();
    search.addExact("userid", inReq.getUser().getId());
    search.addExact("assetsourcepath", sourcepath);
    search.addMatches("status", "approved");
    HitTracker results = itemsearcher.search(search);
    if (results.size() > 0) {
      return true;
    }
    return false;
  }