com.bemis.portal.customer.service.persistence.impl.CustomerLocationPersistenceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.bemis.portal.customer.service.persistence.impl.CustomerLocationPersistenceImpl.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.bemis.portal.customer.service.persistence.impl;

import aQute.bnd.annotation.ProviderType;

import com.bemis.portal.customer.exception.NoSuchCustomerLocationException;
import com.bemis.portal.customer.model.CustomerLocation;
import com.bemis.portal.customer.model.impl.CustomerLocationImpl;
import com.bemis.portal.customer.model.impl.CustomerLocationModelImpl;
import com.bemis.portal.customer.service.persistence.CustomerLocationPersistence;

import com.liferay.portal.kernel.dao.orm.EntityCache;
import com.liferay.portal.kernel.dao.orm.FinderCache;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.service.ServiceContextThreadLocal;
import com.liferay.portal.kernel.service.persistence.CompanyProvider;
import com.liferay.portal.kernel.service.persistence.CompanyProviderWrapper;
import com.liferay.portal.kernel.service.persistence.impl.BasePersistenceImpl;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.spring.extender.service.ServiceReference;

import java.io.Serializable;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * The persistence implementation for the customer location service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Brian Greenwald
 * @see CustomerLocationPersistence
 * @see com.bemis.portal.customer.service.persistence.CustomerLocationUtil
 * @generated
 */
