Subversion Repositories XServices

Rev

Rev 83 | 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;
21
import java.util.Date;
22
import java.util.GregorianCalendar;
23
import java.util.TimeZone;
24
 
25
import javax.jws.WebService;
97 brianR 26
 
66 brianR 27
import net.brutex.xservices.types.DateFormatType;
28
import net.brutex.xservices.types.DateTimeUnits;
29
import net.brutex.xservices.util.BrutexNamespaces;
30
import net.brutex.xservices.ws.DateService;
31
import net.brutex.xservices.ws.XServicesFault;
32
 
97 brianR 33
 
66 brianR 34
/**
35
 * @author Brian Rosenberger
97 brianR 36
 *
66 brianR 37
 */
97 brianR 38
@WebService(
39
		targetNamespace = BrutexNamespaces.WS_XSERVICES,
40
		endpointInterface = "net.brutex.xservices.ws.DateService",
41
		serviceName = DateService.SERVICE_NAME
42
			)
66 brianR 43
public class DateServiceImpl implements DateService {
44
 
45
	private static String ERR_INVALIDFORMAT = "Invalid format pattern.";
46
	private static String ERR_INVALIDTIMEZONE = "Invalid timezone.";
97 brianR 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";
54
			}
55
			throw new XServicesFault("Please supply a valid timezone id or none. Valid timezones are:\n" + valid_ids,
56
					new Exception( ));
57
		}
58
			if (timezone == null || timezone.length()<1 ) timezone = "GMT0";
59
			GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone(timezone));
60
			return c;
66 brianR 61
	}
62
 
97 brianR 63
 
66 brianR 64
	public BigInteger getTimestamp() {
65
		Date d = new Date();
66
		long l = d.getTime();
71 brianR 67
		return new BigInteger(Long.toString(l));
66 brianR 68
	}
97 brianR 69
 
71 brianR 70
	public BigInteger getTimestamp2() {
71
		Date d = new Date();
97 brianR 72
		long l = d.getTime()/1000;
71 brianR 73
		return new BigInteger(Long.toString(l));
74
	}
66 brianR 75
 
97 brianR 76
 
77
	public GregorianCalendar getInTimezone(GregorianCalendar cal,
78
			String timezone) throws XServicesFault {
79
		if(! isValidTimezone(timezone)) throw new XServicesFault(ERR_INVALIDTIMEZONE);
80
		GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone(timezone));
81
		c.setTimeInMillis(cal.getTimeInMillis());
82
		return c;
66 brianR 83
	}
97 brianR 84
 
85
 
86
	public String formatDate(GregorianCalendar cal, DateFormatType format) throws XServicesFault {
87
		return formatDateAdvanced(cal, format.format());
66 brianR 88
	}
97 brianR 89
 
90
 
91
	public String formatDateAdvanced(GregorianCalendar cal, String format)
66 brianR 92
			throws XServicesFault {
97 brianR 93
		String result= null;
66 brianR 94
		try {
97 brianR 95
			SimpleDateFormat f = new SimpleDateFormat(format);
96
			result = f.format(cal.getTime());
97
		} catch (IllegalArgumentException e) {
98
			throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage());
66 brianR 99
		}
97 brianR 100
		return result;
66 brianR 101
	}
97 brianR 102
 
103
 
104
	public GregorianCalendar parseDate(String s, DateFormatType format, String timezone) throws XServicesFault {
105
		return parseDateAdvanced(s, format.format(), timezone);
106
	}
66 brianR 107
 
97 brianR 108
 
109
	public GregorianCalendar parseDateAdvanced(String s, String format, String timezone) throws XServicesFault {
66 brianR 110
		SimpleDateFormat f = null;
111
		Date date = null;
97 brianR 112
		if(timezone==null | timezone.equals("")) timezone = TimeZone.getDefault().getID();
113
		if(! isValidTimezone(timezone)) throw new XServicesFault(ERR_INVALIDTIMEZONE);
114
 
66 brianR 115
		try {
116
			f = new SimpleDateFormat(format);
117
			date = f.parse(s);
97 brianR 118
		} catch(IllegalArgumentException e) {
66 brianR 119
			throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage());
120
		} catch (ParseException e) {
97 brianR 121
			throw new XServicesFault("Cannot parse date: "+ e.getMessage());
66 brianR 122
		}
123
		GregorianCalendar cal = new GregorianCalendar();
124
		cal.setTimeZone(TimeZone.getTimeZone(timezone));
125
		cal.setTime(date);
126
		return cal;
127
	}
97 brianR 128
 
129
 
130
	public BigInteger dateTimeDiff(GregorianCalendar fromCal,
131
			GregorianCalendar toCal) throws XServicesFault {
132
		long diff = toCal.getTimeInMillis() - fromCal.getTimeInMillis();
66 brianR 133
		BigInteger d = new BigInteger(String.valueOf(diff), 10);
134
		return d;
135
	}
97 brianR 136
 
137
 
138
	public BigInteger dateTimeDiff2(GregorianCalendar fromCal,
139
			GregorianCalendar toCal, DateTimeUnits unit) throws XServicesFault {
66 brianR 140
		BigInteger d = dateTimeDiff(fromCal, toCal);
141
		switch (unit) {
142
		case SECONDS:
143
			d = d.divide(new BigInteger("1000"));
144
			break;
145
		case MINUTES:
146
			d = d.divide(new BigInteger("60000"));
147
			break;
148
		case HOURS:
149
			d = d.divide(new BigInteger("3600000"));
150
			break;
151
		case DAYS:
152
			d = d.divide(new BigInteger("86400000"));
153
		}
154
		return d;
155
	}
97 brianR 156
 
157
 
158
	public GregorianCalendar dateAdd(GregorianCalendar cal, BigInteger value, DateTimeUnits unit)
159
			throws XServicesFault {
68 brianR 160
		switch (unit) {
161
		case SECONDS:
162
			cal.add(GregorianCalendar.SECOND, value.intValue());
163
			break;
164
		case MINUTES:
165
			cal.add(GregorianCalendar.MINUTE, value.intValue());
166
			break;
167
		case HOURS:
168
			cal.add(GregorianCalendar.HOUR_OF_DAY, value.intValue());
169
			break;
170
		case DAYS:
171
			cal.add(GregorianCalendar.DAY_OF_MONTH, value.intValue());
172
			break;
173
		default:
174
			cal.add(GregorianCalendar.MILLISECOND, value.intValue());
175
		}
176
		return cal;
177
	}
97 brianR 178
 
66 brianR 179
	private boolean isValidTimezone(String id) {
180
		boolean yes = false;
97 brianR 181
		for( String s: TimeZone.getAvailableIDs()) {
182
			if(s.equals(id)) {
66 brianR 183
				yes = true;
184
				break;
185
			}
186
		}
187
		return yes;
188
	}
189
 
190
}