1
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
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 }