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.StringUtil;
023 import com.liferay.portal.kernel.util.Validator;
024 import com.liferay.portal.service.ServiceContext;
025 import com.liferay.portal.service.ServiceContextFactory;
026 import com.liferay.portal.struts.PortletAction;
027 import com.liferay.portlet.asset.model.AssetCategory;
028 import com.liferay.portlet.asset.model.AssetCategoryConstants;
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] =
112 key + AssetCategoryConstants.PROPERTY_KEY_VALUE_SEPARATOR +
113 value;
114 }
115
116 return categoryProperties;
117 }
118
119 protected JSONObject moveCategory(ActionRequest actionRequest)
120 throws Exception {
121
122 long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
123
124 long parentCategoryId = ParamUtil.getLong(
125 actionRequest, "parentCategoryId");
126 long vocabularyId = ParamUtil.getLong(actionRequest, "vocabularyId");
127
128 ServiceContext serviceContext = ServiceContextFactory.getInstance(
129 AssetCategory.class.getName(), actionRequest);
130
131 AssetCategory category = AssetCategoryServiceUtil.moveCategory(
132 categoryId, parentCategoryId, vocabularyId, serviceContext);
133
134 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
135
136 jsonObject.put("categoryId", category.getCategoryId());
137
138 return jsonObject;
139 }
140
141 protected JSONObject updateCategory(ActionRequest actionRequest)
142 throws Exception {
143
144 long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
145
146 long parentCategoryId = ParamUtil.getLong(
147 actionRequest, "parentCategoryId");
148 Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(
149 actionRequest, "title");
150 Map<Locale, String> descriptionMap =
151 LocalizationUtil.getLocalizationMap(actionRequest, "description");
152 long vocabularyId = ParamUtil.getLong(actionRequest, "vocabularyId");
153 String[] categoryProperties = getCategoryProperties(actionRequest);
154
155 ServiceContext serviceContext = ServiceContextFactory.getInstance(
156 AssetCategory.class.getName(), actionRequest);
157
158 AssetCategory category = null;
159
160 if (categoryId <= 0) {
161
162
163
164 category = AssetCategoryServiceUtil.addCategory(
165 parentCategoryId, titleMap, descriptionMap, vocabularyId,
166 categoryProperties, serviceContext);
167 }
168 else {
169
170
171
172 category = AssetCategoryServiceUtil.updateCategory(
173 categoryId, parentCategoryId, titleMap, descriptionMap,
174 vocabularyId, categoryProperties, serviceContext);
175 }
176
177 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
178
179 jsonObject.put("categoryId", category.getCategoryId());
180 jsonObject.put("parentCategoryId", category.getParentCategoryId());
181
182 return jsonObject;
183 }
184
185 }