Subversion Repositories XServices

Rev

Details | Last modification | View Log | RSS feed

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