001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.service.ServiceContextFactory;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portlet.asset.model.AssetTag;
027    import com.liferay.portlet.asset.model.AssetTagConstants;
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    /**
041     * @author Brian Wing Shun Chan
042     * @author Julio Camarero
043     */
044    public class EditTagAction extends PortletAction {
045    
046            @Override
047            public void processAction(
048                            ActionMapping actionMapping, ActionForm actionForm,
049                            PortletConfig portletConfig, ActionRequest actionRequest,
050                            ActionResponse actionResponse)
051                    throws Exception {
052    
053                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
054    
055                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
056    
057                    try {
058                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
059                                    jsonObject = updateTag(actionRequest);
060                            }
061                            else if (cmd.equals(Constants.MERGE)) {
062                                    jsonObject = mergeTag(actionRequest);
063                            }
064                    }
065                    catch (Exception e) {
066                            jsonObject.putException(e);
067                    }
068    
069                    writeJSON(actionRequest, actionResponse, jsonObject);
070            }
071    
072            @Override
073            public ActionForward render(
074                            ActionMapping actionMapping, ActionForm actionForm,
075                            PortletConfig portletConfig, RenderRequest renderRequest,
076                            RenderResponse renderResponse)
077                    throws Exception {
078    
079                    ActionUtil.getTag(renderRequest);
080    
081                    return actionMapping.findForward(
082                            getForward(renderRequest, "portlet.asset_tag_admin.edit_tag"));
083            }
084    
085            protected String[] getTagProperties(ActionRequest actionRequest) {
086                    int[] tagPropertiesIndexes = StringUtil.split(
087                            ParamUtil.getString(actionRequest, "tagPropertiesIndexes"), 0);
088    
089                    String[] tagProperties = new String[tagPropertiesIndexes.length];
090    
091                    for (int i = 0; i < tagPropertiesIndexes.length; i++) {
092                            int tagPropertiesIndex = tagPropertiesIndexes[i];
093    
094                            String key = ParamUtil.getString(
095                                    actionRequest, "key" + tagPropertiesIndex);
096    
097                            if (Validator.isNull(key)) {
098                                    continue;
099                            }
100    
101                            String value = ParamUtil.getString(
102                                    actionRequest, "value" + tagPropertiesIndex);
103    
104                            tagProperties[i] =
105                                    key + AssetTagConstants.PROPERTY_KEY_VALUE_SEPARATOR + value;
106                    }
107    
108                    return tagProperties;
109            }
110    
111            protected JSONObject mergeTag(ActionRequest actionRequest)
112                    throws Exception {
113    
114                    long fromTagId = ParamUtil.getLong(actionRequest, "fromTagId");
115                    long toTagId = ParamUtil.getLong(actionRequest, "toTagId");
116    
117                    AssetTagServiceUtil.mergeTags(fromTagId, toTagId, false);
118    
119                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
120    
121                    jsonObject.put("tagId", toTagId);
122    
123                    return jsonObject;
124            }
125    
126            protected JSONObject updateTag(ActionRequest actionRequest)
127                    throws Exception {
128    
129                    long tagId = ParamUtil.getLong(actionRequest, "tagId");
130    
131                    String name = ParamUtil.getString(actionRequest, "name");
132    
133                    String[] tagProperties = getTagProperties(actionRequest);
134    
135                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
136                            AssetTag.class.getName(), actionRequest);
137    
138                    AssetTag tag = null;
139    
140                    if (tagId <= 0) {
141    
142                            // Add tag
143    
144                            tag = AssetTagServiceUtil.addTag(
145                                    name, tagProperties, serviceContext);
146                    }
147                    else {
148    
149                            // Update tag
150    
151                            tag = AssetTagServiceUtil.updateTag(
152                                    tagId, name, tagProperties, serviceContext);
153                    }
154    
155                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
156    
157                    jsonObject.put("tagId", tag.getTagId());
158    
159                    return jsonObject;
160            }
161    
162    }