1 package org.campware.cream.modules.scheduledjobs; 2 3 42 43 import java.util.Date ; 45 import java.text.SimpleDateFormat ; 46 import java.util.*; 47 import org.apache.turbine.modules.ScheduledJob; 49 import org.apache.turbine.services.schedule.JobEntry; 50 import org.apache.commons.logging.Log; 51 import org.apache.commons.logging.LogFactory; 52 53 import org.apache.velocity.VelocityContext; 54 import org.apache.turbine.util.mail.HtmlEmail; 55 import org.apache.turbine.Turbine; 56 import org.apache.torque.util.Criteria; 57 import org.campware.cream.om.Notification; 58 import org.campware.cream.om.NotificationPeer; 59 import org.campware.cream.om.OnlineSubscription; 60 import org.campware.cream.om.OnlineSubscriptionPeer; 61 import org.campware.cream.om.PrintSubscription; 62 import org.campware.cream.om.PrintSubscriptionPeer; 63 64 70 public class OnlineSubscriptionJob extends ScheduledJob 71 { 72 73 private static Log log = LogFactory.getLog(OnlineSubscriptionJob.class); 74 75 private int taskcount = 0; 76 77 80 public OnlineSubscriptionJob() 81 { 82 } 84 85 86 92 public void run( JobEntry job ) throws Exception 93 { 94 doExpiredOnlineSubs(); 96 doExpiredPrintSubs(); 98 doOnlineSubsToExpire(); 100 } 101 102 103 private void doOnlineSubsToExpire() throws Exception { 104 Calendar rightNow = Calendar.getInstance(); 105 rightNow.add(Calendar.DATE, 7); 106 Date notiDate= new Date (rightNow.get(Calendar.YEAR),rightNow.get(Calendar.MONTH), rightNow.get(Calendar.DATE)); 107 108 Criteria criteria = new Criteria(); 109 criteria.add(NotificationPeer.NOTIFICATION_ID, new Integer (1002), Criteria.EQUAL); 110 Notification myNotif = (Notification) NotificationPeer.doSelect(criteria).get(0); 111 112 VelocityContext context = new VelocityContext(); 113 ShedVelocityTool velTool= new ShedVelocityTool(context); 114 115 Criteria osubcrit = new Criteria(); 116 osubcrit.add(OnlineSubscriptionPeer.END_DATE, notiDate, Criteria.EQUAL); 117 osubcrit.add(OnlineSubscriptionPeer.STATUS, new Integer (30), Criteria.EQUAL); 118 119 List substoclose = OnlineSubscriptionPeer.doSelect(osubcrit); 120 Iterator i = substoclose.iterator(); 121 122 while (i.hasNext()) 123 { 124 OnlineSubscription subclose = (OnlineSubscription) i.next(); 125 126 String sEmailAddress=subclose.getCustomerRelatedByCustomerId().getEmail(); 127 128 if (sEmailAddress.length()>1){ 129 HtmlEmail ve = new HtmlEmail(); 130 ve.setCharset("UTF-8"); 131 ve.addTo( sEmailAddress, ""); 132 ve.setFrom(Turbine.getConfiguration().getString("mail.smtp.from"), Turbine.getConfiguration().getString("mail.smtp.from.name")); 133 ve.setSubject(myNotif.getSubject()); 134 135 context.put("name", subclose.getCustomerRelatedByCustomerId().getCustomerName1()); 136 context.put("display", subclose.getCustomerRelatedByCustomerId().getCustomerDisplay()); 137 context.put("dear", subclose.getCustomerRelatedByCustomerId().getDear()); 138 context.put("email", subclose.getCustomerRelatedByCustomerId().getEmail()); 139 context.put("custom1", subclose.getCustomerRelatedByCustomerId().getCustom1()); 140 context.put("custom2", subclose.getCustomerRelatedByCustomerId().getCustom2()); 141 context.put("custom3", subclose.getCustomerRelatedByCustomerId().getCustom3()); 142 context.put("custom4", subclose.getCustomerRelatedByCustomerId().getCustom4()); 143 context.put("custom5", subclose.getCustomerRelatedByCustomerId().getCustom5()); 144 context.put("custom6", subclose.getCustomerRelatedByCustomerId().getCustom6()); 145 context.put("productdisplay", subclose.getProduct().getProductDisplay()); 146 context.put("productdescription", subclose.getProduct().getProductDescription()); 147 context.put("startdate", formatDate(subclose.getStartDate())); 148 context.put("enddate", formatDate(subclose.getEndDate())); 149 150 ve.setTextMsg(velTool.evaluate(myNotif.getBody())); 151 ve.send(); 152 } 153 } 154 } 155 156 private void doExpiredOnlineSubs() throws Exception { 157 Criteria criteria = new Criteria(); 158 criteria.add(NotificationPeer.NOTIFICATION_ID, new Integer (1003), Criteria.EQUAL); 159 Notification myNotif = (Notification) NotificationPeer.doSelect(criteria).get(0); 160 161 VelocityContext context = new VelocityContext(); 162 ShedVelocityTool velTool= new ShedVelocityTool(context); 163 164 Criteria osubcrit = new Criteria(); 165 osubcrit.add(OnlineSubscriptionPeer.END_DATE, new Date (), Criteria.LESS_EQUAL); 166 osubcrit.add(OnlineSubscriptionPeer.STATUS, new Integer (30), Criteria.EQUAL); 167 168 List substoclose = OnlineSubscriptionPeer.doSelect(osubcrit); 169 Iterator i = substoclose.iterator(); 170 171 while (i.hasNext()) 172 { 173 OnlineSubscription subclose = (OnlineSubscription) i.next(); 174 175 subclose.setStatus(50); 176 subclose.setModifiedBy("system"); 177 subclose.setModified(new Date ()); 178 subclose.setModified(true); 179 subclose.setNew(false); 180 subclose.save(); 181 182 String sEmailAddress=subclose.getCustomerRelatedByCustomerId().getEmail(); 183 184 if (sEmailAddress.length()>1){ 185 HtmlEmail ve = new HtmlEmail(); 186 ve.setCharset("UTF-8"); 187 ve.addTo( sEmailAddress, ""); 188 ve.setFrom(Turbine.getConfiguration().getString("mail.smtp.from"), Turbine.getConfiguration().getString("mail.smtp.from.name")); 189 ve.setSubject(myNotif.getSubject()); 190 191 context.put("name", subclose.getCustomerRelatedByCustomerId().getCustomerName1()); 192 context.put("display", subclose.getCustomerRelatedByCustomerId().getCustomerDisplay()); 193 context.put("dear", subclose.getCustomerRelatedByCustomerId().getDear()); 194 context.put("email", subclose.getCustomerRelatedByCustomerId().getEmail()); 195 context.put("custom1", subclose.getCustomerRelatedByCustomerId().getCustom1()); 196 context.put("custom2", subclose.getCustomerRelatedByCustomerId().getCustom2()); 197 context.put("custom3", subclose.getCustomerRelatedByCustomerId().getCustom3()); 198 context.put("custom4", subclose.getCustomerRelatedByCustomerId().getCustom4()); 199 context.put("custom5", subclose.getCustomerRelatedByCustomerId().getCustom5()); 200 context.put("custom6", subclose.getCustomerRelatedByCustomerId().getCustom6()); 201 context.put("productdisplay", subclose.getProduct().getProductDisplay()); 202 context.put("productdescription", subclose.getProduct().getProductDescription()); 203 context.put("startdate", formatDate(subclose.getStartDate())); 204 context.put("enddate", formatDate(subclose.getEndDate())); 205 206 ve.setTextMsg(velTool.evaluate(myNotif.getBody())); 207 ve.send(); 208 } 209 210 211 } 212 } 213 214 private void doExpiredPrintSubs() throws Exception { 215 Criteria osubcrit = new Criteria(); 216 osubcrit.add(PrintSubscriptionPeer.END_DATE, new Date (), Criteria.LESS_EQUAL); 217 osubcrit.add(PrintSubscriptionPeer.STATUS, new Integer (30), Criteria.EQUAL); 218 219 List substoclose = PrintSubscriptionPeer.doSelect(osubcrit); 220 Iterator i = substoclose.iterator(); 221 222 while (i.hasNext()) 223 { 224 PrintSubscription subclose = (PrintSubscription) i.next(); 225 226 subclose.setStatus(50); 227 subclose.setModifiedBy("system"); 228 subclose.setModified(new Date ()); 229 subclose.setModified(true); 230 subclose.setNew(false); 231 subclose.save(); 232 } 233 } 234 235 236 private String formatDate(Date d) 237 { 238 SimpleDateFormat formatter = new SimpleDateFormat ("dd.MM.yyyy"); 239 return formatter.format(d); 240 } 241 242 243 } | Popular Tags |