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