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.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 static final int DEFAULT_PARENT_ORGANIZATION_ID = 0;
48  
49      public static final int ANY_PARENT_ORGANIZATION_ID = -1;
50  
51      public static final int ANY_TYPE = -1;
52  
53      public static final int TYPE_REGULAR = 1;
54  
55      public static final String TYPE_REGULAR_LABEL = "regular";
56  
57      public static final int TYPE_LOCATION = 2;
58  
59      public static final String TYPE_LOCATION_LABEL = "location";
60  
61      public static int getType(boolean location) {
62          int type = OrganizationImpl.TYPE_REGULAR;
63  
64          if (location) {
65              type = OrganizationImpl.TYPE_LOCATION;
66          }
67  
68          return type;
69      }
70  
71      public static String getTypeLabel(int type) {
72          if (type == TYPE_LOCATION) {
73              return TYPE_LOCATION_LABEL;
74          }
75          else {
76              return TYPE_REGULAR_LABEL;
77          }
78      }
79  
80      public OrganizationImpl() {
81      }
82  
83      public boolean isRoot() {
84          if (getParentOrganizationId() == DEFAULT_PARENT_ORGANIZATION_ID) {
85              return true;
86          }
87          else {
88              return false;
89          }
90      }
91  
92      public boolean isRegular() {
93          return !isLocation();
94      }
95  
96      public int getType() {
97          if (isLocation()) {
98              return TYPE_LOCATION;
99          }
100         else {
101             return TYPE_REGULAR;
102         }
103     }
104 
105     public String getTypeLabel() {
106         return getTypeLabel(getType());
107     }
108 
109     public Group getGroup() {
110         if (getOrganizationId() > 0) {
111             try {
112                 return GroupLocalServiceUtil.getOrganizationGroup(
113                     getCompanyId(), getOrganizationId());
114             }
115             catch (Exception e) {
116                 _log.error(e);
117             }
118         }
119 
120         return new GroupImpl();
121     }
122 
123     public Address getAddress() {
124         Address address = null;
125 
126         try {
127             List addresses = getAddresses();
128 
129             if (addresses.size() > 0) {
130                 address = (Address)addresses.get(0);
131             }
132         }
133         catch (Exception e) {
134             _log.error(e);
135         }
136 
137         if (address == null) {
138             address = new AddressImpl();
139         }
140 
141         return address;
142     }
143 
144     public List getAddresses() throws PortalException, SystemException {
145         return AddressLocalServiceUtil.getAddresses(
146             getCompanyId(), Organization.class.getName(), getOrganizationId());
147     }
148 
149     private static Log _log = LogFactory.getLog(Organization.class);
150 
151 }