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