1
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
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 }