001
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.StringPool;
023 import com.liferay.portal.kernel.util.StringUtil;
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.DuplicateVocabularyException;
029 import com.liferay.portlet.asset.NoSuchVocabularyException;
030 import com.liferay.portlet.asset.VocabularyNameException;
031 import com.liferay.portlet.asset.model.AssetCategoryConstants;
032 import com.liferay.portlet.asset.model.AssetVocabulary;
033 import com.liferay.portlet.asset.service.AssetVocabularyServiceUtil;
034 import com.liferay.portlet.asset.util.AssetVocabularySettingsHelper;
035
036 import java.util.Locale;
037 import java.util.Map;
038
039 import javax.portlet.ActionRequest;
040 import javax.portlet.ActionResponse;
041 import javax.portlet.PortletConfig;
042 import javax.portlet.RenderRequest;
043 import javax.portlet.RenderResponse;
044
045 import org.apache.struts.action.ActionForm;
046 import org.apache.struts.action.ActionForward;
047 import org.apache.struts.action.ActionMapping;
048
049
054 public class EditVocabularyAction extends PortletAction {
055
056 @Override
057 public void processAction(
058 ActionMapping actionMapping, ActionForm actionForm,
059 PortletConfig portletConfig, ActionRequest actionRequest,
060 ActionResponse actionResponse)
061 throws Exception {
062
063 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
064
065 try {
066 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
067 updateVocabulary(actionRequest);
068 }
069 else if (cmd.equals(Constants.DELETE)) {
070 deleteVocabulary(actionRequest);
071 }
072
073 sendRedirect(actionRequest, actionResponse);
074 }
075 catch (Exception e) {
076 if (e instanceof NoSuchVocabularyException ||
077 e instanceof PrincipalException) {
078
079 SessionErrors.add(actionRequest, e.getClass());
080
081 setForward(actionRequest, "portlet.asset_category_admin.error");
082 }
083 else if (e instanceof DuplicateVocabularyException ||
084 e instanceof VocabularyNameException) {
085
086 SessionErrors.add(actionRequest, e.getClass());
087 }
088 else {
089 throw e;
090 }
091 }
092 }
093
094 @Override
095 public ActionForward render(
096 ActionMapping actionMapping, ActionForm actionForm,
097 PortletConfig portletConfig, RenderRequest renderRequest,
098 RenderResponse renderResponse)
099 throws Exception {
100
101 ActionUtil.getVocabulary(renderRequest);
102
103 return actionMapping.findForward(
104 getForward(
105 renderRequest, "portlet.asset_category_admin.edit_vocabulary"));
106 }
107
108 protected void deleteVocabulary(ActionRequest actionRequest)
109 throws PortalException {
110
111 long[] deleteVocabularyIds = null;
112
113 long vocabularyId = ParamUtil.getLong(actionRequest, "vocabularyId");
114
115 if (vocabularyId > 0) {
116 deleteVocabularyIds = new long[] {vocabularyId};
117 }
118 else {
119 deleteVocabularyIds = StringUtil.split(
120 ParamUtil.getString(actionRequest, "deleteVocabularyIds"), 0L);
121 }
122
123 for (long deleteVocabularyId : deleteVocabularyIds) {
124 AssetVocabularyServiceUtil.deleteVocabulary(deleteVocabularyId);
125 }
126 }
127
128 protected String getSettings(ActionRequest actionRequest) {
129 AssetVocabularySettingsHelper vocabularySettingsHelper =
130 new AssetVocabularySettingsHelper();
131
132 int[] indexes = StringUtil.split(
133 ParamUtil.getString(actionRequest, "indexes"), 0);
134
135 long[] classNameIds = new long[indexes.length];
136 long[] classTypePKs = new long[indexes.length];
137 boolean[] requireds = new boolean[indexes.length];
138
139 for (int i = 0; i < indexes.length; i++) {
140 int index = indexes[i];
141
142 classNameIds[i] = ParamUtil.getLong(
143 actionRequest, "classNameId" + index);
144
145 classTypePKs[i] = ParamUtil.getLong(
146 actionRequest,
147 "subtype" + classNameIds[i] + "-classNameId" + index,
148 AssetCategoryConstants.ALL_CLASS_TYPE_PK);
149
150 requireds[i] = ParamUtil.getBoolean(
151 actionRequest, "required" + index);
152 }
153
154 vocabularySettingsHelper.setClassNameIdsAndClassTypePKs(
155 classNameIds, classTypePKs, requireds);
156
157 boolean multiValued = ParamUtil.getBoolean(
158 actionRequest, "multiValued");
159
160 vocabularySettingsHelper.setMultiValued(multiValued);
161
162 return vocabularySettingsHelper.toString();
163 }
164
165 protected void updateVocabulary(ActionRequest actionRequest)
166 throws Exception {
167
168 long vocabularyId = ParamUtil.getLong(actionRequest, "vocabularyId");
169
170 Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(
171 actionRequest, "title");
172 Map<Locale, String> descriptionMap =
173 LocalizationUtil.getLocalizationMap(actionRequest, "description");
174
175 ServiceContext serviceContext = ServiceContextFactory.getInstance(
176 AssetVocabulary.class.getName(), actionRequest);
177
178 if (vocabularyId <= 0) {
179
180
181
182 AssetVocabularyServiceUtil.addVocabulary(
183 StringPool.BLANK, titleMap, descriptionMap,
184 getSettings(actionRequest), serviceContext);
185 }
186 else {
187
188
189
190 AssetVocabularyServiceUtil.updateVocabulary(
191 vocabularyId, StringPool.BLANK, titleMap, descriptionMap,
192 getSettings(actionRequest), serviceContext);
193 }
194 }
195
196 }