Subversion Repositories XServices

Rev

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