Subversion Repositories XServices

Rev

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