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                    for (long deleteCategoryId : deleteCategoryIds) {
126                            AssetCategoryServiceUtil.deleteCategory(deleteCategoryId);
127                    }
128            }
129    
130            protected String[] getCategoryProperties(ActionRequest actionRequest) {
131                    int[] categoryPropertiesIndexes = StringUtil.split(
132                            ParamUtil.getString(actionRequest, "categoryPropertiesIndexes"), 0);
133    
134                    String[] categoryProperties =
135                            new String[categoryPropertiesIndexes.length];
136    
137                    for (int i = 0; i < categoryPropertiesIndexes.length; i++) {
138                            int categoryPropertiesIndex = categoryPropertiesIndexes[i];
139    
140                            String key = ParamUtil.getString(
141                                    actionRequest, "key" + categoryPropertiesIndex);
142    
143                            if (Validator.isNull(key)) {
144                                    continue;
145                            }
146    
147                            String value = ParamUtil.getString(
148                                    actionRequest, "value" + categoryPropertiesIndex);
149    
150                            categoryProperties[i] =
151                                    key + AssetCategoryConstants.PROPERTY_KEY_VALUE_SEPARATOR +
152                                            value;
153                    }
154    
155                    return categoryProperties;
156            }
157    
158            protected void moveCategory(ActionRequest actionRequest) throws Exception {
159                    long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
160    
161                    long parentCategoryId = ParamUtil.getLong(
162                            actionRequest, "parentCategoryId");
163                    long vocabularyId = ParamUtil.getLong(actionRequest, "vocabularyId");
164    
165                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
166                            AssetCategory.class.getName(), actionRequest);
167    
168                    AssetCategoryServiceUtil.moveCategory(
169                            categoryId, parentCategoryId, vocabularyId, serviceContext);
170            }
171    
172            protected void updateCategory(ActionRequest actionRequest)
173                    throws Exception {
174    
175                    long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
176    
177                    long parentCategoryId = ParamUtil.getLong(
178                            actionRequest, "parentCategoryId");
179                    Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(
180                            actionRequest, "title");
181                    Map<Locale, String> descriptionMap =
182                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
183                    long vocabularyId = ParamUtil.getLong(actionRequest, "vocabularyId");
184                    String[] categoryProperties = getCategoryProperties(actionRequest);
185    
186                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
187                            AssetCategory.class.getName(), actionRequest);
188    
189                    if (categoryId <= 0) {
190    
191                            // Add category
192    
193                            AssetCategoryServiceUtil.addCategory(
194                                    parentCategoryId, titleMap, descriptionMap, vocabularyId,
195                                    categoryProperties, serviceContext);
196                    }
197                    else {
198    
199                            // Update category
200    
201                            AssetCategoryServiceUtil.updateCategory(
202                                    categoryId, parentCategoryId, titleMap, descriptionMap,
203                                    vocabularyId, categoryProperties, serviceContext);
204                    }
205            }
206    
207    }