188 |
brianR |
1 |
package net.brutex.xservices.ws.rs;
|
|
|
2 |
|
|
|
3 |
import java.net.URL;
|
|
|
4 |
import java.util.GregorianCalendar;
|
|
|
5 |
import java.util.List;
|
|
|
6 |
|
|
|
7 |
import javax.ws.rs.core.GenericEntity;
|
|
|
8 |
import javax.ws.rs.core.HttpHeaders;
|
|
|
9 |
import javax.ws.rs.core.Response;
|
|
|
10 |
|
191 |
brianR |
11 |
import org.apache.commons.configuration2.PropertiesConfiguration;
|
|
|
12 |
import org.apache.commons.configuration2.builder.fluent.Configurations;
|
|
|
13 |
import org.apache.commons.configuration2.ex.ConfigurationException;
|
188 |
brianR |
14 |
import org.apache.commons.jcs.JCS;
|
|
|
15 |
import org.apache.commons.jcs.access.exception.CacheException;
|
|
|
16 |
import org.apache.logging.log4j.LogManager;
|
|
|
17 |
import org.apache.logging.log4j.Logger;
|
|
|
18 |
|
|
|
19 |
import net.brutex.mgmt.api.xml.Customer;
|
|
|
20 |
import net.brutex.mgmt.api.xml.DateFilter;
|
|
|
21 |
import net.brutex.mgmt.api.xml.Query;
|
|
|
22 |
import net.brutex.mgmt.api.xml.Query.BOOL;
|
|
|
23 |
import net.brutex.mgmt.api.xml.StringEntity;
|
|
|
24 |
import net.brutex.mgmt.openair.OpenAirRestConnection;
|
|
|
25 |
|
|
|
26 |
public class OpenAirInfoServiceImpl implements OpenAirInfoService {
|
|
|
27 |
|
|
|
28 |
static final Logger logger = LogManager.getLogger();
|
|
|
29 |
|
|
|
30 |
@Override
|
|
|
31 |
public Response getCompanies(HttpHeaders paramHttpHeaders, String search) {
|
|
|
32 |
OpenAirRestConnection con = getOpenAirConnection();
|
|
|
33 |
Query query = new Query(Customer.class);
|
|
|
34 |
|
|
|
35 |
query.addQuery("Address_Country", new StringEntity(search), BOOL.AND);
|
|
|
36 |
query.addQuery("active", new StringEntity("1"), BOOL.AND);
|
|
|
37 |
DateFilter datefilter = new DateFilter("updated");
|
|
|
38 |
GregorianCalendar date = new GregorianCalendar();
|
|
|
39 |
|
|
|
40 |
date.add(GregorianCalendar.MINUTE, -5);
|
|
|
41 |
datefilter.setStartdate(date);
|
|
|
42 |
|
|
|
43 |
query.addFilter(datefilter);
|
|
|
44 |
//query.addQuery("CustomerAccountCode", new StringEntity(search), BOOL.OR);
|
|
|
45 |
List<Customer> resultlist = (List<Customer>) con.getEntitiesByQuery(query);
|
|
|
46 |
GenericEntity generic = new GenericEntity<List<Customer>>(resultlist) {};
|
|
|
47 |
Response response = Response.ok(generic).build();
|
|
|
48 |
return response;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
private OpenAirRestConnection getOpenAirConnection() {
|
|
|
54 |
|
|
|
55 |
/*
|
|
|
56 |
* get details from configuration file
|
|
|
57 |
*/
|
191 |
brianR |
58 |
Configurations configs = new Configurations();
|
188 |
brianR |
59 |
final PropertiesConfiguration props;
|
|
|
60 |
try {
|
|
|
61 |
final String config = "../openair.properties";
|
|
|
62 |
logger.debug("Loading Open Air connection details from " + this.getClass().getClassLoader().getResource("/")
|
|
|
63 |
+ config);
|
|
|
64 |
|
|
|
65 |
final URL configloc = this.getClass().getClassLoader().getResource(config);
|
|
|
66 |
|
191 |
brianR |
67 |
props = configs.properties(configloc);
|
|
|
68 |
|
188 |
brianR |
69 |
final String user = props.getString("user");
|
|
|
70 |
final String password = props.getString("password");
|
|
|
71 |
final String company = props.getString("company");
|
191 |
brianR |
72 |
final String apikey = props.getString("apikey", "_PUT_HERE_");
|
188 |
brianR |
73 |
final String namespace = props.getString("namespace");
|
|
|
74 |
|
|
|
75 |
final OpenAirRestConnection con;
|
|
|
76 |
|
|
|
77 |
con = new OpenAirRestConnection(JCS.getInstance("FileCache"), company, user, password);
|
|
|
78 |
return con;
|
|
|
79 |
} catch (CacheException e) {
|
|
|
80 |
logger.error(e);
|
|
|
81 |
e.printStackTrace();
|
|
|
82 |
} catch (ConfigurationException e) {
|
|
|
83 |
logger.error(e);
|
|
|
84 |
e.printStackTrace();
|
|
|
85 |
} finally {
|
|
|
86 |
|
|
|
87 |
}
|
|
|
88 |
return null;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
}
|