001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.action;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.servlet.SessionMessages;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.LocalizationUtil;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.GroupLocalServiceUtil;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.service.ServiceContextFactory;
030    import com.liferay.portal.struts.PortletAction;
031    import com.liferay.portal.theme.ThemeDisplay;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.portal.util.PortletKeys;
034    import com.liferay.portal.util.WebKeys;
035    import com.liferay.portlet.documentlibrary.DuplicateFileEntryTypeException;
036    import com.liferay.portlet.documentlibrary.NoSuchFileEntryTypeException;
037    import com.liferay.portlet.documentlibrary.NoSuchMetadataSetException;
038    import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
039    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
040    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
041    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeServiceUtil;
042    import com.liferay.portlet.documentlibrary.util.DLUtil;
043    import com.liferay.portlet.dynamicdatamapping.NoSuchStructureException;
044    import com.liferay.portlet.dynamicdatamapping.RequiredStructureException;
045    import com.liferay.portlet.dynamicdatamapping.StructureDuplicateElementException;
046    import com.liferay.portlet.dynamicdatamapping.StructureNameException;
047    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
048    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
049    
050    import java.util.Locale;
051    import java.util.Map;
052    
053    import javax.portlet.ActionRequest;
054    import javax.portlet.ActionResponse;
055    import javax.portlet.PortletConfig;
056    import javax.portlet.PortletRequest;
057    import javax.portlet.RenderRequest;
058    import javax.portlet.RenderResponse;
059    
060    import org.apache.struts.action.ActionForm;
061    import org.apache.struts.action.ActionForward;
062    import org.apache.struts.action.ActionMapping;
063    
064    /**
065     * @author Alexander Chow
066     * @author Sergio Gonz??lez
067     */
068    public class EditFileEntryTypeAction extends PortletAction {
069    
070            @Override
071            public void processAction(
072                            ActionMapping actionMapping, ActionForm actionForm,
073                            PortletConfig portletConfig, ActionRequest actionRequest,
074                            ActionResponse actionResponse)
075                    throws Exception {
076    
077                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
078    
079                    try {
080                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
081                                    updateFileEntryType(actionRequest, actionResponse);
082                            }
083                            else if (cmd.equals(Constants.DELETE)) {
084                                    deleteFileEntryType(actionRequest, actionResponse);
085                            }
086                            else if (cmd.equals(Constants.SUBSCRIBE)) {
087                                    subscribeFileEntryType(actionRequest);
088                            }
089                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
090                                    unsubscribeFileEntryType(actionRequest);
091                            }
092    
093                            if (SessionErrors.isEmpty(actionRequest)) {
094                                    LiferayPortletConfig liferayPortletConfig =
095                                            (LiferayPortletConfig)portletConfig;
096    
097                                    SessionMessages.add(
098                                            actionRequest,
099                                            liferayPortletConfig.getPortletId() +
100                                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
101                                            PortletKeys.DOCUMENT_LIBRARY);
102                            }
103    
104                            sendRedirect(actionRequest, actionResponse);
105                    }
106                    catch (Exception e) {
107                            if (e instanceof DuplicateFileEntryTypeException ||
108                                    e instanceof NoSuchMetadataSetException ||
109                                    e instanceof StructureDuplicateElementException ||
110                                    e instanceof StructureNameException) {
111    
112                                    SessionErrors.add(actionRequest, e.getClass());
113                            }
114                            else if (e instanceof NoSuchFileEntryTypeException ||
115                                             e instanceof NoSuchStructureException ||
116                                             e instanceof PrincipalException) {
117    
118                                    SessionErrors.add(actionRequest, e.getClass());
119    
120                                    setForward(actionRequest, "portlet.document_library.error");
121                            }
122                            else if (e instanceof RequiredStructureException) {
123                                    SessionErrors.add(actionRequest, e.getClass());
124    
125                                    sendRedirect(actionRequest, actionResponse);
126                            }
127                            else {
128                                    throw e;
129                            }
130                    }
131            }
132    
133            @Override
134            public ActionForward render(
135                            ActionMapping actionMapping, ActionForm actionForm,
136                            PortletConfig portletConfig, RenderRequest renderRequest,
137                            RenderResponse renderResponse)
138                    throws Exception {
139    
140                    DLFileEntryType dlFileEntryType = null;
141    
142                    try {
143                            long fileEntryTypeId = ParamUtil.getLong(
144                                    renderRequest, "fileEntryTypeId");
145    
146                            if (fileEntryTypeId > 0) {
147                                    dlFileEntryType = DLFileEntryTypeServiceUtil.getFileEntryType(
148                                            fileEntryTypeId);
149    
150                                    renderRequest.setAttribute(
151                                            WebKeys.DOCUMENT_LIBRARY_FILE_ENTRY_TYPE, dlFileEntryType);
152    
153                                    DDMStructure ddmStructure =
154                                            DDMStructureLocalServiceUtil.fetchStructure(
155                                                    dlFileEntryType.getGroupId(),
156                                                    PortalUtil.getClassNameId(DLFileEntryMetadata.class),
157                                                    DLUtil.getDDMStructureKey(dlFileEntryType));
158    
159                                    if (ddmStructure == null) {
160                                            ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(
161                                                    dlFileEntryType.getGroupId(),
162                                                    PortalUtil.getClassNameId(DLFileEntryMetadata.class),
163                                                    DLUtil.getDeprecatedDDMStructureKey(dlFileEntryType));
164                                    }
165    
166                                    renderRequest.setAttribute(
167                                            WebKeys.DYNAMIC_DATA_MAPPING_STRUCTURE, ddmStructure);
168                            }
169                    }
170                    catch (Exception e) {
171                            if (e instanceof NoSuchFileEntryTypeException ||
172                                    e instanceof PrincipalException) {
173    
174                                    SessionErrors.add(renderRequest, e.getClass());
175    
176                                    return actionMapping.findForward(
177                                            "portlet.document_library.error");
178                            }
179                            else {
180                                    throw e;
181                            }
182                    }
183    
184                    return actionMapping.findForward(
185                            getForward(
186                                    renderRequest,
187                                    "portlet.document_library.edit_file_entry_type"));
188            }
189    
190            protected void deleteFileEntryType(
191                            ActionRequest actionRequest, ActionResponse actionResponse)
192                    throws Exception {
193    
194                    long fileEntryTypeId = ParamUtil.getLong(
195                            actionRequest, "fileEntryTypeId");
196    
197                    DLFileEntryTypeServiceUtil.deleteFileEntryType(fileEntryTypeId);
198            }
199    
200            protected long[] getLongArray(PortletRequest portletRequest, String name) {
201                    String value = portletRequest.getParameter(name);
202    
203                    if (value == null) {
204                            return null;
205                    }
206    
207                    return StringUtil.split(GetterUtil.getString(value), 0L);
208            }
209    
210            protected void subscribeFileEntryType(ActionRequest actionRequest)
211                    throws Exception {
212    
213                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
214                            WebKeys.THEME_DISPLAY);
215    
216                    long fileEntryTypeId = ParamUtil.getLong(
217                            actionRequest, "fileEntryTypeId");
218    
219                    DLAppServiceUtil.subscribeFileEntryType(
220                            themeDisplay.getScopeGroupId(), fileEntryTypeId);
221            }
222    
223            protected void unsubscribeFileEntryType(ActionRequest actionRequest)
224                    throws Exception {
225    
226                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
227                            WebKeys.THEME_DISPLAY);
228    
229                    long fileEntryTypeId = ParamUtil.getLong(
230                            actionRequest, "fileEntryTypeId");
231    
232                    DLAppServiceUtil.unsubscribeFileEntryType(
233                            themeDisplay.getScopeGroupId(), fileEntryTypeId);
234            }
235    
236            protected void updateFileEntryType(
237                            ActionRequest actionRequest, ActionResponse actionResponse)
238                    throws Exception {
239    
240                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
241                            WebKeys.THEME_DISPLAY);
242    
243                    long fileEntryTypeId = ParamUtil.getLong(
244                            actionRequest, "fileEntryTypeId");
245    
246                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
247                            actionRequest, "name");
248                    Map<Locale, String> descriptionMap =
249                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
250    
251                    long[] ddmStructureIds = getLongArray(
252                            actionRequest, "ddmStructuresSearchContainerPrimaryKeys");
253    
254                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
255                            DLFileEntryType.class.getName(), actionRequest);
256    
257                    if (fileEntryTypeId <= 0) {
258    
259                            // Add file entry type
260    
261                            long groupId = themeDisplay.getScopeGroupId();
262    
263                            Group scopeGroup = GroupLocalServiceUtil.getGroup(groupId);
264    
265                            if (scopeGroup.isLayout()) {
266                                    groupId = scopeGroup.getParentGroupId();
267                            }
268    
269                            DLFileEntryTypeServiceUtil.addFileEntryType(
270                                    groupId, null, nameMap, descriptionMap, ddmStructureIds,
271                                    serviceContext);
272                    }
273                    else {
274    
275                            // Update file entry type
276    
277                            DLFileEntryTypeServiceUtil.updateFileEntryType(
278                                    fileEntryTypeId, nameMap, descriptionMap, ddmStructureIds,
279                                    serviceContext);
280                    }
281            }
282    
283    }