1   /**
2    * Copyright (c) 2000-2008 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.util.StringPool;
26  import com.liferay.portal.model.Group;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.service.GroupLocalServiceUtil;
29  import com.liferay.portal.service.UserLocalServiceUtil;
30  import com.liferay.util.dao.hibernate.QueryUtil;
31  
32  import java.util.ArrayList;
33  import java.util.Iterator;
34  import java.util.LinkedHashMap;
35  import java.util.List;
36  
37  /**
38   * <a href="BaseWebDAVStorageImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public abstract class BaseWebDAVStorageImpl implements WebDAVStorage {
44  
45      public String getRootPath() {
46          return _rootPath;
47      }
48  
49      public void setRootPath(String rootPath) {
50          _rootPath = rootPath;
51      }
52  
53      public List getCommunities(WebDAVRequest webDavReq)
54          throws WebDAVException {
55  
56          try {
57              LinkedHashMap groupParams = new LinkedHashMap();
58  
59              groupParams.put("usersGroups", new Long(webDavReq.getUserId()));
60  
61              List communities = new ArrayList();
62  
63              Iterator itr = GroupLocalServiceUtil.search(
64                  webDavReq.getCompanyId(), null, null, groupParams,
65                  QueryUtil.ALL_POS, QueryUtil.ALL_POS).iterator();
66  
67              while (itr.hasNext()) {
68                  Group group = (Group)itr.next();
69  
70                  Resource resource = getResource(group);
71  
72                  communities.add(resource);
73              }
74  
75              Group group = GroupLocalServiceUtil.getUserGroup(
76                  webDavReq.getCompanyId(), webDavReq.getUserId());
77  
78              Resource resource = getResource(group);
79  
80              communities.add(resource);
81  
82              return communities;
83          }
84          catch (Exception e) {
85              throw new WebDAVException(e);
86          }
87      }
88  
89      public boolean isAvailable(WebDAVRequest webDavReq)
90          throws WebDAVException {
91  
92          if (getResource(webDavReq) == null) {
93              return false;
94          }
95          else {
96              return true;
97          }
98      }
99  
100     protected Resource getResource(Group group) throws WebDAVException {
101         try {
102             String parentPath =
103                 getRootPath() + StringPool.SLASH + group.getCompanyId();
104 
105             String name = group.getName();
106 
107             if (group.isUser()) {
108                 long userId = group.getClassPK();
109 
110                 User user = UserLocalServiceUtil.getUserById(userId);
111 
112                 name = user.getFullName();
113             }
114 
115             return new BaseResourceImpl(parentPath, group.getGroupId(), name);
116         }
117         catch (Exception e) {
118             throw new WebDAVException(e);
119         }
120     }
121 
122     private String _rootPath;
123 
124 }