Ejemplo n.º 1
0
  /**
   * Method for invoking subscriptions via the command line
   *
   * @param argv command-line arguments, none used yet
   */
  public static void main(String[] argv) {
    log.info("#### START DELETE: -----" + new Date() + " ----- ####");
    String usage =
        "it.cilea.hku.authority.ScriptRPSubscribe [-t] or nothing to send out subscriptions.";

    Options options = new Options();
    HelpFormatter formatter = new HelpFormatter();
    CommandLine line = null;

    {
      Option opt = new Option("t", "test", false, "Run test session");
      opt.setRequired(false);
      options.addOption(opt);
    }

    {
      Option opt = new Option("h", "help", false, "Print this help message");
      opt.setRequired(false);
      options.addOption(opt);
    }

    try {
      line = new PosixParser().parse(options, argv);
    } catch (Exception e) {
      // automatically generate the help statement
      formatter.printHelp(usage, e.getMessage(), options, "");
      System.exit(1);
    }

    if (line.hasOption("h")) {
      // automatically generate the help statement
      formatter.printHelp(usage, options);
      System.exit(1);
    }

    boolean test = line.hasOption("t");

    if (test) log.setLevel(Level.DEBUG);
    Context context = null;
    try {
      context = new Context();
      Researcher researcher = new Researcher();
      ApplicationService applicationService = researcher.getApplicationService();

      List<CrisComponentsService> serviceComponent = researcher.getAllCrisComponents();
      for (CrisComponentsService service : serviceComponent) {
        for (ICRISComponent component : service.getComponents().values()) {
          RelationConfiguration relationConfiguration = component.getRelationConfiguration();
          if (Item.class.isAssignableFrom(relationConfiguration.getRelationClass())) {
            Integer key = CrisConstants.getEntityType(component.getTarget());
            String query = relationConfiguration.getQuery();
            if (!mapRelationFields.containsKey(key)) {
              List<String> rels = new LinkedList<String>();
              rels.add(query);
              mapRelationFields.put(key, rels);
            } else {
              mapRelationFields.get(key).add(query);
            }
          }
        }
      }
      processDaily(researcher, applicationService, context, test);
    } catch (Exception e) {
      log.error(e.getMessage(), e);
    } finally {
      if (context != null && context.isValid()) {
        // Nothing is actually written
        context.abort();
      }
    }
    log.info("#### END: -----" + new Date() + " ----- ####");
    System.exit(0);
  }
Ejemplo n.º 2
0
  /**
   * Sends an email to the given e-person with details of new items in the given dspace object (MUST
   * be a community or a collection), items that appeared yesterday. No e-mail is sent if there
   * aren't any new items in any of the dspace objects.
   *
   * @param context DSpace context object
   * @param eperson eperson to send to
   * @param rpkeys List of DSpace Objects
   * @param test
   * @throws SearchServiceException
   */
  public static void sendEmail(
      Researcher researcher,
      Context context,
      EPerson eperson,
      List<String> rpkeys,
      boolean test,
      List<String> relationFields)
      throws IOException, MessagingException, SQLException, SearchServiceException {

    CrisSearchService searchService = researcher.getCrisSearchService();

    // Get a resource bundle according to the eperson language preferences
    Locale supportedLocale = I18nUtil.getEPersonLocale(eperson);

    StringBuffer emailText = new StringBuffer();
    boolean isFirst = true;

    for (String rpkey : rpkeys) {
      SolrQuery query = new SolrQuery();
      query.setFields("search.resourceid");
      query.addFilterQuery(
          "{!field f=search.resourcetype}" + Constants.ITEM, "{!field f=inarchive}true");

      for (String tmpRelations : relationFields) {
        String fq = "{!field f=" + tmpRelations + "}" + rpkey;
        query.addFilterQuery(fq);
      }

      query.setRows(Integer.MAX_VALUE);

      if (ConfigurationManager.getBooleanProperty("eperson.subscription.onlynew", false)) {
        // get only the items archived yesterday
        query.setQuery("dateaccessioned:(NOW/DAY-1DAY)");
      } else {
        // get all item modified yesterday but not published the day
        // before
        // and all the item modified today and archived yesterday
        query.setQuery(
            "(item.lastmodified:(NOW/DAY-1DAY) AND dateaccessioned:(NOW/DAY-1DAY)) OR ((item.lastmodified:(NOW/DAY) AND dateaccessioned:(NOW/DAY-1DAY)))");
      }

      QueryResponse qResponse = searchService.search(query);
      SolrDocumentList results = qResponse.getResults();

      // Only add to buffer if there are new items
      if (results.getNumFound() > 0) {

        if (!isFirst) {
          emailText.append("\n---------------------------------------\n");
        } else {
          isFirst = false;
        }

        emailText
            .append(I18nUtil.getMessage("org.dspace.eperson.Subscribe.new-items", supportedLocale))
            .append(" ")
            .append(rpkey)
            .append(": ")
            .append(results.getNumFound())
            .append("\n\n");

        for (SolrDocument solrDoc : results) {

          Item item = Item.find(context, (Integer) solrDoc.getFieldValue("search.resourceid"));

          DCValue[] titles = item.getDC("title", null, Item.ANY);
          emailText
              .append("      ")
              .append(I18nUtil.getMessage("org.dspace.eperson.Subscribe.title", supportedLocale))
              .append(" ");

          if (titles.length > 0) {
            emailText.append(titles[0].value);
          } else {
            emailText.append(
                I18nUtil.getMessage("org.dspace.eperson.Subscribe.untitled", supportedLocale));
          }

          DCValue[] authors = item.getDC("contributor", Item.ANY, Item.ANY);

          if (authors.length > 0) {
            emailText
                .append("\n    ")
                .append(
                    I18nUtil.getMessage("org.dspace.eperson.Subscribe.authors", supportedLocale))
                .append(" ")
                .append(authors[0].value);

            for (int k = 1; k < authors.length; k++) {
              emailText.append("\n             ").append(authors[k].value);
            }
          }

          emailText
              .append("\n         ")
              .append(I18nUtil.getMessage("org.dspace.eperson.Subscribe.id", supportedLocale))
              .append(" ")
              .append(HandleManager.getCanonicalForm(item.getHandle()))
              .append("\n\n");
          context.removeCached(item, item.getID());
        }
      }
    }

    // Send an e-mail if there were any new items
    if (emailText.length() > 0) {

      if (test) {
        log.info(LogManager.getHeader(context, "subscription:", "eperson=" + eperson.getEmail()));
        log.info(LogManager.getHeader(context, "subscription:", "text=" + emailText.toString()));

      } else {

        Email email =
            ConfigurationManager.getEmail(
                I18nUtil.getEmailFilename(supportedLocale, "subscription"));
        email.addRecipient(eperson.getEmail());
        email.addArgument(emailText.toString());
        email.send();

        log.info(
            LogManager.getHeader(context, "sent_subscription", "eperson_id=" + eperson.getID()));
      }
    }
  }