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