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