Subversion Repositories XServices

Rev

Rev 83 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 83 Rev 97
Line 16... Line 16...
16
package net.brutex.xservices.ws.impl;
16
package net.brutex.xservices.ws.impl;
Line 17... Line 17...
17
 
17
 
18
import java.math.BigInteger;
18
import java.math.BigInteger;
19
import java.text.ParseException;
19
import java.text.ParseException;
20
import java.text.SimpleDateFormat;
-
 
21
import java.util.ArrayList;
20
import java.text.SimpleDateFormat;
22
import java.util.Date;
21
import java.util.Date;
23
import java.util.GregorianCalendar;
-
 
24
import java.util.List;
22
import java.util.GregorianCalendar;
Line 25... Line 23...
25
import java.util.TimeZone;
23
import java.util.TimeZone;
-
 
24
 
26
 
25
import javax.jws.WebService;
27
import javax.jws.WebService;
-
 
28
import net.brutex.xservices.types.DateFormatType;
-
 
29
import net.brutex.xservices.types.DateInfoExtendedType;
26
 
30
import net.brutex.xservices.types.DateInfoType;
-
 
31
import net.brutex.xservices.types.DateTimeUnits;
27
import net.brutex.xservices.types.DateFormatType;
32
import net.brutex.xservices.types.TimeZoneType;
28
import net.brutex.xservices.types.DateTimeUnits;
33
import net.brutex.xservices.util.BrutexNamespaces;
29
import net.brutex.xservices.util.BrutexNamespaces;
Line -... Line 30...
-
 
30
import net.brutex.xservices.ws.DateService;
34
import net.brutex.xservices.ws.DateService;
31
import net.brutex.xservices.ws.XServicesFault;
35
import net.brutex.xservices.ws.XServicesFault;
32
 
36
 
33
 
37
/**
34
/**
-
 
35
 * @author Brian Rosenberger
-
 
36
 *
38
 * @author Brian Rosenberger
37
 */
-
 
38
@WebService(
-
 
39
		targetNamespace = BrutexNamespaces.WS_XSERVICES, 
39
 * 
40
		endpointInterface = "net.brutex.xservices.ws.DateService", 
Line 40... Line 41...
40
 */
41
		serviceName = DateService.SERVICE_NAME
41
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.DateService", serviceName = DateService.SERVICE_NAME)
42
			)
Line 42... Line 43...
42
public class DateServiceImpl implements DateService {
43
public class DateServiceImpl implements DateService {
-
 
44
 
-
 
45
	private static String ERR_INVALIDFORMAT = "Invalid format pattern.";
43
 
46
	private static String ERR_INVALIDTIMEZONE = "Invalid timezone.";
-
 
47
	
-
 
48
	public GregorianCalendar getDate(String timezone) throws XServicesFault {		
-
 
49
		if (! isValidTimezone(timezone) ) {
-
 
50
			String valid_ids = "";
-
 
51
			String[] tid = TimeZone.getAvailableIDs();
-
 
52
			for (String s : tid) {
-
 
53
				valid_ids += s + "\n";
44
	private static String ERR_INVALIDFORMAT = "Invalid format pattern.";
54
			}
45
	private static String ERR_INVALIDTIMEZONE = "Invalid timezone.";
55
			throw new XServicesFault("Please supply a valid timezone id or none. Valid timezones are:\n" + valid_ids,
46
 
56
					new Exception( ));
Line 47... Line -...
47
	public DateInfoType getDate() throws XServicesFault {
-
 
48
		GregorianCalendar c = new GregorianCalendar();
-
 
49
		DateInfoType dateinfo = new DateInfoType(c, TimeZone.getDefault());
-
 
50
		return dateinfo;
-
 
51
	}
-
 
52
 
-
 
Line 53... Line 57...
53
	public DateInfoExtendedType getDateExtended() throws XServicesFault {
57
		}
54
		GregorianCalendar c = new GregorianCalendar();
58
			if (timezone == null || timezone.length()<1 ) timezone = "GMT0";
55
		DateInfoExtendedType dateinfo = new DateInfoExtendedType(c,
59
			GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone(timezone));
56
				TimeZone.getDefault());
60
			return c;
Line 67... Line 71...
67
		Date d = new Date();
71
		Date d = new Date();
68
		long l = d.getTime() / 1000;
72
		long l = d.getTime()/1000;
69
		return new BigInteger(Long.toString(l));
73
		return new BigInteger(Long.toString(l));
70
	}
74
	}
Line -... Line 75...
-
 
75
 
71
 
76
	
72
	public String getInTimezone(Date date, String timezone)
77
	public GregorianCalendar getInTimezone(GregorianCalendar cal,
73
			throws XServicesFault {
-
 
74
		if (!isValidTimezone(timezone))
78
			String timezone) throws XServicesFault {
75
			throw new XServicesFault(ERR_INVALIDTIMEZONE);
79
		if(! isValidTimezone(timezone)) throw new XServicesFault(ERR_INVALIDTIMEZONE);
76
		TimeZone targetzone = TimeZone.getTimeZone(timezone);
80
		GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone(timezone));
77
		String targetstring = DateFormatType.ISO8601.format(date, null, targetzone);
81
		c.setTimeInMillis(cal.getTimeInMillis());
78
		return targetstring;
82
		return c;
Line -... Line 83...
-
 
83
	}
79
	}
