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