1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.webdav;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryUtil;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.model.Company;
20  import com.liferay.portal.model.Group;
21  import com.liferay.portal.service.CompanyLocalServiceUtil;
22  import com.liferay.portal.service.GroupLocalServiceUtil;
23  
24  import java.util.ArrayList;
25  import java.util.LinkedHashMap;
26  import java.util.List;
27  
28  /**
29   * <a href="CompanyWebDAVStorageImpl.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Alexander Chow
32   */
33  public class CompanyWebDAVStorageImpl extends BaseWebDAVStorageImpl {
34  
35      public Resource getResource(WebDAVRequest webDavRequest)
36          throws WebDAVException {
37  
38          String path = getRootPath() + webDavRequest.getPath();
39  
40          return new BaseResourceImpl(
41              path, StringPool.BLANK, WebDAVUtil.getWebId(path));
42      }
43  
44      public List<Resource> getResources(WebDAVRequest webDavRequest)
45          throws WebDAVException {
46  
47          try {
48              long companyId = webDavRequest.getCompanyId();
49              long userId = webDavRequest.getUserId();
50  
51              List<Resource> resources = new ArrayList<Resource>();
52  
53              // Communities
54  
55              LinkedHashMap<String, Object> params =
56                  new LinkedHashMap<String, Object>();
57  
58              params.put("usersGroups", userId);
59  
60              List<Group> groups = GroupLocalServiceUtil.search(
61                  companyId, null, null, params, QueryUtil.ALL_POS,
62                  QueryUtil.ALL_POS);
63  
64              for (Group group : groups) {
65                  Resource resource = getResource(group);
66  
67                  resources.add(resource);
68              }
69  
70              // Organizations
71  
72              groups = GroupLocalServiceUtil.getUserOrganizationsGroups(
73                  userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
74  
75              for (Group group : groups) {
76                  Resource resource = getResource(group);
77  
78                  resources.add(resource);
79              }
80  
81              // User
82  
83              Group group = GroupLocalServiceUtil.getUserGroup(companyId, userId);
84  
85              Resource resource = getResource(group);
86  
87              resources.add(resource);
88  
89              return resources;
90          }
91          catch (Exception e) {
92              throw new WebDAVException(e);
93          }
94      }
95  
96      protected Resource getResource(Group group) throws WebDAVException {
97          try {
98              Company company = CompanyLocalServiceUtil.getCompanyById(
99                  group.getCompanyId());
100 
101             String webId = company.getWebId();
102 
103             String parentPath = getRootPath() + StringPool.SLASH + webId;
104 
105             String name = group.getFriendlyURL();
106 
107             name = name.substring(1, name.length());
108 
109             return new BaseResourceImpl(parentPath, name, name);
110         }
111         catch (Exception e) {
112             throw new WebDAVException(e);
113         }
114     }
115 
116 }