84
	
80
 
-
 
81
	public String formatDate(Date cal, DateFormatType format)
85
	
82
			throws XServicesFault {
86
	public String formatDate(GregorianCalendar cal, DateFormatType format) throws XServicesFault {
Line -... Line 87...
-
 
87
		return formatDateAdvanced(cal, format.format());
83
		return format.format(cal, null, null);
88
	}
84
	}
89
	
85
 
90
	
-
 
91
	public String formatDateAdvanced(GregorianCalendar cal, String format)
86
	public String formatDateAdvanced(Date cal, String format)
92
			throws XServicesFault {
87
			throws XServicesFault {
93
		String result= null;
-
 
94
		try {
-
 
95
			SimpleDateFormat f = new SimpleDateFormat(format);
-
 
96
			result = f.format(cal.getTime());
88
		String result = null;
97
		} catch (IllegalArgumentException e) {
89
		SimpleDateFormat f = new SimpleDateFormat(format);
98
			throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage());
Line -... Line 99...
-
 
99
		}
90
		result = f.format(cal);
100
		return result;
91
		return result;
-
 
92
	}
-
 
93
 
-
 
94
	public Date parseDate(String s, DateFormatType format, String timezone) 
-
 
95
			throws XServicesFault {
-
 
96
		if (timezone == null | timezone.equals(""))
-
 
97
			timezone = TimeZone.getDefault().getID();
101
	}
98
		if (!isValidTimezone(timezone))
-
 
99
			throw new XServicesFault(ERR_INVALIDTIMEZONE);
-
 
100
		try {
-
 
101
			return format.parse(s, null, TimeZone.getTimeZone(timezone));
102
	
Line -... Line 103...
-
 
103
	
102
		} catch (ParseException e) {
104
	public GregorianCalendar parseDate(String s, DateFormatType format, String timezone) throws XServicesFault {
103
			throw new XServicesFault(e);
-
 
104
		}
105
		return parseDateAdvanced(s, format.format(), timezone);
105
	}
106
	}
106
 
-
 
107
	public GregorianCalendar parseDateAdvanced(String s, String format,
107
 
108
			String timezone) throws XServicesFault {
-
 
109
		SimpleDateFormat f = null;
108
	
Line 110... Line 109...
110
		Date date = null;
109
	public GregorianCalendar parseDateAdvanced(String s, String format, String timezone) throws XServicesFault {
111
		if (timezone == null | timezone.equals(""))
110
		SimpleDateFormat f = null;
112
			timezone = TimeZone.getDefault().getID();
111
		Date date = null;
113
		if (!isValidTimezone(timezone))
112
		if(timezone==null | timezone.equals("")) timezone = TimeZone.getDefault().getID();
Line 125... Line 124...
125
		cal.setTimeZone(TimeZone.getTimeZone(timezone));
124
		cal.setTimeZone(TimeZone.getTimeZone(timezone));
126
		cal.setTime(date);
125
		cal.setTime(date);
127
		return cal;
126
		return cal;
128
	}
127
	}
