Esempio n. 1
0
  /**
   * Add options to the search scope field. This field determines in what communities or collections
   * to search for the query.
   *
   * <p>The scope list will depend upon the current search scope. There are three cases:
   *
   * <p>No current scope: All top level communities are listed.
   *
   * <p>The current scope is a community: All collections contained within the community are listed.
   *
   * <p>The current scope is a collection: All parent communities are listed.
   *
   * @param scope The current scope field.
   */
  protected void buildScopeList(Select scope) throws SQLException, WingException {

    DSpaceObject scopeDSO = getScope();
    if (scopeDSO == null) {
      // No scope, display all root level communities
      scope.addOption("/", T_all_of_dspace);
      scope.setOptionSelected("/");
      for (Community community : Community.findAllTop(context)) {
        scope.addOption(community.getHandle(), community.getMetadata("name"));
      }
    } else if (scopeDSO instanceof Community) {
      // The scope is a community, display all collections contained
      // within
      Community community = (Community) scopeDSO;
      scope.addOption("/", T_all_of_dspace);
      scope.addOption(community.getHandle(), community.getMetadata("name"));
      scope.setOptionSelected(community.getHandle());

      for (Collection collection : community.getCollections()) {
        scope.addOption(collection.getHandle(), collection.getMetadata("name"));
      }
    } else if (scopeDSO instanceof Collection) {
      // The scope is a collection, display all parent collections.
      Collection collection = (Collection) scopeDSO;
      scope.addOption("/", T_all_of_dspace);
      scope.addOption(collection.getHandle(), collection.getMetadata("name"));
      scope.setOptionSelected(collection.getHandle());

      Community[] communities = collection.getCommunities()[0].getAllParents();
      for (Community community : communities) {
        scope.addOption(community.getHandle(), community.getMetadata("name"));
      }
    }
  }
  public static void applyFiltersCommunity(Context c, Community community) throws Exception {
    Community[] subcommunities = community.getSubcommunities();
    for (int i = 0; i < subcommunities.length; i++) {
      applyFiltersCommunity(c, subcommunities[i]);
    }

    Collection[] collections = community.getCollections();
    for (int j = 0; j < collections.length; j++) {
      applyFiltersCollection(c, collections[j]);
    }
  }
  /**
   * Show a community home page, or deal with button press on home page
   *
   * @param context Context object
   * @param request the HTTP request
   * @param response the HTTP response
   * @param community the community
   */
  private void communityHome(
      Context context,
      HttpServletRequest request,
      HttpServletResponse response,
      Community community)
      throws ServletException, IOException, SQLException {
    // Handle click on a browse or search button
    if (!handleButton(request, response, IdentifierService.getURL(community))) {
      // No button pressed, display community home page
      log.info(
          LogManager.getHeader(context, "view_community", "community_id=" + community.getID()));

      // Get the collections within the community
      Collection[] collections = (Collection[]) community.getCollections().toArray();

      // get any subcommunities of the community
      Community[] subcommunities = (Community[]) community.getSubCommunities().toArray();

      // perform any necessary pre-processing
      preProcessCommunityHome(context, request, response, community);

      // is the user a COMMUNITY_EDITOR?
      //            if (community.canEditBoolean())
      if (AuthorizeManager.canEdit(community, context)) {
        // set a variable to create an edit button
        request.setAttribute("editor_button", new Boolean(true));
      }

      // can they add to this community?
      if (AuthorizeManager.authorizeActionBoolean(context, community, Constants.ADD)) {
        // set a variable to create an edit button
        request.setAttribute("add_button", new Boolean(true));
      }

      // can they remove from this community?
      if (AuthorizeManager.authorizeActionBoolean(context, community, Constants.REMOVE)) {
        // set a variable to create an edit button
        request.setAttribute("remove_button", new Boolean(true));
      }

      // Forward to community home page
      request.setAttribute("community", community);
      request.setAttribute("collections", collections);
      request.setAttribute("subcommunities", subcommunities);
      JSPManager.showJSP(request, response, "/community-home.jsp");
    }
  }
  /**
   * Get a preserved community backup file and respective children from cloud.
   *
   * @param context context DSpace
   * @param ref ID of the community
   * @param establishConnection true if pretend establish connection to cloud
   * @return true if file correctly sent to cloud, or false if not
   */
  public Boolean getCommunityAndChilds(Context context, Integer ref, Boolean establishConnection) {
    // if true make the connection available
    if (establishConnection == true) {
      this.makeConnection();
      this.filesInCloud.putAll(this.newCloudConnection.getInfoFilesIn(Constants.COMMUNITY));
    }

    // get file community from cloud
    getCommunity(context, ref, false);

    Community obj;
    Community[] subCommunities;
    Collection[] collections;

    // get community and the respective sub-communities and childs
    try {
      obj = Community.find(context, ref);
      subCommunities = obj.getSubcommunities();
      collections = obj.getCollections();
    } catch (Exception ex) {
      // it means it is the first father in the order, so close connection
      if (establishConnection == true) this.closeConnection();
      Logger.getLogger(ActualContentManagement.class.getName()).log(Level.SEVERE, null, ex);
      return false;
    }

    // get from cloud all the respective files sub-communities and childs
    if (subCommunities.length != 0) {
      for (int i = 0; i < subCommunities.length; i++)
        getCommunityAndChilds(context, subCommunities[i].getID(), false);
    }

    // get from cloud all files collections and childs
    if (collections.length != 0) {
      for (int i = 0; i < collections.length; i++)
        getCollectionAndChilds(context, collections[i].getID(), false);
    }

    // it means it is the first father in the order
    if (establishConnection == true) this.closeConnection();

    return true;
  }
  protected void doDSGet(Context context, HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException, SQLException, AuthorizeException {
    // Get the query
    String query = request.getParameter("query");
    int start = UIUtil.getIntParameter(request, "start");
    String advanced = request.getParameter("advanced");
    String fromAdvanced = request.getParameter("from_advanced");
    int sortBy = UIUtil.getIntParameter(request, "sort_by");
    String order = request.getParameter("order");
    int rpp = UIUtil.getIntParameter(request, "rpp");
    String advancedQuery = "";
    HashMap queryHash = new HashMap();

    // can't start earlier than 0 in the results!
    if (start < 0) {
      start = 0;
    }

    int collCount = 0;
    int commCount = 0;
    int itemCount = 0;

    Item[] resultsItems;
    Collection[] resultsCollections;
    Community[] resultsCommunities;

    QueryResults qResults = null;
    QueryArgs qArgs = new QueryArgs();
    SortOption sortOption = null;

    if (request.getParameter("etal") != null)
      qArgs.setEtAl(UIUtil.getIntParameter(request, "etal"));

    try {
      if (sortBy > 0) {
        sortOption = SortOption.getSortOption(sortBy);
        qArgs.setSortOption(sortOption);
      }

      if (SortOption.ASCENDING.equalsIgnoreCase(order)) {
        qArgs.setSortOrder(SortOption.ASCENDING);
      } else {
        qArgs.setSortOrder(SortOption.DESCENDING);
      }
    } catch (Exception e) {
    }

    if (rpp > 0) {
      qArgs.setPageSize(rpp);
    }

    // if the "advanced" flag is set, build the query string from the
    // multiple query fields
    if (advanced != null) {
      query = qArgs.buildQuery(request);
      advancedQuery = qArgs.buildHTTPQuery(request);
    }

    // Ensure the query is non-null
    if (query == null) {
      query = "";
    }

    // Get the location parameter, if any
    String location = request.getParameter("location");
    String newURL;

    // If there is a location parameter, we should redirect to
    // do the search with the correct location.
    if ((location != null) && !location.equals("")) {
      String url = "";

      if (!location.equals("/")) {
        // Location points to a resource
        url = "/resource/" + location;
      }

      // Encode the query
      query = URLEncoder.encode(query, Constants.DEFAULT_ENCODING);

      if (advancedQuery.length() > 0) {
        query = query + "&from_advanced=true&" + advancedQuery;
      }

      // Do the redirect
      response.sendRedirect(
          response.encodeRedirectURL(
              request.getContextPath() + url + "/simple-search?query=" + query));

      return;
    }

    // Build log information
    String logInfo = "";

    // Get our location
    Community community = UIUtil.getCommunityLocation(request);
    Collection collection = UIUtil.getCollectionLocation(request);

    // get the start of the query results page
    //        List resultObjects = null;
    qArgs.setQuery(query);
    qArgs.setStart(start);

    // Perform the search
    if (collection != null) {
      logInfo = "collection_id=" + collection.getID() + ",";

      // Values for drop-down box
      request.setAttribute("community", community);
      request.setAttribute("collection", collection);

      qResults = DSQuery.doQuery(context, qArgs, collection);
    } else if (community != null) {
      logInfo = "community_id=" + community.getID() + ",";

      request.setAttribute("community", community);

      // Get the collections within the community for the dropdown box
      request.setAttribute("collection.array", community.getCollections());

      qResults = DSQuery.doQuery(context, qArgs, community);
    } else {
      // Get all communities for dropdown box
      //            Community[] communities = Community.findAll(context);
      Community[] communities =
          (Community[]) ApplicationService.findAllCommunities(context).toArray();
      request.setAttribute("community.array", communities);

      qResults = DSQuery.doQuery(context, qArgs);
    }

    // now instantiate the results and put them in their buckets
    for (int i = 0; i < qResults.getHitTypes().size(); i++) {
      String myURI = (String) qResults.getHitURIs().get(i);
      Integer myType = (Integer) qResults.getHitTypes().get(i);

      // add the URI to the appropriate lists
      switch (myType.intValue()) {
        case Constants.ITEM:
          itemCount++;
          break;

        case Constants.COLLECTION:
          collCount++;
          break;

        case Constants.COMMUNITY:
          commCount++;
          break;
      }
    }

    // Make objects from the URIs - make arrays, fill them out
    resultsCommunities = new Community[commCount];
    resultsCollections = new Collection[collCount];
    resultsItems = new Item[itemCount];

    for (int i = 0; i < qResults.getHitTypes().size(); i++) {
      Integer myId = (Integer) qResults.getHitIds().get(i);
      String myURI = (String) qResults.getHitURIs().get(i);
      Integer myType = (Integer) qResults.getHitTypes().get(i);

      switch (myType.intValue()) {
        case Constants.ITEM:
          if (myId != null) {
            //                    resultsItems[itemCount] = Item.find(context, myId);
            resultsItems[itemCount] = ApplicationService.get(context, Item.class, myId);
          } else {
            ObjectIdentifier oi = ObjectIdentifier.parseCanonicalForm(myURI);
            resultsItems[itemCount] = (Item) oi.getObject(context);
          }

          if (resultsItems[itemCount] == null) {
            throw new SQLException("Query \"" + query + "\" returned unresolvable item");
          }
          itemCount++;
          break;

        case Constants.COLLECTION:
          if (myId != null) {
            //                    resultsCollections[collCount] = Collection.find(context, myId);
            resultsCollections[collCount] = ApplicationService.get(context, Collection.class, myId);
          } else {
            ObjectIdentifier oi = ObjectIdentifier.parseCanonicalForm(myURI);
            resultsCollections[collCount] = (Collection) oi.getObject(context);
          }

          if (resultsCollections[collCount] == null) {
            throw new SQLException("Query \"" + query + "\" returned unresolvable collection");
          }

          collCount++;
          break;

        case Constants.COMMUNITY:
          if (myId != null) {
            //                    resultsCommunities[commCount] = Community.find(context, myId);
            resultsCommunities[commCount] = ApplicationService.get(context, Community.class, myId);
          } else {
            ObjectIdentifier oi = ObjectIdentifier.parseCanonicalForm(myURI);
            resultsCommunities[commCount] = (Community) oi.getObject(context);
          }

          if (resultsCommunities[commCount] == null) {
            throw new SQLException("Query \"" + query + "\" returned unresolvable community");
          }

          commCount++;
          break;
      }
    }

    // Log
    log.info(
        LogManager.getHeader(
            context,
            "search",
            logInfo
                + "query=\""
                + query
                + "\",results=("
                + resultsCommunities.length
                + ","
                + resultsCollections.length
                + ","
                + resultsItems.length
                + ")"));

    // Pass in some page qualities
    // total number of pages
    int pageTotal = 1 + ((qResults.getHitCount() - 1) / qResults.getPageSize());

    // current page being displayed
    int pageCurrent = 1 + (qResults.getStart() / qResults.getPageSize());

    // pageLast = min(pageCurrent+9,pageTotal)
    int pageLast = ((pageCurrent + 9) > pageTotal) ? pageTotal : (pageCurrent + 9);

    // pageFirst = max(1,pageCurrent-9)
    int pageFirst = ((pageCurrent - 9) > 1) ? (pageCurrent - 9) : 1;

    // Pass the results to the display JSP
    request.setAttribute("items", resultsItems);
    request.setAttribute("communities", resultsCommunities);
    request.setAttribute("collections", resultsCollections);

    request.setAttribute("pagetotal", new Integer(pageTotal));
    request.setAttribute("pagecurrent", new Integer(pageCurrent));
    request.setAttribute("pagelast", new Integer(pageLast));
    request.setAttribute("pagefirst", new Integer(pageFirst));

    request.setAttribute("queryresults", qResults);

    // And the original query string
    request.setAttribute("query", query);

    request.setAttribute("order", qArgs.getSortOrder());
    request.setAttribute("sortedBy", sortOption);

    if ((fromAdvanced != null) && (qResults.getHitCount() == 0)) {
      // send back to advanced form if no results
      Community[] communities =
          (Community[]) ApplicationService.findAllCommunities(context).toArray();
      //            Community[] communities = Community.findAll(context);
      request.setAttribute("communities", communities);
      request.setAttribute("no_results", "yes");

      queryHash = qArgs.buildQueryHash(request);

      Iterator i = queryHash.keySet().iterator();

      while (i.hasNext()) {
        String key = (String) i.next();
        String value = (String) queryHash.get(key);

        request.setAttribute(key, value);
      }

      JSPManager.showJSP(request, response, "/search/advanced.jsp");
    } else {
      JSPManager.showJSP(request, response, "/search/results.jsp");
    }
  }