Subversion Repositories XServices

Rev

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

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