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