1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.model.impl;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.model.Address;
26  import com.liferay.portal.model.Group;
27  import com.liferay.portal.model.Organization;
28  import com.liferay.portal.model.OrganizationConstants;
29  import com.liferay.portal.service.AddressLocalServiceUtil;
30  import com.liferay.portal.service.GroupLocalServiceUtil;
31  
32  import java.util.List;
33  
34  /**
35   * <a href="OrganizationImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class OrganizationImpl
41      extends OrganizationModelImpl implements Organization {
42  
43      public OrganizationImpl() {
44      }
45  
46      public boolean isRoot() {
47          if (getParentOrganizationId() ==
48                  OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
49  
50              return true;
51          }
52          else {
53              return false;
54          }
55      }
56  
57      public boolean isRegular() {
58          return !isLocation();
59      }
60  
61      public int getType() {
62          if (isLocation()) {
63              return OrganizationConstants.TYPE_LOCATION;
64          }
65          else {
66              return OrganizationConstants.TYPE_REGULAR;
67          }
68      }
69  
70      public int getType(boolean location) {
71          int type = OrganizationConstants.TYPE_REGULAR;
72  
73          if (location) {
74              type = OrganizationConstants.TYPE_LOCATION;
75          }
76  
77          return type;
78      }
79  
80      public String getTypeLabel() {
81          return getTypeLabel(getType());
82      }
83  
84      public String getTypeLabel(int type) {
85          if (type == OrganizationConstants.TYPE_LOCATION) {
86              return OrganizationConstants.TYPE_LOCATION_LABEL;
87          }
88          else {
89              return OrganizationConstants.TYPE_REGULAR_LABEL;
90          }
91      }
92  
93      public Group getGroup() {
94          if (getOrganizationId() > 0) {
95              try {
96                  return GroupLocalServiceUtil.getOrganizationGroup(
97                      getCompanyId(), getOrganizationId());
98              }
99              catch (Exception e) {
100                 _log.error(e);
101             }
102         }
103 
104         return new GroupImpl();
105     }
106 
107     public int getPrivateLayoutsPageCount() {
108         try {
109             Group group = getGroup();
110 
111             if (group == null) {
112                 return 0;
113             }
114             else {
115                 return group.getPrivateLayoutsPageCount();
116             }
117         }
118         catch (Exception e) {
119             _log.error(e);
120         }
121 
122         return 0;
123     }
124 
125     public boolean hasPrivateLayouts() {
126         if (getPrivateLayoutsPageCount() > 0) {
127             return true;
128         }
129         else {
130             return false;
131         }
132     }
133 
134     public int getPublicLayoutsPageCount() {
135         try {
136             Group group = getGroup();
137 
138             if (group == null) {
139                 return 0;
140             }
141             else {
142                 return group.getPublicLayoutsPageCount();
143             }
144         }
145         catch (Exception e) {
146             _log.error(e);
147         }
148 
149         return 0;
150     }
151 
152     public boolean hasPublicLayouts() {
153         if (getPublicLayoutsPageCount() > 0) {
154             return true;
155         }
156         else {
157             return false;
158         }
159     }
160 
161     public Address getAddress() {
162         Address address = null;
163 
164         try {
165             List<Address> addresses = getAddresses();
166 
167             if (addresses.size() > 0) {
168                 address = addresses.get(0);
169             }
170         }
171         catch (Exception e) {
172             _log.error(e);
173         }
174 
175         if (address == null) {
176             address = new AddressImpl();
177         }
178 
179         return address;
180     }
181 
182     public List<Address> getAddresses() throws SystemException {
183         return AddressLocalServiceUtil.getAddresses(
184             getCompanyId(), Organization.class.getName(), getOrganizationId());
185     }
186 
187     private static Log _log = LogFactoryUtil.getLog(Organization.class);
188 
189 }