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.webdav;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.webdav.BaseResourceImpl;
019    import com.liferay.portal.kernel.webdav.BaseWebDAVStorageImpl;
020    import com.liferay.portal.kernel.webdav.Resource;
021    import com.liferay.portal.kernel.webdav.WebDAVException;
022    import com.liferay.portal.kernel.webdav.WebDAVRequest;
023    import com.liferay.portal.kernel.webdav.WebDAVUtil;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.service.UserLocalServiceUtil;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    /**
032     * @author Alexander Chow
033     */
034    public class CompanyWebDAVStorageImpl extends BaseWebDAVStorageImpl {
035    
036            public Resource getResource(WebDAVRequest webDavRequest) {
037                    String path = getRootPath() + webDavRequest.getPath();
038    
039                    return new BaseResourceImpl(path, StringPool.BLANK, StringPool.BLANK);
040            }
041    
042            public List<Resource> getResources(WebDAVRequest webDavRequest)
043                    throws WebDAVException {
044    
045                    try {
046                            long userId = webDavRequest.getUserId();
047    
048                            return getResources(userId);
049                    }
050                    catch (Exception e) {
051                            throw new WebDAVException(e);
052                    }
053            }
054    
055            protected List<Resource> getResources(long userId) throws Exception {
056                    User user = UserLocalServiceUtil.getUserById(userId);
057    
058                    List<Group> groups = WebDAVUtil.getGroups(user);
059    
060                    List<Resource> resources = new ArrayList<Resource>(groups.size());
061    
062                    for (Group group : groups) {
063                            String parentPath = getRootPath();
064    
065                            String name = group.getFriendlyURL();
066    
067                            name = name.substring(1);
068    
069                            resources.add(new BaseResourceImpl(parentPath, name, name));
070                    }
071    
072                    return resources;
073            }
074    
075    }