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