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