001
014
015 package com.liferay.portlet.layoutsadmin.action;
016
017 import com.liferay.portal.NoSuchGroupException;
018 import com.liferay.portal.kernel.lar.ExportImportHelperUtil;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.servlet.SessionErrors;
022 import com.liferay.portal.kernel.util.Constants;
023 import com.liferay.portal.kernel.util.DateRange;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.ParamUtil;
026 import com.liferay.portal.kernel.util.UniqueList;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.model.Layout;
029 import com.liferay.portal.security.auth.PrincipalException;
030 import com.liferay.portal.service.LayoutLocalServiceUtil;
031 import com.liferay.portal.service.LayoutServiceUtil;
032 import com.liferay.portal.struts.PortletAction;
033 import com.liferay.portlet.sites.action.ActionUtil;
034
035 import java.util.Date;
036 import java.util.List;
037 import java.util.Map;
038
039 import javax.portlet.ActionRequest;
040 import javax.portlet.ActionResponse;
041 import javax.portlet.PortletConfig;
042 import javax.portlet.PortletContext;
043 import javax.portlet.PortletRequest;
044 import javax.portlet.PortletRequestDispatcher;
045 import javax.portlet.RenderRequest;
046 import javax.portlet.RenderResponse;
047 import javax.portlet.ResourceRequest;
048 import javax.portlet.ResourceResponse;
049
050 import org.apache.struts.action.ActionForm;
051 import org.apache.struts.action.ActionForward;
052 import org.apache.struts.action.ActionMapping;
053
054
058 public class ExportLayoutsAction extends PortletAction {
059
060 @Override
061 public void processAction(
062 ActionMapping actionMapping, ActionForm actionForm,
063 PortletConfig portletConfig, ActionRequest actionRequest,
064 ActionResponse actionResponse)
065 throws Exception {
066
067 hideDefaultSuccessMessage(actionRequest);
068
069 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
070
071 try {
072 String fileName = ParamUtil.getString(
073 actionRequest, "exportFileName");
074 long groupId = ParamUtil.getLong(actionRequest, "groupId");
075 boolean privateLayout = ParamUtil.getBoolean(
076 actionRequest, "privateLayout");
077 long[] layoutIds = getLayoutIds(actionRequest);
078 DateRange dateRange = ExportImportHelperUtil.getDateRange(
079 actionRequest, groupId, privateLayout, 0, null);
080
081 Date startDate = dateRange.getStartDate();
082 Date endDate = dateRange.getEndDate();
083
084 if (Validator.isNotNull(cmd)) {
085 LayoutServiceUtil.exportLayoutsAsFileInBackground(
086 fileName, groupId, privateLayout, layoutIds,
087 actionRequest.getParameterMap(), startDate, endDate,
088 fileName);
089
090 String redirect = ParamUtil.getString(
091 actionRequest, "redirect");
092
093 sendRedirect(actionRequest, actionResponse, redirect);
094 }
095 else {
096 if (startDate != null) {
097 actionResponse.setRenderParameter(
098 "startDate", String.valueOf(startDate.getTime()));
099 }
100
101 if (endDate != null) {
102 actionResponse.setRenderParameter(
103 "endDate", String.valueOf(endDate.getTime()));
104 }
105 }
106 }
107 catch (Exception e) {
108 _log.error(e, e);
109
110 SessionErrors.add(actionRequest, e.getClass());
111
112 String pagesRedirect = ParamUtil.getString(
113 actionRequest, "pagesRedirect");
114
115 sendRedirect(actionRequest, actionResponse, pagesRedirect);
116 }
117 }
118
119 @Override
120 public ActionForward render(
121 ActionMapping actionMapping, ActionForm actionForm,
122 PortletConfig portletConfig, RenderRequest renderRequest,
123 RenderResponse renderResponse)
124 throws Exception {
125
126 try {
127 ActionUtil.getGroup(renderRequest);
128 }
129 catch (Exception e) {
130 if (e instanceof NoSuchGroupException ||
131 e instanceof PrincipalException) {
132
133 SessionErrors.add(renderRequest, e.getClass());
134
135 return actionMapping.findForward("portlet.layouts_admin.error");
136 }
137 else {
138 throw e;
139 }
140 }
141
142 return actionMapping.findForward(
143 getForward(renderRequest, "portlet.layouts_admin.export_layouts"));
144 }
145
146 @Override
147 public void serveResource(
148 ActionMapping actionMapping, ActionForm actionForm,
149 PortletConfig portletConfig, ResourceRequest resourceRequest,
150 ResourceResponse resourceResponse)
151 throws Exception {
152
153 PortletContext portletContext = portletConfig.getPortletContext();
154
155 PortletRequestDispatcher portletRequestDispatcher =
156 portletContext.getRequestDispatcher(
157 "/html/portlet/layouts_admin/export_layouts_processes.jsp");
158
159 portletRequestDispatcher.include(resourceRequest, resourceResponse);
160 }
161
162 protected long[] getLayoutIds(PortletRequest portletRequest)
163 throws Exception {
164
165 List<Layout> layouts = new UniqueList<Layout>();
166
167 Map<Long, Boolean> layoutIdMap = ExportImportHelperUtil.getLayoutIdMap(
168 portletRequest);
169
170 for (Map.Entry<Long, Boolean> entry : layoutIdMap.entrySet()) {
171 long plid = GetterUtil.getLong(String.valueOf(entry.getKey()));
172 boolean includeChildren = entry.getValue();
173
174 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
175
176 if (!layouts.contains(layout)) {
177 layouts.add(layout);
178 }
179
180 if (includeChildren) {
181 layouts.addAll(layout.getAllChildren());
182 }
183 }
184
185 return ExportImportHelperUtil.getLayoutIds(layouts);
186 }
187
188 private static Log _log = LogFactoryUtil.getLog(ExportLayoutsAction.class);
189
190 }