KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > providers > DateProvider


1 /*
2  * Copyright (C) 2002 Erik Swenson - erik@oreports.com
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */

19
20 package org.efs.openreports.providers;
21
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.Date JavaDoc;
24
25 import com.opensymphony.xwork.ActionContext;
26 import com.opensymphony.xwork.interceptor.component.ComponentManager;
27
28 import org.apache.log4j.Logger;
29 import org.efs.openreports.objects.ORProperty;
30
31 public class DateProvider implements PropertiesProviderAware
32 {
33     protected static Logger log =
34         Logger.getLogger(DateProvider.class.getName());
35
36     protected SimpleDateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("MM/dd/yyyy");
37
38     private PropertiesProvider propertiesProvider;
39     
40     // constructor for Spring IOC
41
public DateProvider(PropertiesProvider propertiesProvider) throws ProviderException
42     {
43         this.propertiesProvider = propertiesProvider;
44         init();
45     }
46
47     // constructor for WebWork IOC
48
public DateProvider() throws ProviderException
49     {
50         ComponentManager container =
51             (ComponentManager) ActionContext.getContext().get(
52                 "com.opensymphony.xwork.interceptor.component.ComponentManager");
53
54         container.initializeObject(this);
55         
56         init();
57     }
58     
59     protected void init() throws ProviderException
60     {
61         String JavaDoc dateFormat = "MM/dd/yyyy";
62
63         ORProperty property = propertiesProvider.getProperty(ORProperty.DATE_FORMAT);
64         if (property != null && property.getValue() != null
65                 && property.getValue().trim().length() > 0)
66         {
67             dateFormat = property.getValue();
68         }
69
70         setDateFormat(dateFormat);
71
72         log.info("DateFormat: " + dateFormat);
73         log.info("Created");
74     }
75     
76     public SimpleDateFormat JavaDoc getDateFormat()
77     {
78         return dateFormat;
79     }
80
81     public void setDateFormat(String JavaDoc format)
82     {
83         dateFormat = new SimpleDateFormat JavaDoc(format);
84     }
85
86     public Date JavaDoc parseDate(String JavaDoc date) throws ProviderException
87     {
88         try
89         {
90             return dateFormat.parse(date);
91         }
92         catch (Exception JavaDoc e)
93         {
94             throw new ProviderException("Use " + dateFormat.toPattern());
95         }
96     }
97     
98     public String JavaDoc formatDate(Date JavaDoc date)
99     {
100         return dateFormat.format(date);
101     }
102
103     public void setPropertiesProvider(PropertiesProvider propertiesProvider)
104     {
105         this.propertiesProvider = propertiesProvider;
106     }
107
108 }
Popular Tags