Subversion Repositories XServices

Rev

Rev 188 | Rev 193 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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