001    /**
002     * Copyright (c) 2000-2010 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            public Country addCountry(
037                            String name, String a2, String a3, String number, String idd,
038                            boolean active)
039                    throws PortalException, SystemException {
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, false);
077    
078                    return country;
079            }
080    
081            public List<Country> getCountries() throws SystemException {
082                    return countryPersistence.findAll();
083            }
084    
085            public List<Country> getCountries(boolean active) throws SystemException {
086                    return countryPersistence.findByActive(active);
087            }
088    
089            public Country getCountry(long countryId)
090                    throws PortalException, SystemException {
091    
092                    return countryPersistence.findByPrimaryKey(countryId);
093            }
094    
095            public Country getCountryByA2(String a2)
096                    throws PortalException, SystemException {
097    
098                    return countryPersistence.findByA2(a2);
099            }
100    
101            public Country getCountryByA3(String a3)
102                    throws PortalException, SystemException {
103    
104                    return countryPersistence.findByA3(a3);
105            }
106    
107            public Country getCountryByName(String name)
108                    throws PortalException, SystemException {
109    
110                    return countryPersistence.findByName(name);
111            }
112    
113    }