001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.exception.RegionCodeException;
018    import com.liferay.portal.exception.RegionNameException;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.Region;
022    import com.liferay.portal.security.auth.PrincipalException;
023    import com.liferay.portal.service.base.RegionServiceBaseImpl;
024    
025    import java.util.List;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class RegionServiceImpl extends RegionServiceBaseImpl {
031    
032            @Override
033            public Region addRegion(
034                            long countryId, String regionCode, String name, boolean active)
035                    throws PortalException {
036    
037                    if (!getPermissionChecker().isOmniadmin()) {
038                            throw new PrincipalException.MustBeOmniadmin(
039                                    getPermissionChecker());
040                    }
041    
042                    countryPersistence.findByPrimaryKey(countryId);
043    
044                    if (Validator.isNull(regionCode)) {
045                            throw new RegionCodeException();
046                    }
047    
048                    if (Validator.isNull(name)) {
049                            throw new RegionNameException();
050                    }
051    
052                    long regionId = counterLocalService.increment();
053    
054                    Region region = regionPersistence.create(regionId);
055    
056                    region.setCountryId(countryId);
057                    region.setRegionCode(regionCode);
058                    region.setName(name);
059                    region.setActive(active);
060    
061                    regionPersistence.update(region);
062    
063                    return region;
064            }
065    
066            @Override
067            public Region fetchRegion(long regionId) {
068                    return regionPersistence.fetchByPrimaryKey(regionId);
069            }
070    
071            @Override
072            public Region fetchRegion(long countryId, String regionCode) {
073                    return regionPersistence.fetchByC_R(countryId, regionCode);
074            }
075    
076            @Override
077            public Region getRegion(long regionId) throws PortalException {
078                    return regionPersistence.findByPrimaryKey(regionId);
079            }
080    
081            @Override
082            public Region getRegion(long countryId, String regionCode)
083                    throws PortalException {
084    
085                    return regionPersistence.findByC_R(countryId, regionCode);
086            }
087    
088            @Override
089            public List<Region> getRegions() {
090                    return regionPersistence.findAll();
091            }
092    
093            @Override
094            public List<Region> getRegions(boolean active) {
095                    return regionPersistence.findByActive(active);
096            }
097    
098            @Override
099            public List<Region> getRegions(long countryId) {
100                    return regionPersistence.findByCountryId(countryId);
101            }
102    
103            @Override
104            public List<Region> getRegions(long countryId, boolean active) {
105                    return regionPersistence.findByC_A(countryId, active);
106            }
107    
108    }