1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
13   */
14  
15  package com.liferay.portlet.documentlibrary.action;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.json.JSONArray;
19  import com.liferay.portal.kernel.json.JSONFactoryUtil;
20  import com.liferay.portal.kernel.json.JSONObject;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.struts.JSONAction;
23  import com.liferay.portlet.documentlibrary.model.DLFolder;
24  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
25  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
26  import com.liferay.portlet.documentlibrary.service.http.DLFolderJSONSerializer;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  import org.apache.struts.action.ActionForm;
35  import org.apache.struts.action.ActionMapping;
36  
37  /**
38   * <a href="GetFoldersAction.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Edward Shin
41   */
42  public class GetFoldersAction extends JSONAction {
43  
44      public String getJSON(
45              ActionMapping mapping, ActionForm form, HttpServletRequest request,
46              HttpServletResponse response)
47          throws Exception {
48  
49          long groupId = ParamUtil.getLong(request, "groupId");
50          long parentFolderId = ParamUtil.getLong(request, "folderId");
51  
52          List<DLFolder> folders = DLFolderLocalServiceUtil.getFolders(
53              groupId, parentFolderId);
54  
55          JSONArray jsonArray = toJSONArray(folders);
56  
57          return jsonArray.toString();
58      }
59  
60      protected JSONArray toJSONArray(List<DLFolder> folders)
61          throws SystemException {
62  
63          JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
64  
65          for (DLFolder folder : folders) {
66              jsonArray.put(toJSONObject(folder));
67          }
68  
69          return jsonArray;
70      }
71  
72      protected JSONObject toJSONObject(DLFolder folder) throws SystemException {
73          JSONObject jsonObj = DLFolderJSONSerializer.toJSONObject(folder);
74  
75          List<Long> subfolderIds = new ArrayList<Long>();
76  
77          subfolderIds.add(folder.getFolderId());
78  
79          DLFolderLocalServiceUtil.getSubfolderIds(
80              subfolderIds, folder.getGroupId(), folder.getFolderId());
81  
82          int subFoldersCount = subfolderIds.size() - 1;
83          int fileEntriesCount =
84              DLFileEntryLocalServiceUtil.getFileEntriesAndShortcutsCount(
85                  subfolderIds);
86  
87          jsonObj.put("subFoldersCount", subFoldersCount);
88          jsonObj.put("fileEntriesCount", fileEntriesCount);
89  
90          return jsonObj;
91      }
92  
93  }