001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.sharepoint;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.model.Group;
021    import com.liferay.portal.model.Organization;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.service.GroupLocalServiceUtil;
024    import com.liferay.portal.service.OrganizationLocalServiceUtil;
025    import com.liferay.portal.service.UserServiceUtil;
026    
027    import java.util.LinkedHashMap;
028    import java.util.List;
029    
030    /**
031     * @author Bruno Farache
032     */
033    public class CompanySharepointStorageImpl extends BaseSharepointStorageImpl {
034    
035            public Tree getFoldersTree(SharepointRequest sharepointRequest)
036                    throws Exception {
037    
038                    Tree foldersTree = new Tree();
039    
040                    LinkedHashMap<String, Object> groupParams =
041                            new LinkedHashMap<String, Object>();
042    
043                    groupParams.put("usersGroups", new Long(sharepointRequest.getUserId()));
044    
045                    List<Group> groups = GroupLocalServiceUtil.search(
046                            sharepointRequest.getCompanyId(), null, null, groupParams,
047                            QueryUtil.ALL_POS, QueryUtil.ALL_POS);
048    
049                    Group userGroup = GroupLocalServiceUtil.getUserGroup(
050                            sharepointRequest.getCompanyId(), sharepointRequest.getUserId());
051    
052                    groups.add(userGroup);
053    
054                    List<Organization> organizations =
055                            OrganizationLocalServiceUtil.getUserOrganizations(
056                                    sharepointRequest.getUserId(), true);
057    
058                    for (Organization organization : organizations) {
059                            groups.add(organization.getGroup());
060                    }
061    
062                    for (Group group : groups) {
063                            String path = getGroupPath(group);
064    
065                            foldersTree.addChild(getFolderTree(path));
066                    }
067    
068                    foldersTree.addChild(getFolderTree(StringPool.BLANK));
069    
070                    return foldersTree;
071            }
072    
073            protected String getGroupPath(Group group) throws Exception {
074                    StringBundler sb = new StringBundler(5);
075    
076                    String name = group.getName();
077    
078                    if (group.isUser()) {
079                            User user = UserServiceUtil.getUserById(group.getClassPK());
080    
081                            name = user.getFullName();
082                    }
083                    else if (group.isOrganization()) {
084                            Organization organization =
085                                    OrganizationLocalServiceUtil.getOrganization(
086                                            group.getOrganizationId());
087    
088                            name = organization.getName();
089                    }
090    
091                    sb.append(name);
092                    sb.append(StringPool.SPACE);
093                    sb.append(StringPool.OPEN_BRACKET);
094                    sb.append(group.getGroupId());
095                    sb.append(StringPool.CLOSE_BRACKET);
096    
097                    return sb.toString();
098            }
099    
100    }