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.assetcategoryadmin.action;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.LocalizationUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.UnicodeProperties;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.ServiceContextFactory;
027    import com.liferay.portal.struts.PortletAction;
028    import com.liferay.portlet.asset.model.AssetCategoryConstants;
029    import com.liferay.portlet.asset.model.AssetVocabulary;
030    import com.liferay.portlet.asset.service.AssetVocabularyServiceUtil;
031    
032    import java.util.LinkedHashSet;
033    import java.util.Locale;
034    import java.util.Map;
035    import java.util.Set;
036    
037    import javax.portlet.ActionRequest;
038    import javax.portlet.ActionResponse;
039    import javax.portlet.PortletConfig;
040    import javax.portlet.RenderRequest;
041    import javax.portlet.RenderResponse;
042    
043    import org.apache.struts.action.ActionForm;
044    import org.apache.struts.action.ActionForward;
045    import org.apache.struts.action.ActionMapping;
046    
047    /**
048     * @author Brian Wing Shun Chan
049     * @author Julio Camarero
050     * @author Juan Fernández
051     */
052    public class EditVocabularyAction extends PortletAction {
053    
054            @Override
055            public void processAction(
056                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
057                            ActionRequest actionRequest, ActionResponse actionResponse)
058                    throws Exception {
059    
060                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
061    
062                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
063    
064                    try {
065                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
066                                    jsonObject = updateVocabulary(actionRequest);
067                            }
068                    }
069                    catch (Exception e) {
070                            jsonObject.putException(e);
071                    }
072    
073                    writeJSON(actionRequest, actionResponse, jsonObject);
074            }
075    
076            @Override
077            public ActionForward render(
078                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
079                            RenderRequest renderRequest, RenderResponse renderResponse)
080                    throws Exception {
081    
082                    ActionUtil.getVocabulary(renderRequest);
083    
084                    return mapping.findForward(
085                            getForward(
086                                    renderRequest, "portlet.asset_category_admin.edit_vocabulary"));
087            }
088    
089            protected UnicodeProperties getSettingsProperties(
090                    ActionRequest actionRequest) {
091    
092                    UnicodeProperties settingsProperties = new UnicodeProperties();
093    
094                    boolean multiValued = ParamUtil.getBoolean(
095                            actionRequest, "multiValued");
096    
097                    settingsProperties.setProperty(
098                            "multiValued", String.valueOf(multiValued));
099    
100                    int[] indexes = StringUtil.split(
101                            ParamUtil.getString(actionRequest, "indexes"), 0);
102    
103                    Set<Long> selectedClassNameIds = new LinkedHashSet<Long>();
104                    Set<Long> requiredClassNameIds = new LinkedHashSet<Long>();
105    
106                    for (int index : indexes) {
107                            long classNameId = ParamUtil.getLong(
108                                    actionRequest, "classNameId" + index);
109    
110                            boolean required = ParamUtil.getBoolean(
111                                    actionRequest, "required" + index);
112    
113                            if (classNameId == AssetCategoryConstants.ALL_CLASS_NAME_IDS) {
114                                    selectedClassNameIds.clear();
115                                    selectedClassNameIds.add(classNameId);
116    
117                                    if (required) {
118                                            requiredClassNameIds.clear();
119                                            requiredClassNameIds.add(classNameId);
120                                    }
121    
122                                    break;
123                            }
124                            else {
125                                    selectedClassNameIds.add(classNameId);
126    
127                                    if (required) {
128                                            requiredClassNameIds.add(classNameId);
129                                    }
130                            }
131                    }
132    
133                    settingsProperties.setProperty(
134                            "selectedClassNameIds", StringUtil.merge(selectedClassNameIds));
135                    settingsProperties.setProperty(
136                            "requiredClassNameIds", StringUtil.merge(requiredClassNameIds));
137    
138                    return settingsProperties;
139            }
140    
141            protected JSONObject updateVocabulary(ActionRequest actionRequest)
142                    throws Exception {
143    
144                    long vocabularyId = ParamUtil.getLong(actionRequest, "vocabularyId");
145    
146                    Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(
147                            actionRequest, "title");
148                    Map<Locale, String> descriptionMap =
149                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
150    
151                    UnicodeProperties settingsProperties = getSettingsProperties(
152                            actionRequest);
153    
154                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
155                            AssetVocabulary.class.getName(), actionRequest);
156    
157                    AssetVocabulary vocabulary = null;
158    
159                    if (vocabularyId <= 0) {
160    
161                            // Add vocabulary
162    
163                            vocabulary = AssetVocabularyServiceUtil.addVocabulary(
164                                    StringPool.BLANK, titleMap, descriptionMap,
165                                    settingsProperties.toString(), serviceContext);
166                    }
167                    else {
168    
169                            // Update vocabulary
170    
171                            vocabulary = AssetVocabularyServiceUtil.updateVocabulary(
172                                    vocabularyId, StringPool.BLANK, titleMap, descriptionMap,
173                                    settingsProperties.toString(), serviceContext);
174                    }
175    
176                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
177    
178                    jsonObject.put("vocabularyId", vocabulary.getVocabularyId());
179    
180                    return jsonObject;
181            }
182    
183    }