001    /**
002     * Copyright (c) 2000-2012 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.ParamUtil;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.service.GroupLocalServiceUtil;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.service.ServiceContextFactory;
029    import com.liferay.portal.struts.PortletAction;
030    import com.liferay.portal.theme.ThemeDisplay;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portal.util.PortletKeys;
033    import com.liferay.portal.util.WebKeys;
034    import com.liferay.portlet.documentlibrary.DuplicateFileEntryTypeException;
035    import com.liferay.portlet.documentlibrary.NoSuchFileEntryTypeException;
036    import com.liferay.portlet.documentlibrary.NoSuchMetadataSetException;
037    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
038    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
039    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
040    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeServiceUtil;
041    import com.liferay.portlet.documentlibrary.util.DLUtil;
042    import com.liferay.portlet.dynamicdatamapping.NoSuchStructureException;
043    import com.liferay.portlet.dynamicdatamapping.RequiredStructureException;
044    import com.liferay.portlet.dynamicdatamapping.StructureDuplicateElementException;
045    import com.liferay.portlet.dynamicdatamapping.StructureNameException;
046    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
047    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
048    
049    import javax.portlet.ActionRequest;
050    import javax.portlet.ActionResponse;
051    import javax.portlet.PortletConfig;
052    import javax.portlet.PortletRequest;
053    import javax.portlet.RenderRequest;
054    import javax.portlet.RenderResponse;
055    
056    import org.apache.struts.action.ActionForm;
057    import org.apache.struts.action.ActionForward;
058    import org.apache.struts.action.ActionMapping;
059    
060    /**
061     * @author Alexander Chow
062     * @author Sergio González
063     */
064    public class EditFileEntryTypeAction extends PortletAction {
065    
066            @Override
067            public void processAction(
068                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
069                            ActionRequest actionRequest, ActionResponse actionResponse)
070                    throws Exception {
071    
072                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
073    
074                    try {
075                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
076                                    updateFileEntryType(actionRequest, actionResponse);
077                            }
078                            else if (cmd.equals(Constants.DELETE)) {
079                                    deleteFileEntryType(actionRequest, actionResponse);
080                            }
081                            else if (cmd.equals(Constants.SUBSCRIBE)) {
082                                    subscribeFileEntryType(actionRequest);
083                            }
084                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
085                                    unsubscribeFileEntryType(actionRequest);
086                            }
087    
088                            if (SessionErrors.isEmpty(actionRequest)) {
089                                    LiferayPortletConfig liferayPortletConfig =
090                                            (LiferayPortletConfig)portletConfig;
091    
092                                    SessionMessages.add(
093                                            actionRequest,
094                                            liferayPortletConfig.getPortletId() +
095                                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
096                                            PortletKeys.DOCUMENT_LIBRARY);
097                            }
098    
099                            sendRedirect(actionRequest, actionResponse);
100                    }
101                    catch (Exception e) {
102                            if (e instanceof DuplicateFileEntryTypeException ||
103                                    e instanceof NoSuchMetadataSetException ||
104                                    e instanceof StructureDuplicateElementException ||
105                                    e instanceof StructureNameException) {
106    
107                                    SessionErrors.add(actionRequest, e.getClass());
108                            }
109                            else if (e instanceof NoSuchFileEntryTypeException ||
110                                             e instanceof NoSuchStructureException ||
111                                             e instanceof PrincipalException) {
112    
113                                    SessionErrors.add(actionRequest, e.getClass());
114    
115                                    setForward(actionRequest, "portlet.document_library.error");
116                            }
117                            else if (e instanceof RequiredStructureException) {
118                                    SessionErrors.add(actionRequest, e.getClass());
119    
120                                    sendRedirect(actionRequest, actionResponse);
121                            }
122                            else {
123                                    throw e;
124                            }
125                    }
126            }
127    
128            @Override
129            public ActionForward render(
130                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
131                            RenderRequest renderRequest, RenderResponse renderResponse)
132                    throws Exception {
133    
134                    DLFileEntryType dlFileEntryType = null;
135    
136                    try {
137                            long fileEntryTypeId = ParamUtil.getLong(
138                                    renderRequest, "fileEntryTypeId");
139    
140                            if (fileEntryTypeId > 0) {
141                                    dlFileEntryType = DLFileEntryTypeServiceUtil.getFileEntryType(
142                                            fileEntryTypeId);
143    
144                                    renderRequest.setAttribute(
145                                            WebKeys.DOCUMENT_LIBRARY_FILE_ENTRY_TYPE, dlFileEntryType);
146    
147                                    DDMStructure ddmStructure =
148                                            DDMStructureLocalServiceUtil.fetchStructure(
149                                                    dlFileEntryType.getGroupId(),
150                                                    PortalUtil.getClassNameId(DLFileEntry.class),
151                                                    DLUtil.getDDMStructureKey(dlFileEntryType));
152    
153                                    if (ddmStructure == null) {
154                                            ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(
155                                                    dlFileEntryType.getGroupId(),
156                                                    PortalUtil.getClassNameId(DLFileEntry.class),
157                                                    DLUtil.getDeprecatedDDMStructureKey(dlFileEntryType));
158                                    }
159    
160                                    renderRequest.setAttribute(
161                                            WebKeys.DYNAMIC_DATA_MAPPING_STRUCTURE, ddmStructure);
162                            }
163                    }
164                    catch (Exception e) {
165                            if (e instanceof NoSuchFileEntryTypeException ||
166                                    e instanceof PrincipalException) {
167    
168                                    SessionErrors.add(renderRequest, e.getClass());
169    
170                                    return mapping.findForward("portlet.document_library.error");
171                            }
172                            else {
173                                    throw e;
174                            }
175                    }
176    
177                    return mapping.findForward(
178                            getForward(
179                                    renderRequest,
180                                    "portlet.document_library.edit_file_entry_type"));
181            }
182    
183            protected void deleteFileEntryType(
184                            ActionRequest actionRequest, ActionResponse actionResponse)
185                    throws Exception {
186    
187                    long fileEntryTypeId = ParamUtil.getLong(
188                            actionRequest, "fileEntryTypeId");
189    
190                    DLFileEntryTypeServiceUtil.deleteFileEntryType(fileEntryTypeId);
191            }
192    
193            protected long[] getLongArray(PortletRequest portletRequest, String name) {
194                    String value = portletRequest.getParameter(name);
195    
196                    if (value == null) {
197                            return null;
198                    }
199    
200                    return StringUtil.split(GetterUtil.getString(value), 0L);
201            }
202    
203            protected void subscribeFileEntryType(ActionRequest actionRequest)
204                    throws Exception {
205    
206                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
207                            WebKeys.THEME_DISPLAY);
208    
209                    long fileEntryTypeId = ParamUtil.getLong(
210                            actionRequest, "fileEntryTypeId");
211    
212                    DLAppServiceUtil.subscribeFileEntryType(
213                            themeDisplay.getScopeGroupId(), fileEntryTypeId);
214            }
215    
216            protected void unsubscribeFileEntryType(ActionRequest actionRequest)
217                    throws Exception {
218    
219                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
220                            WebKeys.THEME_DISPLAY);
221    
222                    long fileEntryTypeId = ParamUtil.getLong(
223                            actionRequest, "fileEntryTypeId");
224    
225                    DLAppServiceUtil.unsubscribeFileEntryType(
226                            themeDisplay.getScopeGroupId(), fileEntryTypeId);
227            }
228    
229            protected void updateFileEntryType(
230                            ActionRequest actionRequest, ActionResponse actionResponse)
231                    throws Exception {
232    
233                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
234                            WebKeys.THEME_DISPLAY);
235    
236                    long fileEntryTypeId = ParamUtil.getLong(
237                            actionRequest, "fileEntryTypeId");
238    
239                    String name = ParamUtil.getString(actionRequest, "name");
240                    String description = ParamUtil.getString(actionRequest, "description");
241                    long[] ddmStructureIds = getLongArray(
242                            actionRequest, "ddmStructuresSearchContainerPrimaryKeys");
243    
244                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
245                            DLFileEntryType.class.getName(), actionRequest);
246    
247                    if (fileEntryTypeId <= 0) {
248    
249                            // Add file entry type
250    
251                            long groupId = themeDisplay.getScopeGroupId();
252    
253                            Group scopeGroup = GroupLocalServiceUtil.getGroup(groupId);
254    
255                            if (scopeGroup.isLayout()) {
256                                    groupId = scopeGroup.getParentGroupId();
257                            }
258    
259                            DLFileEntryTypeServiceUtil.addFileEntryType(
260                                    groupId, name, description, ddmStructureIds, serviceContext);
261                    }
262                    else {
263    
264                            // Update file entry type
265    
266                            DLFileEntryTypeServiceUtil.updateFileEntryType(
267                                    fileEntryTypeId, name, description, ddmStructureIds,
268                                    serviceContext);
269                    }
270            }
271    
272    }