Package utils

Source Code of utils.AddressManager

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package utils;

import dao.DAO;
import domain.District;
import domain.address.Address;
import domain.address.AddressDistrict;
import domain.address.AddressObject;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import web.generic.GenericSFController;

/**
*
* @author petr
*/
public class AddressManager {


    static public void correctAddress(DAO dao, Address address) {
        AddressObject street = getStreet(address);
        if (street != null) {
            List<AddressObject> items = dao.getChildList(street);
            for (int i = 0; i < items.size(); i++) {
                if (include(items.get(i), address.getBuilding())) {
                    address.setAddressObject(items.get(i));
                    break;
                }
            }
        }
    }

    static private AddressObject getStreet(Address address) {
        AddressObject ao = address.getAddressObject();
        while (ao == null) {
            if (ao.getType().getLevel() == 5) {
                return ao;
            }
            ao = ao.getParent();
        }
        return null;
    }

    /**
     * Can be null!
     * @param streetID
     * @param buildingNumberString
     * @return
     */
    static public District getDefaultDistrict(DAO dao, int streetID, String buildingNumberString) {
        AddressObject object = dao.getById(AddressObject.class, streetID);
        if (object == null || object.getType().getLevel() != 5) {
            return null;
        }
        List<AddressDistrict> adList = dao.AddressDistrictList(object);
        for (AddressDistrict ad : adList) {
            if (include(ad, buildingNumberString)) {
                return ad.getDistrict();
            }
        }
        return null;
    }

    static private boolean include(List<String> numbers, int buildingNumber) {
        if (numbers.size() == 1 && numbers.get(0).isEmpty()) {
            return true;
        }
        String s;
        for (int i = 0; i < numbers.size(); i++) {
            s = numbers.get(i);
            if (buildingNumber > 0) {//только в этом случае есть смысл искать по диапазонам
                if (Pattern.matches(AddressObject.pattern1, s)) {//указана сторона
                    int breakPos = s.indexOf("-");
                    int start = Integer.parseInt(s.substring(2, breakPos));
                    int end = Integer.parseInt(s.substring(breakPos + 1, s.length() - 1));
                    if (buildingNumber % 2 == start % 2 && start <= buildingNumber && buildingNumber <= end) {
                        return true;
                    }
                } else if (Pattern.matches(AddressObject.pattern3, s)) {//без указания стороны
                    int breakPos = s.indexOf("-");
                    int start = Integer.parseInt(s.substring(0, breakPos));
                    int end = Integer.parseInt(s.substring(breakPos + 1, s.length()));
                    if (start <= buildingNumber && buildingNumber <= end) {
                        return true;
                    }
                }
            } else if (!Pattern.matches(AddressObject.pattern1, s) && !Pattern.matches(AddressObject.pattern3, s)) {//s не диапазон
                if (s.equalsIgnoreCase("" + buildingNumber)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Поверяет на вхождение в квартал
     * @param block
     * @param buildingNumberString
     * @return
     */
    static private boolean include(AddressObject block, String buildingNumberString) {
        if (block.getType().getLevel() <= 5) {
            return false;
        } else {
            int buildingNumber = 0;
            if (Pattern.matches("[0-9]+", buildingNumberString)) {
                buildingNumber = Integer.parseInt(buildingNumberString);
            }
            ArrayList<String> numbers = createNumbers(block);
            return include(numbers, buildingNumber);
        }
    }

    /**
     * Проверяет входит ли дом с данным номером в квартал
     * @param block
     * @param buildingNumber
     * @return
     */
    static private boolean include(AddressDistrict block, String buildingNumberString) {
        int buildingNumber = 0;
        if (Pattern.matches("[0-9]+", buildingNumberString)) {
            buildingNumber = Integer.parseInt(buildingNumberString);
        }

        ArrayList<String> numbers = createNumbers(block);
        return include(numbers, buildingNumber);
    }

    static private ArrayList<String> createNumbers(AddressObject block) {
        return createNumbers(block.getTitle().trim());
    }

    static private ArrayList<String> createNumbers(AddressDistrict block) {
        return createNumbers(block.getTitle().trim());
    }

    static private ArrayList<String> createNumbers(String title) {
        ArrayList<String> res = new ArrayList<String>(0);
        String s = "";
        for (int i = 0; i < title.length(); i++) {
            if (title.charAt(i) == ',') {
                res.add(s.trim());
                s = "";
            } else {
                s += title.charAt(i);
            }
        }
        res.add(s.trim());
        return res;
    }
}
TOP

Related Classes of utils.AddressManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.