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.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.model.Address;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portal.model.Organization;
30  import com.liferay.portal.model.OrganizationConstants;
31  import com.liferay.portal.service.AddressLocalServiceUtil;
32  import com.liferay.portal.service.GroupLocalServiceUtil;
33  
34  import java.util.List;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
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 boolean isRoot() {
52          if (getParentOrganizationId() ==
53                  OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
54  
55              return true;
56          }
57          else {
58              return false;
59          }
60      }
61  
62      public boolean isRegular() {
63          return !isLocation();
64      }
65  
66      public int getType() {
67          if (isLocation()) {
68              return OrganizationConstants.TYPE_LOCATION;
69          }
70          else {
71              return OrganizationConstants.TYPE_REGULAR;
72          }
73      }
74  
75      public int getType(boolean location) {
76          int type = OrganizationConstants.TYPE_REGULAR;
77  
78          if (location) {
79              type = OrganizationConstants.TYPE_LOCATION;
80          }
81  
82          return type;
83      }
84  
85      public String getTypeLabel() {
86          return getTypeLabel(getType());
87      }
88  
89      public String getTypeLabel(int type) {
90          if (type == OrganizationConstants.TYPE_LOCATION) {
91              return OrganizationConstants.TYPE_LOCATION_LABEL;
92          }
93          else {
94              return OrganizationConstants.TYPE_REGULAR_LABEL;
95          }
96      }
97  
98      public Group getGroup() {
99          if (getOrganizationId() > 0) {
100             try {
101                 return GroupLocalServiceUtil.getOrganizationGroup(
102                     getCompanyId(), getOrganizationId());
103             }
104             catch (Exception e) {
105                 _log.error(e);
106             }
107         }
108 
109         return new GroupImpl();
110     }
111 
112     public Address getAddress() {
113         Address address = null;
114 
115         try {
116             List<Address> addresses = getAddresses();
117 
118             if (addresses.size() > 0) {
119                 address = addresses.get(0);
120             }
121         }
122         catch (Exception e) {
123             _log.error(e);
124         }
125 
126         if (address == null) {
127             address = new AddressImpl();
128         }
129 
130         return address;
131     }
132 
133     public List<Address> getAddresses()
134         throws PortalException, SystemException {
135 
136         return AddressLocalServiceUtil.getAddresses(
137             getCompanyId(), Organization.class.getName(), getOrganizationId());
138     }
139 
140     private static Log _log = LogFactory.getLog(Organization.class);
141 
142 }