1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.sharepoint;
24  
25  import com.liferay.portal.kernel.dao.orm.QueryUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.model.Group;
28  import com.liferay.portal.model.Organization;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.service.GroupLocalServiceUtil;
31  import com.liferay.portal.service.OrganizationLocalServiceUtil;
32  import com.liferay.portal.service.UserServiceUtil;
33  
34  import java.util.LinkedHashMap;
35  import java.util.List;
36  
37  /**
38   * <a href="CompanySharepointStorageImpl.java.html"><b><i>View Source</i></b>
39   * </a>
40   *
41   * @author Bruno Farache
42   */
43  public class CompanySharepointStorageImpl extends BaseSharepointStorageImpl {
44  
45      public Tree getFoldersTree(SharepointRequest sharepointRequest)
46          throws Exception {
47  
48          Tree foldersTree = new Tree();
49  
50          LinkedHashMap<String, Object> groupParams =
51              new LinkedHashMap<String, Object>();
52  
53          groupParams.put("usersGroups", new Long(sharepointRequest.getUserId()));
54  
55          List<Group> groups = GroupLocalServiceUtil.search(
56              sharepointRequest.getCompanyId(), null, null, groupParams,
57              QueryUtil.ALL_POS, QueryUtil.ALL_POS);
58  
59          Group userGroup = GroupLocalServiceUtil.getUserGroup(
60              sharepointRequest.getCompanyId(), sharepointRequest.getUserId());
61  
62          groups.add(userGroup);
63  
64          List<Organization> organizations =
65              OrganizationLocalServiceUtil.getUserOrganizations(
66                  sharepointRequest.getUserId(), true);
67  
68          for (Organization organization : organizations) {
69              groups.add(organization.getGroup());
70          }
71  
72          for (Group group : groups) {
73              String path = getGroupPath(group);
74  
75              foldersTree.addChild(getFolderTree(path));
76          }
77  
78          foldersTree.addChild(getFolderTree(StringPool.BLANK));
79  
80          return foldersTree;
81      }
82  
83      protected String getGroupPath(Group group) throws Exception {
84          StringBuilder sb = new StringBuilder();
85  
86          String name = group.getName();
87  
88          long classPK = group.getClassPK();
89  
90          if (group.isUser()) {
91              User user = UserServiceUtil.getUserById(classPK);
92  
93              name = user.getFullName();
94          }
95          else if (group.isOrganization()) {
96              Organization organization =
97                  OrganizationLocalServiceUtil.getOrganization(classPK);
98  
99              name = organization.getName();
100         }
101 
102         sb.append(name);
103         sb.append(StringPool.SPACE);
104         sb.append(StringPool.OPEN_BRACKET);
105         sb.append(group.getGroupId());
106         sb.append(StringPool.CLOSE_BRACKET);
107 
108         return sb.toString();
109     }
110 
111 }