Rev 198 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
/*
* Copyright 2017 Brian Rosenberger (Brutex Network)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.brutex.xservices.ws.impl;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.activation.DataHandler;
import javax.jws.WebParam;
import javax.jws.WebService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.jcs.JCS;
import org.apache.commons.jcs.access.exception.CacheException;
import net.brutex.DocBuilder.DocBuilder;
import net.brutex.mgmt.api.generator.JITCompiler;
import net.brutex.mgmt.api.xml.AnyEntity;
import net.brutex.mgmt.api.xml.Customer;
import net.brutex.mgmt.api.xml.DateFilter;
import net.brutex.mgmt.api.xml.Project;
import net.brutex.mgmt.api.xml.Query;
import net.brutex.mgmt.api.xml.Query.BOOL;
import net.brutex.mgmt.api.xml.QueryEntity;
import net.brutex.mgmt.api.xml.StringEntity;
import net.brutex.mgmt.api.xml.TimesheetEntry;
import net.brutex.mgmt.api.xml.TimesheetFilter;
import net.brutex.mgmt.api.xml.User;
import net.brutex.mgmt.openair.OpenAirRestConnection;
import net.brutex.xservices.types.ant.AttachmentType;
import net.brutex.xservices.util.BrutexNamespaces;
import net.brutex.xservices.ws.OpenAirProxyService;
import net.brutex.xservices.ws.XServicesFault;
/**
* @author Brian Rosenberger
*
*/
@Slf4j
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.OpenAirProxyService", serviceName = OpenAirProxyService.SERVICE_NAME)
public class OpenAirProxyServiceImpl implements OpenAirProxyService {
/*
* (non-Javadoc)
*
* @see
* net.brutex.xservices.ws.OpenAirProxyService#getTimeEntryList(java.lang.
* String)
*/
@Override
public List<TimesheetEntry> getTimeEntryList(int oa_projectid, int oa_customerid, int oa_userid,
GregorianCalendar startdate, GregorianCalendar enddate, boolean includeNonBillable, List<TimesheetFilter.TimesheetFilterType> filter) throws XServicesFault {
return getTimeEntries(oa_projectid, oa_customerid, oa_userid, startdate, enddate, includeNonBillable, filter);
}
private List<TimesheetEntry> getTimeEntries(final int oa_projectid, final int oa_customerid, final int oa_userid,
final GregorianCalendar fromDate, final GregorianCalendar toDate, final boolean includeNonBillable, List<TimesheetFilter.TimesheetFilterType> filter)
throws XServicesFault {
List<TimesheetEntry> resultlist = new ArrayList<TimesheetEntry>();
if( filter != null && filter.size()>0) {
for( TimesheetFilter.TimesheetFilterType f : filter) {
resultlist.addAll(
getTimeEntriesRAW(oa_projectid, oa_customerid, oa_userid, fromDate, toDate, includeNonBillable, f)
);
}
} else {
resultlist = getTimeEntriesRAW(oa_projectid, oa_customerid, oa_userid, fromDate, toDate, includeNonBillable, null);
}
return resultlist;
}
private List<TimesheetEntry> getTimeEntriesRAW(final int oa_projectid, final int oa_customerid, final int oa_userid,
final GregorianCalendar fromDate, final GregorianCalendar toDate, final boolean includeNonBillable, TimesheetFilter.TimesheetFilterType filter)
throws XServicesFault {
final OpenAirRestConnection con = getOpenAirConnection();
if (con == null) {
throw new XServicesFault("Could not establish Open Air connection.");
}
Query query = new Query(TimesheetEntry.class);
if (oa_projectid > 0) {
query.addQuery(new QueryEntity("projectid", new StringEntity(String.valueOf(oa_projectid))), BOOL.AND);
}
if (oa_customerid > 0) {
query.addQuery(new QueryEntity("customerid", new StringEntity(String.valueOf(oa_customerid))), BOOL.AND);
}
if (oa_userid > 0) {
query.addQuery(new QueryEntity("userid", new StringEntity(String.valueOf(oa_userid))), BOOL.AND);
}
if (fromDate != null) {
//adjust
fromDate.add(GregorianCalendar.DAY_OF_MONTH, -1);
DateFilter date = new DateFilter("date");
date.setStartdate(fromDate);
if (toDate != null) {
toDate.add(GregorianCalendar.DAY_OF_MONTH, 1);
date.setEnddate(toDate);
}
query.addFilter(date);
}
if (filter != null) {
query.addFilter(new TimesheetFilter(filter));
}
List<TimesheetEntry> timesheets = (List<TimesheetEntry>) con.getEntitiesByQuery(query);
if (!includeNonBillable)
timesheets = filterBillableOnly(timesheets);
return timesheets;
/*
* Query q = new Query(Project.class);
*
* //QueryEntity qe = new QueryEntity("Portfolio__c", new
* StringEntity("Serena")); QueryEntity qe = new QueryEntity("id", new
* StringEntity("19738")); //DWP Bank QueryEntity qe2 = new
* QueryEntity("id", new StringEntity("19737")); //PNW //QueryEntity qe2
* = new QueryEntity("customer_name", new StringEntity(
* "Gerencia Informatica Seguridad Social (GISS)"));
*
* q.addQuery(qe, Query.BOOL.OR); q.addQuery(qe2, Query.BOOL.OR);
*
* List<Project> list = (List<Project>) con.getEntitiesByQuery(q, true);
*
* /*for (Project p : list) {
* Logger.getLogger("TestMain").debug(p.getName());
* System.out.println(p.toString()); }
*/
// User user = con.getUserByLogin("brosenberger");
// System.out.println(user.toString());
}
@Override
public AttachmentType getExcelTimesheet(int oa_projectid, int oa_customerid, int oa_userid,
GregorianCalendar startdate, GregorianCalendar enddate, boolean includeNonBillable, List<TimesheetFilter.TimesheetFilterType> filter, AttachmentType template)
throws XServicesFault {
List<TimesheetEntry> list = getTimeEntries(oa_projectid, oa_customerid, oa_userid, startdate, enddate,
includeNonBillable, filter);
AttachmentType t = getExcelTimesheet2(list, includeNonBillable, template);
/*
* Try to set a time sheet name:
* SignOff <From> - <To> <Project> - <Id> <User>.xlxs
*
*/
String filename = "SignOff";
if(startdate!=null) {
filename += "_";
filename += startdate.toZonedDateTime()
.format( DateTimeFormatter.ofPattern( "uuuu-MM-dd" ) );
}
if(enddate!=null) {
filename += "-";
filename += enddate.toZonedDateTime()
.format( DateTimeFormatter.ofPattern( "uuuu-MM-dd" ) );
}
if(oa_projectid > 0) {
//Project spezifiziert
filename += "_";
if(list.size()>0) {
TimesheetEntry anyentry = list.get(0);
filename += anyentry.getProject().getName();
} else {
filename +="Emtpy";
}
} else {
//Mehrere Projekte, gibt es vielleicht nur eins?
int i = 0;
Set<String> namen = new HashSet<String>();
for (TimesheetEntry entry : list) {
namen.add( entry.getProject().getId() );
}
if(namen.size()>1) {
filename += "_Multiple_Projects";
} else {
if(list.size()>0) {
TimesheetEntry anyentry = list.get(0);
filename += anyentry.getProject().getName();
} else {
filename +="_Emtpy";
}
}
}
//Username
if(oa_userid>0) {
filename += "_";
if(list.size()>0) {
TimesheetEntry anyentry = list.get(0);
filename += anyentry.getUser().getName();
}
}
filename = filename.replace(" ","_");
filename = filename.replaceAll("[^A-Za-z0-9_\\-]", "");
filename += ".xlsx";
t.setFilename(filename);
return t;
}
@Override
public AttachmentType getExcelTimesheet2(List<TimesheetEntry> entries, boolean includeNonBillable,
AttachmentType templatefile) throws XServicesFault {
DocBuilder builder = new DocBuilder();
DataHandler dh;
if (templatefile != null) {
dh = builder.createTimesheet(entries, templatefile.getContent());
} else {
dh = builder.createTimesheet(entries, null);
}
AttachmentType t = new AttachmentType();
t.setContent(dh);
t.setFilename("timesheet.xlsx");
return t;
}
@Override
public List<Project> getProjectsByExternalId(String externalid) throws XServicesFault {
final OpenAirRestConnection con = getOpenAirConnection();
if (con == null) {
throw new XServicesFault("Could not establish Open Air connection.");
}
Query query = new Query(Project.class);
if (externalid.length() > 0) {
query.addQuery(new QueryEntity("externalid", new StringEntity(externalid)), BOOL.AND);
}
List<Project> projects = (List<Project>) con.getEntitiesByQuery(query);
return projects;
}
/*
* (non-Javadoc)
*
* @see
* net.brutex.xservices.ws.OpenAirProxyService#getProjectsByOppId(java.lang.
* String)
*/
@Override
public List<Project> getProjectsByOppId(String oppid) throws XServicesFault {
final OpenAirRestConnection con = getOpenAirConnection();
if (con == null) {
throw new XServicesFault("Could not establish Open Air connection.");
}
Query query = new Query(Project.class);
if (oppid.length() > 0) {
query.addQuery(new QueryEntity("opportunity_num__c", new StringEntity(oppid)), BOOL.OR);
query.addQuery(new QueryEntity("SAP_Oracle_Number__c", new StringEntity(oppid)), BOOL.OR);
}
List<Project> projects = (List<Project>) con.getEntitiesByQuery(query);
return projects;
}
@Override
public Customer getCustomerById(int customerid) throws XServicesFault {
final OpenAirRestConnection con = getOpenAirConnection();
if (con == null) {
throw new XServicesFault("Could not establish Open Air connection.");
}
Query query = new Query(Customer.class);
if (customerid > 0) {
query.addQuery(new QueryEntity("id", new StringEntity(String.valueOf(customerid))), BOOL.AND);
}
List<Customer> customer = (List<Customer>) con.getEntitiesByQuery(query);
if (customer.isEmpty()) {
return null;
} else {
return customer.get(0);
}
}
/* (non-Javadoc)
* @see net.brutex.xservices.ws.OpenAirProxyService#getUpdatedCustomer(java.util.GregorianCalendar)
*/
@Override
public List<Customer> getUpdatedCustomer(GregorianCalendar date, int offset) throws XServicesFault {
final OpenAirRestConnection con = getOpenAirConnection();
if (con == null) {
throw new XServicesFault("Could not establish Open Air connection.");
}
Query query = new Query(Customer.class);
//query.addQuery("Address_Country", new StringEntity(search), BOOL.AND);
query.addQuery("active", new StringEntity("1"), BOOL.AND);
DateFilter datefilter = new DateFilter("updated");
date.add(GregorianCalendar.MINUTE, -1*offset);
datefilter.setStartdate(date);
query.addFilter(datefilter);
List<Customer> customer = (List<Customer>) con.getEntitiesByQuery(query);
if (customer.isEmpty()) {
return null;
} else {
return customer;
}
}
@Override
public List<Project> getUpdatedProjects(GregorianCalendar date, int offset) throws XServicesFault {
final OpenAirRestConnection con = getOpenAirConnection();
if (con == null) {
throw new XServicesFault("Could not establish Open Air connection.");
}
Query query = new Query(Project.class);
//query.addQuery("Address_Country", new StringEntity(search), BOOL.AND);
query.addQuery("Stage", new StringEntity("3"), BOOL.AND);
DateFilter datefilter = new DateFilter("updated");
date.add(GregorianCalendar.MINUTE, -1*offset);
datefilter.setStartdate(date);
query.addFilter(datefilter);
List<Project> project = (List<Project>) con.getEntitiesByQuery(query);
if (project.isEmpty()) {
return null;
} else {
return project;
}
}
@Override
public User getUserById(int userid) throws XServicesFault {
Query query = new Query(User.class);
if (userid > 0) {
query.addQuery(new QueryEntity("id", new StringEntity(String.valueOf(userid))), BOOL.AND);
} else {
return null;
}
return getUserByQuery(query);
}
@Override
public User getUserByUsername(String username) throws XServicesFault {
Query query = new Query(User.class);
query.addQuery(new QueryEntity("nickname", new StringEntity(username)), BOOL.AND);
return getUserByQuery(query);
}
@Override
public List<AnyEntity> getAnyObject(String objecttype, List<QueryParameter> queries) throws XServicesFault {
JITCompiler.load();
final OpenAirRestConnection con = getOpenAirConnection();
if (con == null) {
throw new XServicesFault("Could not establish Open Air connection.");
}
Class clazz;
try {
Class.forName("net.brutex.mgmt.api.xml.EntityType");
Class.forName("net.brutex.mgmt.api.xml.AbstractEntity");
clazz = Class.forName("net.brutex.mgmt.api.xml."+objecttype);
} catch (ClassNotFoundException ex ) {
throw new XServicesFault(ex);
}
Query query = new Query(clazz);
for(QueryParameter p : queries) {
query.addQuery(new QueryEntity(p.field, new StringEntity(p.value)), BOOL.AND);
}
List<AnyEntity> anyentity = (List<AnyEntity>) con.getEntitiesByQuery(query);
if (anyentity.isEmpty()) {
return null;
} else {
return anyentity;
}
}
private User getUserByQuery(Query query) throws XServicesFault {
final OpenAirRestConnection con = getOpenAirConnection();
if (con == null) {
throw new XServicesFault("Could not establish Open Air connection.");
}
User user = (User) con.getSingleEntityByQuery(query);
return user;
}
List<TimesheetEntry> filterBillableOnly(List<TimesheetEntry> timesheets) {
List<TimesheetEntry> list = new ArrayList<TimesheetEntry>();
for (TimesheetEntry entry : timesheets) {
if (entry.getProjecttask().isBillable()) {
list.add(entry);
}
}
return list;
}
private OpenAirRestConnection getOpenAirConnection() {
/*
* get details from configuration file
*/
final PropertiesConfiguration props;
try {
final String config = "../openair.properties";
log.debug("Loading Open Air connection details from " + this.getClass().getClassLoader().getResource("/")
+ config);
final URL configloc = this.getClass().getClassLoader().getResource(config);
props = new PropertiesConfiguration();
props.read( new InputStreamReader( new BufferedInputStream( configloc.openStream() )));
final String user = props.getString("user");
final String password = props.getString("password");
final String company = props.getString("company");
final String apikey = props.getString("apikey", "_PUT_HERE_");
final String namespace = props.getString("namespace");
final OpenAirRestConnection con;
con = new OpenAirRestConnection(JCS.getInstance("OACache"), company, user, password);
return con;
} catch (CacheException | ConfigurationException | IOException e) {
log.error(e.getMessage());
e.printStackTrace();
} finally {
}
return null;
}
}