001    /**
002     * Copyright (c) 2000-2013 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            @Override
037            public Resource getResource(WebDAVRequest webDAVRequest) {
038                    String path = getRootPath() + webDAVRequest.getPath();
039    
040                    return new BaseResourceImpl(path, StringPool.BLANK, StringPool.BLANK);
041            }
042    
043            @Override
044            public List<Resource> getResources(WebDAVRequest webDAVRequest)
045                    throws WebDAVException {
046    
047                    try {
048                            long userId = webDAVRequest.getUserId();
049    
050                            return getResources(userId);
051                    }
052                    catch (Exception e) {
053                            throw new WebDAVException(e);
054                    }
055            }
056    
057            protected List<Resource> getResources(long userId) throws Exception {
058                    User user = UserLocalServiceUtil.getUserById(userId);
059    
060                    List<Group> groups = WebDAVUtil.getGroups(user);
061    
062                    List<Resource> resources = new ArrayList<Resource>(groups.size());
063    
064                    for (Group group : groups) {
065                            String parentPath = getRootPath();
066    
067                            String name = group.getFriendlyURL();
068    
069                            name = name.substring(1);
070    
071                            resources.add(new BaseResourceImpl(parentPath, name, name));
072                    }
073    
074                    return resources;
075            }
076    
077    }