1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.RegionCodeException;
18  import com.liferay.portal.RegionNameException;
19  import com.liferay.portal.kernel.exception.PortalException;
20  import com.liferay.portal.kernel.exception.SystemException;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.model.Region;
23  import com.liferay.portal.security.auth.PrincipalException;
24  import com.liferay.portal.service.base.RegionServiceBaseImpl;
25  
26  import java.util.List;
27  
28  /**
29   * <a href="RegionServiceImpl.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class RegionServiceImpl extends RegionServiceBaseImpl {
34  
35      public Region addRegion(
36              long countryId, String regionCode, String name, boolean active)
37          throws PortalException, SystemException {
38  
39          if (!getPermissionChecker().isOmniadmin()) {
40              throw new PrincipalException();
41          }
42  
43          countryPersistence.findByPrimaryKey(countryId);
44  
45          if (Validator.isNull(regionCode)) {
46              throw new RegionCodeException();
47          }
48  
49          if (Validator.isNull(name)) {
50              throw new RegionNameException();
51          }
52  
53          long regionId = counterLocalService.increment();
54  
55          Region region = regionPersistence.create(regionId);
56  
57          region.setCountryId(countryId);
58          region.setRegionCode(regionCode);
59          region.setName(name);
60          region.setActive(active);
61  
62          regionPersistence.update(region, false);
63  
64          return region;
65      }
66  
67      public List<Region> getRegions() throws SystemException {
68          return regionPersistence.findAll();
69      }
70  
71      public List<Region> getRegions(long countryId) throws SystemException {
72          return regionPersistence.findByCountryId(countryId);
73      }
74  
75      public List<Region> getRegions(boolean active) throws SystemException {
76          return regionPersistence.findByActive(active);
77      }
78  
79      public List<Region> getRegions(long countryId, boolean active)
80          throws SystemException {
81  
82          return regionPersistence.findByC_A(countryId, active);
83      }
84  
85      public Region getRegion(long regionId)
86          throws PortalException, SystemException {
87  
88          return regionPersistence.findByPrimaryKey(regionId);
89      }
90  
91  }