KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > campware > cream > modules > scheduledjobs > OnlineSubscriptionJob


1 package org.campware.cream.modules.scheduledjobs;
2
3 /* ====================================================================
4  * Copyright (C) 2003-2005 Media Development Loan Fund
5  *
6  * * contact: contact@campware.org - http://www.campware.org
7  * Campware encourages further development. Please let us know.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
27  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  * ====================================================================
36  *
37  * This software consists of voluntary contributions made by many
38  * individuals on behalf of the Apache Software Foundation. For more
39  * information on the Apache Software Foundation, please see
40  * <http://www.apache.org/>.
41  */

42
43 //JDK
44
import java.util.Date JavaDoc;
45 import java.text.SimpleDateFormat JavaDoc;
46 import java.util.*;
47 //Turbine
48
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 /**
65  * Online Subscription Job.
66  *
67  * Close old subscriptions and send notifications
68  * @author <a HREF="mailto:pandzic@volny.cz">Nenad Pandzic</a>
69  */

70 public class OnlineSubscriptionJob extends ScheduledJob
71 {
72     /** Logging */
73     private static Log log = LogFactory.getLog(OnlineSubscriptionJob.class);
74
75     private int taskcount = 0;
76
77     /**
78      * Constructor
79      */

80      public OnlineSubscriptionJob()
81      {
82          //do Task initialization here
83
}
84
85
86     /**
87      * Run the Jobentry from the scheduler queue.
88      * From ScheduledJob.
89      *
90      * @param job The job to run.
91      */

92     public void run( JobEntry job ) throws Exception JavaDoc
93     {
94         // First we resolve online subscriptions
95
doExpiredOnlineSubs();
96         // Then we resolve print subscriptions
97
doExpiredPrintSubs();
98         // now we inform days in advance 7
99
doOnlineSubsToExpire();
100     }
101
102
103     private void doOnlineSubsToExpire() throws Exception JavaDoc{
104         Calendar rightNow = Calendar.getInstance();
105         rightNow.add(Calendar.DATE, 7);
106         Date JavaDoc notiDate= new Date JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc 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 JavaDoc{
157         Criteria criteria = new Criteria();
158         criteria.add(NotificationPeer.NOTIFICATION_ID, new Integer JavaDoc(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 JavaDoc(), Criteria.LESS_EQUAL);
166         osubcrit.add(OnlineSubscriptionPeer.STATUS, new Integer JavaDoc(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 JavaDoc());
178           subclose.setModified(true);
179           subclose.setNew(false);
180           subclose.save();
181
182           String JavaDoc 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 JavaDoc{
215         Criteria osubcrit = new Criteria();
216         osubcrit.add(PrintSubscriptionPeer.END_DATE, new Date JavaDoc(), Criteria.LESS_EQUAL);
217         osubcrit.add(PrintSubscriptionPeer.STATUS, new Integer JavaDoc(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 JavaDoc());
229           subclose.setModified(true);
230           subclose.setNew(false);
231           subclose.save();
232         }
233     }
234
235
236     private String JavaDoc formatDate(Date JavaDoc d)
237     {
238         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc ("dd.MM.yyyy");
239         return formatter.format(d);
240     }
241     
242     
243 }
Popular Tags