001    /**
002     * Copyright (c) 2000-present 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.portal.convert.action;
016    
017    import com.liferay.portal.kernel.util.Constants;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.portal.struts.PortletAction;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
025    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
026    import com.liferay.portlet.expando.model.ExpandoBridge;
027    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
028    import com.liferay.portlet.expando.util.ExpandoPresetUtil;
029    
030    import javax.portlet.ActionRequest;
031    import javax.portlet.ActionResponse;
032    import javax.portlet.PortletConfig;
033    import javax.portlet.RenderRequest;
034    import javax.portlet.RenderResponse;
035    
036    import org.apache.struts.action.ActionForm;
037    import org.apache.struts.action.ActionForward;
038    import org.apache.struts.action.ActionMapping;
039    
040    /**
041     * @author Alexander Chow
042     */
043    public class EditDocumentLibraryExtraSettingsAction extends PortletAction {
044    
045            @Override
046            public void processAction(
047                            ActionMapping actionMapping, ActionForm actionForm,
048                            PortletConfig portletConfig, ActionRequest actionRequest,
049                            ActionResponse actionResponse)
050                    throws Exception {
051    
052                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
053    
054                    if (cmd.equals("convert")) {
055                            convert(actionRequest, actionResponse);
056                    }
057    
058                    sendRedirect(actionRequest, actionResponse);
059            }
060    
061            @Override
062            public ActionForward render(
063                            ActionMapping actionMapping, ActionForm actionForm,
064                            PortletConfig portletConfig, RenderRequest renderRequest,
065                            RenderResponse renderResponse)
066                    throws Exception {
067    
068                    return actionMapping.findForward(
069                            getForward(
070                                    renderRequest,
071                                    "portlet.admin.edit_document_library_extra_settings"));
072            }
073    
074            protected int addCustomField(long companyId, String name, String preset)
075                    throws Exception {
076    
077                    ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
078                            companyId, DLFileEntryConstants.getClassName(), 0);
079    
080                    if (preset.startsWith("Preset")) {
081                            return ExpandoPresetUtil.addPresetExpando(
082                                    expandoBridge, preset, name);
083                    }
084    
085                    int type = GetterUtil.getInteger(preset);
086    
087                    expandoBridge.addAttribute(name, type);
088    
089                    return type;
090            }
091    
092            protected void convert(
093                            ActionRequest actionRequest, ActionResponse actionResponse)
094                    throws Exception {
095    
096                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
097                            WebKeys.THEME_DISPLAY);
098    
099                    String[] keys = StringUtil.split(
100                            ParamUtil.getString(actionRequest, "keys"));
101    
102                    String[] presets = new String[keys.length];
103    
104                    int[] types = new int[keys.length];
105    
106                    for (int i = 0; i < keys.length; i++) {
107                            presets[i] = ParamUtil.getString(actionRequest, "type_" + keys[i]);
108    
109                            types[i] = addCustomField(
110                                    themeDisplay.getCompanyId(), keys[i], presets[i]);
111                    }
112    
113                    DLFileEntryLocalServiceUtil.convertExtraSettings(keys);
114            }
115    
116    }