Subversion Repositories XServices

Rev

Rev 198 | Details | Compare with Previous | 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
 
199 brianR 11
import lombok.extern.slf4j.Slf4j;
191 brianR 12
import org.apache.commons.configuration2.PropertiesConfiguration;
13
import org.apache.commons.configuration2.builder.fluent.Configurations;
14
import org.apache.commons.configuration2.ex.ConfigurationException;
188 brianR 15
import org.apache.commons.jcs.JCS;
16
import org.apache.commons.jcs.access.exception.CacheException;
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
 
199 brianR 25
@Slf4j
188 brianR 26
public class OpenAirInfoServiceImpl implements OpenAirInfoService {
27
 
199 brianR 28
 
188 brianR 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
		 */
191 brianR 57
		Configurations configs = new Configurations();
188 brianR 58
		final PropertiesConfiguration props;
59
		try {
60
			final String config = "../openair.properties";
199 brianR 61
			log.debug("Loading Open Air connection details from '{}'.", this.getClass().getClassLoader().getResource("/")
188 brianR 62
					+ config);
63
 
64
			final URL configloc = this.getClass().getClassLoader().getResource(config);
65
 
191 brianR 66
			props = configs.properties(configloc);
67
 
188 brianR 68
			final String user = props.getString("user");
69
			final String password = props.getString("password");
70
			final String company = props.getString("company");
193 brianR 71
			final String apikey = props.getString("apikey", "9x7G49ENkLCJ81i9XZJU");
188 brianR 72
			final String namespace = props.getString("namespace");
73
 
74
			final OpenAirRestConnection con;
75
 
76
			con = new OpenAirRestConnection(JCS.getInstance("FileCache"), company, user, password);
77
			return con;
199 brianR 78
		} catch (CacheException | ConfigurationException e) {
79
			log.error(e.getMessage());
188 brianR 80
			e.printStackTrace();
81
		} finally {
82
 
83
		}
84
		return null;
85
	}
86
 
87
}