package net.ihe.xcpd.resp.model;import java.io.Serializable;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.List;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.EntityManager;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Query;import javax.persistence.SequenceGenerator;import javax.persistence.Table;import net.ihe.gazelle.simulator.common.utils.AndOrWhere;import org.hibernate.validator.NotNull;import org.jboss.seam.Component;import org.jboss.seam.annotations.Logger;import org.jboss.seam.annotations.Name;import org.jboss.seam.log.Log;/** * @author Abderrazek Boufahja > INRIA Rennes IHE development Project * */@Entity@Name("ipToCountry")@Table(name = "ip_to_country", schema = "public")@SequenceGenerator(name = "ip_to_country_sequence", sequenceName = "ip_to_country_id_seq", allocationSize = 1)public class IpToCountry  implements Serializable{        /**     *      */    private static final long serialVersionUID = 1L;         @Logger    private static Log log;    @Id    @Column(name = "id", unique = true, nullable = false)    @NotNull    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ip_to_country_sequence")    private Integer id;        @Column(name = "ip_from")    private Double ipFrom;        @Column(name = "ip_to")    private Double ipTo;        @Column(name = "country_code")    private String countryCode;        @Column(name = "country_name")    private String countryName;        public IpToCountry(){    }        public IpToCountry(Double ipFrom, Double ipTo, String countryCode,            String countryName) {        super();        this.ipFrom = ipFrom;        this.ipTo = ipTo;        this.countryCode = countryCode;        this.countryName = countryName;    }        public void setId(Integer id) {        this.id = id;    }    public Integer getId() {        return id;    }    public Double getIpFrom() {        return ipFrom;    }    public void setIpFrom(Double ipFrom) {        this.ipFrom = ipFrom;    }    public Double getIpTo() {        return ipTo;    }    public void setIpTo(Double ipTo) {        this.ipTo = ipTo;    }    public String getCountryCode() {        return countryCode;    }    public void setCountryCode(String countryCode) {        this.countryCode = countryCode;    }    public String getCountryName() {        return countryName;    }    public void setCountryName(String countryName) {        this.countryName = countryName;    }    public int hashCode() {        final int prime = 31;        int result = 1;        result = prime * result                + ((countryCode == null) ? 0 : countryCode.hashCode());        result = prime * result                + ((countryName == null) ? 0 : countryName.hashCode());        result = prime * result + ((ipFrom == null) ? 0 : ipFrom.hashCode());        result = prime * result + ((ipTo == null) ? 0 : ipTo.hashCode());        return result;    }    public boolean equals(Object obj) {        if (this == obj)            return true;        if (obj == null)            return false;        if (getClass() != obj.getClass())            return false;        IpToCountry other = (IpToCountry) obj;        if (countryCode == null) {            if (other.countryCode != null)                return false;        } else if (!countryCode.equals(other.countryCode))            return false;        if (countryName == null) {            if (other.countryName != null)                return false;        } else if (!countryName.equals(other.countryName))            return false;        if (ipFrom == null) {            if (other.ipFrom != null)                return false;        } else if (!ipFrom.equals(other.ipFrom))            return false;        if (ipTo == null) {            if (other.ipTo != null)                return false;        } else if (!ipTo.equals(other.ipTo))            return false;        return true;    }    public String toString() {        return "IpToCountry [countryCode=" + countryCode + ", countryName="                + countryName + ", ipFrom=" + ipFrom + ", ipTo=" + ipTo + "]";    }        public static double ipAddress2ipNumber(String address){        String val = address.replace('.', '/');        System.out.println("val = " + val);         String[] listint = val.split("/");        double somm = 0;        somm = Double.valueOf(listint[0]).doubleValue()*16777216 + Double.valueOf(listint[1]).doubleValue()*65536 +            Double.valueOf(listint[2]).doubleValue()*256 + Double.valueOf(listint[3]).doubleValue();         return somm;    }        @SuppressWarnings({ "unchecked" })    public static List<IpToCountry> getIpToCountryFiltred(String ip,String countryName, String countryCode){        EntityManager em = (EntityManager) Component.getInstance("entityManager");        Query query ;        StringBuffer queryString = new StringBuffer() ;        HashSet < String> prefixes = new HashSet<String>();        HashSet < String> prefixesUsed = new HashSet<String>();        HashMap<String ,String > mapOfJoin = new HashMap<String, String>() ;        HashMap<String , Object > mapOfParameters = new HashMap<String, Object>() ;        if (countryName != null){            AndOrWhere.addANDorWHERE(queryString) ;            prefixes.add("IpToCountry itc");            queryString.append( "itc.countryName = :countryName" );            mapOfParameters.put("countryName", countryName);        }        if (countryCode != null){            AndOrWhere.addANDorWHERE(queryString) ;            prefixes.add("IpToCountry itc");            queryString.append( "itc.countryCode = :countryCode" );            mapOfParameters.put("countryCode", countryCode);        }                if (ip != null){            double somm = IpToCountry.ipAddress2ipNumber(ip);            AndOrWhere.addANDorWHERE(queryString);            prefixes.add("IpToCountry itc");            queryString.append( "itc.ipFrom<=:somm AND itc.ipTo>=:somm" );            mapOfParameters.put("somm", somm);        }                prefixes.add("IpToCountry itc");        List<String> listOfPrefixes = new ArrayList<String>( prefixes ) ;        StringBuffer firstPartOfQuery = new StringBuffer() ;        for ( int i= 0 ; i< listOfPrefixes.size() ; i++  )        {            if ( i == 0 )             {                firstPartOfQuery.append("SELECT distinct itc FROM "    ) ;            }            if ( !prefixesUsed.contains(listOfPrefixes.get(i)) )            {                if ( prefixesUsed.size() > 0 ) firstPartOfQuery.append(" , "  ) ;                firstPartOfQuery.append( listOfPrefixes.get(i) ) ;                if (mapOfJoin.containsKey(listOfPrefixes.get(i)))                    firstPartOfQuery.append(" " + mapOfJoin.get(listOfPrefixes.get(i)) +" " ) ;                prefixesUsed.add( listOfPrefixes.get(i) ) ;            }        }        queryString.insert(0, firstPartOfQuery ) ;        if ( queryString.toString().trim().length() == 0 ) return null ;        query = em.createQuery(queryString.toString()) ;        List<String> listOfParameters =  new ArrayList<String>( mapOfParameters.keySet() ) ;        for ( String param : listOfParameters ){            query.setParameter( param , mapOfParameters.get(param) ) ;        }        log.info("quesryString = " + queryString);        List<IpToCountry> listOfIpToCountry = ( List<IpToCountry> )query.getResultList();         return listOfIpToCountry ;    }    }