1 40 package org.dspace.eperson; 41 42 import java.io.IOException ; 43 import java.sql.SQLException ; 44 import java.text.ParseException ; 45 import java.util.ArrayList ; 46 import java.util.Date ; 47 import java.util.List ; 48 49 import javax.mail.MessagingException ; 50 51 import org.apache.log4j.Logger; 52 import org.dspace.authorize.AuthorizeException; 53 import org.dspace.authorize.AuthorizeManager; 54 import org.dspace.content.Collection; 55 import org.dspace.content.DCDate; 56 import org.dspace.content.DCValue; 57 import org.dspace.content.Item; 58 import org.dspace.core.ConfigurationManager; 59 import org.dspace.core.Context; 60 import org.dspace.core.Email; 61 import org.dspace.core.LogManager; 62 import org.dspace.handle.HandleManager; 63 import org.dspace.search.Harvest; 64 import org.dspace.search.HarvestedItemInfo; 65 import org.dspace.storage.rdbms.DatabaseManager; 66 import org.dspace.storage.rdbms.TableRow; 67 import org.dspace.storage.rdbms.TableRowIterator; 68 69 75 public class Subscribe 76 { 77 78 private static Logger log = Logger.getLogger(Subscribe.class); 79 80 91 public static void subscribe(Context context, EPerson eperson, 92 Collection collection) throws SQLException , AuthorizeException 93 { 94 if (AuthorizeManager.isAdmin(context) 96 || ((context.getCurrentUser() != null) && (context 97 .getCurrentUser().getID() == eperson.getID()))) 98 { 99 TableRowIterator r = DatabaseManager.query(context, 101 "SELECT * FROM subscription WHERE eperson_id= ? " + 102 " AND collection_id= ? ", 103 eperson.getID(),collection.getID()); 104 105 if (!r.hasNext()) 106 { 107 TableRow row = DatabaseManager.create(context, "subscription"); 109 row.setColumn("eperson_id", eperson.getID()); 110 row.setColumn("collection_id", collection.getID()); 111 DatabaseManager.update(context, row); 112 113 log.info(LogManager.getHeader(context, "subscribe", 114 "eperson_id=" + eperson.getID() + ",collection_id=" 115 + collection.getID())); 116 } 117 118 r.close(); 119 } 120 else 121 { 122 throw new AuthorizeException( 123 "Only admin or e-person themselves can subscribe"); 124 } 125 } 126 127 139 public static void unsubscribe(Context context, EPerson eperson, 140 Collection collection) throws SQLException , AuthorizeException 141 { 142 if (AuthorizeManager.isAdmin(context) 144 || ((context.getCurrentUser() != null) && (context 145 .getCurrentUser().getID() == eperson.getID()))) 146 { 147 if (collection == null) 148 { 149 DatabaseManager.updateQuery(context, 151 "DELETE FROM subscription WHERE eperson_id= ? ", 152 eperson.getID()); 153 } 154 else 155 { 156 DatabaseManager.updateQuery(context, 157 "DELETE FROM subscription WHERE eperson_id= ? " + 158 "AND collection_id= ? ", 159 eperson.getID(),collection.getID()); 160 161 log.info(LogManager.getHeader(context, "unsubscribe", 162 "eperson_id=" + eperson.getID() + ",collection_id=" 163 + collection.getID())); 164 } 165 } 166 else 167 { 168 throw new AuthorizeException( 169 "Only admin or e-person themselves can unsubscribe"); 170 } 171 } 172 173 182 public static Collection[] getSubscriptions(Context context, EPerson eperson) 183 throws SQLException 184 { 185 TableRowIterator tri = DatabaseManager.query(context, 186 "SELECT collection_id FROM subscription WHERE eperson_id= ? ", 187 eperson.getID()); 188 189 List collections = new ArrayList (); 190 191 while (tri.hasNext()) 192 { 193 TableRow row = tri.next(); 194 195 collections.add(Collection.find(context, row 196 .getIntColumn("collection_id"))); 197 } 198 199 tri.close(); 200 201 Collection[] collArray = new Collection[collections.size()]; 202 203 return (Collection[]) collections.toArray(collArray); 204 } 205 206 217 public static boolean isSubscribed(Context context, EPerson eperson, 218 Collection collection) throws SQLException 219 { 220 TableRowIterator tri = DatabaseManager.query(context, 221 "SELECT * FROM subscription WHERE eperson_id= ? " + 222 "AND collection_id= ? ", 223 eperson.getID(),collection.getID()); 224 225 boolean result = tri.hasNext(); 226 tri.close(); 227 228 return result; 229 } 230 231 247 public static void processDaily(Context context) throws SQLException , 248 IOException 249 { 250 TableRowIterator tri = DatabaseManager.query(context, 252 "SELECT * FROM subscription ORDER BY eperson_id"); 253 254 EPerson currentEPerson = null; 255 List collections = null; 257 while (tri.hasNext()) 259 { 260 TableRow row = tri.next(); 261 262 if ((currentEPerson == null) 264 || (row.getIntColumn("eperson_id") != currentEPerson 265 .getID())) 266 { 267 if (currentEPerson != null) 269 { 270 271 try 272 { 273 sendEmail(context, currentEPerson, collections); 274 } 275 catch (MessagingException me) 276 { 277 log.error("Failed to send subscription to eperson_id=" 278 + currentEPerson.getID()); 279 log.error(me); 280 } 281 } 282 283 currentEPerson = EPerson.find(context, row 284 .getIntColumn("eperson_id")); 285 collections = new ArrayList (); 286 } 287 288 collections.add(Collection.find(context, row 289 .getIntColumn("collection_id"))); 290 } 291 292 tri.close(); 293 294 if (currentEPerson != null) 296 { 297 try 298 { 299 sendEmail(context, currentEPerson, collections); 300 } 301 catch (MessagingException me) 302 { 303 log.error("Failed to send subscription to eperson_id=" 304 + currentEPerson.getID()); 305 log.error(me); 306 } 307 } 308 } 309 310 322 public static void sendEmail(Context context, EPerson eperson, 323 List collections) throws IOException , MessagingException , 324 SQLException 325 { 326 Date thisTimeYesterday = new Date (System.currentTimeMillis() 328 - (24 * 60 * 60 * 1000)); 329 330 DCDate dcDateYesterday = new DCDate(thisTimeYesterday); 331 332 String isoDateYesterday = dcDateYesterday.toString().substring(0, 10); 334 335 String startDate = isoDateYesterday; 336 String endDate = isoDateYesterday + "T23:59:59Z"; 337 338 StringBuffer emailText = new StringBuffer (); 341 boolean isFirst = true; 342 343 for (int i = 0; i < collections.size(); i++) 344 { 345 Collection c = (Collection) collections.get(i); 346 347 try { 348 List itemInfos = Harvest.harvest(context, c, startDate, endDate, 0, 0, true, false, false); 358 if (itemInfos.size() > 0) 360 { 361 if (!isFirst) 362 { 363 emailText 364 .append("\n---------------------------------------\n"); 365 } 366 else 367 { 368 isFirst = false; 369 } 370 371 emailText.append("New items in collection ").append( 372 c.getMetadata("name")).append(": ").append( 373 itemInfos.size()).append("\n\n"); 374 375 for (int j = 0; j < itemInfos.size(); j++) 376 { 377 HarvestedItemInfo hii = (HarvestedItemInfo) itemInfos 378 .get(j); 379 380 DCValue[] titles = hii.item.getDC("title", null, Item.ANY); 381 emailText.append(" Title: "); 382 383 if (titles.length > 0) 384 { 385 emailText.append(titles[0].value); 386 } 387 else 388 { 389 emailText.append("Untitled"); 390 } 391 392 DCValue[] authors = hii.item.getDC("contributor", Item.ANY, 393 Item.ANY); 394 395 if (authors.length > 0) 396 { 397 emailText.append("\n Authors: ").append( 398 authors[0].value); 399 400 for (int k = 1; k < authors.length; k++) 401 { 402 emailText.append("\n ").append( 403 authors[k].value); 404 } 405 } 406 407 emailText.append("\n ID: ").append( 408 HandleManager.getCanonicalForm(hii.handle)).append( 409 "\n\n"); 410 } 411 } 412 } 413 catch (ParseException pe) 414 { 415 } 417 } 418 419 if (emailText.length() > 0) 421 { 422 Email email = ConfigurationManager.getEmail("subscription"); 423 424 email.addRecipient(eperson.getEmail()); 425 email.addArgument(emailText.toString()); 426 email.send(); 427 428 log.info(LogManager.getHeader(context, "sent_subscription", 429 "eperson_id=" + eperson.getID())); 430 } 431 } 432 433 439 public static void main(String [] argv) 440 { 441 Context context = null; 442 443 try 444 { 445 context = new Context(); 446 processDaily(context); 447 context.complete(); 448 } 449 catch( Exception e ) 450 { 451 log.fatal(e); 452 } 453 finally 454 { 455 if( context != null && context.isValid() ) 456 { 457 context.abort(); 459 } 460 } 461 } 462 } 463 | Popular Tags |