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