001
014
015 package com.liferay.portlet.assettagadmin.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.ParamUtil;
021 import com.liferay.portal.kernel.util.StringUtil;
022 import com.liferay.portal.kernel.util.Validator;
023 import com.liferay.portal.security.auth.PrincipalException;
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.AssetTagException;
028 import com.liferay.portlet.asset.DuplicateTagException;
029 import com.liferay.portlet.asset.NoSuchTagException;
030 import com.liferay.portlet.asset.model.AssetTag;
031 import com.liferay.portlet.asset.model.AssetTagConstants;
032 import com.liferay.portlet.asset.service.AssetTagServiceUtil;
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 EditTagAction 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 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
058
059 try {
060 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
061 updateTag(actionRequest);
062 }
063 else if (cmd.equals(Constants.DELETE)) {
064 deleteTag(actionRequest);
065 }
066 else if (cmd.equals(Constants.MERGE)) {
067 mergeTag(actionRequest);
068 }
069
070 sendRedirect(actionRequest, actionResponse);
071 }
072 catch (Exception e) {
073 if (e instanceof AssetTagException) {
074 SessionErrors.add(actionRequest, e.getClass(), e);
075 }
076 else if (e instanceof DuplicateTagException) {
077 SessionErrors.add(actionRequest, e.getClass());
078 }
079 else if (e instanceof NoSuchTagException ||
080 e instanceof PrincipalException) {
081
082 SessionErrors.add(actionRequest, e.getClass());
083
084 setForward(actionRequest, "portlet.asset_tag_admin.error");
085 }
086 else {
087 throw e;
088 }
089 }
090 }
091
092 @Override
093 public ActionForward render(
094 ActionMapping actionMapping, ActionForm actionForm,
095 PortletConfig portletConfig, RenderRequest renderRequest,
096 RenderResponse renderResponse)
097 throws Exception {
098
099 ActionUtil.getTag(renderRequest);
100
101 return actionMapping.findForward(
102 getForward(renderRequest, "portlet.asset_tag_admin.edit_tag"));
103 }
104
105 protected void deleteTag(ActionRequest actionRequest)
106 throws PortalException {
107
108 long[] deleteTagIds = null;
109
110 long tagId = ParamUtil.getLong(actionRequest, "tagId");
111
112 if (tagId > 0) {
113 deleteTagIds = new long[] {tagId};
114 }
115 else {
116 deleteTagIds = StringUtil.split(
117 ParamUtil.getString(actionRequest, "deleteTagIds"), 0L);
118 }
119
120 for (long deleteTagId : deleteTagIds) {
121 AssetTagServiceUtil.deleteTag(deleteTagId);
122 }
123 }
124
125 protected String[] getTagProperties(ActionRequest actionRequest) {
126 int[] tagPropertiesIndexes = StringUtil.split(
127 ParamUtil.getString(actionRequest, "tagPropertiesIndexes"), 0);
128
129 String[] tagProperties = new String[tagPropertiesIndexes.length];
130
131 for (int i = 0; i < tagPropertiesIndexes.length; i++) {
132 int tagPropertiesIndex = tagPropertiesIndexes[i];
133
134 String key = ParamUtil.getString(
135 actionRequest, "key" + tagPropertiesIndex);
136
137 if (Validator.isNull(key)) {
138 continue;
139 }
140
141 String value = ParamUtil.getString(
142 actionRequest, "value" + tagPropertiesIndex);
143
144 tagProperties[i] =
145 key + AssetTagConstants.PROPERTY_KEY_VALUE_SEPARATOR + value;
146 }
147
148 return tagProperties;
149 }
150
151 protected void mergeTag(ActionRequest actionRequest) throws Exception {
152 long[] mergeTagIds = StringUtil.split(
153 ParamUtil.getString(actionRequest, "mergeTagIds"), 0L);
154 long targetTagId = ParamUtil.getLong(actionRequest, "targetTagId");
155 boolean overrideTagsProperties = ParamUtil.getBoolean(
156 actionRequest, "overrideTagsProperties");
157
158 for (long mergeTagId : mergeTagIds) {
159 if (targetTagId == mergeTagId) {
160 continue;
161 }
162
163 AssetTagServiceUtil.mergeTags(
164 mergeTagId, targetTagId, overrideTagsProperties);
165 }
166 }
167
168 protected void updateTag(ActionRequest actionRequest) throws Exception {
169 long tagId = ParamUtil.getLong(actionRequest, "tagId");
170
171 String name = ParamUtil.getString(actionRequest, "name");
172
173 String[] tagProperties = getTagProperties(actionRequest);
174
175 ServiceContext serviceContext = ServiceContextFactory.getInstance(
176 AssetTag.class.getName(), actionRequest);
177
178 if (tagId <= 0) {
179
180
181
182 AssetTagServiceUtil.addTag(name, tagProperties, serviceContext);
183 }
184 else {
185
186
187
188 AssetTagServiceUtil.updateTag(
189 tagId, name, tagProperties, serviceContext);
190 }
191 }
192
193 }