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.CountryA2Exception;
018    import com.liferay.portal.CountryA3Exception;
019    import com.liferay.portal.CountryIddException;
020    import com.liferay.portal.CountryNameException;
021    import com.liferay.portal.CountryNumberException;
022    import com.liferay.portal.kernel.exception.PortalException;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Country;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.service.base.CountryServiceBaseImpl;
027    
028    import java.util.List;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class CountryServiceImpl extends CountryServiceBaseImpl {
034    
035            @Override
036            public Country addCountry(
037                            String name, String a2, String a3, String number, String idd,
038                            boolean active)
039                    throws PortalException {
040    
041                    if (!getPermissionChecker().isOmniadmin()) {
042                            throw new PrincipalException();
043                    }
044    
045                    if (Validator.isNull(name)) {
046                            throw new CountryNameException();
047                    }
048    
049                    if (Validator.isNull(a2)) {
050                            throw new CountryA2Exception();
051                    }
052    
053                    if (Validator.isNull(a3)) {
054                            throw new CountryA3Exception();
055                    }
056    
057                    if (Validator.isNull(number)) {
058                            throw new CountryNumberException();
059                    }
060    
061                    if (Validator.isNull(idd)) {
062                            throw new CountryIddException();
063                    }
064    
065                    long countryId = counterLocalService.increment();
066    
067                    Country country = countryPersistence.create(countryId);
068    
069                    country.setName(name);
070                    country.setA2(a2);
071                    country.setA3(a3);
072                    country.setNumber(number);
073                    country.setIdd(idd);
074                    country.setActive(active);
075    
076                    countryPersistence.update(country);
077    
078                    return country;
079            }
080    
081            @Override
082            public Country fetchCountry(long countryId) {
083                    return countryPersistence.fetchByPrimaryKey(countryId);
084            }
085    
086            @Override
087            public Country fetchCountryByA2(String a2) {
088                    return countryPersistence.fetchByA2(a2);
089            }
090    
091            @Override
092            public Country fetchCountryByA3(String a3) {
093                    return countryPersistence.fetchByA3(a3);
094            }
095    
096            @Override
097            public List<Country> getCountries() {
098                    return countryPersistence.findAll();
099            }
100    
101            @Override
102            public List<Country> getCountries(boolean active) {
103                    return countryPersistence.findByActive(active);
104            }
105    
106            @Override
107            public Country getCountry(long countryId) throws PortalException {
108                    return countryPersistence.findByPrimaryKey(countryId);
109            }
110    
111            @Override
112            public Country getCountryByA2(String a2) throws PortalException {
113                    return countryPersistence.findByA2(a2);
114            }
115    
116            @Override
117            public Country getCountryByA3(String a3) throws PortalException {
118                    return countryPersistence.findByA3(a3);
119            }
120    
121            @Override
122            public Country getCountryByName(String name) throws PortalException {
123                    return countryPersistence.findByName(name);
124            }
125    
126    }