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