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