Subversion Repositories XServices

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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