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.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.Company;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.model.User;
027    import com.liferay.portal.service.CompanyLocalServiceUtil;
028    import com.liferay.portal.service.UserLocalServiceUtil;
029    
030    import java.util.ArrayList;
031    import java.util.List;
032    
033    /**
034     * @author Alexander Chow
035     */
036    public class CompanyWebDAVStorageImpl extends BaseWebDAVStorageImpl {
037    
038            public Resource getResource(WebDAVRequest webDavRequest) {
039                    String path = getRootPath() + webDavRequest.getPath();
040    
041                    return new BaseResourceImpl(path, StringPool.BLANK, StringPool.BLANK);
042            }
043    
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)
058                    throws Exception {
059    
060                    User user = UserLocalServiceUtil.getUserById(userId);
061    
062                    Company company = CompanyLocalServiceUtil.getCompanyById(
063                            user.getCompanyId());
064    
065                    List<Group> groups = WebDAVUtil.getGroups(user);
066    
067                    List<Resource> resources = new ArrayList<Resource>(groups.size());
068    
069                    for (Group group : groups) {
070                            String parentPath =
071                                    getRootPath() + StringPool.SLASH + company.getWebId();
072    
073                            String name = group.getFriendlyURL();
074    
075                            name = name.substring(1, name.length());
076    
077                            resources.add(new BaseResourceImpl(parentPath, name, name));
078                    }
079    
080                    return resources;
081            }
082    
083    }