@ProviderType
public class CustomerLocationPersistenceImpl extends BasePersistenceImpl<CustomerLocation>
        implements CustomerLocationPersistence {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never modify or reference this class directly. Always use {@link CustomerLocationUtil} to access the customer location persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
     */
    public static final String FINDER_CLASS_NAME_ENTITY = CustomerLocationImpl.class.getName();
    public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY + ".List1";
    public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY + ".List2";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(
            CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationModelImpl.FINDER_CACHE_ENABLED,
            CustomerLocationImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(
            CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationModelImpl.FINDER_CACHE_ENABLED,
            CustomerLocationImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(
            CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationModelImpl.FINDER_CACHE_ENABLED,
            Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
    public static final FinderPath FINDER_PATH_FETCH_BY_BEMISCUSTOMERID = new FinderPath(
            CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationModelImpl.FINDER_CACHE_ENABLED,
            CustomerLocationImpl.class, FINDER_CLASS_NAME_ENTITY, "fetchByBemisCustomerId",
            new String[] { String.class.getName() }, CustomerLocationModelImpl.BEMISCUSTOMERID_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_BEMISCUSTOMERID = new FinderPath(
            CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationModelImpl.FINDER_CACHE_ENABLED,
            Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByBemisCustomerId",
            new String[] { String.class.getName() });

    /**
     * Returns the customer location where bemisCustomerId = &#63; or throws a {@link NoSuchCustomerLocationException} if it could not be found.
     *
     * @param bemisCustomerId the bemis customer ID
     * @return the matching customer location
     * @throws NoSuchCustomerLocationException if a matching customer location could not be found
     */
    @Override
    public CustomerLocation findByBemisCustomerId(String bemisCustomerId) throws NoSuchCustomerLocationException {
        CustomerLocation customerLocation = fetchByBemisCustomerId(bemisCustomerId);

        if (customerLocation == null) {
            StringBundler msg = new StringBundler(4);

            msg.append(_NO_SUCH_ENTITY_WITH_KEY);

            msg.append("bemisCustomerId=");
            msg.append(bemisCustomerId);

            msg.append(StringPool.CLOSE_CURLY_BRACE);

            if (_log.isDebugEnabled()) {
                _log.debug(msg.toString());
            }

            throw new NoSuchCustomerLocationException(msg.toString());
        }

        return customerLocation;
    }

    /**
     * Returns the customer location where bemisCustomerId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
     *
     * @param bemisCustomerId the bemis customer ID
     * @return the matching customer location, or <code>null</code> if a matching customer location could not be found
     */
    @Override
    public CustomerLocation fetchByBemisCustomerId(String bemisCustomerId) {
        return fetchByBemisCustomerId(bemisCustomerId, true);
    }

    /**
     * Returns the customer location where bemisCustomerId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
     *
     * @param bemisCustomerId the bemis customer ID
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the matching customer location, or <code>null</code> if a matching customer location could not be found
     */
    @Override
    public CustomerLocation fetchByBemisCustomerId(String bemisCustomerId, boolean retrieveFromCache) {
        Object[] finderArgs = new Object[] { bemisCustomerId };

        Object result = null;

        if (retrieveFromCache) {
            result = finderCache.getResult(FINDER_PATH_FETCH_BY_BEMISCUSTOMERID, finderArgs, this);
        }

        if (result instanceof CustomerLocation) {
            CustomerLocation customerLocation = (CustomerLocation) result;

            if (!Objects.equals(bemisCustomerId, customerLocation.getBemisCustomerId())) {
                result = null;
            }
        }

        if (result == null) {
            StringBundler query = new StringBundler(3);

            query.append(_SQL_SELECT_CUSTOMERLOCATION_WHERE);

            boolean bindBemisCustomerId = false;

            if (bemisCustomerId == null) {
                query.append(_FINDER_COLUMN_BEMISCUSTOMERID_BEMISCUSTOMERID_1);
            } else if (bemisCustomerId.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_BEMISCUSTOMERID_BEMISCUSTOMERID_3);
            } else {
                bindBemisCustomerId = true;

                query.append(_FINDER_COLUMN_BEMISCUSTOMERID_BEMISCUSTOMERID_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindBemisCustomerId) {
                    qPos.add(bemisCustomerId);
                }

                List<CustomerLocation> list = q.list();

                if (list.isEmpty()) {
                    finderCache.putResult(FINDER_PATH_FETCH_BY_BEMISCUSTOMERID, finderArgs, list);
                } else {
                    CustomerLocation customerLocation = list.get(0);

                    result = customerLocation;

                    cacheResult(customerLocation);

                    if ((customerLocation.getBemisCustomerId() == null)
                            || !customerLocation.getBemisCustomerId().equals(bemisCustomerId)) {
                        finderCache.putResult(FINDER_PATH_FETCH_BY_BEMISCUSTOMERID, finderArgs, customerLocation);
                    }
                }
            } catch (Exception e) {
                finderCache.removeResult(FINDER_PATH_FETCH_BY_BEMISCUSTOMERID, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        if (result instanceof List<?>) {
            return null;
        } else {
            return (CustomerLocation) result;
        }
    }

    /**
     * Removes the customer location where bemisCustomerId = &#63; from the database.
     *
     * @param bemisCustomerId the bemis customer ID
     * @return the customer location that was removed
     */
    @Override
    public CustomerLocation removeByBemisCustomerId(String bemisCustomerId) throws NoSuchCustomerLocationException {
        CustomerLocation customerLocation = findByBemisCustomerId(bemisCustomerId);

        return remove(customerLocation);
    }

    /**
     * Returns the number of customer locations where bemisCustomerId = &#63;.
     *
     * @param bemisCustomerId the bemis customer ID
     * @return the number of matching customer locations
     */
    @Override
    public int countByBemisCustomerId(String bemisCustomerId) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_BEMISCUSTOMERID;

        Object[] finderArgs = new Object[] { bemisCustomerId };

        Long count = (Long) finderCache.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_CUSTOMERLOCATION_WHERE);

            boolean bindBemisCustomerId = false;

            if (bemisCustomerId == null) {
                query.append(_FINDER_COLUMN_BEMISCUSTOMERID_BEMISCUSTOMERID_1);
            } else if (bemisCustomerId.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_BEMISCUSTOMERID_BEMISCUSTOMERID_3);
            } else {
                bindBemisCustomerId = true;

                query.append(_FINDER_COLUMN_BEMISCUSTOMERID_BEMISCUSTOMERID_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindBemisCustomerId) {
                    qPos.add(bemisCustomerId);
                }

                count = (Long) q.uniqueResult();

                finderCache.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    private static final String _FINDER_COLUMN_BEMISCUSTOMERID_BEMISCUSTOMERID_1 = "customerLocation.bemisCustomerId IS NULL";
    private static final String _FINDER_COLUMN_BEMISCUSTOMERID_BEMISCUSTOMERID_2 = "customerLocation.bemisCustomerId = ?";
    private static final String _FINDER_COLUMN_BEMISCUSTOMERID_BEMISCUSTOMERID_3 = "(customerLocation.bemisCustomerId IS NULL OR customerLocation.bemisCustomerId = '')";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_BEMISPARENTID = new FinderPath(
            CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationModelImpl.FINDER_CACHE_ENABLED,
            CustomerLocationImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByBemisParentId",
            new String[] { String.class.getName(),

                    Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BEMISPARENTID = new FinderPath(
            CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationModelImpl.FINDER_CACHE_ENABLED,
            CustomerLocationImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByBemisParentId",
            new String[] { String.class.getName() }, CustomerLocationModelImpl.BEMISPARENTID_COLUMN_BITMASK
                    | CustomerLocationModelImpl.BEMISCUSTOMERID_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_BEMISPARENTID = new FinderPath(
            CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationModelImpl.FINDER_CACHE_ENABLED,
            Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByBemisParentId",
            new String[] { String.class.getName() });

    /**
     * Returns all the customer locations where bemisParentId = &#63;.
     *
     * @param bemisParentId the bemis parent ID
     * @return the matching customer locations
     */
    @Override
    public List<CustomerLocation> findByBemisParentId(String bemisParentId) {
        return findByBemisParentId(bemisParentId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the customer locations where bemisParentId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link CustomerLocationModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param bemisParentId the bemis parent ID
     * @param start the lower bound of the range of customer locations
     * @param end the upper bound of the range of customer locations (not inclusive)
     * @return the range of matching customer locations
     */
    @Override
    public List<CustomerLocation> findByBemisParentId(String bemisParentId, int start, int end) {
        return findByBemisParentId(bemisParentId, start, end, null);
    }

    /**
     * Returns an ordered range of all the customer locations where bemisParentId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link CustomerLocationModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param bemisParentId the bemis parent ID
     * @param start the lower bound of the range of customer locations
     * @param end the upper bound of the range of customer locations (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching customer locations
     */
    @Override
    public List<CustomerLocation> findByBemisParentId(String bemisParentId, int start, int end,
            OrderByComparator<CustomerLocation> orderByComparator) {
        return findByBemisParentId(bemisParentId, start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the customer locations where bemisParentId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link CustomerLocationModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param bemisParentId the bemis parent ID
     * @param start the lower bound of the range of customer locations
     * @param end the upper bound of the range of customer locations (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the ordered range of matching customer locations
     */
    @Override
    public List<CustomerLocation> findByBemisParentId(String bemisParentId, int start, int end,
            OrderByComparator<CustomerLocation> orderByComparator, boolean retrieveFromCache) {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BEMISPARENTID;
            finderArgs = new Object[] { bemisParentId };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_BEMISPARENTID;
            finderArgs = new Object[] { bemisParentId,

                    start, end, orderByComparator };
        }

        List<CustomerLocation> list = null;

        if (retrieveFromCache) {
            list = (List<CustomerLocation>) finderCache.getResult(finderPath, finderArgs, this);

            if ((list != null) && !list.isEmpty()) {
                for (CustomerLocation customerLocation : list) {
                    if (!Objects.equals(bemisParentId, customerLocation.getBemisParentId())) {
                        list = null;

                        break;
                    }
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(3 + (orderByComparator.getOrderByFields().length * 2));
            } else {
                query = new StringBundler(3);
            }

            query.append(_SQL_SELECT_CUSTOMERLOCATION_WHERE);

            boolean bindBemisParentId = false;

            if (bemisParentId == null) {
                query.append(_FINDER_COLUMN_BEMISPARENTID_BEMISPARENTID_1);
            } else if (bemisParentId.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_BEMISPARENTID_BEMISPARENTID_3);
            } else {
                bindBemisParentId = true;

                query.append(_FINDER_COLUMN_BEMISPARENTID_BEMISPARENTID_2);
            }

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            } else if (pagination) {
                query.append(CustomerLocationModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindBemisParentId) {
                    qPos.add(bemisParentId);
                }

                if (!pagination) {
                    list = (List<CustomerLocation>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<CustomerLocation>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                finderCache.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first customer location in the ordered set where bemisParentId = &#63;.
     *
     * @param bemisParentId the bemis parent ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching customer location
     * @throws NoSuchCustomerLocationException if a matching customer location could not be found
     */
    @Override
    public CustomerLocation findByBemisParentId_First(String bemisParentId,
            OrderByComparator<CustomerLocation> orderByComparator) throws NoSuchCustomerLocationException {
        CustomerLocation customerLocation = fetchByBemisParentId_First(bemisParentId, orderByComparator);

        if (customerLocation != null) {
            return customerLocation;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("bemisParentId=");
        msg.append(bemisParentId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchCustomerLocationException(msg.toString());
    }

    /**
     * Returns the first customer location in the ordered set where bemisParentId = &#63;.
     *
     * @param bemisParentId the bemis parent ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching customer location, or <code>null</code> if a matching customer location could not be found
     */
    @Override
    public CustomerLocation fetchByBemisParentId_First(String bemisParentId,
            OrderByComparator<CustomerLocation> orderByComparator) {
        List<CustomerLocation> list = findByBemisParentId(bemisParentId, 0, 1, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last customer location in the ordered set where bemisParentId = &#63;.
     *
     * @param bemisParentId the bemis parent ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching customer location
     * @throws NoSuchCustomerLocationException if a matching customer location could not be found
     */
    @Override
    public CustomerLocation findByBemisParentId_Last(String bemisParentId,
            OrderByComparator<CustomerLocation> orderByComparator) throws NoSuchCustomerLocationException {
        CustomerLocation customerLocation = fetchByBemisParentId_Last(bemisParentId, orderByComparator);

        if (customerLocation != null) {
            return customerLocation;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("bemisParentId=");
        msg.append(bemisParentId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchCustomerLocationException(msg.toString());
    }

    /**
     * Returns the last customer location in the ordered set where bemisParentId = &#63;.
     *
     * @param bemisParentId the bemis parent ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching customer location, or <code>null</code> if a matching customer location could not be found
     */
    @Override
    public CustomerLocation fetchByBemisParentId_Last(String bemisParentId,
            OrderByComparator<CustomerLocation> orderByComparator) {
        int count = countByBemisParentId(bemisParentId);

        if (count == 0) {
            return null;
        }

        List<CustomerLocation> list = findByBemisParentId(bemisParentId, count - 1, count, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the customer locations before and after the current customer location in the ordered set where bemisParentId = &#63;.
     *
     * @param customerLocationId the primary key of the current customer location
     * @param bemisParentId the bemis parent ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next customer location
     * @throws NoSuchCustomerLocationException if a customer location with the primary key could not be found
     */
    @Override
    public CustomerLocation[] findByBemisParentId_PrevAndNext(long customerLocationId, String bemisParentId,
            OrderByComparator<CustomerLocation> orderByComparator) throws NoSuchCustomerLocationException {
        CustomerLocation customerLocation = findByPrimaryKey(customerLocationId);

        Session session = null;

        try {
            session = openSession();

            CustomerLocation[] array = new CustomerLocationImpl[3];

            array[0] = getByBemisParentId_PrevAndNext(session, customerLocation, bemisParentId, orderByComparator,
                    true);

            array[1] = customerLocation;

            array[2] = getByBemisParentId_PrevAndNext(session, customerLocation, bemisParentId, orderByComparator,
                    false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected CustomerLocation getByBemisParentId_PrevAndNext(Session session, CustomerLocation customerLocation,
            String bemisParentId, OrderByComparator<CustomerLocation> orderByComparator, boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(4 + (orderByComparator.getOrderByConditionFields().length * 3)
                    + (orderByComparator.getOrderByFields().length * 3));
        } else {
            query = new StringBundler(3);
        }

        query.append(_SQL_SELECT_CUSTOMERLOCATION_WHERE);

        boolean bindBemisParentId = false;

        if (bemisParentId == null) {
            query.append(_FINDER_COLUMN_BEMISPARENTID_BEMISPARENTID_1);
        } else if (bemisParentId.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_BEMISPARENTID_BEMISPARENTID_3);
        } else {
            bindBemisParentId = true;

            query.append(_FINDER_COLUMN_BEMISPARENTID_BEMISPARENTID_2);
        }

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            query.append(CustomerLocationModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindBemisParentId) {
            qPos.add(bemisParentId);
        }

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(customerLocation);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<CustomerLocation> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Removes all the customer locations where bemisParentId = &#63; from the database.
     *
     * @param bemisParentId the bemis parent ID
     */
    @Override
    public void removeByBemisParentId(String bemisParentId) {
        for (CustomerLocation customerLocation : findByBemisParentId(bemisParentId, QueryUtil.ALL_POS,
                QueryUtil.ALL_POS, null)) {
            remove(customerLocation);
        }
    }

    /**
     * Returns the number of customer locations where bemisParentId = &#63;.
     *
     * @param bemisParentId the bemis parent ID
     * @return the number of matching customer locations
     */
    @Override
    public int countByBemisParentId(String bemisParentId) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_BEMISPARENTID;

        Object[] finderArgs = new Object[] { bemisParentId };

        Long count = (Long) finderCache.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_CUSTOMERLOCATION_WHERE);

            boolean bindBemisParentId = false;

            if (bemisParentId == null) {
                query.append(_FINDER_COLUMN_BEMISPARENTID_BEMISPARENTID_1);
            } else if (bemisParentId.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_BEMISPARENTID_BEMISPARENTID_3);
            } else {
                bindBemisParentId = true;

                query.append(_FINDER_COLUMN_BEMISPARENTID_BEMISPARENTID_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindBemisParentId) {
                    qPos.add(bemisParentId);
                }

                count = (Long) q.uniqueResult();

                finderCache.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    private static final String _FINDER_COLUMN_BEMISPARENTID_BEMISPARENTID_1 = "customerLocation.bemisParentId IS NULL";
    private static final String _FINDER_COLUMN_BEMISPARENTID_BEMISPARENTID_2 = "customerLocation.bemisParentId = ?";
    private static final String _FINDER_COLUMN_BEMISPARENTID_BEMISPARENTID_3 = "(customerLocation.bemisParentId IS NULL OR customerLocation.bemisParentId = '')";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_CUSTOMERACTIVESTATUSCDE = new FinderPath(
            CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationModelImpl.FINDER_CACHE_ENABLED,
            CustomerLocationImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByCustomerActiveStatusCde",
            new String[] { String.class.getName(),

                    Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CUSTOMERACTIVESTATUSCDE = new FinderPath(
            CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationModelImpl.FINDER_CACHE_ENABLED,
            CustomerLocationImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByCustomerActiveStatusCde",
            new String[] { String.class.getName() },
            CustomerLocationModelImpl.CUSTOMERACTIVESTATUSCDE_COLUMN_BITMASK
                    | CustomerLocationModelImpl.BEMISCUSTOMERID_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_CUSTOMERACTIVESTATUSCDE = new FinderPath(
            CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationModelImpl.FINDER_CACHE_ENABLED,
            Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByCustomerActiveStatusCde",
            new String[] { String.class.getName() });

    /**
     * Returns all the customer locations where customerActiveStatusCde = &#63;.
     *
     * @param customerActiveStatusCde the customer active status cde
     * @return the matching customer locations
     */
    @Override
    public List<CustomerLocation> findByCustomerActiveStatusCde(String customerActiveStatusCde) {
        return findByCustomerActiveStatusCde(customerActiveStatusCde, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the customer locations where customerActiveStatusCde = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link CustomerLocationModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param customerActiveStatusCde the customer active status cde
     * @param start the lower bound of the range of customer locations
     * @param end the upper bound of the range of customer locations (not inclusive)
     * @return the range of matching customer locations
     */
    @Override
    public List<CustomerLocation> findByCustomerActiveStatusCde(String customerActiveStatusCde, int start,
            int end) {
        return findByCustomerActiveStatusCde(customerActiveStatusCde, start, end, null);
    }

    /**
     * Returns an ordered range of all the customer locations where customerActiveStatusCde = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link CustomerLocationModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param customerActiveStatusCde the customer active status cde
     * @param start the lower bound of the range of customer locations
     * @param end the upper bound of the range of customer locations (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching customer locations
     */
    @Override
    public List<CustomerLocation> findByCustomerActiveStatusCde(String customerActiveStatusCde, int start, int end,
            OrderByComparator<CustomerLocation> orderByComparator) {
        return findByCustomerActiveStatusCde(customerActiveStatusCde, start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the customer locations where customerActiveStatusCde = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link CustomerLocationModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param customerActiveStatusCde the customer active status cde
     * @param start the lower bound of the range of customer locations
     * @param end the upper bound of the range of customer locations (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the ordered range of matching customer locations
     */
    @Override
    public List<CustomerLocation> findByCustomerActiveStatusCde(String customerActiveStatusCde, int start, int end,
            OrderByComparator<CustomerLocation> orderByComparator, boolean retrieveFromCache) {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CUSTOMERACTIVESTATUSCDE;
            finderArgs = new Object[] { customerActiveStatusCde };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_CUSTOMERACTIVESTATUSCDE;
            finderArgs = new Object[] { customerActiveStatusCde,

                    start, end, orderByComparator };
        }

        List<CustomerLocation> list = null;

        if (retrieveFromCache) {
            list = (List<CustomerLocation>) finderCache.getResult(finderPath, finderArgs, this);

            if ((list != null) && !list.isEmpty()) {
                for (CustomerLocation customerLocation : list) {
                    if (!Objects.equals(customerActiveStatusCde, customerLocation.getCustomerActiveStatusCde())) {
                        list = null;

                        break;
                    }
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(3 + (orderByComparator.getOrderByFields().length * 2));
            } else {
                query = new StringBundler(3);
            }

            query.append(_SQL_SELECT_CUSTOMERLOCATION_WHERE);

            boolean bindCustomerActiveStatusCde = false;

            if (customerActiveStatusCde == null) {
                query.append(_FINDER_COLUMN_CUSTOMERACTIVESTATUSCDE_CUSTOMERACTIVESTATUSCDE_1);
            } else if (customerActiveStatusCde.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_CUSTOMERACTIVESTATUSCDE_CUSTOMERACTIVESTATUSCDE_3);
            } else {
                bindCustomerActiveStatusCde = true;

                query.append(_FINDER_COLUMN_CUSTOMERACTIVESTATUSCDE_CUSTOMERACTIVESTATUSCDE_2);
            }

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            } else if (pagination) {
                query.append(CustomerLocationModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindCustomerActiveStatusCde) {
                    qPos.add(customerActiveStatusCde);
                }

                if (!pagination) {
                    list = (List<CustomerLocation>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<CustomerLocation>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                finderCache.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first customer location in the ordered set where customerActiveStatusCde = &#63;.
     *
     * @param customerActiveStatusCde the customer active status cde
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching customer location
     * @throws NoSuchCustomerLocationException if a matching customer location could not be found
     */
    @Override
    public CustomerLocation findByCustomerActiveStatusCde_First(String customerActiveStatusCde,
            OrderByComparator<CustomerLocation> orderByComparator) throws NoSuchCustomerLocationException {
        CustomerLocation customerLocation = fetchByCustomerActiveStatusCde_First(customerActiveStatusCde,
                orderByComparator);

        if (customerLocation != null) {
            return customerLocation;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("customerActiveStatusCde=");
        msg.append(customerActiveStatusCde);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchCustomerLocationException(msg.toString());
    }

    /**
     * Returns the first customer location in the ordered set where customerActiveStatusCde = &#63;.
     *
     * @param customerActiveStatusCde the customer active status cde
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching customer location, or <code>null</code> if a matching customer location could not be found
     */
    @Override
    public CustomerLocation fetchByCustomerActiveStatusCde_First(String customerActiveStatusCde,
            OrderByComparator<CustomerLocation> orderByComparator) {
        List<CustomerLocation> list = findByCustomerActiveStatusCde(customerActiveStatusCde, 0, 1,
                orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last customer location in the ordered set where customerActiveStatusCde = &#63;.
     *
     * @param customerActiveStatusCde the customer active status cde
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching customer location
     * @throws NoSuchCustomerLocationException if a matching customer location could not be found
     */
    @Override
    public CustomerLocation findByCustomerActiveStatusCde_Last(String customerActiveStatusCde,
            OrderByComparator<CustomerLocation> orderByComparator) throws NoSuchCustomerLocationException {
        CustomerLocation customerLocation = fetchByCustomerActiveStatusCde_Last(customerActiveStatusCde,
                orderByComparator);

        if (customerLocation != null) {
            return customerLocation;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("customerActiveStatusCde=");
        msg.append(customerActiveStatusCde);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchCustomerLocationException(msg.toString());
    }

    /**
     * Returns the last customer location in the ordered set where customerActiveStatusCde = &#63;.
     *
     * @param customerActiveStatusCde the customer active status cde
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching customer location, or <code>null</code> if a matching customer location could not be found
     */
    @Override
    public CustomerLocation fetchByCustomerActiveStatusCde_Last(String customerActiveStatusCde,
            OrderByComparator<CustomerLocation> orderByComparator) {
        int count = countByCustomerActiveStatusCde(customerActiveStatusCde);

        if (count == 0) {
            return null;
        }

        List<CustomerLocation> list = findByCustomerActiveStatusCde(customerActiveStatusCde, count - 1, count,
                orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the customer locations before and after the current customer location in the ordered set where customerActiveStatusCde = &#63;.
     *
     * @param customerLocationId the primary key of the current customer location
     * @param customerActiveStatusCde the customer active status cde
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next customer location
     * @throws NoSuchCustomerLocationException if a customer location with the primary key could not be found
     */
    @Override
    public CustomerLocation[] findByCustomerActiveStatusCde_PrevAndNext(long customerLocationId,
            String customerActiveStatusCde, OrderByComparator<CustomerLocation> orderByComparator)
            throws NoSuchCustomerLocationException {
        CustomerLocation customerLocation = findByPrimaryKey(customerLocationId);

        Session session = null;

        try {
            session = openSession();

            CustomerLocation[] array = new CustomerLocationImpl[3];

            array[0] = getByCustomerActiveStatusCde_PrevAndNext(session, customerLocation, customerActiveStatusCde,
                    orderByComparator, true);

            array[1] = customerLocation;

            array[2] = getByCustomerActiveStatusCde_PrevAndNext(session, customerLocation, customerActiveStatusCde,
                    orderByComparator, false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected CustomerLocation getByCustomerActiveStatusCde_PrevAndNext(Session session,
            CustomerLocation customerLocation, String customerActiveStatusCde,
            OrderByComparator<CustomerLocation> orderByComparator, boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(4 + (orderByComparator.getOrderByConditionFields().length * 3)
                    + (orderByComparator.getOrderByFields().length * 3));
        } else {
            query = new StringBundler(3);
        }

        query.append(_SQL_SELECT_CUSTOMERLOCATION_WHERE);

        boolean bindCustomerActiveStatusCde = false;

        if (customerActiveStatusCde == null) {
            query.append(_FINDER_COLUMN_CUSTOMERACTIVESTATUSCDE_CUSTOMERACTIVESTATUSCDE_1);
        } else if (customerActiveStatusCde.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_CUSTOMERACTIVESTATUSCDE_CUSTOMERACTIVESTATUSCDE_3);
        } else {
            bindCustomerActiveStatusCde = true;

            query.append(_FINDER_COLUMN_CUSTOMERACTIVESTATUSCDE_CUSTOMERACTIVESTATUSCDE_2);
        }

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            query.append(CustomerLocationModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindCustomerActiveStatusCde) {
            qPos.add(customerActiveStatusCde);
        }

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(customerLocation);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<CustomerLocation> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Removes all the customer locations where customerActiveStatusCde = &#63; from the database.
     *
     * @param customerActiveStatusCde the customer active status cde
     */
    @Override
    public void removeByCustomerActiveStatusCde(String customerActiveStatusCde) {
        for (CustomerLocation customerLocation : findByCustomerActiveStatusCde(customerActiveStatusCde,
                QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(customerLocation);
        }
    }

    /**
     * Returns the number of customer locations where customerActiveStatusCde = &#63;.
     *
     * @param customerActiveStatusCde the customer active status cde
     * @return the number of matching customer locations
     */
    @Override
    public int countByCustomerActiveStatusCde(String customerActiveStatusCde) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_CUSTOMERACTIVESTATUSCDE;

        Object[] finderArgs = new Object[] { customerActiveStatusCde };

        Long count = (Long) finderCache.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_CUSTOMERLOCATION_WHERE);

            boolean bindCustomerActiveStatusCde = false;

            if (customerActiveStatusCde == null) {
                query.append(_FINDER_COLUMN_CUSTOMERACTIVESTATUSCDE_CUSTOMERACTIVESTATUSCDE_1);
            } else if (customerActiveStatusCde.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_CUSTOMERACTIVESTATUSCDE_CUSTOMERACTIVESTATUSCDE_3);
            } else {
                bindCustomerActiveStatusCde = true;

                query.append(_FINDER_COLUMN_CUSTOMERACTIVESTATUSCDE_CUSTOMERACTIVESTATUSCDE_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindCustomerActiveStatusCde) {
                    qPos.add(customerActiveStatusCde);
                }

                count = (Long) q.uniqueResult();

                finderCache.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    private static final String _FINDER_COLUMN_CUSTOMERACTIVESTATUSCDE_CUSTOMERACTIVESTATUSCDE_1 = "customerLocation.customerActiveStatusCde IS NULL";
    private static final String _FINDER_COLUMN_CUSTOMERACTIVESTATUSCDE_CUSTOMERACTIVESTATUSCDE_2 = "customerLocation.customerActiveStatusCde = ?";
    private static final String _FINDER_COLUMN_CUSTOMERACTIVESTATUSCDE_CUSTOMERACTIVESTATUSCDE_3 = "(customerLocation.customerActiveStatusCde IS NULL OR customerLocation.customerActiveStatusCde = '')";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_CUSTOMERNAME_CUSTOMERCITY = new FinderPath(
            CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationModelImpl.FINDER_CACHE_ENABLED,
            CustomerLocationImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByCustomerName_CustomerCity",
            new String[] { String.class.getName(), String.class.getName(),

                    Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CUSTOMERNAME_CUSTOMERCITY = new FinderPath(
            CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationModelImpl.FINDER_CACHE_ENABLED,
            CustomerLocationImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "findByCustomerName_CustomerCity", new String[] { String.class.getName(), String.class.getName() },
            CustomerLocationModelImpl.CUSTOMERNAME_COLUMN_BITMASK
                    | CustomerLocationModelImpl.CUSTOMERCITY_COLUMN_BITMASK
                    | CustomerLocationModelImpl.BEMISCUSTOMERID_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_CUSTOMERNAME_CUSTOMERCITY = new FinderPath(
            CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationModelImpl.FINDER_CACHE_ENABLED,
            Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByCustomerName_CustomerCity",
            new String[] { String.class.getName(), String.class.getName() });

    /**
     * Returns all the customer locations where customerName = &#63; and customerCity = &#63;.
     *
     * @param customerName the customer name
     * @param customerCity the customer city
     * @return the matching customer locations
     */
    @Override
    public List<CustomerLocation> findByCustomerName_CustomerCity(String customerName, String customerCity) {
        return findByCustomerName_CustomerCity(customerName, customerCity, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
                null);
    }

    /**
     * Returns a range of all the customer locations where customerName = &#63; and customerCity = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link CustomerLocationModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param customerName the customer name
     * @param customerCity the customer city
     * @param start the lower bound of the range of customer locations
     * @param end the upper bound of the range of customer locations (not inclusive)
     * @return the range of matching customer locations
     */
    @Override
    public List<CustomerLocation> findByCustomerName_CustomerCity(String customerName, String customerCity,
            int start, int end) {
        return findByCustomerName_CustomerCity(customerName, customerCity, start, end, null);
    }

    /**
     * Returns an ordered range of all the customer locations where customerName = &#63; and customerCity = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link CustomerLocationModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param customerName the customer name
     * @param customerCity the customer city
     * @param start the lower bound of the range of customer locations
     * @param end the upper bound of the range of customer locations (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching customer locations
     */
    @Override
    public List<CustomerLocation> findByCustomerName_CustomerCity(String customerName, String customerCity,
            int start, int end, OrderByComparator<CustomerLocation> orderByComparator) {
        return findByCustomerName_CustomerCity(customerName, customerCity, start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the customer locations where customerName = &#63; and customerCity = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link CustomerLocationModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param customerName the customer name
     * @param customerCity the customer city
     * @param start the lower bound of the range of customer locations
     * @param end the upper bound of the range of customer locations (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the ordered range of matching customer locations
     */
    @Override
    public List<CustomerLocation> findByCustomerName_CustomerCity(String customerName, String customerCity,
            int start, int end, OrderByComparator<CustomerLocation> orderByComparator, boolean retrieveFromCache) {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CUSTOMERNAME_CUSTOMERCITY;
            finderArgs = new Object[] { customerName, customerCity };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_CUSTOMERNAME_CUSTOMERCITY;
            finderArgs = new Object[] { customerName, customerCity,

                    start, end, orderByComparator };
        }

        List<CustomerLocation> list = null;

        if (retrieveFromCache) {
            list = (List<CustomerLocation>) finderCache.getResult(finderPath, finderArgs, this);

            if ((list != null) && !list.isEmpty()) {
                for (CustomerLocation customerLocation : list) {
                    if (!Objects.equals(customerName, customerLocation.getCustomerName())
                            || !Objects.equals(customerCity, customerLocation.getCustomerCity())) {
                        list = null;

                        break;
                    }
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(4 + (orderByComparator.getOrderByFields().length * 2));
            } else {
                query = new StringBundler(4);
            }

            query.append(_SQL_SELECT_CUSTOMERLOCATION_WHERE);

            boolean bindCustomerName = false;

            if (customerName == null) {
                query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERNAME_1);
            } else if (customerName.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERNAME_3);
            } else {
                bindCustomerName = true;

                query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERNAME_2);
            }

            boolean bindCustomerCity = false;

            if (customerCity == null) {
                query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERCITY_1);
            } else if (customerCity.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERCITY_3);
            } else {
                bindCustomerCity = true;

                query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERCITY_2);
            }

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            } else if (pagination) {
                query.append(CustomerLocationModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindCustomerName) {
                    qPos.add(customerName);
                }

                if (bindCustomerCity) {
                    qPos.add(customerCity);
                }

                if (!pagination) {
                    list = (List<CustomerLocation>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<CustomerLocation>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                finderCache.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first customer location in the ordered set where customerName = &#63; and customerCity = &#63;.
     *
     * @param customerName the customer name
     * @param customerCity the customer city
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching customer location
     * @throws NoSuchCustomerLocationException if a matching customer location could not be found
     */
    @Override
    public CustomerLocation findByCustomerName_CustomerCity_First(String customerName, String customerCity,
            OrderByComparator<CustomerLocation> orderByComparator) throws NoSuchCustomerLocationException {
        CustomerLocation customerLocation = fetchByCustomerName_CustomerCity_First(customerName, customerCity,
                orderByComparator);

        if (customerLocation != null) {
            return customerLocation;
        }

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("customerName=");
        msg.append(customerName);

        msg.append(", customerCity=");
        msg.append(customerCity);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchCustomerLocationException(msg.toString());
    }

    /**
     * Returns the first customer location in the ordered set where customerName = &#63; and customerCity = &#63;.
     *
     * @param customerName the customer name
     * @param customerCity the customer city
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching customer location, or <code>null</code> if a matching customer location could not be found
     */
    @Override
    public CustomerLocation fetchByCustomerName_CustomerCity_First(String customerName, String customerCity,
            OrderByComparator<CustomerLocation> orderByComparator) {
        List<CustomerLocation> list = findByCustomerName_CustomerCity(customerName, customerCity, 0, 1,
                orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last customer location in the ordered set where customerName = &#63; and customerCity = &#63;.
     *
     * @param customerName the customer name
     * @param customerCity the customer city
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching customer location
     * @throws NoSuchCustomerLocationException if a matching customer location could not be found
     */
    @Override
    public CustomerLocation findByCustomerName_CustomerCity_Last(String customerName, String customerCity,
            OrderByComparator<CustomerLocation> orderByComparator) throws NoSuchCustomerLocationException {
        CustomerLocation customerLocation = fetchByCustomerName_CustomerCity_Last(customerName, customerCity,
                orderByComparator);

        if (customerLocation != null) {
            return customerLocation;
        }

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("customerName=");
        msg.append(customerName);

        msg.append(", customerCity=");
        msg.append(customerCity);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchCustomerLocationException(msg.toString());
    }

    /**
     * Returns the last customer location in the ordered set where customerName = &#63; and customerCity = &#63;.
     *
     * @param customerName the customer name
     * @param customerCity the customer city
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching customer location, or <code>null</code> if a matching customer location could not be found
     */
    @Override
    public CustomerLocation fetchByCustomerName_CustomerCity_Last(String customerName, String customerCity,
            OrderByComparator<CustomerLocation> orderByComparator) {
        int count = countByCustomerName_CustomerCity(customerName, customerCity);

        if (count == 0) {
            return null;
        }

        List<CustomerLocation> list = findByCustomerName_CustomerCity(customerName, customerCity, count - 1, count,
                orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the customer locations before and after the current customer location in the ordered set where customerName = &#63; and customerCity = &#63;.
     *
     * @param customerLocationId the primary key of the current customer location
     * @param customerName the customer name
     * @param customerCity the customer city
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next customer location
     * @throws NoSuchCustomerLocationException if a customer location with the primary key could not be found
     */
    @Override
    public CustomerLocation[] findByCustomerName_CustomerCity_PrevAndNext(long customerLocationId,
            String customerName, String customerCity, OrderByComparator<CustomerLocation> orderByComparator)
            throws NoSuchCustomerLocationException {
        CustomerLocation customerLocation = findByPrimaryKey(customerLocationId);

        Session session = null;

        try {
            session = openSession();

            CustomerLocation[] array = new CustomerLocationImpl[3];

            array[0] = getByCustomerName_CustomerCity_PrevAndNext(session, customerLocation, customerName,
                    customerCity, orderByComparator, true);

            array[1] = customerLocation;

            array[2] = getByCustomerName_CustomerCity_PrevAndNext(session, customerLocation, customerName,
                    customerCity, orderByComparator, false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected CustomerLocation getByCustomerName_CustomerCity_PrevAndNext(Session session,
            CustomerLocation customerLocation, String customerName, String customerCity,
            OrderByComparator<CustomerLocation> orderByComparator, boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(5 + (orderByComparator.getOrderByConditionFields().length * 3)
                    + (orderByComparator.getOrderByFields().length * 3));
        } else {
            query = new StringBundler(4);
        }

        query.append(_SQL_SELECT_CUSTOMERLOCATION_WHERE);

        boolean bindCustomerName = false;

        if (customerName == null) {
            query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERNAME_1);
        } else if (customerName.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERNAME_3);
        } else {
            bindCustomerName = true;

            query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERNAME_2);
        }

        boolean bindCustomerCity = false;

        if (customerCity == null) {
            query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERCITY_1);
        } else if (customerCity.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERCITY_3);
        } else {
            bindCustomerCity = true;

            query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERCITY_2);
        }

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            query.append(CustomerLocationModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindCustomerName) {
            qPos.add(customerName);
        }

        if (bindCustomerCity) {
            qPos.add(customerCity);
        }

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(customerLocation);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<CustomerLocation> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Removes all the customer locations where customerName = &#63; and customerCity = &#63; from the database.
     *
     * @param customerName the customer name
     * @param customerCity the customer city
     */
    @Override
    public void removeByCustomerName_CustomerCity(String customerName, String customerCity) {
        for (CustomerLocation customerLocation : findByCustomerName_CustomerCity(customerName, customerCity,
                QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(customerLocation);
        }
    }

    /**
     * Returns the number of customer locations where customerName = &#63; and customerCity = &#63;.
     *
     * @param customerName the customer name
     * @param customerCity the customer city
     * @return the number of matching customer locations
     */
    @Override
    public int countByCustomerName_CustomerCity(String customerName, String customerCity) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_CUSTOMERNAME_CUSTOMERCITY;

        Object[] finderArgs = new Object[] { customerName, customerCity };

        Long count = (Long) finderCache.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(3);

            query.append(_SQL_COUNT_CUSTOMERLOCATION_WHERE);

            boolean bindCustomerName = false;

            if (customerName == null) {
                query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERNAME_1);
            } else if (customerName.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERNAME_3);
            } else {
                bindCustomerName = true;

                query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERNAME_2);
            }

            boolean bindCustomerCity = false;

            if (customerCity == null) {
                query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERCITY_1);
            } else if (customerCity.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERCITY_3);
            } else {
                bindCustomerCity = true;

                query.append(_FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERCITY_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindCustomerName) {
                    qPos.add(customerName);
                }

                if (bindCustomerCity) {
                    qPos.add(customerCity);
                }

                count = (Long) q.uniqueResult();

                finderCache.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    private static final String _FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERNAME_1 = "customerLocation.customerName IS NULL AND ";
    private static final String _FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERNAME_2 = "customerLocation.customerName = ? AND ";
    private static final String _FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERNAME_3 = "(customerLocation.customerName IS NULL OR customerLocation.customerName = '') AND ";
    private static final String _FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERCITY_1 = "customerLocation.customerCity IS NULL";
    private static final String _FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERCITY_2 = "customerLocation.customerCity = ?";
    private static final String _FINDER_COLUMN_CUSTOMERNAME_CUSTOMERCITY_CUSTOMERCITY_3 = "(customerLocation.customerCity IS NULL OR customerLocation.customerCity = '')";

    public CustomerLocationPersistenceImpl() {
        setModelClass(CustomerLocation.class);
    }

    /**
     * Caches the customer location in the entity cache if it is enabled.
     *
     * @param customerLocation the customer location
     */
    @Override
    public void cacheResult(CustomerLocation customerLocation) {
        entityCache.putResult(CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationImpl.class,
                customerLocation.getPrimaryKey(), customerLocation);

        finderCache.putResult(FINDER_PATH_FETCH_BY_BEMISCUSTOMERID,
                new Object[] { customerLocation.getBemisCustomerId() }, customerLocation);

        customerLocation.resetOriginalValues();
    }

    /**
     * Caches the customer locations in the entity cache if it is enabled.
     *
     * @param customerLocations the customer locations
     */
    @Override
    public void cacheResult(List<CustomerLocation> customerLocations) {
        for (CustomerLocation customerLocation : customerLocations) {
            if (entityCache.getResult(CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationImpl.class,
                    customerLocation.getPrimaryKey()) == null) {
                cacheResult(customerLocation);
            } else {
                customerLocation.resetOriginalValues();
            }
        }
    }

    /**
     * Clears the cache for all customer locations.
     *
     * <p>
     * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache() {
        entityCache.clearCache(CustomerLocationImpl.class);

        finderCache.clearCache(FINDER_CLASS_NAME_ENTITY);
        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    /**
     * Clears the cache for the customer location.
     *
     * <p>
     * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache(CustomerLocation customerLocation) {
        entityCache.removeResult(CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationImpl.class,
                customerLocation.getPrimaryKey());

        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        clearUniqueFindersCache((CustomerLocationModelImpl) customerLocation);
    }

    @Override
    public void clearCache(List<CustomerLocation> customerLocations) {
        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        for (CustomerLocation customerLocation : customerLocations) {
            entityCache.removeResult(CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationImpl.class,
                    customerLocation.getPrimaryKey());

            clearUniqueFindersCache((CustomerLocationModelImpl) customerLocation);
        }
    }

    protected void cacheUniqueFindersCache(CustomerLocationModelImpl customerLocationModelImpl, boolean isNew) {
        if (isNew) {
            Object[] args = new Object[] { customerLocationModelImpl.getBemisCustomerId() };

            finderCache.putResult(FINDER_PATH_COUNT_BY_BEMISCUSTOMERID, args, Long.valueOf(1));
            finderCache.putResult(FINDER_PATH_FETCH_BY_BEMISCUSTOMERID, args, customerLocationModelImpl);
        } else {
            if ((customerLocationModelImpl.getColumnBitmask()
                    & FINDER_PATH_FETCH_BY_BEMISCUSTOMERID.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { customerLocationModelImpl.getBemisCustomerId() };

                finderCache.putResult(FINDER_PATH_COUNT_BY_BEMISCUSTOMERID, args, Long.valueOf(1));
                finderCache.putResult(FINDER_PATH_FETCH_BY_BEMISCUSTOMERID, args, customerLocationModelImpl);
            }
        }
    }

    protected void clearUniqueFindersCache(CustomerLocationModelImpl customerLocationModelImpl) {
        Object[] args = new Object[] { customerLocationModelImpl.getBemisCustomerId() };

        finderCache.removeResult(FINDER_PATH_COUNT_BY_BEMISCUSTOMERID, args);
        finderCache.removeResult(FINDER_PATH_FETCH_BY_BEMISCUSTOMERID, args);

        if ((customerLocationModelImpl.getColumnBitmask()
                & FINDER_PATH_FETCH_BY_BEMISCUSTOMERID.getColumnBitmask()) != 0) {
            args = new Object[] { customerLocationModelImpl.getOriginalBemisCustomerId() };

            finderCache.removeResult(FINDER_PATH_COUNT_BY_BEMISCUSTOMERID, args);
            finderCache.removeResult(FINDER_PATH_FETCH_BY_BEMISCUSTOMERID, args);
        }
    }

    /**
     * Creates a new customer location with the primary key. Does not add the customer location to the database.
     *
     * @param customerLocationId the primary key for the new customer location
     * @return the new customer location
     */
    @Override
    public CustomerLocation create(long customerLocationId) {
        CustomerLocation customerLocation = new CustomerLocationImpl();

        customerLocation.setNew(true);
        customerLocation.setPrimaryKey(customerLocationId);

        customerLocation.setCompanyId(companyProvider.getCompanyId());

        return customerLocation;
    }

    /**
     * Removes the customer location with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param customerLocationId the primary key of the customer location
     * @return the customer location that was removed
     * @throws NoSuchCustomerLocationException if a customer location with the primary key could not be found
     */
    @Override
    public CustomerLocation remove(long customerLocationId) throws NoSuchCustomerLocationException {
        return remove((Serializable) customerLocationId);
    }

    /**
     * Removes the customer location with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param primaryKey the primary key of the customer location
     * @return the customer location that was removed
     * @throws NoSuchCustomerLocationException if a customer location with the primary key could not be found
     */
    @Override
    public CustomerLocation remove(Serializable primaryKey) throws NoSuchCustomerLocationException {
        Session session = null;

        try {
            session = openSession();

            CustomerLocation customerLocation = (CustomerLocation) session.get(CustomerLocationImpl.class,
                    primaryKey);

            if (customerLocation == null) {
                if (_log.isDebugEnabled()) {
                    _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
                }

                throw new NoSuchCustomerLocationException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            return remove(customerLocation);
        } catch (NoSuchCustomerLocationException nsee) {
            throw nsee;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    @Override
    protected CustomerLocation removeImpl(CustomerLocation customerLocation) {
        customerLocation = toUnwrappedModel(customerLocation);

        Session session = null;

        try {
            session = openSession();

            if (!session.contains(customerLocation)) {
                customerLocation = (CustomerLocation) session.get(CustomerLocationImpl.class,
                        customerLocation.getPrimaryKeyObj());
            }

            if (customerLocation != null) {
                session.delete(customerLocation);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        if (customerLocation != null) {
            clearCache(customerLocation);
        }

        return customerLocation;
    }

    @Override
    public CustomerLocation updateImpl(CustomerLocation customerLocation) {
        customerLocation = toUnwrappedModel(customerLocation);

        boolean isNew = customerLocation.isNew();

        CustomerLocationModelImpl customerLocationModelImpl = (CustomerLocationModelImpl) customerLocation;

        ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();

        Date now = new Date();

        if (isNew && (customerLocation.getCreateDate() == null)) {
            if (serviceContext == null) {
                customerLocation.setCreateDate(now);
            } else {
                customerLocation.setCreateDate(serviceContext.getCreateDate(now));
            }
        }

        if (!customerLocationModelImpl.hasSetModifiedDate()) {
            if (serviceContext == null) {
                customerLocation.setModifiedDate(now);
            } else {
                customerLocation.setModifiedDate(serviceContext.getModifiedDate(now));
            }
        }

        Session session = null;

        try {
            session = openSession();

            if (customerLocation.isNew()) {
                session.save(customerLocation);

                customerLocation.setNew(false);
            } else {
                customerLocation = (CustomerLocation) session.merge(customerLocation);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

        if (isNew || !CustomerLocationModelImpl.COLUMN_BITMASK_ENABLED) {
            finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
        }

        else {
            if ((customerLocationModelImpl.getColumnBitmask()
                    & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BEMISPARENTID.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { customerLocationModelImpl.getOriginalBemisParentId() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_BEMISPARENTID, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BEMISPARENTID, args);

                args = new Object[] { customerLocationModelImpl.getBemisParentId() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_BEMISPARENTID, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BEMISPARENTID, args);
            }

            if ((customerLocationModelImpl.getColumnBitmask()
                    & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CUSTOMERACTIVESTATUSCDE.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { customerLocationModelImpl.getOriginalCustomerActiveStatusCde() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_CUSTOMERACTIVESTATUSCDE, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CUSTOMERACTIVESTATUSCDE, args);

                args = new Object[] { customerLocationModelImpl.getCustomerActiveStatusCde() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_CUSTOMERACTIVESTATUSCDE, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CUSTOMERACTIVESTATUSCDE, args);
            }

            if ((customerLocationModelImpl.getColumnBitmask()
                    & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CUSTOMERNAME_CUSTOMERCITY.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { customerLocationModelImpl.getOriginalCustomerName(),
                        customerLocationModelImpl.getOriginalCustomerCity() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_CUSTOMERNAME_CUSTOMERCITY, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CUSTOMERNAME_CUSTOMERCITY, args);

                args = new Object[] { customerLocationModelImpl.getCustomerName(),
                        customerLocationModelImpl.getCustomerCity() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_CUSTOMERNAME_CUSTOMERCITY, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CUSTOMERNAME_CUSTOMERCITY, args);
            }
        }

        entityCache.putResult(CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationImpl.class,
                customerLocation.getPrimaryKey(), customerLocation, false);

        clearUniqueFindersCache(customerLocationModelImpl);
        cacheUniqueFindersCache(customerLocationModelImpl, isNew);

        customerLocation.resetOriginalValues();

        return customerLocation;
    }

    protected CustomerLocation toUnwrappedModel(CustomerLocation customerLocation) {
        if (customerLocation instanceof CustomerLocationImpl) {
            return customerLocation;
        }

        CustomerLocationImpl customerLocationImpl = new CustomerLocationImpl();

        customerLocationImpl.setNew(customerLocation.isNew());
        customerLocationImpl.setPrimaryKey(customerLocation.getPrimaryKey());

        customerLocationImpl.setCustomerLocationId(customerLocation.getCustomerLocationId());
        customerLocationImpl.setCompanyId(customerLocation.getCompanyId());
        customerLocationImpl.setUserId(customerLocation.getUserId());
        customerLocationImpl.setUserName(customerLocation.getUserName());
        customerLocationImpl.setCreateDate(customerLocation.getCreateDate());
        customerLocationImpl.setModifiedDate(customerLocation.getModifiedDate());
        customerLocationImpl.setBemisCustomerId(customerLocation.getBemisCustomerId());
        customerLocationImpl.setBemisParentId(customerLocation.getBemisParentId());
        customerLocationImpl.setCustomerName(customerLocation.getCustomerName());
        customerLocationImpl.setCustomerAddress1(customerLocation.getCustomerAddress1());
        customerLocationImpl.setCustomerAddress2(customerLocation.getCustomerAddress2());
        customerLocationImpl.setCustomerAddress3(customerLocation.getCustomerAddress3());
        customerLocationImpl.setCustomerCity(customerLocation.getCustomerCity());
        customerLocationImpl.setCustomerState(customerLocation.getCustomerState());
        customerLocationImpl.setCustomerPostalCde(customerLocation.getCustomerPostalCde());
        customerLocationImpl.setCustomerCountry(customerLocation.getCustomerCountry());
        customerLocationImpl.setCustomerPhoneNbr(customerLocation.getCustomerPhoneNbr());
        customerLocationImpl.setCustomerPoBox(customerLocation.getCustomerPoBox());
        customerLocationImpl.setCustomerPoCity(customerLocation.getCustomerPoCity());
        customerLocationImpl.setCustomerPoState(customerLocation.getCustomerPoState());
        customerLocationImpl.setCustomerPoPostalCde(customerLocation.getCustomerPoPostalCde());
        customerLocationImpl.setCustomerActiveStatusCde(customerLocation.getCustomerActiveStatusCde());

        return customerLocationImpl;
    }

    /**
     * Returns the customer location with the primary key or throws a {@link com.liferay.portal.kernel.exception.NoSuchModelException} if it could not be found.
     *
     * @param primaryKey the primary key of the customer location
     * @return the customer location
     * @throws NoSuchCustomerLocationException if a customer location with the primary key could not be found
     */
    @Override
    public CustomerLocation findByPrimaryKey(Serializable primaryKey) throws NoSuchCustomerLocationException {
        CustomerLocation customerLocation = fetchByPrimaryKey(primaryKey);

        if (customerLocation == null) {
            if (_log.isDebugEnabled()) {
                _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            throw new NoSuchCustomerLocationException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
        }

        return customerLocation;
    }

    /**
     * Returns the customer location with the primary key or throws a {@link NoSuchCustomerLocationException} if it could not be found.
     *
     * @param customerLocationId the primary key of the customer location
     * @return the customer location
     * @throws NoSuchCustomerLocationException if a customer location with the primary key could not be found
     */
    @Override
    public CustomerLocation findByPrimaryKey(long customerLocationId) throws NoSuchCustomerLocationException {
        return findByPrimaryKey((Serializable) customerLocationId);
    }

    /**
     * Returns the customer location with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param primaryKey the primary key of the customer location
     * @return the customer location, or <code>null</code> if a customer location with the primary key could not be found
     */
    @Override
    public CustomerLocation fetchByPrimaryKey(Serializable primaryKey) {
        Serializable serializable = entityCache.getResult(CustomerLocationModelImpl.ENTITY_CACHE_ENABLED,
                CustomerLocationImpl.class, primaryKey);

        if (serializable == nullModel) {
            return null;
        }

        CustomerLocation customerLocation = (CustomerLocation) serializable;

        if (customerLocation == null) {
            Session session = null;

            try {
                session = openSession();

                customerLocation = (CustomerLocation) session.get(CustomerLocationImpl.class, primaryKey);

                if (customerLocation != null) {
                    cacheResult(customerLocation);
                } else {
                    entityCache.putResult(CustomerLocationModelImpl.ENTITY_CACHE_ENABLED,
                            CustomerLocationImpl.class, primaryKey, nullModel);
                }
            } catch (Exception e) {
                entityCache.removeResult(CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationImpl.class,
                        primaryKey);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return customerLocation;
    }

    /**
     * Returns the customer location with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param customerLocationId the primary key of the customer location
     * @return the customer location, or <code>null</code> if a customer location with the primary key could not be found
     */
    @Override
    public CustomerLocation fetchByPrimaryKey(long customerLocationId) {
        return fetchByPrimaryKey((Serializable) customerLocationId);
    }

    @Override
    public Map<Serializable, CustomerLocation> fetchByPrimaryKeys(Set<Serializable> primaryKeys) {
        if (primaryKeys.isEmpty()) {
            return Collections.emptyMap();
        }

        Map<Serializable, CustomerLocation> map = new HashMap<Serializable, CustomerLocation>();

        if (primaryKeys.size() == 1) {
            Iterator<Serializable> iterator = primaryKeys.iterator();

            Serializable primaryKey = iterator.next();

            CustomerLocation customerLocation = fetchByPrimaryKey(primaryKey);

            if (customerLocation != null) {
                map.put(primaryKey, customerLocation);
            }

            return map;
        }

        Set<Serializable> uncachedPrimaryKeys = null;

        for (Serializable primaryKey : primaryKeys) {
            Serializable serializable = entityCache.getResult(CustomerLocationModelImpl.ENTITY_CACHE_ENABLED,
                    CustomerLocationImpl.class, primaryKey);

            if (serializable != nullModel) {
                if (serializable == null) {
                    if (uncachedPrimaryKeys == null) {
                        uncachedPrimaryKeys = new HashSet<Serializable>();
                    }

                    uncachedPrimaryKeys.add(primaryKey);
                } else {
                    map.put(primaryKey, (CustomerLocation) serializable);
                }
            }
        }

        if (uncachedPrimaryKeys == null) {
            return map;
        }

        StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) + 1);

        query.append(_SQL_SELECT_CUSTOMERLOCATION_WHERE_PKS_IN);

        for (Serializable primaryKey : uncachedPrimaryKeys) {
            query.append(String.valueOf(primaryKey));

            query.append(StringPool.COMMA);
        }

        query.setIndex(query.index() - 1);

        query.append(StringPool.CLOSE_PARENTHESIS);

        String sql = query.toString();

        Session session = null;

        try {
            session = openSession();

            Query q = session.createQuery(sql);

            for (CustomerLocation customerLocation : (List<CustomerLocation>) q.list()) {
                map.put(customerLocation.getPrimaryKeyObj(), customerLocation);

                cacheResult(customerLocation);

                uncachedPrimaryKeys.remove(customerLocation.getPrimaryKeyObj());
            }

            for (Serializable primaryKey : uncachedPrimaryKeys) {
                entityCache.putResult(CustomerLocationModelImpl.ENTITY_CACHE_ENABLED, CustomerLocationImpl.class,
                        primaryKey, nullModel);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        return map;
    }

    /**
     * Returns all the customer locations.
     *
     * @return the customer locations
     */
    @Override
    public List<CustomerLocation> findAll() {
        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the customer locations.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link CustomerLocationModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of customer locations
     * @param end the upper bound of the range of customer locations (not inclusive)
     * @return the range of customer locations
     */
    @Override
    public List<CustomerLocation> findAll(int start, int end) {
        return findAll(start, end, null);
    }

    /**
     * Returns an ordered range of all the customer locations.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link CustomerLocationModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of customer locations
     * @param end the upper bound of the range of customer locations (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of customer locations
     */
    @Override
    public List<CustomerLocation> findAll(int start, int end,
            OrderByComparator<CustomerLocation> orderByComparator) {
        return findAll(start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the customer locations.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link CustomerLocationModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of customer locations
     * @param end the upper bound of the range of customer locations (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the ordered range of customer locations
     */
    @Override
    public List<CustomerLocation> findAll(int start, int end, OrderByComparator<CustomerLocation> orderByComparator,
            boolean retrieveFromCache) {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
            finderArgs = FINDER_ARGS_EMPTY;
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
            finderArgs = new Object[] { start, end, orderByComparator };
        }

        List<CustomerLocation> list = null;

        if (retrieveFromCache) {
            list = (List<CustomerLocation>) finderCache.getResult(finderPath, finderArgs, this);
        }

        if (list == null) {
            StringBundler query = null;
            String sql = null;

            if (orderByComparator != null) {
                query = new StringBundler(2 + (orderByComparator.getOrderByFields().length * 2));

                query.append(_SQL_SELECT_CUSTOMERLOCATION);

                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);

                sql = query.toString();
            } else {
                sql = _SQL_SELECT_CUSTOMERLOCATION;

                if (pagination) {
                    sql = sql.concat(CustomerLocationModelImpl.ORDER_BY_JPQL);
                }
            }

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                if (!pagination) {
                    list = (List<CustomerLocation>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<CustomerLocation>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                finderCache.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Removes all the customer locations from the database.
     *
     */
    @Override
    public void removeAll() {
        for (CustomerLocation customerLocation : findAll()) {
            remove(customerLocation);
        }
    }

    /**
     * Returns the number of customer locations.
     *
     * @return the number of customer locations
     */
    @Override
    public int countAll() {
        Long count = (Long) finderCache.getResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY, this);

        if (count == null) {
            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(_SQL_COUNT_CUSTOMERLOCATION);

                count = (Long) q.uniqueResult();

                finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY, count);
            } catch (Exception e) {
                finderCache.removeResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    @Override
    protected Map<String, Integer> getTableColumnsMap() {
        return CustomerLocationModelImpl.TABLE_COLUMNS_MAP;
    }

    /**
     * Initializes the customer location persistence.
     */
    public void afterPropertiesSet() {
    }

    public void destroy() {
        entityCache.removeCache(CustomerLocationImpl.class.getName());
        finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
        finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    @ServiceReference(type = CompanyProviderWrapper.class)
    protected CompanyProvider companyProvider;
    @ServiceReference(type = EntityCache.class)
    protected EntityCache entityCache;
    @ServiceReference(type = FinderCache.class)
    protected FinderCache finderCache;
    private static final String _SQL_SELECT_CUSTOMERLOCATION = "SELECT customerLocation FROM CustomerLocation customerLocation";
    private static final String _SQL_SELECT_CUSTOMERLOCATION_WHERE_PKS_IN = "SELECT customerLocation FROM CustomerLocation customerLocation WHERE customerLocationId IN (";
    private static final String _SQL_SELECT_CUSTOMERLOCATION_WHERE = "SELECT customerLocation FROM CustomerLocation customerLocation WHERE ";
    private static final String _SQL_COUNT_CUSTOMERLOCATION = "SELECT COUNT(customerLocation) FROM CustomerLocation customerLocation";
    private static final String _SQL_COUNT_CUSTOMERLOCATION_WHERE = "SELECT COUNT(customerLocation) FROM CustomerLocation customerLocation WHERE ";
    private static final String _ORDER_BY_ENTITY_ALIAS = "customerLocation.";
    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No CustomerLocation exists with the primary key ";
    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No CustomerLocation exists with the key {";
    private static final Log _log = LogFactoryUtil.getLog(CustomerLocationPersistenceImpl.class);
}