1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.documentlibrary.action;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.json.JSONArray;
24  import com.liferay.portal.kernel.json.JSONFactoryUtil;
25  import com.liferay.portal.kernel.json.JSONObject;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.struts.JSONAction;
28  import com.liferay.portlet.documentlibrary.model.DLFolder;
29  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
30  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
31  import com.liferay.portlet.documentlibrary.service.http.DLFolderJSONSerializer;
32  
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionMapping;
41  
42  /**
43   * <a href="GetFoldersAction.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Edward Shin
46   *
47   */
48  public class GetFoldersAction extends JSONAction {
49  
50      public String getJSON(
51              ActionMapping mapping, ActionForm form, HttpServletRequest request,
52              HttpServletResponse response)
53          throws Exception {
54  
55          long groupId = ParamUtil.getLong(request, "groupId");
56          long parentFolderId = ParamUtil.getLong(request, "folderId");
57  
58          List<DLFolder> folders = DLFolderLocalServiceUtil.getFolders(
59              groupId, parentFolderId);
60  
61          JSONArray jsonArray = toJSONArray(folders);
62  
63          return jsonArray.toString();
64      }
65  
66      protected JSONArray toJSONArray(List<DLFolder> folders)
67          throws SystemException {
68  
69          JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
70  
71          for (DLFolder folder : folders) {
72              jsonArray.put(toJSONObject(folder));
73          }
74  
75          return jsonArray;
76      }
77  
78      protected JSONObject toJSONObject(DLFolder folder) throws SystemException {
79          JSONObject jsonObj = DLFolderJSONSerializer.toJSONObject(folder);
80  
81          List<Long> subfolderIds = new ArrayList<Long>();
82  
83          subfolderIds.add(folder.getFolderId());
84  
85          DLFolderLocalServiceUtil.getSubfolderIds(
86              subfolderIds, folder.getGroupId(), folder.getFolderId());
87  
88          int subFoldersCount = subfolderIds.size() - 1;
89          int fileEntriesCount =
90              DLFileEntryLocalServiceUtil.getFileEntriesAndShortcutsCount(
91                  subfolderIds);
92  
93          jsonObj.put("subFoldersCount", subFoldersCount);
94          jsonObj.put("fileEntriesCount", fileEntriesCount);
95  
96          return jsonObj;
97      }
98  
99  }