001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }