Subversion Repositories XServices

Rev

Rev 97 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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