001    /**
002     * Copyright (c) 2000-present 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.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.NoSuchTagException;
029    import com.liferay.portlet.asset.model.AssetTag;
030    import com.liferay.portlet.asset.model.AssetTagConstants;
031    import com.liferay.portlet.asset.service.AssetTagServiceUtil;
032    
033    import javax.portlet.ActionRequest;
034    import javax.portlet.ActionResponse;
035    import javax.portlet.PortletConfig;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    
039    import org.apache.struts.action.ActionForm;
040    import org.apache.struts.action.ActionForward;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     * @author Julio Camarero
046     */
047    public class EditTagAction extends PortletAction {
048    
049            @Override
050            public void processAction(
051                            ActionMapping actionMapping, ActionForm actionForm,
052                            PortletConfig portletConfig, ActionRequest actionRequest,
053                            ActionResponse actionResponse)
054                    throws Exception {
055    
056                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
057    
058                    try {
059                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
060                                    updateTag(actionRequest);
061                            }
062                            else if (cmd.equals(Constants.DELETE)) {
063                                    deleteTag(actionRequest);
064                            }
065                            else if (cmd.equals(Constants.MERGE)) {
066                                    mergeTag(actionRequest);
067                            }
068    
069                            sendRedirect(actionRequest, actionResponse);
070                    }
071                    catch (Exception e) {
072                            if (e instanceof AssetTagException) {
073                                    SessionErrors.add(actionRequest, e.getClass(), e);
074                            }
075                            else if (e instanceof NoSuchTagException ||
076                                             e instanceof PrincipalException) {
077    
078                                    SessionErrors.add(actionRequest, e.getClass());
079    
080                                    setForward(actionRequest, "portlet.asset_tag_admin.error");
081                            }
082                            else {
083                                    throw e;
084                            }
085                    }
086            }
087    
088            @Override
089            public ActionForward render(
090                            ActionMapping actionMapping, ActionForm actionForm,
091                            PortletConfig portletConfig, RenderRequest renderRequest,
092                            RenderResponse renderResponse)
093                    throws Exception {
094    
095                    ActionUtil.getTag(renderRequest);
096    
097                    return actionMapping.findForward(
098                            getForward(renderRequest, "portlet.asset_tag_admin.edit_tag"));
099            }
100    
101            protected void deleteTag(ActionRequest actionRequest)
102                    throws PortalException {
103    
104                    long[] deleteTagIds = null;
105    
106                    long tagId = ParamUtil.getLong(actionRequest, "tagId");
107    
108                    if (tagId > 0) {
109                            deleteTagIds = new long[] {tagId};
110                    }
111                    else {
112                            deleteTagIds = StringUtil.split(
113                                    ParamUtil.getString(actionRequest, "deleteTagIds"), 0L);
114                    }
115    
116                    for (long deleteTagId : deleteTagIds) {
117                            AssetTagServiceUtil.deleteTag(deleteTagId);
118                    }
119            }
120    
121            protected String[] getTagProperties(ActionRequest actionRequest) {
122                    int[] tagPropertiesIndexes = StringUtil.split(
123                            ParamUtil.getString(actionRequest, "tagPropertiesIndexes"), 0);
124    
125                    String[] tagProperties = new String[tagPropertiesIndexes.length];
126    
127                    for (int i = 0; i < tagPropertiesIndexes.length; i++) {
128                            int tagPropertiesIndex = tagPropertiesIndexes[i];
129    
130                            String key = ParamUtil.getString(
131                                    actionRequest, "key" + tagPropertiesIndex);
132    
133                            if (Validator.isNull(key)) {
134                                    continue;
135                            }
136    
137                            String value = ParamUtil.getString(
138                                    actionRequest, "value" + tagPropertiesIndex);
139    
140                            tagProperties[i] =
141                                    key + AssetTagConstants.PROPERTY_KEY_VALUE_SEPARATOR + value;
142                    }
143    
144                    return tagProperties;
145            }
146    
147            protected void mergeTag(ActionRequest actionRequest) throws Exception {
148                    long[] mergeTagIds = StringUtil.split(
149                            ParamUtil.getString(actionRequest, "mergeTagIds"), 0L);
150                    long targetTagId = ParamUtil.getLong(actionRequest, "targetTagId");
151                    boolean overrideTagsProperties = ParamUtil.getBoolean(
152                            actionRequest, "overrideTagsProperties");
153    
154                    for (long mergeTagId : mergeTagIds) {
155                            if (targetTagId == mergeTagId) {
156                                    continue;
157                            }
158    
159                            AssetTagServiceUtil.mergeTags(
160                                    mergeTagId, targetTagId, overrideTagsProperties);
161                    }
162            }
163    
164            protected void updateTag(ActionRequest actionRequest) throws Exception {
165                    long tagId = ParamUtil.getLong(actionRequest, "tagId");
166    
167                    String name = ParamUtil.getString(actionRequest, "name");
168    
169                    String[] tagProperties = getTagProperties(actionRequest);
170    
171                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
172                            AssetTag.class.getName(), actionRequest);
173    
174                    if (tagId <= 0) {
175    
176                            // Add tag
177    
178                            AssetTagServiceUtil.addTag(name, tagProperties, serviceContext);
179                    }
180                    else {
181    
182                            // Update tag
183    
184                            AssetTagServiceUtil.updateTag(
185                                    tagId, name, tagProperties, serviceContext);
186                    }
187            }
188    
189    }