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