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  public class CompanyWebDAVStorageImpl extends BaseWebDAVStorageImpl {
42  
43      public Resource getResource(WebDAVRequest webDavRequest)
44          throws WebDAVException {
45  
46          String path = getRootPath() + webDavRequest.getPath();
47  
48          return new BaseResourceImpl(
49              path, StringPool.BLANK, WebDAVUtil.getWebId(path));
50      }
51  
52      public List<Resource> getResources(WebDAVRequest webDavRequest)
53          throws WebDAVException {
54  
55          try {
56              LinkedHashMap<String, Object> groupParams =
57                  new LinkedHashMap<String, Object>();
58  
59              groupParams.put("usersGroups", new Long(webDavRequest.getUserId()));
60  
61              List<Resource> resources = new ArrayList<Resource>();
62  
63              List<Group> groups = GroupLocalServiceUtil.search(
64                  webDavRequest.getCompanyId(), null, null, groupParams,
65                  QueryUtil.ALL_POS, QueryUtil.ALL_POS);
66  
67              for (Group group : groups) {
68                  Resource resource = getResource(group);
69  
70                  resources.add(resource);
71              }
72  
73              Group group = GroupLocalServiceUtil.getUserGroup(
74                  webDavRequest.getCompanyId(), webDavRequest.getUserId());
75  
76              Resource resource = getResource(group);
77  
78              resources.add(resource);
79  
80              return resources;
81          }
82          catch (Exception e) {
83              throw new WebDAVException(e);
84          }
85      }
86  
87      protected Resource getResource(Group group) throws WebDAVException {
88          try {
89              Company company = CompanyLocalServiceUtil.getCompanyById(
90                  group.getCompanyId());
91  
92              String webId = company.getWebId();
93  
94              String parentPath = getRootPath() + StringPool.SLASH + webId;
95  
96              String name = group.getFriendlyURL();
97  
98              name = name.substring(1, name.length());
99  
100             return new BaseResourceImpl(parentPath, name, name);
101         }
102         catch (Exception e) {
103             throw new WebDAVException(e);
104         }
105     }
106 
107 }