001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.RegionCodeException;
018    import com.liferay.portal.RegionNameException;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.Region;
023    import com.liferay.portal.security.ac.AccessControlled;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.service.base.RegionServiceBaseImpl;
026    
027    import java.util.List;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class RegionServiceImpl extends RegionServiceBaseImpl {
033    
034            @Override
035            public Region addRegion(
036                            long countryId, String regionCode, String name, boolean active)
037                    throws PortalException, SystemException {
038    
039                    if (!getPermissionChecker().isOmniadmin()) {
040                            throw new PrincipalException();
041                    }
042    
043                    countryPersistence.findByPrimaryKey(countryId);
044    
045                    if (Validator.isNull(regionCode)) {
046                            throw new RegionCodeException();
047                    }
048    
049                    if (Validator.isNull(name)) {
050                            throw new RegionNameException();
051                    }
052    
053                    long regionId = counterLocalService.increment();
054    
055                    Region region = regionPersistence.create(regionId);
056    
057                    region.setCountryId(countryId);
058                    region.setRegionCode(regionCode);
059                    region.setName(name);
060                    region.setActive(active);
061    
062                    regionPersistence.update(region);
063    
064                    return region;
065            }
066    
067            @Override
068            public Region fetchRegion(long regionId) throws SystemException {
069                    return regionPersistence.fetchByPrimaryKey(regionId);
070            }
071    
072            @Override
073            public Region fetchRegion(long countryId, String regionCode)
074                    throws SystemException {
075    
076                    return regionPersistence.fetchByC_R(countryId, regionCode);
077            }
078    
079            @Override
080            public Region getRegion(long regionId)
081                    throws PortalException, SystemException {
082    
083                    return regionPersistence.findByPrimaryKey(regionId);
084            }
085    
086            @Override
087            public Region getRegion(long countryId, String regionCode)
088                    throws PortalException, SystemException {
089    
090                    return regionPersistence.findByC_R(countryId, regionCode);
091            }
092    
093            @Override
094            public List<Region> getRegions() throws SystemException {
095                    return regionPersistence.findAll();
096            }
097    
098            @Override
099            public List<Region> getRegions(boolean active) throws SystemException {
100                    return regionPersistence.findByActive(active);
101            }
102    
103            @Override
104            public List<Region> getRegions(long countryId) throws SystemException {
105                    return regionPersistence.findByCountryId(countryId);
106            }
107    
108            @AccessControlled(guestAccessEnabled = true)
109            @Override
110            public List<Region> getRegions(long countryId, boolean active)
111                    throws SystemException {
112    
113                    return regionPersistence.findByC_A(countryId, active);
114            }
115    
116    }