001    /**
002     * Copyright (c) 2000-2013 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.Validator;
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.AssetCategory;
029    import com.liferay.portlet.asset.service.AssetCategoryServiceUtil;
030    
031    import java.util.Locale;
032    import java.util.Map;
033    
034    import javax.portlet.ActionRequest;
035    import javax.portlet.ActionResponse;
036    import javax.portlet.PortletConfig;
037    import javax.portlet.RenderRequest;
038    import javax.portlet.RenderResponse;
039    
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionForward;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     * @author Julio Camarero
047     */
048    public class EditCategoryAction extends PortletAction {
049    
050            @Override
051            public void processAction(
052                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
053                            ActionRequest actionRequest, ActionResponse actionResponse)
054                    throws Exception {
055    
056                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
057    
058                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
059    
060                    try {
061                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
062                                    jsonObject = updateCategory(actionRequest);
063                            }
064                            else if (cmd.equals(Constants.MOVE)) {
065                                    jsonObject = moveCategory(actionRequest);
066                            }
067                    }
068                    catch (Exception e) {
069                            jsonObject.putException(e);
070                    }
071    
072                    writeJSON(actionRequest, actionResponse, jsonObject);
073            }
074    
075            @Override
076            public ActionForward render(
077                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
078                            RenderRequest renderRequest, RenderResponse renderResponse)
079                    throws Exception {
080    
081                    ActionUtil.getCategory(renderRequest);
082                    ActionUtil.getVocabularies(renderRequest);
083    
084                    return mapping.findForward(
085                            getForward(
086                                    renderRequest, "portlet.asset_category_admin.edit_category"));
087            }
088    
089            protected String[] getCategoryProperties(ActionRequest actionRequest) {
090                    int[] categoryPropertiesIndexes = StringUtil.split(
091                            ParamUtil.getString(actionRequest, "categoryPropertiesIndexes"), 0);
092    
093                    String[] categoryProperties =
094                            new String[categoryPropertiesIndexes.length];
095    
096                    for (int i = 0; i < categoryPropertiesIndexes.length; i++) {
097                            int categoryPropertiesIndex = categoryPropertiesIndexes[i];
098    
099                            String key = ParamUtil.getString(
100                                    actionRequest, "key" + categoryPropertiesIndex);
101    
102                            if (Validator.isNull(key)) {
103                                    continue;
104                            }
105    
106                            String value = ParamUtil.getString(
107                                    actionRequest, "value" + categoryPropertiesIndex);
108    
109                            categoryProperties[i] = key + StringPool.COLON + value;
110                    }
111    
112                    return categoryProperties;
113            }
114    
115            protected JSONObject moveCategory(ActionRequest actionRequest)
116                    throws Exception {
117    
118                    long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
119    
120                    long parentCategoryId = ParamUtil.getLong(
121                            actionRequest, "parentCategoryId");
122                    long vocabularyId = ParamUtil.getLong(actionRequest, "vocabularyId");
123    
124                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
125                            AssetCategory.class.getName(), actionRequest);
126    
127                    AssetCategory category = AssetCategoryServiceUtil.moveCategory(
128                            categoryId, parentCategoryId, vocabularyId, serviceContext);
129    
130                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
131    
132                    jsonObject.put("categoryId", category.getCategoryId());
133    
134                    return jsonObject;
135            }
136    
137            protected JSONObject updateCategory(ActionRequest actionRequest)
138                    throws Exception {
139    
140                    long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
141    
142                    long parentCategoryId = ParamUtil.getLong(
143                            actionRequest, "parentCategoryId");
144                    Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(
145                            actionRequest, "title");
146                    Map<Locale, String> descriptionMap =
147                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
148                    long vocabularyId = ParamUtil.getLong(actionRequest, "vocabularyId");
149                    String[] categoryProperties = getCategoryProperties(actionRequest);
150    
151                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
152                            AssetCategory.class.getName(), actionRequest);
153    
154                    AssetCategory category = null;
155    
156                    if (categoryId <= 0) {
157    
158                            // Add category
159    
160                            category = AssetCategoryServiceUtil.addCategory(
161                                    parentCategoryId, titleMap, descriptionMap, vocabularyId,
162                                    categoryProperties, serviceContext);
163                    }
164                    else {
165    
166                            // Update category
167    
168                            category = AssetCategoryServiceUtil.updateCategory(
169                                    categoryId, parentCategoryId, titleMap, descriptionMap,
170                                    vocabularyId, categoryProperties, serviceContext);
171                    }
172    
173                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
174    
175                    jsonObject.put("categoryId", category.getCategoryId());
176    
177                    return jsonObject;
178            }
179    
180    }