1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.model.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.model.Address;
30  import com.liferay.portal.model.Group;
31  import com.liferay.portal.model.Organization;
32  import com.liferay.portal.model.OrganizationConstants;
33  import com.liferay.portal.service.AddressLocalServiceUtil;
34  import com.liferay.portal.service.GroupLocalServiceUtil;
35  import com.liferay.portal.service.OrganizationLocalServiceUtil;
36  
37  import java.util.List;
38  
39  /**
40   * <a href="OrganizationImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class OrganizationImpl
46      extends OrganizationModelImpl implements Organization {
47  
48      public OrganizationImpl() {
49      }
50  
51      public Address getAddress() {
52          Address address = null;
53  
54          try {
55              List<Address> addresses = getAddresses();
56  
57              if (addresses.size() > 0) {
58                  address = addresses.get(0);
59              }
60          }
61          catch (Exception e) {
62              _log.error(e);
63          }
64  
65          if (address == null) {
66              address = new AddressImpl();
67          }
68  
69          return address;
70      }
71  
72      public List<Address> getAddresses() throws SystemException {
73          return AddressLocalServiceUtil.getAddresses(
74              getCompanyId(), Organization.class.getName(), getOrganizationId());
75      }
76  
77      public Group getGroup() {
78          if (getOrganizationId() > 0) {
79              try {
80                  return GroupLocalServiceUtil.getOrganizationGroup(
81                      getCompanyId(), getOrganizationId());
82              }
83              catch (Exception e) {
84                  _log.error(e);
85              }
86          }
87  
88          return new GroupImpl();
89      }
90  
91      public Organization getParentOrganization()
92          throws PortalException, SystemException {
93  
94          if (getParentOrganizationId() ==
95                  OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
96  
97              return null;
98          }
99  
100         return OrganizationLocalServiceUtil.getOrganization(
101             getParentOrganizationId());
102     }
103 
104     public int getPrivateLayoutsPageCount() {
105         try {
106             Group group = getGroup();
107 
108             if (group == null) {
109                 return 0;
110             }
111             else {
112                 return group.getPrivateLayoutsPageCount();
113             }
114         }
115         catch (Exception e) {
116             _log.error(e);
117         }
118 
119         return 0;
120     }
121 
122     public int getPublicLayoutsPageCount() {
123         try {
124             Group group = getGroup();
125 
126             if (group == null) {
127                 return 0;
128             }
129             else {
130                 return group.getPublicLayoutsPageCount();
131             }
132         }
133         catch (Exception e) {
134             _log.error(e);
135         }
136 
137         return 0;
138     }
139 
140     public int getType() {
141         if (isLocation()) {
142             return OrganizationConstants.TYPE_LOCATION;
143         }
144         else {
145             return OrganizationConstants.TYPE_REGULAR;
146         }
147     }
148 
149     public int getType(boolean location) {
150         int type = OrganizationConstants.TYPE_REGULAR;
151 
152         if (location) {
153             type = OrganizationConstants.TYPE_LOCATION;
154         }
155 
156         return type;
157     }
158 
159     public String getTypeLabel() {
160         return getTypeLabel(getType());
161     }
162 
163     public String getTypeLabel(int type) {
164         if (type == OrganizationConstants.TYPE_LOCATION) {
165             return OrganizationConstants.TYPE_LOCATION_LABEL;
166         }
167         else {
168             return OrganizationConstants.TYPE_REGULAR_LABEL;
169         }
170     }
171 
172     public boolean hasPrivateLayouts() {
173         if (getPrivateLayoutsPageCount() > 0) {
174             return true;
175         }
176         else {
177             return false;
178         }
179     }
180 
181     public boolean hasPublicLayouts() {
182         if (getPublicLayoutsPageCount() > 0) {
183             return true;
184         }
185         else {
186             return false;
187         }
188     }
189 
190     public boolean isRegular() {
191         return !isLocation();
192     }
193 
194     public boolean isRoot() {
195         if (getParentOrganizationId() ==
196                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
197 
198             return true;
199         }
200         else {
201             return false;
202         }
203     }
204 
205     private static Log _log = LogFactoryUtil.getLog(Organization.class);
206 
207 }