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.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    /**
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] = key + StringPool.COLON + value;
105                    }
106    
107                    return tagProperties;
108            }
109    
110            protected JSONObject mergeTag(ActionRequest actionRequest)
111                    throws Exception {
112    
113                    long fromTagId = ParamUtil.getLong(actionRequest, "fromTagId");
114                    long toTagId = ParamUtil.getLong(actionRequest, "toTagId");
115    
116                    AssetTagServiceUtil.mergeTags(fromTagId, toTagId, false);
117    
118                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
119    
120                    jsonObject.put("tagId", toTagId);
121    
122                    return jsonObject;
123            }
124    
125            protected JSONObject updateTag(ActionRequest actionRequest)
126                    throws Exception {
127    
128                    long tagId = ParamUtil.getLong(actionRequest, "tagId");
129    
130                    String name = ParamUtil.getString(actionRequest, "name");
131    
132                    String[] tagProperties = getTagProperties(actionRequest);
133    
134                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
135                            AssetTag.class.getName(), actionRequest);
136    
137                    AssetTag tag = null;
138    
139                    if (tagId <= 0) {
140    
141                            // Add tag
142    
143                            tag = AssetTagServiceUtil.addTag(
144                                    name, tagProperties, serviceContext);
145                    }
146                    else {
147    
148                            // Update tag
149    
150                            tag = AssetTagServiceUtil.updateTag(
151                                    tagId, name, tagProperties, serviceContext);
152                    }
153    
154                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
155    
156                    jsonObject.put("tagId", tag.getTagId());
157    
158                    return jsonObject;
159            }
160    
161    }