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