001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.documentlibrary.action;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.ParamUtil;
026: import com.liferay.portal.struts.JSONAction;
027: import com.liferay.portlet.documentlibrary.model.DLFolder;
028: import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
029: import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
030: import com.liferay.portlet.documentlibrary.service.http.DLFolderJSONSerializer;
031: import com.liferay.util.JSONUtil;
032:
033: import java.util.ArrayList;
034: import java.util.List;
035:
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.http.HttpServletResponse;
038:
039: import org.apache.struts.action.ActionForm;
040: import org.apache.struts.action.ActionMapping;
041:
042: import org.json.JSONArray;
043: import org.json.JSONObject;
044:
045: /**
046: * <a href="GetFoldersAction.java.html"><b><i>View Source</i></b></a>
047: *
048: * @author Edward Shin
049: *
050: */
051: public class GetFoldersAction extends JSONAction {
052:
053: public String getJSON(ActionMapping mapping, ActionForm form,
054: HttpServletRequest req, HttpServletResponse res)
055: throws Exception {
056:
057: long groupId = ParamUtil.getLong(req, "groupId");
058: long parentFolderId = ParamUtil.getLong(req, "folderId");
059:
060: List folders = DLFolderLocalServiceUtil.getFolders(groupId,
061: parentFolderId);
062:
063: JSONArray jsonArray = toJSONArray(folders);
064:
065: return jsonArray.toString();
066: }
067:
068: protected JSONArray toJSONArray(List folders)
069: throws PortalException, SystemException {
070:
071: JSONArray jsonArray = new JSONArray();
072:
073: for (int i = 0; i < folders.size(); i++) {
074: DLFolder folder = (DLFolder) folders.get(i);
075:
076: jsonArray.put(toJSONObject(folder));
077: }
078:
079: return jsonArray;
080: }
081:
082: protected JSONObject toJSONObject(DLFolder folder)
083: throws PortalException, SystemException {
084:
085: JSONObject jsonObj = DLFolderJSONSerializer
086: .toJSONObject(folder);
087:
088: List subfolderIds = new ArrayList();
089:
090: subfolderIds.add(new Long(folder.getFolderId()));
091:
092: DLFolderLocalServiceUtil.getSubfolderIds(subfolderIds, folder
093: .getGroupId(), folder.getFolderId());
094:
095: int subFoldersCount = subfolderIds.size() - 1;
096: int fileEntriesCount = DLFileEntryLocalServiceUtil
097: .getFileEntriesAndShortcutsCount(subfolderIds);
098:
099: JSONUtil.put(jsonObj, "subFoldersCount", subFoldersCount);
100: JSONUtil.put(jsonObj, "fileEntriesCount", fileEntriesCount);
101:
102: return jsonObj;
103: }
104:
105: }
|