Subversion Repositories XServices

Rev

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

Rev 190 Rev 197
1
/*
1
/*
2
 *   Copyright 2017 Brian Rosenberger (Brutex Network)
2
 *   Copyright 2017 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
 
-
 
18
import java.io.BufferedInputStream;
-
 
19
import java.io.IOException;
17
 
20
import java.io.InputStreamReader;
18
import java.net.URL;
21
import java.net.URL;
19
import java.time.format.DateTimeFormatter;
22
import java.time.format.DateTimeFormatter;
20
import java.util.ArrayList;
23
import java.util.ArrayList;
21
import java.util.GregorianCalendar;
24
import java.util.GregorianCalendar;
22
import java.util.HashSet;
25
import java.util.HashSet;
23
import java.util.List;
26
import java.util.List;
24
import java.util.Set;
27
import java.util.Set;
25
 
28
 
26
import javax.activation.DataHandler;
29
import javax.activation.DataHandler;
27
import javax.jws.WebParam;
30
import javax.jws.WebParam;
28
import javax.jws.WebService;
31
import javax.jws.WebService;
29
 
32
 
30
import org.apache.commons.configuration.ConfigurationException;
33
import org.apache.commons.configuration2.ex.ConfigurationException;
31
import org.apache.commons.configuration.PropertiesConfiguration;
34
import org.apache.commons.configuration2.PropertiesConfiguration;
32
import org.apache.commons.jcs.JCS;
35
import org.apache.commons.jcs.JCS;
33
import org.apache.commons.jcs.access.exception.CacheException;
36
import org.apache.commons.jcs.access.exception.CacheException;
34
import org.apache.logging.log4j.LogManager;
37
import org.apache.logging.log4j.LogManager;
35
import org.apache.logging.log4j.Logger;
38
import org.apache.logging.log4j.Logger;
36
 
39
 
37
import net.brutex.DocBuilder.DocBuilder;
40
import net.brutex.DocBuilder.DocBuilder;
38
import net.brutex.mgmt.api.generator.JITCompiler;
41
import net.brutex.mgmt.api.generator.JITCompiler;
39
import net.brutex.mgmt.api.xml.AnyEntity;
42
import net.brutex.mgmt.api.xml.AnyEntity;
40
import net.brutex.mgmt.api.xml.Customer;
43
import net.brutex.mgmt.api.xml.Customer;
41
import net.brutex.mgmt.api.xml.DateFilter;
44
import net.brutex.mgmt.api.xml.DateFilter;
42
import net.brutex.mgmt.api.xml.Project;
45
import net.brutex.mgmt.api.xml.Project;
43
import net.brutex.mgmt.api.xml.Query;
46
import net.brutex.mgmt.api.xml.Query;
44
import net.brutex.mgmt.api.xml.Query.BOOL;
47
import net.brutex.mgmt.api.xml.Query.BOOL;
45
import net.brutex.mgmt.api.xml.QueryEntity;
48
import net.brutex.mgmt.api.xml.QueryEntity;
46
import net.brutex.mgmt.api.xml.StringEntity;
49
import net.brutex.mgmt.api.xml.StringEntity;
47
import net.brutex.mgmt.api.xml.TimesheetEntry;
50
import net.brutex.mgmt.api.xml.TimesheetEntry;
48
import net.brutex.mgmt.api.xml.TimesheetFilter;
51
import net.brutex.mgmt.api.xml.TimesheetFilter;
49
import net.brutex.mgmt.api.xml.User;
52
import net.brutex.mgmt.api.xml.User;
50
import net.brutex.mgmt.openair.OpenAirRestConnection;
53
import net.brutex.mgmt.openair.OpenAirRestConnection;
51
import net.brutex.xservices.types.ant.AttachmentType;
54
import net.brutex.xservices.types.ant.AttachmentType;
52
import net.brutex.xservices.util.BrutexNamespaces;
55
import net.brutex.xservices.util.BrutexNamespaces;
53
import net.brutex.xservices.ws.OpenAirProxyService;
56
import net.brutex.xservices.ws.OpenAirProxyService;
54
import net.brutex.xservices.ws.XServicesFault;
57
import net.brutex.xservices.ws.XServicesFault;
55
 
58
 
56
/**
59
/**
57
 * @author Brian Rosenberger
60
 * @author Brian Rosenberger
58
 * 
61
 * 
59
 */
62
 */
60
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.OpenAirProxyService", serviceName = OpenAirProxyService.SERVICE_NAME)
63
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.OpenAirProxyService", serviceName = OpenAirProxyService.SERVICE_NAME)
61
public class OpenAirProxyServiceImpl implements OpenAirProxyService {
64
public class OpenAirProxyServiceImpl implements OpenAirProxyService {
62
 
65
 
63
	/*
66
	/*
64
	 * Log4j2 Set Up
67
	 * Log4j2 Set Up
65
	 */
68
	 */
66
	private final Logger logger = LogManager.getLogger(OpenAirProxyServiceImpl.class);
69
	private final Logger logger = LogManager.getLogger(OpenAirProxyServiceImpl.class);
67
 
70
 
68
	/*
71
	/*
69
	 * (non-Javadoc)
72
	 * (non-Javadoc)
70
	 * 
73
	 * 
71
	 * @see
74
	 * @see
72
	 * net.brutex.xservices.ws.OpenAirProxyService#getTimeEntryList(java.lang.
75
	 * net.brutex.xservices.ws.OpenAirProxyService#getTimeEntryList(java.lang.
73
	 * String)
76
	 * String)
74
	 */
77
	 */
75
 
78
 
76
	@Override
79
	@Override
77
	public List<TimesheetEntry> getTimeEntryList(int oa_projectid, int oa_customerid, int oa_userid,
80
	public List<TimesheetEntry> getTimeEntryList(int oa_projectid, int oa_customerid, int oa_userid,
78
			GregorianCalendar startdate, GregorianCalendar enddate, boolean includeNonBillable, List<TimesheetFilter.TimesheetFilterType> filter) throws XServicesFault {
81
			GregorianCalendar startdate, GregorianCalendar enddate, boolean includeNonBillable, List<TimesheetFilter.TimesheetFilterType> filter) throws XServicesFault {
79
		return getTimeEntries(oa_projectid, oa_customerid, oa_userid, startdate, enddate, includeNonBillable, filter);
82
		return getTimeEntries(oa_projectid, oa_customerid, oa_userid, startdate, enddate, includeNonBillable, filter);
80
	}
83
	}
81
	
84
	
82
	
85
	
83
	private List<TimesheetEntry> getTimeEntries(final int oa_projectid, final int oa_customerid, final int oa_userid,
86
	private List<TimesheetEntry> getTimeEntries(final int oa_projectid, final int oa_customerid, final int oa_userid,
84
			final GregorianCalendar fromDate, final GregorianCalendar toDate, final boolean includeNonBillable, List<TimesheetFilter.TimesheetFilterType> filter)
87
			final GregorianCalendar fromDate, final GregorianCalendar toDate, final boolean includeNonBillable, List<TimesheetFilter.TimesheetFilterType> filter)
85
					throws XServicesFault {	
88
					throws XServicesFault {	
86
		
89
		
87
		List<TimesheetEntry> resultlist = new ArrayList<TimesheetEntry>();
90
		List<TimesheetEntry> resultlist = new ArrayList<TimesheetEntry>();
88
		
91
		
89
		if( filter != null && filter.size()>0) {
92
		if( filter != null && filter.size()>0) {
90
				for( TimesheetFilter.TimesheetFilterType f : filter) {
93
				for( TimesheetFilter.TimesheetFilterType f : filter) {
91
					resultlist.addAll(
94
					resultlist.addAll(
92
									getTimeEntriesRAW(oa_projectid, oa_customerid, oa_userid, fromDate, toDate, includeNonBillable, f)
95
									getTimeEntriesRAW(oa_projectid, oa_customerid, oa_userid, fromDate, toDate, includeNonBillable, f)
93
									);
96
									);
94
				}
97
				}
95
		} else {
98
		} else {
96
			resultlist = getTimeEntriesRAW(oa_projectid, oa_customerid, oa_userid, fromDate, toDate, includeNonBillable, null);
99
			resultlist = getTimeEntriesRAW(oa_projectid, oa_customerid, oa_userid, fromDate, toDate, includeNonBillable, null);
97
		}
100
		}
98
		return resultlist;
101
		return resultlist;
99
	}
102
	}
100
			
103
			
101
	
104
	
102
	
105
	
103
 
106
 
104
	private List<TimesheetEntry> getTimeEntriesRAW(final int oa_projectid, final int oa_customerid, final int oa_userid,
107
	private List<TimesheetEntry> getTimeEntriesRAW(final int oa_projectid, final int oa_customerid, final int oa_userid,
105
			final GregorianCalendar fromDate, final GregorianCalendar toDate, final boolean includeNonBillable, TimesheetFilter.TimesheetFilterType filter)
108
			final GregorianCalendar fromDate, final GregorianCalendar toDate, final boolean includeNonBillable, TimesheetFilter.TimesheetFilterType filter)
106
					throws XServicesFault {
109
					throws XServicesFault {
107
 
110
 
108
		final OpenAirRestConnection con = getOpenAirConnection();
111
		final OpenAirRestConnection con = getOpenAirConnection();
109
		if (con == null) {
112
		if (con == null) {
110
			throw new XServicesFault("Could not establish Open Air connection.");
113
			throw new XServicesFault("Could not establish Open Air connection.");
111
		}
114
		}
112
 
115
 
113
		Query query = new Query(TimesheetEntry.class);
116
		Query query = new Query(TimesheetEntry.class);
114
 
117
 
115
		if (oa_projectid > 0) {
118
		if (oa_projectid > 0) {
116
			query.addQuery(new QueryEntity("projectid", new StringEntity(String.valueOf(oa_projectid))), BOOL.AND);
119
			query.addQuery(new QueryEntity("projectid", new StringEntity(String.valueOf(oa_projectid))), BOOL.AND);
117
		}
120
		}
118
 
121
 
119
		if (oa_customerid > 0) {
122
		if (oa_customerid > 0) {
120
			query.addQuery(new QueryEntity("customerid", new StringEntity(String.valueOf(oa_customerid))), BOOL.AND);
123
			query.addQuery(new QueryEntity("customerid", new StringEntity(String.valueOf(oa_customerid))), BOOL.AND);
121
		}
124
		}
122
 
125
 
123
		if (oa_userid > 0) {
126
		if (oa_userid > 0) {
124
			query.addQuery(new QueryEntity("userid", new StringEntity(String.valueOf(oa_userid))), BOOL.AND);
127
			query.addQuery(new QueryEntity("userid", new StringEntity(String.valueOf(oa_userid))), BOOL.AND);
125
		}
128
		}
126
 
129
 
127
		if (fromDate != null) {
130
		if (fromDate != null) {
128
			 //adjust
131
			 //adjust
129
			fromDate.add(GregorianCalendar.DAY_OF_MONTH, -1);
132
			fromDate.add(GregorianCalendar.DAY_OF_MONTH, -1);
130
			DateFilter date = new DateFilter("date");
133
			DateFilter date = new DateFilter("date");
131
			date.setStartdate(fromDate);
134
			date.setStartdate(fromDate);
132
			if (toDate != null) {
135
			if (toDate != null) {
133
				toDate.add(GregorianCalendar.DAY_OF_MONTH, 1);
136
				toDate.add(GregorianCalendar.DAY_OF_MONTH, 1);
134
				date.setEnddate(toDate);
137
				date.setEnddate(toDate);
135
			}
138
			}
136
			query.addFilter(date);
139
			query.addFilter(date);
137
		}
140
		}
138
		
141
		
139
		if (filter != null) {
142
		if (filter != null) {
140
			query.addFilter(new TimesheetFilter(filter));
143
			query.addFilter(new TimesheetFilter(filter));
141
		}
144
		}
142
		
145
		
143
	
146
	
144
		List<TimesheetEntry> timesheets = (List<TimesheetEntry>) con.getEntitiesByQuery(query);
147
		List<TimesheetEntry> timesheets = (List<TimesheetEntry>) con.getEntitiesByQuery(query);
145
		if (!includeNonBillable)
148
		if (!includeNonBillable)
146
			timesheets = filterBillableOnly(timesheets);
149
			timesheets = filterBillableOnly(timesheets);
147
		return timesheets;
150
		return timesheets;
148
 
151
 
149
		/*
152
		/*
150
		 * Query q = new Query(Project.class);
153
		 * Query q = new Query(Project.class);
151
		 * 
154
		 * 
152
		 * //QueryEntity qe = new QueryEntity("Portfolio__c", new
155
		 * //QueryEntity qe = new QueryEntity("Portfolio__c", new
153
		 * StringEntity("Serena")); QueryEntity qe = new QueryEntity("id", new
156
		 * StringEntity("Serena")); QueryEntity qe = new QueryEntity("id", new
154
		 * StringEntity("19738")); //DWP Bank QueryEntity qe2 = new
157
		 * StringEntity("19738")); //DWP Bank QueryEntity qe2 = new
155
		 * QueryEntity("id", new StringEntity("19737")); //PNW //QueryEntity qe2
158
		 * QueryEntity("id", new StringEntity("19737")); //PNW //QueryEntity qe2
156
		 * = new QueryEntity("customer_name", new StringEntity(
159
		 * = new QueryEntity("customer_name", new StringEntity(
157
		 * "Gerencia Informatica Seguridad Social (GISS)"));
160
		 * "Gerencia Informatica Seguridad Social (GISS)"));
158
		 * 
161
		 * 
159
		 * q.addQuery(qe, Query.BOOL.OR); q.addQuery(qe2, Query.BOOL.OR);
162
		 * q.addQuery(qe, Query.BOOL.OR); q.addQuery(qe2, Query.BOOL.OR);
160
		 * 
163
		 * 
161
		 * List<Project> list = (List<Project>) con.getEntitiesByQuery(q, true);
164
		 * List<Project> list = (List<Project>) con.getEntitiesByQuery(q, true);
162
		 * 
165
		 * 
163
		 * /*for (Project p : list) {
166
		 * /*for (Project p : list) {
164
		 * Logger.getLogger("TestMain").debug(p.getName());
167
		 * Logger.getLogger("TestMain").debug(p.getName());
165
		 * System.out.println(p.toString()); }
168
		 * System.out.println(p.toString()); }
166
		 */
169
		 */
167
		// User user = con.getUserByLogin("brosenberger");
170
		// User user = con.getUserByLogin("brosenberger");
168
 
171
 
169
		// System.out.println(user.toString());
172
		// System.out.println(user.toString());
170
	}
173
	}
171
 
174
 
172
	@Override
175
	@Override
173
	public AttachmentType getExcelTimesheet(int oa_projectid, int oa_customerid, int oa_userid,
176
	public AttachmentType getExcelTimesheet(int oa_projectid, int oa_customerid, int oa_userid,
174
			GregorianCalendar startdate, GregorianCalendar enddate, boolean includeNonBillable, List<TimesheetFilter.TimesheetFilterType> filter, AttachmentType template)
177
			GregorianCalendar startdate, GregorianCalendar enddate, boolean includeNonBillable, List<TimesheetFilter.TimesheetFilterType> filter, AttachmentType template)
175
					throws XServicesFault {
178
					throws XServicesFault {
176
 
179
 
177
		List<TimesheetEntry> list = getTimeEntries(oa_projectid, oa_customerid, oa_userid, startdate, enddate,
180
		List<TimesheetEntry> list = getTimeEntries(oa_projectid, oa_customerid, oa_userid, startdate, enddate,
178
				includeNonBillable, filter);
181
				includeNonBillable, filter);
179
		AttachmentType t = getExcelTimesheet2(list, includeNonBillable, template);
182
		AttachmentType t = getExcelTimesheet2(list, includeNonBillable, template);
180
		
183
		
181
		/*
184
		/*
182
		 * Try to set a time sheet name:
185
		 * Try to set a time sheet name:
183
		 * SignOff <From> - <To> <Project> - <Id> <User>.xlxs
186
		 * SignOff <From> - <To> <Project> - <Id> <User>.xlxs
184
		 * 
187
		 * 
185
		 */
188
		 */
186
		String filename = "SignOff";
189
		String filename = "SignOff";
187
		if(startdate!=null) {
190
		if(startdate!=null) {
188
			filename += "_";
191
			filename += "_";
189
			filename += startdate.toZonedDateTime()
192
			filename += startdate.toZonedDateTime()
190
							.format( DateTimeFormatter.ofPattern( "uuuu-MM-dd" ) );
193
							.format( DateTimeFormatter.ofPattern( "uuuu-MM-dd" ) );
191
		}
194
		}
192
		if(enddate!=null) {
195
		if(enddate!=null) {
193
			filename += "-";
196
			filename += "-";
194
			filename += enddate.toZonedDateTime()
197
			filename += enddate.toZonedDateTime()
195
							.format( DateTimeFormatter.ofPattern( "uuuu-MM-dd" ) );
198
							.format( DateTimeFormatter.ofPattern( "uuuu-MM-dd" ) );
196
		}
199
		}
197
		if(oa_projectid > 0) {
200
		if(oa_projectid > 0) {
198
			//Project spezifiziert
201
			//Project spezifiziert
199
			filename += "_";
202
			filename += "_";
200
			if(list.size()>0) {
203
			if(list.size()>0) {
201
				TimesheetEntry anyentry = list.get(0);
204
				TimesheetEntry anyentry = list.get(0);
202
				filename += anyentry.getProject().getName();
205
				filename += anyentry.getProject().getName();
203
			} else {
206
			} else {
204
				filename +="Emtpy";
207
				filename +="Emtpy";
205
			}
208
			}
206
		} else {
209
		} else {
207
			//Mehrere Projekte, gibt es vielleicht nur eins?
210
			//Mehrere Projekte, gibt es vielleicht nur eins?
208
			int i = 0;
211
			int i = 0;
209
			Set<String> namen = new HashSet<String>();
212
			Set<String> namen = new HashSet<String>();
210
			for (TimesheetEntry entry : list) {
213
			for (TimesheetEntry entry : list) {
211
				namen.add( entry.getProject().getId() );				
214
				namen.add( entry.getProject().getId() );				
212
			}
215
			}
213
			if(namen.size()>1) {
216
			if(namen.size()>1) {
214
				filename += "_Multiple_Projects";
217
				filename += "_Multiple_Projects";
215
			} else {
218
			} else {
216
				if(list.size()>0) {
219
				if(list.size()>0) {
217
					TimesheetEntry anyentry = list.get(0);
220
					TimesheetEntry anyentry = list.get(0);
218
					filename += anyentry.getProject().getName();
221
					filename += anyentry.getProject().getName();
219
				} else {
222
				} else {
220
					filename +="_Emtpy";
223
					filename +="_Emtpy";
221
				}
224
				}
222
			}
225
			}
223
		}
226
		}
224
		//Username
227
		//Username
225
		if(oa_userid>0) {
228
		if(oa_userid>0) {
226
			filename += "_";
229
			filename += "_";
227
			if(list.size()>0) {
230
			if(list.size()>0) {
228
				TimesheetEntry anyentry = list.get(0);
231
				TimesheetEntry anyentry = list.get(0);
229
				filename += anyentry.getUser().getName();
232
				filename += anyentry.getUser().getName();
230
			}
233
			}
231
		}
234
		}
232
		
235
		
233
		filename = filename.replace(" ","_");
236
		filename = filename.replace(" ","_");
234
		filename = filename.replaceAll("[^A-Za-z0-9_\\-]", "");
237
		filename = filename.replaceAll("[^A-Za-z0-9_\\-]", "");
235
		filename += ".xlsx";
238
		filename += ".xlsx";
236
		t.setFilename(filename);
239
		t.setFilename(filename);
237
		
240
		
238
		return t;
241
		return t;
239
	}
242
	}
240
 
243
 
241
	@Override
244
	@Override
242
	public AttachmentType getExcelTimesheet2(List<TimesheetEntry> entries, boolean includeNonBillable,
245
	public AttachmentType getExcelTimesheet2(List<TimesheetEntry> entries, boolean includeNonBillable,
243
			AttachmentType templatefile) throws XServicesFault {
246
			AttachmentType templatefile) throws XServicesFault {
244
		DocBuilder builder = new DocBuilder();
247
		DocBuilder builder = new DocBuilder();
245
		DataHandler dh;
248
		DataHandler dh;
246
		if (templatefile != null) {
249
		if (templatefile != null) {
247
			dh = builder.createTimesheet(entries, templatefile.getContent());
250
			dh = builder.createTimesheet(entries, templatefile.getContent());
248
		} else {
251
		} else {
249
			dh = builder.createTimesheet(entries, null);
252
			dh = builder.createTimesheet(entries, null);
250
		}
253
		}
251
		AttachmentType t = new AttachmentType();
254
		AttachmentType t = new AttachmentType();
252
		t.setContent(dh);
255
		t.setContent(dh);
253
		
256
		
254
		t.setFilename("timesheet.xlsx");
257
		t.setFilename("timesheet.xlsx");
255
		return t;
258
		return t;
256
	}
259
	}
257
 
260
 
258
	@Override
261
	@Override
259
	public List<Project> getProjectsByExternalId(String externalid) throws XServicesFault {
262
	public List<Project> getProjectsByExternalId(String externalid) throws XServicesFault {
260
 
263
 
261
		final OpenAirRestConnection con = getOpenAirConnection();
264
		final OpenAirRestConnection con = getOpenAirConnection();
262
		if (con == null) {
265
		if (con == null) {
263
			throw new XServicesFault("Could not establish Open Air connection.");
266
			throw new XServicesFault("Could not establish Open Air connection.");
264
		}
267
		}
265
 
268
 
266
		Query query = new Query(Project.class);
269
		Query query = new Query(Project.class);
267
 
270
 
268
		if (externalid.length() > 0) {
271
		if (externalid.length() > 0) {
269
			query.addQuery(new QueryEntity("externalid", new StringEntity(externalid)), BOOL.AND);
272
			query.addQuery(new QueryEntity("externalid", new StringEntity(externalid)), BOOL.AND);
270
		}
273
		}
271
 
274
 
272
		List<Project> projects = (List<Project>) con.getEntitiesByQuery(query);
275
		List<Project> projects = (List<Project>) con.getEntitiesByQuery(query);
273
 
276
 
274
		return projects;
277
		return projects;
275
	}
278
	}
276
 
279
 
277
	/*
280
	/*
278
	 * (non-Javadoc)
281
	 * (non-Javadoc)
279
	 * 
282
	 * 
280
	 * @see
283
	 * @see
281
	 * net.brutex.xservices.ws.OpenAirProxyService#getProjectsByOppId(java.lang.
284
	 * net.brutex.xservices.ws.OpenAirProxyService#getProjectsByOppId(java.lang.
282
	 * String)
285
	 * String)
283
	 */
286
	 */
284
	@Override
287
	@Override
285
	public List<Project> getProjectsByOppId(String oppid) throws XServicesFault {
288
	public List<Project> getProjectsByOppId(String oppid) throws XServicesFault {
286
		final OpenAirRestConnection con = getOpenAirConnection();
289
		final OpenAirRestConnection con = getOpenAirConnection();
287
		if (con == null) {
290
		if (con == null) {
288
			throw new XServicesFault("Could not establish Open Air connection.");
291
			throw new XServicesFault("Could not establish Open Air connection.");
289
		}
292
		}
290
 
293
 
291
		Query query = new Query(Project.class);
294
		Query query = new Query(Project.class);
292
 
295
 
293
		if (oppid.length() > 0) {
296
		if (oppid.length() > 0) {
294
			query.addQuery(new QueryEntity("opportunity_num__c", new StringEntity(oppid)), BOOL.OR);
297
			query.addQuery(new QueryEntity("opportunity_num__c", new StringEntity(oppid)), BOOL.OR);
295
			query.addQuery(new QueryEntity("SAP_Oracle_Number__c", new StringEntity(oppid)), BOOL.OR);
298
			query.addQuery(new QueryEntity("SAP_Oracle_Number__c", new StringEntity(oppid)), BOOL.OR);
296
		}
299
		}
297
 
300
 
298
		List<Project> projects = (List<Project>) con.getEntitiesByQuery(query);
301
		List<Project> projects = (List<Project>) con.getEntitiesByQuery(query);
299
 
302
 
300
		return projects;
303
		return projects;
301
	}
304
	}
302
 
305
 
303
	@Override
306
	@Override
304
	public Customer getCustomerById(int customerid) throws XServicesFault {
307
	public Customer getCustomerById(int customerid) throws XServicesFault {
305
		final OpenAirRestConnection con = getOpenAirConnection();
308
		final OpenAirRestConnection con = getOpenAirConnection();
306
		if (con == null) {
309
		if (con == null) {
307
			throw new XServicesFault("Could not establish Open Air connection.");
310
			throw new XServicesFault("Could not establish Open Air connection.");
308
		}
311
		}
309
 
312
 
310
		Query query = new Query(Customer.class);
313
		Query query = new Query(Customer.class);
311
 
314
 
312
		if (customerid > 0) {
315
		if (customerid > 0) {
313
			query.addQuery(new QueryEntity("id", new StringEntity(String.valueOf(customerid))), BOOL.AND);
316
			query.addQuery(new QueryEntity("id", new StringEntity(String.valueOf(customerid))), BOOL.AND);
314
		}
317
		}
315
 
318
 
316
		List<Customer> customer = (List<Customer>) con.getEntitiesByQuery(query);
319
		List<Customer> customer = (List<Customer>) con.getEntitiesByQuery(query);
317
 
320
 
318
		if (customer.isEmpty()) {
321
		if (customer.isEmpty()) {
319
			return null;
322
			return null;
320
		} else {
323
		} else {
321
			return customer.get(0);
324
			return customer.get(0);
322
		}
325
		}
323
	}
326
	}
324
	
327
	
325
	
328
	
326
	
329
	
327
	/* (non-Javadoc)
330
	/* (non-Javadoc)
328
	 * @see net.brutex.xservices.ws.OpenAirProxyService#getUpdatedCustomer(java.util.GregorianCalendar)
331
	 * @see net.brutex.xservices.ws.OpenAirProxyService#getUpdatedCustomer(java.util.GregorianCalendar)
329
	 */
332
	 */
330
	@Override
333
	@Override
331
	public List<Customer> getUpdatedCustomer(GregorianCalendar date, int offset) throws XServicesFault {
334
	public List<Customer> getUpdatedCustomer(GregorianCalendar date, int offset) throws XServicesFault {
332
			
335
			
333
		final OpenAirRestConnection con = getOpenAirConnection();
336
		final OpenAirRestConnection con = getOpenAirConnection();
334
		if (con == null) {
337
		if (con == null) {
335
			throw new XServicesFault("Could not establish Open Air connection.");
338
			throw new XServicesFault("Could not establish Open Air connection.");
336
		}
339
		}
337
 
340
 
338
		Query query = new Query(Customer.class);
341
		Query query = new Query(Customer.class);
339
		//query.addQuery("Address_Country", new StringEntity(search), BOOL.AND);
342
		//query.addQuery("Address_Country", new StringEntity(search), BOOL.AND);
340
		query.addQuery("active", new StringEntity("1"), BOOL.AND);
343
		query.addQuery("active", new StringEntity("1"), BOOL.AND);
341
		
344
		
342
		DateFilter datefilter = new DateFilter("updated");
345
		DateFilter datefilter = new DateFilter("updated");
343
		date.add(GregorianCalendar.MINUTE, -1*offset);
346
		date.add(GregorianCalendar.MINUTE, -1*offset);
344
		datefilter.setStartdate(date);		
347
		datefilter.setStartdate(date);		
345
		query.addFilter(datefilter);
348
		query.addFilter(datefilter);
346
 
349
 
347
		List<Customer> customer = (List<Customer>) con.getEntitiesByQuery(query);
350
		List<Customer> customer = (List<Customer>) con.getEntitiesByQuery(query);
348
 
351
 
349
		if (customer.isEmpty()) {
352
		if (customer.isEmpty()) {
350
			return null;
353
			return null;
351
		} else {
354
		} else {
352
			return customer;
355
			return customer;
353
		}
356
		}
354
	}
357
	}
355
	
358
	
356
	
359
	
357
	@Override
360
	@Override
358
	public List<Project> getUpdatedProjects(GregorianCalendar date, int offset) throws XServicesFault {
361
	public List<Project> getUpdatedProjects(GregorianCalendar date, int offset) throws XServicesFault {
359
			
362
			
360
		final OpenAirRestConnection con = getOpenAirConnection();
363
		final OpenAirRestConnection con = getOpenAirConnection();
361
		if (con == null) {
364
		if (con == null) {
362
			throw new XServicesFault("Could not establish Open Air connection.");
365
			throw new XServicesFault("Could not establish Open Air connection.");
363
		}
366
		}
364
 
367
 
365
		Query query = new Query(Project.class);
368
		Query query = new Query(Project.class);
366
		//query.addQuery("Address_Country", new StringEntity(search), BOOL.AND);
369
		//query.addQuery("Address_Country", new StringEntity(search), BOOL.AND);
367
		query.addQuery("Stage", new StringEntity("3"), BOOL.AND);
370
		query.addQuery("Stage", new StringEntity("3"), BOOL.AND);
368
		
371
		
369
		DateFilter datefilter = new DateFilter("updated");
372
		DateFilter datefilter = new DateFilter("updated");
370
		date.add(GregorianCalendar.MINUTE, -1*offset);
373
		date.add(GregorianCalendar.MINUTE, -1*offset);
371
		datefilter.setStartdate(date);		
374
		datefilter.setStartdate(date);		
372
		query.addFilter(datefilter);
375
		query.addFilter(datefilter);
373
 
376
 
374
		List<Project> project = (List<Project>) con.getEntitiesByQuery(query);
377
		List<Project> project = (List<Project>) con.getEntitiesByQuery(query);
375
 
378
 
376
		if (project.isEmpty()) {
379
		if (project.isEmpty()) {
377
			return null;
380
			return null;
378
		} else {
381
		} else {
379
			return project;
382
			return project;
380
		}
383
		}
381
	}
384
	}
382
 
385
 
383
	@Override
386
	@Override
384
	public User getUserById(int userid) throws XServicesFault {
387
	public User getUserById(int userid) throws XServicesFault {
385
		Query query = new Query(User.class);
388
		Query query = new Query(User.class);
386
		if (userid > 0) {
389
		if (userid > 0) {
387
			query.addQuery(new QueryEntity("id", new StringEntity(String.valueOf(userid))), BOOL.AND);
390
			query.addQuery(new QueryEntity("id", new StringEntity(String.valueOf(userid))), BOOL.AND);
388
		} else {
391
		} else {
389
			return null;
392
			return null;
390
		}
393
		}
391
		return getUserByQuery(query);
394
		return getUserByQuery(query);
392
	}
395
	}
393
	
396
	
394
 
397
 
395
	@Override
398
	@Override
396
	public User getUserByUsername(String username) throws XServicesFault {
399
	public User getUserByUsername(String username) throws XServicesFault {
397
		Query query = new Query(User.class);
400
		Query query = new Query(User.class);
398
		query.addQuery(new QueryEntity("nickname", new StringEntity(username)), BOOL.AND);
401
		query.addQuery(new QueryEntity("nickname", new StringEntity(username)), BOOL.AND);
399
		return getUserByQuery(query);
402
		return getUserByQuery(query);
400
	}
403
	}
401
	
404
	
402
	
405
	
403
	@Override
406
	@Override
404
	public List<AnyEntity> getAnyObject(String objecttype, List<QueryParameter> queries) throws XServicesFault {
407
	public List<AnyEntity> getAnyObject(String objecttype, List<QueryParameter> queries) throws XServicesFault {
405
		JITCompiler.load();
408
		JITCompiler.load();
406
		final OpenAirRestConnection con = getOpenAirConnection();
409
		final OpenAirRestConnection con = getOpenAirConnection();
407
		if (con == null) {
410
		if (con == null) {
408
			throw new XServicesFault("Could not establish Open Air connection.");
411
			throw new XServicesFault("Could not establish Open Air connection.");
409
		}
412
		}
410
		Class clazz;
413
		Class clazz;
411
		try {
414
		try {
412
			Class.forName("net.brutex.mgmt.api.xml.EntityType");
415
			Class.forName("net.brutex.mgmt.api.xml.EntityType");
413
			Class.forName("net.brutex.mgmt.api.xml.AbstractEntity");
416
			Class.forName("net.brutex.mgmt.api.xml.AbstractEntity");
414
			clazz = Class.forName("net.brutex.mgmt.api.xml."+objecttype);
417
			clazz = Class.forName("net.brutex.mgmt.api.xml."+objecttype);
415
		} catch (ClassNotFoundException ex ) {
418
		} catch (ClassNotFoundException ex ) {
416
			throw new XServicesFault(ex);
419
			throw new XServicesFault(ex);
417
		}
420
		}
418
		Query query = new Query(clazz);
421
		Query query = new Query(clazz);
419
 
422
 
420
		for(QueryParameter p : queries) {
423
		for(QueryParameter p : queries) {
421
			query.addQuery(new QueryEntity(p.field, new StringEntity(p.value)), BOOL.AND);
424
			query.addQuery(new QueryEntity(p.field, new StringEntity(p.value)), BOOL.AND);
422
		}
425
		}
423
 
426
 
424
		List<AnyEntity> anyentity = (List<AnyEntity>) con.getEntitiesByQuery(query);
427
		List<AnyEntity> anyentity = (List<AnyEntity>) con.getEntitiesByQuery(query);
425
 
428
 
426
		if (anyentity.isEmpty()) {
429
		if (anyentity.isEmpty()) {
427
			return null;
430
			return null;
428
		} else {
431
		} else {
429
			return anyentity;
432
			return anyentity;
430
		}
433
		}
431
	}
434
	}
432
	
435
	
433
	private User getUserByQuery(Query query) throws XServicesFault {
436
	private User getUserByQuery(Query query) throws XServicesFault {
434
		final OpenAirRestConnection con = getOpenAirConnection();
437
		final OpenAirRestConnection con = getOpenAirConnection();
435
		if (con == null) {
438
		if (con == null) {
436
			throw new XServicesFault("Could not establish Open Air connection.");
439
			throw new XServicesFault("Could not establish Open Air connection.");
437
		}
440
		}
438
		User user = (User) con.getSingleEntityByQuery(query);
441
		User user = (User) con.getSingleEntityByQuery(query);
439
		return user;
442
		return user;
440
	}
443
	}
441
	
444
	
442
	
445
	
443
 
446
 
444
	List<TimesheetEntry> filterBillableOnly(List<TimesheetEntry> timesheets) {
447
	List<TimesheetEntry> filterBillableOnly(List<TimesheetEntry> timesheets) {
445
		List<TimesheetEntry> list = new ArrayList<TimesheetEntry>();
448
		List<TimesheetEntry> list = new ArrayList<TimesheetEntry>();
446
		for (TimesheetEntry entry : timesheets) {
449
		for (TimesheetEntry entry : timesheets) {
447
			if (entry.getProjecttask().isBillable()) {
450
			if (entry.getProjecttask().isBillable()) {
448
				list.add(entry);
451
				list.add(entry);
449
			}
452
			}
450
		}
453
		}
451
		return list;
454
		return list;
452
	}
455
	}
453
 
456
 
454
	private OpenAirRestConnection getOpenAirConnection() {
457
	private OpenAirRestConnection getOpenAirConnection() {
455
 
458
 
456
		/*
459
		/*
457
		 * get details from configuration file
460
		 * get details from configuration file
458
		 */
461
		 */
459
		final PropertiesConfiguration props;
462
		final PropertiesConfiguration props;
460
		try {
463
		try {
461
			final String config = "../openair.properties";
464
			final String config = "../openair.properties";
462
			logger.debug("Loading Open Air connection details from " + this.getClass().getClassLoader().getResource("/")
465
			logger.debug("Loading Open Air connection details from " + this.getClass().getClassLoader().getResource("/")
463
					+ config);
466
					+ config);
464
 
467
 
465
			final URL configloc = this.getClass().getClassLoader().getResource(config);
468
			final URL configloc = this.getClass().getClassLoader().getResource(config);
466
 
469
 
-
 
470
			props = new PropertiesConfiguration();
467
			props = new PropertiesConfiguration(configloc);
471
			props.read( new InputStreamReader( new BufferedInputStream( configloc.openStream() )));
468
			final String user = props.getString("user");
472
			final String user = props.getString("user");
469
			final String password = props.getString("password");
473
			final String password = props.getString("password");
470
			final String company = props.getString("company");
474
			final String company = props.getString("company");
471
			final String apikey = props.getString("apikey", "_PUT_HERE_");
475
			final String apikey = props.getString("apikey", "_PUT_HERE_");
472
			final String namespace = props.getString("namespace");
476
			final String namespace = props.getString("namespace");
473
 
477
 
474
			final OpenAirRestConnection con;
478
			final OpenAirRestConnection con;
475
 
479
 
476
			con = new OpenAirRestConnection(JCS.getInstance("OACache"), company, user, password);
480
			con = new OpenAirRestConnection(JCS.getInstance("OACache"), company, user, password);
477
			return con;
481
			return con;
478
		} catch (CacheException e) {
482
		} catch (CacheException e) {
479
			logger.error(e);
483
			logger.error(e);
480
			e.printStackTrace();
484
			e.printStackTrace();
481
		} catch (ConfigurationException e) {
485
		} catch (ConfigurationException e) {
482
			logger.error(e);
486
			logger.error(e);
-
 
487
			e.printStackTrace();
-
 
488
		} catch (IOException e) {
-
 
489
			logger.error(e);
483
			e.printStackTrace();
490
			e.printStackTrace();
484
		} finally {
491
		} finally {
485
 
492
 
486
		}
493
		}
487
		return null;
494
		return null;
488
	}
495
	}
489
	
496
	
490
	
497
	
491
 
498
 
492
	
499
	
493
 
500
 
494
}
501
}