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.portlet.assetcategoryadmin.action;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
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.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.security.auth.PrincipalException;
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.AssetCategoryNameException;
029    import com.liferay.portlet.asset.DuplicateCategoryException;
030    import com.liferay.portlet.asset.NoSuchCategoryException;
031    import com.liferay.portlet.asset.model.AssetCategory;
032    import com.liferay.portlet.asset.model.AssetCategoryConstants;
033    import com.liferay.portlet.asset.service.AssetCategoryServiceUtil;
034    
035    import java.util.Locale;
036    import java.util.Map;
037    
038    import javax.portlet.ActionRequest;
039    import javax.portlet.ActionResponse;
040    import javax.portlet.PortletConfig;
041    import javax.portlet.RenderRequest;
042    import javax.portlet.RenderResponse;
043    
044    import org.apache.struts.action.ActionForm;
045    import org.apache.struts.action.ActionForward;
046    import org.apache.struts.action.ActionMapping;
047    
048    /**
049     * @author Brian Wing Shun Chan
050     * @author Julio Camarero
051     */
052    public class EditCategoryAction extends PortletAction {
053    
054            @Override
055            public void processAction(
056                            ActionMapping actionMapping, ActionForm actionForm,
057                            PortletConfig portletConfig, ActionRequest actionRequest,
058                            ActionResponse actionResponse)
059                    throws Exception {
060    
061                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
062    
063                    try {
064                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
065                                    updateCategory(actionRequest);
066                            }
067                            else if (cmd.equals(Constants.DELETE)) {
068                                    deleteCategory(actionRequest);
069                            }
070                            else if (cmd.equals(Constants.MOVE)) {
071                                    moveCategory(actionRequest);
072                            }
073    
074                            sendRedirect(actionRequest, actionResponse);
075                    }
076                    catch (Exception e) {
077                            if (e instanceof NoSuchCategoryException ||
078                                    e instanceof PrincipalException) {
079    
080                                    SessionErrors.add(actionRequest, e.getClass());
081    
082                                    setForward(actionRequest, "portlet.asset_category_admin.error");
083                            }
084                            else if (e instanceof AssetCategoryNameException ||
085                                             e instanceof DuplicateCategoryException) {
086    
087                                    SessionErrors.add(actionRequest, e.getClass());
088                            }
089                            else {
090                                    throw e;
091                            }
092                    }
093            }
094    
095            @Override
096            public ActionForward render(
097                            ActionMapping actionMapping, ActionForm actionForm,
098                            PortletConfig portletConfig, RenderRequest renderRequest,
099                            RenderResponse renderResponse)
100                    throws Exception {
101    
102                    ActionUtil.getCategory(renderRequest);
103                    ActionUtil.getVocabularies(renderRequest);
104    
105                    return actionMapping.findForward(
106                            getForward(
107                                    renderRequest, "portlet.asset_category_admin.edit_category"));
108            }
109    
110            protected void deleteCategory(ActionRequest actionRequest)
111                    throws PortalException {
112    
113                    long[] deleteCategoryIds = null;
114    
115                    long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
116    
117                    if (categoryId > 0) {
118                            deleteCategoryIds = new long[] {categoryId};
119                    }
120                    else {
121                            deleteCategoryIds = StringUtil.split(
122                                    ParamUtil.getString(actionRequest, "deleteCategoryIds"), 0L);
123                    }
124    
125                    AssetCategoryServiceUtil.deleteCategories(deleteCategoryIds);
126            }
127    
128            protected String[] getCategoryProperties(ActionRequest actionRequest) {
129                    int[] categoryPropertiesIndexes = StringUtil.split(
130                            ParamUtil.getString(actionRequest, "categoryPropertiesIndexes"), 0);
131    
132                    String[] categoryProperties =
133                            new String[categoryPropertiesIndexes.length];
134    
135                    for (int i = 0; i < categoryPropertiesIndexes.length; i++) {
136                            int categoryPropertiesIndex = categoryPropertiesIndexes[i];
137    
138                            String key = ParamUtil.getString(
139                                    actionRequest, "key" + categoryPropertiesIndex);
140    
141                            if (Validator.isNull(key)) {
142                                    continue;
143                            }
144    
145                            String value = ParamUtil.getString(
146                                    actionRequest, "value" + categoryPropertiesIndex);
147    
148                            categoryProperties[i] =
149                                    key + AssetCategoryConstants.PROPERTY_KEY_VALUE_SEPARATOR +
150                                            value;
151                    }
152    
153                    return categoryProperties;
154            }
155    
156            protected void moveCategory(ActionRequest actionRequest) throws Exception {
157                    long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
158    
159                    long parentCategoryId = ParamUtil.getLong(
160                            actionRequest, "parentCategoryId");
161                    long vocabularyId = ParamUtil.getLong(actionRequest, "vocabularyId");
162    
163                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
164                            AssetCategory.class.getName(), actionRequest);
165    
166                    AssetCategoryServiceUtil.moveCategory(
167                            categoryId, parentCategoryId, vocabularyId, serviceContext);
168            }
169    
170            protected void updateCategory(ActionRequest actionRequest)
171                    throws Exception {
172    
173                    long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
174    
175                    long parentCategoryId = ParamUtil.getLong(
176                            actionRequest, "parentCategoryId");
177                    Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(
178                            actionRequest, "title");
179                    Map<Locale, String> descriptionMap =
180                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
181                    long vocabularyId = ParamUtil.getLong(actionRequest, "vocabularyId");
182                    String[] categoryProperties = getCategoryProperties(actionRequest);
183    
184                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
185                            AssetCategory.class.getName(), actionRequest);
186    
187                    if (categoryId <= 0) {
188    
189                            // Add category
190    
191                            AssetCategoryServiceUtil.addCategory(
192                                    parentCategoryId, titleMap, descriptionMap, vocabularyId,
193                                    categoryProperties, serviceContext);
194                    }
195                    else {
196    
197                            // Update category
198    
199                            AssetCategoryServiceUtil.updateCategory(
200                                    categoryId, parentCategoryId, titleMap, descriptionMap,
201                                    vocabularyId, categoryProperties, serviceContext);
202                    }
203            }
204    
205    }