Line -... Line 128...
-
 
128
	
129
 
129
	
130
	public BigInteger dateTimeDiff(Date fromCal, Date toCal)
130
	public BigInteger dateTimeDiff(GregorianCalendar fromCal,
131
			throws XServicesFault {
131
			GregorianCalendar toCal) throws XServicesFault {
132
		long diff = toCal.getTime() - fromCal.getTime();
132
		long diff = toCal.getTimeInMillis() - fromCal.getTimeInMillis();
133
		BigInteger d = new BigInteger(String.valueOf(diff), 10);
133
		BigInteger d = new BigInteger(String.valueOf(diff), 10);
134
		return d;
134
		return d;
Line -... Line 135...
-
 
135
	}
135
	}
136
	
136
 
137
	
137
	public BigInteger dateTimeDiff2(Date fromCal, Date toCal, DateTimeUnits unit)
138
	public BigInteger dateTimeDiff2(GregorianCalendar fromCal,
138
			throws XServicesFault {
139
			GregorianCalendar toCal, DateTimeUnits unit) throws XServicesFault {
139
		BigInteger d = dateTimeDiff(fromCal, toCal);
140
		BigInteger d = dateTimeDiff(fromCal, toCal);
140
		switch (unit) {
141
		switch (unit) {
141
		case SECONDS:
142
		case SECONDS:
Line 147... Line 148...
147
		case HOURS:
148
		case HOURS:
148
			d = d.divide(new BigInteger("3600000"));
149
			d = d.divide(new BigInteger("3600000"));
149
			break;
150
			break;
150
		case DAYS:
151
		case DAYS:
151
			d = d.divide(new BigInteger("86400000"));
152
			d = d.divide(new BigInteger("86400000"));
152
			break;
-
 
153
		case YEARS:
-
 
154
			d = d.divide(new BigInteger("31536000000"));
-
 
155
			break;
-
 
156
		}
153
		}
157
		return d;
154
		return d;
158
	}
155
	}
Line -... Line 156...
-
 
156
	
159
 
157
	
160
	public GregorianCalendar dateAdd(GregorianCalendar cal, BigInteger value,
158
	public GregorianCalendar dateAdd(GregorianCalendar cal, BigInteger value, DateTimeUnits unit)
161
			DateTimeUnits unit) throws XServicesFault {
159
			throws XServicesFault {
162
		switch (unit) {
160
		switch (unit) {
163
		case SECONDS:
161
		case SECONDS:
164
			cal.add(GregorianCalendar.SECOND, value.intValue());
162
			cal.add(GregorianCalendar.SECOND, value.intValue());
165
			break;
163
			break;
Line 170... Line 168...
170
			cal.add(GregorianCalendar.HOUR_OF_DAY, value.intValue());
168
			cal.add(GregorianCalendar.HOUR_OF_DAY, value.intValue());
171
			break;
169
			break;
172
		case DAYS:
170
		case DAYS:
173
			cal.add(GregorianCalendar.DAY_OF_MONTH, value.intValue());
171
			cal.add(GregorianCalendar.DAY_OF_MONTH, value.intValue());
174
			break;
172
			break;
175
		case YEARS:
-
 
176
			cal.add(GregorianCalendar.YEAR, value.intValue());
-
 
177
			break;
-
 
178
		default:
173
		default:
179
			cal.add(GregorianCalendar.MILLISECOND, value.intValue());
174
			cal.add(GregorianCalendar.MILLISECOND, value.intValue());
180
		}
175
		}
181
		return cal;
176
		return cal;
182
	}
177
	}
Line 190... Line 185...
190
			}
185
			}
191
		}
186
		}
192
		return yes;
187
		return yes;
193
	}
188
	}
Line 194... Line -...
194
 
-
 
195
	public List<TimeZoneType> getTimezones() {
-
 
196
		List<TimeZoneType> output = new ArrayList<TimeZoneType>();
-
 
197
		for (String s : TimeZone.getAvailableIDs()) {
-
 
198
			output.add(new TimeZoneType(TimeZone.getTimeZone(s)));
-
 
199
		}
-
 
200
		return output;
-
 
201
	}
189