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.UnicodeProperties;
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.AssetCategoryConstants;
029 import com.liferay.portlet.asset.model.AssetVocabulary;
030 import com.liferay.portlet.asset.service.AssetVocabularyServiceUtil;
031
032 import java.util.LinkedHashSet;
033 import java.util.Locale;
034 import java.util.Map;
035 import java.util.Set;
036
037 import javax.portlet.ActionRequest;
038 import javax.portlet.ActionResponse;
039 import javax.portlet.PortletConfig;
040 import javax.portlet.RenderRequest;
041 import javax.portlet.RenderResponse;
042
043 import org.apache.struts.action.ActionForm;
044 import org.apache.struts.action.ActionForward;
045 import org.apache.struts.action.ActionMapping;
046
047
052 public class EditVocabularyAction 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 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
062
063 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
064
065 try {
066 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
067 jsonObject = updateVocabulary(actionRequest);
068 }
069 }
070 catch (Exception e) {
071 jsonObject.putException(e);
072 }
073
074 writeJSON(actionRequest, actionResponse, jsonObject);
075 }
076
077 @Override
078 public ActionForward render(
079 ActionMapping actionMapping, ActionForm actionForm,
080 PortletConfig portletConfig, RenderRequest renderRequest,
081 RenderResponse renderResponse)
082 throws Exception {
083
084 ActionUtil.getVocabulary(renderRequest);
085
086 return actionMapping.findForward(
087 getForward(
088 renderRequest, "portlet.asset_category_admin.edit_vocabulary"));
089 }
090
091 protected UnicodeProperties getSettingsProperties(
092 ActionRequest actionRequest) {
093
094 UnicodeProperties settingsProperties = new UnicodeProperties();
095
096 boolean multiValued = ParamUtil.getBoolean(
097 actionRequest, "multiValued");
098
099 settingsProperties.setProperty(
100 "multiValued", String.valueOf(multiValued));
101
102 int[] indexes = StringUtil.split(
103 ParamUtil.getString(actionRequest, "indexes"), 0);
104
105 Set<Long> selectedClassNameIds = new LinkedHashSet<Long>();
106 Set<Long> requiredClassNameIds = new LinkedHashSet<Long>();
107
108 for (int index : indexes) {
109 long classNameId = ParamUtil.getLong(
110 actionRequest, "classNameId" + index);
111
112 boolean required = ParamUtil.getBoolean(
113 actionRequest, "required" + index);
114
115 if (classNameId == AssetCategoryConstants.ALL_CLASS_NAME_IDS) {
116 selectedClassNameIds.clear();
117 selectedClassNameIds.add(classNameId);
118
119 if (required) {
120 requiredClassNameIds.clear();
121 requiredClassNameIds.add(classNameId);
122 }
123
124 break;
125 }
126 else {
127 selectedClassNameIds.add(classNameId);
128
129 if (required) {
130 requiredClassNameIds.add(classNameId);
131 }
132 }
133 }
134
135 settingsProperties.setProperty(
136 "selectedClassNameIds", StringUtil.merge(selectedClassNameIds));
137 settingsProperties.setProperty(
138 "requiredClassNameIds", StringUtil.merge(requiredClassNameIds));
139
140 return settingsProperties;
141 }
142
143 protected JSONObject updateVocabulary(ActionRequest actionRequest)
144 throws Exception {
145
146 long vocabularyId = ParamUtil.getLong(actionRequest, "vocabularyId");
147
148 Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(
149 actionRequest, "title");
150 Map<Locale, String> descriptionMap =
151 LocalizationUtil.getLocalizationMap(actionRequest, "description");
152
153 UnicodeProperties settingsProperties = getSettingsProperties(
154 actionRequest);
155
156 ServiceContext serviceContext = ServiceContextFactory.getInstance(
157 AssetVocabulary.class.getName(), actionRequest);
158
159 AssetVocabulary vocabulary = null;
160
161 if (vocabularyId <= 0) {
162
163
164
165 vocabulary = AssetVocabularyServiceUtil.addVocabulary(
166 StringPool.BLANK, titleMap, descriptionMap,
167 settingsProperties.toString(), serviceContext);
168 }
169 else {
170
171
172
173 vocabulary = AssetVocabularyServiceUtil.updateVocabulary(
174 vocabularyId, StringPool.BLANK, titleMap, descriptionMap,
175 settingsProperties.toString(), serviceContext);
176 }
177
178 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
179
180 jsonObject.put("vocabularyId", vocabulary.getVocabularyId());
181
182 return jsonObject;
183 }
184
185 }