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.kernel.webdav.BaseResourceImpl;
20  import com.liferay.portal.kernel.webdav.BaseWebDAVStorageImpl;
21  import com.liferay.portal.kernel.webdav.Resource;
22  import com.liferay.portal.kernel.webdav.WebDAVException;
23  import com.liferay.portal.kernel.webdav.WebDAVRequest;
24  import com.liferay.portal.model.Company;
25  import com.liferay.portal.model.Group;
26  import com.liferay.portal.service.CompanyLocalServiceUtil;
27  import com.liferay.portal.service.GroupLocalServiceUtil;
28  
29  import java.util.ArrayList;
30  import java.util.LinkedHashMap;
31  import java.util.List;
32  
33  /**
34   * <a href="CompanyWebDAVStorageImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Alexander Chow
37   */
38  public class CompanyWebDAVStorageImpl extends BaseWebDAVStorageImpl {
39  
40      public Resource getResource(WebDAVRequest webDavRequest) {
41          String path = getRootPath() + webDavRequest.getPath();
42  
43          return new BaseResourceImpl(path, StringPool.BLANK, StringPool.BLANK);
44      }
45  
46      public List<Resource> getResources(WebDAVRequest webDavRequest)
47          throws WebDAVException {
48  
49          try {
50              long companyId = webDavRequest.getCompanyId();
51              long userId = webDavRequest.getUserId();
52  
53              List<Resource> resources = new ArrayList<Resource>();
54  
55              // Communities
56  
57              LinkedHashMap<String, Object> params =
58                  new LinkedHashMap<String, Object>();
59  
60              params.put("usersGroups", userId);
61  
62              List<Group> groups = GroupLocalServiceUtil.search(
63                  companyId, null, null, params, QueryUtil.ALL_POS,
64                  QueryUtil.ALL_POS);
65  
66              for (Group group : groups) {
67                  Resource resource = getResource(group);
68  
69                  resources.add(resource);
70              }
71  
72              // Organizations
73  
74              groups = GroupLocalServiceUtil.getUserOrganizationsGroups(
75                  userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
76  
77              for (Group group : groups) {
78                  Resource resource = getResource(group);
79  
80                  resources.add(resource);
81              }
82  
83              // User
84  
85              Group group = GroupLocalServiceUtil.getUserGroup(companyId, userId);
86  
87              Resource resource = getResource(group);
88  
89              resources.add(resource);
90  
91              return resources;
92          }
93          catch (Exception e) {
94              throw new WebDAVException(e);
95          }
96      }
97  
98      protected Resource getResource(Group group) throws WebDAVException {
99          try {
100             Company company = CompanyLocalServiceUtil.getCompanyById(
101                 group.getCompanyId());
102 
103             String webId = company.getWebId();
104 
105             String parentPath = getRootPath() + StringPool.SLASH + webId;
106 
107             String name = group.getFriendlyURL();
108 
109             name = name.substring(1, name.length());
110 
111             return new BaseResourceImpl(parentPath, name, name);
112         }
113         catch (Exception e) {
114             throw new WebDAVException(e);
115         }
116     }
117 
118 }