1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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          String path = getRootPath() + webDavRequest.getPath();
37  
38          return new BaseResourceImpl(path, StringPool.BLANK, StringPool.BLANK);
39      }
40  
41      public List<Resource> getResources(WebDAVRequest webDavRequest)
42          throws WebDAVException {
43  
44          try {
45              long companyId = webDavRequest.getCompanyId();
46              long userId = webDavRequest.getUserId();
47  
48              List<Resource> resources = new ArrayList<Resource>();
49  
50              // Communities
51  
52              LinkedHashMap<String, Object> params =
53                  new LinkedHashMap<String, Object>();
54  
55              params.put("usersGroups", userId);
56  
57              List<Group> groups = GroupLocalServiceUtil.search(
58                  companyId, null, null, params, QueryUtil.ALL_POS,
59                  QueryUtil.ALL_POS);
60  
61              for (Group group : groups) {
62                  Resource resource = getResource(group);
63  
64                  resources.add(resource);
65              }
66  
67              // Organizations
68  
69              groups = GroupLocalServiceUtil.getUserOrganizationsGroups(
70                  userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
71  
72              for (Group group : groups) {
73                  Resource resource = getResource(group);
74  
75                  resources.add(resource);
76              }
77  
78              // User
79  
80              Group group = GroupLocalServiceUtil.getUserGroup(companyId, userId);
81  
82              Resource resource = getResource(group);
83  
84              resources.add(resource);
85  
86              return resources;
87          }
88          catch (Exception e) {
89              throw new WebDAVException(e);
90          }
91      }
92  
93      protected Resource getResource(Group group) throws WebDAVException {
94          try {
95              Company company = CompanyLocalServiceUtil.getCompanyById(
96                  group.getCompanyId());
97  
98              String webId = company.getWebId();
99  
100             String parentPath = getRootPath() + StringPool.SLASH + webId;
101 
102             String name = group.getFriendlyURL();
103 
104             name = name.substring(1, name.length());
105 
106             return new BaseResourceImpl(parentPath, name, name);
107         }
108         catch (Exception e) {
109             throw new WebDAVException(e);
110         }
111     }
112 
113 }