001
014
015 package com.liferay.portlet.assettagadmin.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.ParamUtil;
021 import com.liferay.portal.kernel.util.StringPool;
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.AssetTag;
028 import com.liferay.portlet.asset.service.AssetTagServiceUtil;
029
030 import javax.portlet.ActionRequest;
031 import javax.portlet.ActionResponse;
032 import javax.portlet.PortletConfig;
033 import javax.portlet.RenderRequest;
034 import javax.portlet.RenderResponse;
035
036 import org.apache.struts.action.ActionForm;
037 import org.apache.struts.action.ActionForward;
038 import org.apache.struts.action.ActionMapping;
039
040
044 public class EditTagAction extends PortletAction {
045
046 @Override
047 public void processAction(
048 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
049 ActionRequest actionRequest, ActionResponse actionResponse)
050 throws Exception {
051
052 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
053
054 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
055
056 try {
057 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
058 jsonObject = updateTag(actionRequest);
059 }
060 else if (cmd.equals(Constants.MERGE)) {
061 jsonObject = mergeTag(actionRequest);
062 }
063 }
064 catch (Exception e) {
065 jsonObject.putException(e);
066 }
067
068 writeJSON(actionRequest, actionResponse, jsonObject);
069 }
070
071 @Override
072 public ActionForward render(
073 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
074 RenderRequest renderRequest, RenderResponse renderResponse)
075 throws Exception {
076
077 ActionUtil.getTag(renderRequest);
078
079 return mapping.findForward(
080 getForward(renderRequest, "portlet.asset_tag_admin.edit_tag"));
081 }
082
083 protected String[] getTagProperties(ActionRequest actionRequest) {
084 int[] tagPropertiesIndexes = StringUtil.split(
085 ParamUtil.getString(actionRequest, "tagPropertiesIndexes"), 0);
086
087 String[] tagProperties = new String[tagPropertiesIndexes.length];
088
089 for (int i = 0; i < tagPropertiesIndexes.length; i++) {
090 int tagPropertiesIndex = tagPropertiesIndexes[i];
091
092 String key = ParamUtil.getString(
093 actionRequest, "key" + tagPropertiesIndex);
094
095 if (Validator.isNull(key)) {
096 continue;
097 }
098
099 String value = ParamUtil.getString(
100 actionRequest, "value" + tagPropertiesIndex);
101
102 tagProperties[i] = key + StringPool.COLON + value;
103 }
104
105 return tagProperties;
106 }
107
108 protected JSONObject mergeTag(ActionRequest actionRequest)
109 throws Exception {
110
111 long fromTagId = ParamUtil.getLong(actionRequest, "fromTagId");
112 long toTagId = ParamUtil.getLong(actionRequest, "toTagId");
113
114 AssetTagServiceUtil.mergeTags(fromTagId, toTagId, false);
115
116 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
117
118 jsonObject.put("tagId", toTagId);
119
120 return jsonObject;
121 }
122
123 protected JSONObject updateTag(ActionRequest actionRequest)
124 throws Exception {
125
126 long tagId = ParamUtil.getLong(actionRequest, "tagId");
127
128 String name = ParamUtil.getString(actionRequest, "name");
129
130 String[] tagProperties = getTagProperties(actionRequest);
131
132 ServiceContext serviceContext = ServiceContextFactory.getInstance(
133 AssetTag.class.getName(), actionRequest);
134
135 AssetTag tag = null;
136
137 if (tagId <= 0) {
138
139
140
141 tag = AssetTagServiceUtil.addTag(
142 name, tagProperties, serviceContext);
143 }
144 else {
145
146
147
148 tag = AssetTagServiceUtil.updateTag(
149 tagId, name, tagProperties, serviceContext);
150 }
151
152 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
153
154 jsonObject.put("tagId", tag.getTagId());
155
156 return jsonObject;
157 }
158
159 }