Subversion Repositories XServices

Rev

Rev 188 | Rev 193 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

package net.brutex.xservices.ws.rs;

import java.net.URL;
import java.util.GregorianCalendar;
import java.util.List;

import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;

import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.jcs.JCS;
import org.apache.commons.jcs.access.exception.CacheException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.brutex.mgmt.api.xml.Customer;
import net.brutex.mgmt.api.xml.DateFilter;
import net.brutex.mgmt.api.xml.Query;
import net.brutex.mgmt.api.xml.Query.BOOL;
import net.brutex.mgmt.api.xml.StringEntity;
import net.brutex.mgmt.openair.OpenAirRestConnection;

public class OpenAirInfoServiceImpl implements OpenAirInfoService {
        
        static final Logger logger = LogManager.getLogger();

        @Override
        public Response getCompanies(HttpHeaders paramHttpHeaders, String search) {
                OpenAirRestConnection con = getOpenAirConnection();
                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");
                GregorianCalendar date = new GregorianCalendar();

                date.add(GregorianCalendar.MINUTE, -5);
                datefilter.setStartdate(date);
                
                query.addFilter(datefilter);
                //query.addQuery("CustomerAccountCode", new StringEntity(search), BOOL.OR);
                List<Customer> resultlist = (List<Customer>) con.getEntitiesByQuery(query);
                GenericEntity generic = new GenericEntity<List<Customer>>(resultlist) {};
                Response response = Response.ok(generic).build();
                return response;
        }
        
        
        
        private OpenAirRestConnection getOpenAirConnection() {

                /*
                 * get details from configuration file
                 */
                Configurations configs = new Configurations();
                final PropertiesConfiguration props;
                try {
                        final String config = "../openair.properties";
                        logger.debug("Loading Open Air connection details from " + this.getClass().getClassLoader().getResource("/")
                                        + config);

                        final URL configloc = this.getClass().getClassLoader().getResource(config);

                        props = configs.properties(configloc);
                        
                        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("FileCache"), company, user, password);
                        return con;
                } catch (CacheException e) {
                        logger.error(e);
                        e.printStackTrace();
                } catch (ConfigurationException e) {
                        logger.error(e);
                        e.printStackTrace();
                } finally {

                }
                return null;
        }

}