📄 subscribe.java
字号:
* only sent out when a collection has actually received new items, so that * people's mailboxes are not clogged with many "no new items" mails. * <P> * Yesterday's newly available items are included. If this is run at for * example midday, any items that have been made available during the * current day will not be included, but will be included in the next day's * run. * <P> * For example, if today's date is 2002-10-10 (in UTC) items made available * during 2002-10-09 (UTC) will be included. * * @param context * DSpace context object */ public static void processDaily(Context context) throws SQLException, IOException { // Grab the subscriptions TableRowIterator tri = DatabaseManager.query(context, "SELECT * FROM subscription ORDER BY eperson_id"); EPerson currentEPerson = null; List collections = null; // List of Collections // Go through the list collating subscriptions for each e-person while (tri.hasNext()) { TableRow row = tri.next(); // Does this row relate to the same e-person as the last? if ((currentEPerson == null) || (row.getIntColumn("eperson_id") != currentEPerson .getID())) { // New e-person. Send mail for previous e-person if (currentEPerson != null) { try { sendEmail(context, currentEPerson, collections); } catch (MessagingException me) { log.error("Failed to send subscription to eperson_id=" + currentEPerson.getID()); log.error(me); } } currentEPerson = EPerson.find(context, row .getIntColumn("eperson_id")); collections = new ArrayList(); } collections.add(Collection.find(context, row .getIntColumn("collection_id"))); } // Process the last person if (currentEPerson != null) { try { sendEmail(context, currentEPerson, collections); } catch (MessagingException me) { log.error("Failed to send subscription to eperson_id=" + currentEPerson.getID()); log.error(me); } } } /** * Sends an email to the given e-person with details of new items in the * given collections, items that appeared yesterday. No e-mail is sent if * there aren't any new items in any of the collections. * * @param context * DSpace context object * @param eperson * eperson to send to * @param collections * List of collection IDs (Integers) */ public static void sendEmail(Context context, EPerson eperson, List collections) throws IOException, MessagingException, SQLException { // Get the start and end dates for yesterday Date thisTimeYesterday = new Date(System.currentTimeMillis() - (24 * 60 * 60 * 1000)); DCDate dcDateYesterday = new DCDate(thisTimeYesterday); // this time yesterday in ISO 8601, stripping the time String isoDateYesterday = dcDateYesterday.toString().substring(0, 10); String startDate = isoDateYesterday; String endDate = isoDateYesterday + "T23:59:59Z"; // FIXME: text of email should be more configurable from an // i18n viewpoint StringBuffer emailText = new StringBuffer(); boolean isFirst = true; for (int i = 0; i < collections.size(); i++) { Collection c = (Collection) collections.get(i); List itemInfos = Harvest.harvest(context, c, startDate, endDate, 0, // Limit // and // offset // zero, // get // everything 0, true, // Need item objects false, // But not containers false); // Or withdrawals // Only add to buffer if there are new items if (itemInfos.size() > 0) { if (!isFirst) { emailText .append("\n---------------------------------------\n"); } else { isFirst = false; } emailText.append("New items in collection ").append( c.getMetadata("name")).append(": ").append( itemInfos.size()).append("\n\n"); for (int j = 0; j < itemInfos.size(); j++) { HarvestedItemInfo hii = (HarvestedItemInfo) itemInfos .get(j); DCValue[] titles = hii.item.getDC("title", null, Item.ANY); emailText.append(" Title: "); if (titles.length > 0) { emailText.append(titles[0].value); } else { emailText.append("Untitled"); } DCValue[] authors = hii.item.getDC("contributor", Item.ANY, Item.ANY); if (authors.length > 0) { emailText.append("\n Authors: ").append( authors[0].value); for (int k = 1; k < authors.length; k++) { emailText.append("\n ").append( authors[k].value); } } emailText.append("\n ID: ").append( HandleManager.getCanonicalForm(hii.handle)).append( "\n\n"); } } } // Send an e-mail if there were any new items if (emailText.length() > 0) { Email email = ConfigurationManager.getEmail("subscription"); email.addRecipient(eperson.getEmail()); email.addArgument(emailText.toString()); email.send(); log.info(LogManager.getHeader(context, "sent_subscription", "eperson_id=" + eperson.getID())); } } /** * Method for invoking subscriptions via the command line * * @param argv * command-line arguments, none used yet */ public static void main(String[] argv) { Context context = null; try { context = new Context(); processDaily(context); context.complete(); } catch( Exception e ) { log.fatal(e); } finally { if( context != null && context.isValid() ) { // Nothing is actually written context.abort(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -