Subversion Repositories XServices

Rev

Rev 198 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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