001    /**
002     * Copyright (c) 2000-2012 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            @Override
036            public Tree getFoldersTree(SharepointRequest sharepointRequest)
037                    throws Exception {
038    
039                    Tree foldersTree = new Tree();
040    
041                    LinkedHashMap<String, Object> groupParams =
042                            new LinkedHashMap<String, Object>();
043    
044                    groupParams.put("usersGroups", new Long(sharepointRequest.getUserId()));
045    
046                    List<Group> groups = GroupLocalServiceUtil.search(
047                            sharepointRequest.getCompanyId(), null, null, groupParams,
048                            QueryUtil.ALL_POS, QueryUtil.ALL_POS);
049    
050                    Group userGroup = GroupLocalServiceUtil.getUserGroup(
051                            sharepointRequest.getCompanyId(), sharepointRequest.getUserId());
052    
053                    groups.add(userGroup);
054    
055                    List<Organization> organizations =
056                            OrganizationLocalServiceUtil.getUserOrganizations(
057                                    sharepointRequest.getUserId());
058    
059                    for (Organization organization : organizations) {
060                            groups.add(organization.getGroup());
061                    }
062    
063                    for (Group group : groups) {
064                            String path = getGroupPath(group);
065    
066                            foldersTree.addChild(getFolderTree(path));
067                    }
068    
069                    foldersTree.addChild(getFolderTree(StringPool.BLANK));
070    
071                    return foldersTree;
072            }
073    
074            protected String getGroupPath(Group group) throws Exception {
075                    StringBundler sb = new StringBundler(5);
076    
077                    String name = group.getName();
078    
079                    if (group.isUser()) {
080                            User user = UserServiceUtil.getUserById(group.getClassPK());
081    
082                            name = user.getFullName();
083                    }
084                    else if (group.isOrganization()) {
085                            Organization organization =
086                                    OrganizationLocalServiceUtil.getOrganization(
087                                            group.getOrganizationId());
088    
089                            name = organization.getName();
090                    }
091    
092                    sb.append(name);
093                    sb.append(StringPool.SPACE);
094                    sb.append(StringPool.OPEN_BRACKET);
095                    sb.append(group.getGroupId());
096                    sb.append(StringPool.CLOSE_BRACKET);
097    
098                    return sb.toString();
099            }
100    
101    }