Subversion Repositories XServices

Rev

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

Rev 66 Rev 68
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
	@Override
47
	@Override
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
	@Override
63
	@Override
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
		BigInteger timestamp = new BigInteger(Long.toString(l));
68
		return timestamp;
68
		return timestamp;
69
	}
69
	}
70
 
70
 
71
	@Override
71
	@Override
72
	public GregorianCalendar getInTimezone(GregorianCalendar cal,
72
	public GregorianCalendar getInTimezone(GregorianCalendar cal,
73
			String timezone) throws XServicesFault {
73
			String timezone) throws XServicesFault {
74
		if(! isValidTimezone(timezone)) throw new XServicesFault(ERR_INVALIDTIMEZONE);
74
		if(! isValidTimezone(timezone)) throw new XServicesFault(ERR_INVALIDTIMEZONE);
75
		GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone(timezone));
75
		GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone(timezone));
76
		c.setTimeInMillis(cal.getTimeInMillis());
76
		c.setTimeInMillis(cal.getTimeInMillis());
77
		return c;
77
		return c;
78
	}
78
	}
79
	
79
	
80
	@Override
80
	@Override
81
	public String formatDate(GregorianCalendar cal, DateFormatType format) throws XServicesFault {
81
	public String formatDate(GregorianCalendar cal, DateFormatType format) throws XServicesFault {
82
		return formatDateAdvanced(cal, format.format());
82
		return formatDateAdvanced(cal, format.format());
83
	}
83
	}
84
	
84
	
85
	@Override
85
	@Override
86
	public String formatDateAdvanced(GregorianCalendar cal, String format)
86
	public String formatDateAdvanced(GregorianCalendar cal, String format)
87
			throws XServicesFault {
87
			throws XServicesFault {
88
		String result= null;
88
		String result= null;
89
		try {
89
		try {
90
			SimpleDateFormat f = new SimpleDateFormat(format);
90
			SimpleDateFormat f = new SimpleDateFormat(format);
91
			result = f.format(cal.getTime());
91
			result = f.format(cal.getTime());
92
		} catch (IllegalArgumentException e) {
92
		} catch (IllegalArgumentException e) {
93
			throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage());
93
			throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage());
94
		}
94
		}
95
		return result;
95
		return result;
96
	}
96
	}
97
	
97
	
98
	@Override
98
	@Override
99
	public GregorianCalendar parseDate(String s, DateFormatType format, String timezone) throws XServicesFault {
99
	public GregorianCalendar parseDate(String s, DateFormatType format, String timezone) throws XServicesFault {
100
		return parseDateAdvanced(s, format.format(), timezone);
100
		return parseDateAdvanced(s, format.format(), timezone);
101
	}
101
	}
102
 
102
 
103
	@Override
103
	@Override
104
	public GregorianCalendar parseDateAdvanced(String s, String format, String timezone) throws XServicesFault {
104
	public GregorianCalendar parseDateAdvanced(String s, String format, String timezone) throws XServicesFault {
105
		SimpleDateFormat f = null;
105
		SimpleDateFormat f = null;
106
		Date date = null;
106
		Date date = null;
107
		if(timezone==null | timezone.equals("")) timezone = TimeZone.getDefault().getID();
107
		if(timezone==null | timezone.equals("")) timezone = TimeZone.getDefault().getID();
108
		if(! isValidTimezone(timezone)) throw new XServicesFault(ERR_INVALIDTIMEZONE);
108
		if(! isValidTimezone(timezone)) throw new XServicesFault(ERR_INVALIDTIMEZONE);
109
		
109
		
110
		try {
110
		try {
111
			f = new SimpleDateFormat(format);
111
			f = new SimpleDateFormat(format);
112
			date = f.parse(s);
112
			date = f.parse(s);
113
		} catch(IllegalArgumentException e) {
113
		} catch(IllegalArgumentException e) {
114
			throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage());
114
			throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage());
115
		} catch (ParseException e) {
115
		} catch (ParseException e) {
116
			throw new XServicesFault("Cannot parse date: "+ e.getMessage());
116
			throw new XServicesFault("Cannot parse date: "+ e.getMessage());
117
		}
117
		}
118
		GregorianCalendar cal = new GregorianCalendar();
118
		GregorianCalendar cal = new GregorianCalendar();
119
		cal.setTimeZone(TimeZone.getTimeZone(timezone));
119
		cal.setTimeZone(TimeZone.getTimeZone(timezone));
120
		cal.setTime(date);
120
		cal.setTime(date);
121
		return cal;
121
		return cal;
122
	}
122
	}
123
	
123
	
124
	@Override
124
	@Override
125
	public BigInteger dateTimeDiff(GregorianCalendar fromCal,
125
	public BigInteger dateTimeDiff(GregorianCalendar fromCal,
126
			GregorianCalendar toCal) throws XServicesFault {
126
			GregorianCalendar toCal) throws XServicesFault {
127
		long diff = toCal.getTimeInMillis() - fromCal.getTimeInMillis();
127
		long diff = toCal.getTimeInMillis() - fromCal.getTimeInMillis();
128
		BigInteger d = new BigInteger(String.valueOf(diff), 10);
128
		BigInteger d = new BigInteger(String.valueOf(diff), 10);
129
		return d;
129
		return d;
130
	}
130
	}
131
	
131
	
132
	@Override
132
	@Override
133
	public BigInteger dateTimeDiff2(GregorianCalendar fromCal,
133
	public BigInteger dateTimeDiff2(GregorianCalendar fromCal,
134
			GregorianCalendar toCal, DateTimeUnits unit) throws XServicesFault {
134
			GregorianCalendar toCal, DateTimeUnits unit) throws XServicesFault {
135
		BigInteger d = dateTimeDiff(fromCal, toCal);
135
		BigInteger d = dateTimeDiff(fromCal, toCal);
136
		switch (unit) {
136
		switch (unit) {
137
		case SECONDS:
137
		case SECONDS:
138
			d = d.divide(new BigInteger("1000"));
138
			d = d.divide(new BigInteger("1000"));
139
			break;
139
			break;
140
		case MINUTES:
140
		case MINUTES:
141
			d = d.divide(new BigInteger("60000"));
141
			d = d.divide(new BigInteger("60000"));
142
			break;
142
			break;
143
		case HOURS:
143
		case HOURS:
144
			d = d.divide(new BigInteger("3600000"));
144
			d = d.divide(new BigInteger("3600000"));
145
			break;
145
			break;
146
		case DAYS:
146
		case DAYS:
147
			d = d.divide(new BigInteger("86400000"));
147
			d = d.divide(new BigInteger("86400000"));
148
		}
148
		}
149
		return d;
149
		return d;
150
	}
150
	}
-
 
151
	
-
 
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
	}
151
	
173
	
152
	private boolean isValidTimezone(String id) {
174
	private boolean isValidTimezone(String id) {
153
		boolean yes = false;
175
		boolean yes = false;
154
		for( String s: TimeZone.getAvailableIDs()) {
176
		for( String s: TimeZone.getAvailableIDs()) {
155
			if(s.equals(id)) {
177
			if(s.equals(id)) {
156
				yes = true;
178
				yes = true;
157
				break;
179
				break;
158
			}
180
			}
159
		}
181
		}
160
		return yes;
182
		return yes;
161
	}
183
	}
162
 
184
 
163
}
185
}