1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
13   */
14  
15  package com.liferay.portal.model.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.model.Address;
22  import com.liferay.portal.model.Group;
23  import com.liferay.portal.model.Organization;
24  import com.liferay.portal.model.OrganizationConstants;
25  import com.liferay.portal.service.AddressLocalServiceUtil;
26  import com.liferay.portal.service.GroupLocalServiceUtil;
27  import com.liferay.portal.service.OrganizationLocalServiceUtil;
28  
29  import java.util.List;
30  
31  /**
32   * <a href="OrganizationImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class OrganizationImpl
37      extends OrganizationModelImpl implements Organization {
38  
39      public OrganizationImpl() {
40      }
41  
42      public Address getAddress() {
43          Address address = null;
44  
45          try {
46              List<Address> addresses = getAddresses();
47  
48              if (addresses.size() > 0) {
49                  address = addresses.get(0);
50              }
51          }
52          catch (Exception e) {
53              _log.error(e);
54          }
55  
56          if (address == null) {
57              address = new AddressImpl();
58          }
59  
60          return address;
61      }
62  
63      public List<Address> getAddresses() throws SystemException {
64          return AddressLocalServiceUtil.getAddresses(
65              getCompanyId(), Organization.class.getName(), getOrganizationId());
66      }
67  
68      public Group getGroup() {
69          if (getOrganizationId() > 0) {
70              try {
71                  return GroupLocalServiceUtil.getOrganizationGroup(
72                      getCompanyId(), getOrganizationId());
73              }
74              catch (Exception e) {
75                  _log.error(e);
76              }
77          }
78  
79          return new GroupImpl();
80      }
81  
82      public Organization getParentOrganization()
83          throws PortalException, SystemException {
84  
85          if (getParentOrganizationId() ==
86                  OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
87  
88              return null;
89          }
90  
91          return OrganizationLocalServiceUtil.getOrganization(
92              getParentOrganizationId());
93      }
94  
95      public int getPrivateLayoutsPageCount() {
96          try {
97              Group group = getGroup();
98  
99              if (group == null) {
100                 return 0;
101             }
102             else {
103                 return group.getPrivateLayoutsPageCount();
104             }
105         }
106         catch (Exception e) {
107             _log.error(e);
108         }
109 
110         return 0;
111     }
112 
113     public int getPublicLayoutsPageCount() {
114         try {
115             Group group = getGroup();
116 
117             if (group == null) {
118                 return 0;
119             }
120             else {
121                 return group.getPublicLayoutsPageCount();
122             }
123         }
124         catch (Exception e) {
125             _log.error(e);
126         }
127 
128         return 0;
129     }
130 
131     public int getType() {
132         if (isLocation()) {
133             return OrganizationConstants.TYPE_LOCATION;
134         }
135         else {
136             return OrganizationConstants.TYPE_REGULAR;
137         }
138     }
139 
140     public int getType(boolean location) {
141         int type = OrganizationConstants.TYPE_REGULAR;
142 
143         if (location) {
144             type = OrganizationConstants.TYPE_LOCATION;
145         }
146 
147         return type;
148     }
149 
150     public String getTypeLabel() {
151         return getTypeLabel(getType());
152     }
153 
154     public String getTypeLabel(int type) {
155         if (type == OrganizationConstants.TYPE_LOCATION) {
156             return OrganizationConstants.TYPE_LOCATION_LABEL;
157         }
158         else {
159             return OrganizationConstants.TYPE_REGULAR_LABEL;
160         }
161     }
162 
163     public boolean hasPrivateLayouts() {
164         if (getPrivateLayoutsPageCount() > 0) {
165             return true;
166         }
167         else {
168             return false;
169         }
170     }
171 
172     public boolean hasPublicLayouts() {
173         if (getPublicLayoutsPageCount() > 0) {
174             return true;
175         }
176         else {
177             return false;
178         }
179     }
180 
181     public boolean isRegular() {
182         return !isLocation();
183     }
184 
185     public boolean isRoot() {
186         if (getParentOrganizationId() ==
187                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
188 
189             return true;
190         }
191         else {
192             return false;
193         }
194     }
195 
196     private static Log _log = LogFactoryUtil.getLog(Organization.class);
197 
198 }