1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.taggedcontent.util;
24  
25  import com.liferay.portal.kernel.util.ArrayUtil;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portlet.PortletPreferencesFactoryUtil;
30  import com.liferay.portlet.tags.model.TagsAsset;
31  import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
32  import com.liferay.util.xml.XMLFormatter;
33  
34  import java.io.IOException;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.PortletPreferences;
38  
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  
42  import org.dom4j.Document;
43  import org.dom4j.DocumentFactory;
44  import org.dom4j.Element;
45  
46  /**
47   * <a href="AssetPublisherUtil.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Raymond Augé
50   *
51   */
52  public class AssetPublisherUtil {
53  
54      public static void addAndStoreSelection(
55              ActionRequest req, String className, long classPK, int assetOrder)
56          throws Exception {
57  
58          String referringPortletResource =
59              ParamUtil.getString(req, "referringPortletResource");
60  
61          if (Validator.isNull(referringPortletResource)) {
62              return;
63          }
64  
65          TagsAsset asset = TagsAssetLocalServiceUtil.getAsset(
66              className, classPK);
67  
68          PortletPreferences prefs =
69              PortletPreferencesFactoryUtil.getPortletSetup(
70                  req, referringPortletResource, true, true);
71  
72          addSelection(className, asset.getAssetId(), assetOrder, prefs);
73  
74          prefs.store();
75      }
76  
77      public static void addSelection(ActionRequest req, PortletPreferences prefs)
78          throws Exception {
79  
80          String assetType = ParamUtil.getString(req, "assetType");
81          long assetId = ParamUtil.getLong(req, "assetId");
82          int assetOrder = ParamUtil.getInteger(req, "assetOrder");
83  
84          addSelection(assetType, assetId, assetOrder, prefs);
85      }
86  
87      public static void addSelection(
88              String assetType, long assetId, int assetOrder,
89              PortletPreferences prefs)
90          throws Exception {
91  
92          String[] manualEntries = prefs.getValues(
93              "manual-entries", new String[0]);
94  
95          String assetConfig = _assetConfiguration(assetType, assetId);
96  
97          if (assetOrder > -1) {
98              manualEntries[assetOrder] = assetConfig;
99          }
100         else {
101             manualEntries = ArrayUtil.append(manualEntries, assetConfig);
102         }
103 
104         prefs.setValues("manual-entries", manualEntries);
105     }
106 
107     private static String _assetConfiguration(String assetType, long assetId) {
108         String xml = null;
109 
110         try {
111             DocumentFactory docFactory = DocumentFactory.getInstance();
112 
113             Document doc = docFactory.createDocument("UTF-8");
114 
115             Element asset = doc.addElement("asset");
116 
117             asset.addElement("asset-type").addText(assetType);
118             asset.addElement("asset-id").addText(String.valueOf(assetId));
119 
120             xml = XMLFormatter.toString(doc, StringPool.BLANK);
121         }
122         catch (IOException ioe) {
123             if (_log.isWarnEnabled()) {
124                 _log.warn(ioe);
125             }
126         }
127 
128         return xml;
129     }
130 
131     private static Log _log = LogFactory.getLog(AssetPublisherUtil.class);
132 
133 }