Subversion Repositories XServices

Rev

Rev 114 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
66 brianR 1
/*
2
 *   Copyright 2011 Brian Rosenberger (Brutex Network)
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
package net.brutex.xservices.ws.impl;
17
 
18
import java.math.BigInteger;
19
import java.text.ParseException;
20
import java.text.SimpleDateFormat;
114 brianR 21
import java.util.ArrayList;
66 brianR 22
import java.util.Date;
23
import java.util.GregorianCalendar;
114 brianR 24
import java.util.List;
66 brianR 25
import java.util.TimeZone;
26
 
27
import javax.jws.WebService;
28
import net.brutex.xservices.types.DateFormatType;
114 brianR 29
import net.brutex.xservices.types.DateInfoExtendedType;
30
import net.brutex.xservices.types.DateInfoType;
66 brianR 31
import net.brutex.xservices.types.DateTimeUnits;
114 brianR 32
import net.brutex.xservices.types.TimeZoneType;
66 brianR 33
import net.brutex.xservices.util.BrutexNamespaces;
34
import net.brutex.xservices.ws.DateService;
35
import net.brutex.xservices.ws.XServicesFault;
36
 
37
/**
38
 * @author Brian Rosenberger
114 brianR 39
 *
66 brianR 40
 */
114 brianR 41
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.DateService", serviceName = DateService.SERVICE_NAME)
66 brianR 42
public class DateServiceImpl implements DateService {
43
 
44
	private static String ERR_INVALIDFORMAT = "Invalid format pattern.";
45
	private static String ERR_INVALIDTIMEZONE = "Invalid timezone.";
114 brianR 46
 
47
	public DateInfoType getDate() throws XServicesFault {
48
		GregorianCalendar c = new GregorianCalendar();
49
		DateInfoType dateinfo = new DateInfoType(c, TimeZone.getDefault());
50
		return dateinfo;
66 brianR 51
	}
52
 
114 brianR 53
	public DateInfoExtendedType getDateExtended() throws XServicesFault {
54
		GregorianCalendar c = new GregorianCalendar();
55
		DateInfoExtendedType dateinfo = new DateInfoExtendedType(c,
56
				TimeZone.getDefault());
57
		return dateinfo;
58
	}
59
 
66 brianR 60
	public BigInteger getTimestamp() {
61
		Date d = new Date();
62
		long l = d.getTime();
71 brianR 63
		return new BigInteger(Long.toString(l));
66 brianR 64
	}
114 brianR 65
 
71 brianR 66
	public BigInteger getTimestamp2() {
67
		Date d = new Date();
114 brianR 68
		long l = d.getTime() / 1000;
71 brianR 69
		return new BigInteger(Long.toString(l));
70
	}
66 brianR 71
 
114 brianR 72
	public String getInTimezone(Date date, String timezone)
73
			throws XServicesFault {
74
		if (!isValidTimezone(timezone))
75
			throw new XServicesFault(ERR_INVALIDTIMEZONE);
76
		TimeZone targetzone = TimeZone.getTimeZone(timezone);
77
		String targetstring = DateFormatType.ISO8601.format(date, null, targetzone);
78
		return targetstring;
66 brianR 79
	}
114 brianR 80
 
81
	public String formatDate(Date cal, DateFormatType format)
82
			throws XServicesFault {
83
		return format.format(cal, null, null);
66 brianR 84
	}
114 brianR 85
 
86
	public String formatDateAdvanced(Date cal, String format)
66 brianR 87
			throws XServicesFault {
114 brianR 88
		String result = null;
89
		SimpleDateFormat f = new SimpleDateFormat(format);
90
		result = f.format(cal);
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();
98
		if (!isValidTimezone(timezone))
99
			throw new XServicesFault(ERR_INVALIDTIMEZONE);
66 brianR 100
		try {
114 brianR 101
			return format.parse(s, null, TimeZone.getTimeZone(timezone));
102
		} catch (ParseException e) {
103
			throw new XServicesFault(e);
66 brianR 104
		}
105
	}
106
 
114 brianR 107
	public GregorianCalendar parseDateAdvanced(String s, String format,
108
			String timezone) throws XServicesFault {
66 brianR 109
		SimpleDateFormat f = null;
110
		Date date = null;
114 brianR 111
		if (timezone == null | timezone.equals(""))
112
			timezone = TimeZone.getDefault().getID();
113
		if (!isValidTimezone(timezone))
114
			throw new XServicesFault(ERR_INVALIDTIMEZONE);
115
 
66 brianR 116
		try {
117
			f = new SimpleDateFormat(format);
118
			date = f.parse(s);
114 brianR 119
		} catch (IllegalArgumentException e) {
66 brianR 120
			throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage());
121
		} catch (ParseException e) {
114 brianR 122
			throw new XServicesFault("Cannot parse date: " + e.getMessage());
66 brianR 123
		}
124
		GregorianCalendar cal = new GregorianCalendar();
125
		cal.setTimeZone(TimeZone.getTimeZone(timezone));
126
		cal.setTime(date);
127
		return cal;
128
	}
114 brianR 129
 
130
	public BigInteger dateTimeDiff(Date fromCal, Date toCal)
131
			throws XServicesFault {
132
		long diff = toCal.getTime() - fromCal.getTime();
66 brianR 133
		BigInteger d = new BigInteger(String.valueOf(diff), 10);
134
		return d;
135
	}
114 brianR 136
 
137
	public BigInteger dateTimeDiff2(Date fromCal, Date toCal, DateTimeUnits unit)
138
			throws XServicesFault {
66 brianR 139
		BigInteger d = dateTimeDiff(fromCal, toCal);
140
		switch (unit) {
141
		case SECONDS:
142
			d = d.divide(new BigInteger("1000"));
143
			break;
144
		case MINUTES:
145
			d = d.divide(new BigInteger("60000"));
146
			break;
147
		case HOURS:
148
			d = d.divide(new BigInteger("3600000"));
149
			break;
150
		case DAYS:
151
			d = d.divide(new BigInteger("86400000"));
114 brianR 152
			break;
153
		case YEARS:
154
			d = d.divide(new BigInteger("31536000000"));
155
			break;
66 brianR 156
		}
157
		return d;
158
	}
114 brianR 159
 
160
	public GregorianCalendar dateAdd(GregorianCalendar cal, BigInteger value,
161
			DateTimeUnits unit) throws XServicesFault {
68 brianR 162
		switch (unit) {
163
		case SECONDS:
164
			cal.add(GregorianCalendar.SECOND, value.intValue());
165
			break;
166
		case MINUTES:
167
			cal.add(GregorianCalendar.MINUTE, value.intValue());
168
			break;
169
		case HOURS:
170
			cal.add(GregorianCalendar.HOUR_OF_DAY, value.intValue());
171
			break;
172
		case DAYS:
173
			cal.add(GregorianCalendar.DAY_OF_MONTH, value.intValue());
174
			break;
114 brianR 175
		case YEARS:
176
			cal.add(GregorianCalendar.YEAR, value.intValue());
177
			break;
68 brianR 178
		default:
179
			cal.add(GregorianCalendar.MILLISECOND, value.intValue());
180
		}
181
		return cal;
182
	}
114 brianR 183
 
66 brianR 184
	private boolean isValidTimezone(String id) {
185
		boolean yes = false;
114 brianR 186
		for (String s : TimeZone.getAvailableIDs()) {
187
			if (s.equals(id)) {
66 brianR 188
				yes = true;
189
				break;
190
			}
191
		}
192
		return yes;
193
	}
194
 
114 brianR 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
	}
66 brianR 202
}