1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.webdav;
24  
25  import com.liferay.portal.kernel.dao.orm.QueryUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.model.Company;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portal.service.CompanyLocalServiceUtil;
30  import com.liferay.portal.service.GroupLocalServiceUtil;
31  
32  import java.util.ArrayList;
33  import java.util.LinkedHashMap;
34  import java.util.List;
35  
36  /**
37   * <a href="CompanyWebDAVStorageImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Alexander Chow
40   *
41   */
42  public class CompanyWebDAVStorageImpl extends BaseWebDAVStorageImpl {
43  
44      public Resource getResource(WebDAVRequest webDavRequest)
45          throws WebDAVException {
46  
47          String path = getRootPath() + webDavRequest.getPath();
48  
49          return new BaseResourceImpl(
50              path, StringPool.BLANK, WebDAVUtil.getWebId(path));
51      }
52  
53      public List<Resource> getResources(WebDAVRequest webDavRequest)
54          throws WebDAVException {
55  
56          try {
57              LinkedHashMap<String, Object> groupParams =
58                  new LinkedHashMap<String, Object>();
59  
60              groupParams.put("usersGroups", new Long(webDavRequest.getUserId()));
61  
62              List<Resource> resources = new ArrayList<Resource>();
63  
64              List<Group> groups = GroupLocalServiceUtil.search(
65                  webDavRequest.getCompanyId(), null, null, groupParams,
66                  QueryUtil.ALL_POS, QueryUtil.ALL_POS);
67  
68              for (Group group : groups) {
69                  Resource resource = getResource(group);
70  
71                  resources.add(resource);
72              }
73  
74              Group group = GroupLocalServiceUtil.getUserGroup(
75                  webDavRequest.getCompanyId(), webDavRequest.getUserId());
76  
77              Resource resource = getResource(group);
78  
79              resources.add(resource);
80  
81              return resources;
82          }
83          catch (Exception e) {
84              throw new WebDAVException(e);
85          }
86      }
87  
88      protected Resource getResource(Group group) throws WebDAVException {
89          try {
90              Company company = CompanyLocalServiceUtil.getCompanyById(
91                  group.getCompanyId());
92  
93              String webId = company.getWebId();
94  
95              String parentPath = getRootPath() + StringPool.SLASH + webId;
96  
97              String name = group.getFriendlyURL();
98  
99              name = name.substring(1, name.length());
100 
101             return new BaseResourceImpl(parentPath, name, name);
102         }
103         catch (Exception e) {
104             throw new WebDAVException(e);
105         }
106     }
107 
108 }