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.portal.theme.ThemeDisplay;
30 import com.liferay.portal.util.PortalUtil;
31 import com.liferay.portal.util.WebKeys;
32 import com.liferay.portlet.PortletPreferencesFactoryUtil;
33 import com.liferay.portlet.tags.model.TagsAsset;
34 import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
35 import com.liferay.util.xml.XMLFormatter;
36
37 import java.io.IOException;
38
39 import java.util.HashMap;
40 import java.util.Map;
41
42 import javax.portlet.ActionRequest;
43 import javax.portlet.PortletPreferences;
44 import javax.portlet.PortletRequest;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpSession;
48
49 import org.apache.commons.logging.Log;
50 import org.apache.commons.logging.LogFactory;
51
52 import org.dom4j.Document;
53 import org.dom4j.DocumentFactory;
54 import org.dom4j.Element;
55
56
62 public class AssetPublisherUtil {
63
64 public static void addAndStoreSelection(
65 ActionRequest req, String className, long classPK, int assetOrder)
66 throws Exception {
67
68 String referringPortletResource =
69 ParamUtil.getString(req, "referringPortletResource");
70
71 if (Validator.isNull(referringPortletResource)) {
72 return;
73 }
74
75 TagsAsset asset = TagsAssetLocalServiceUtil.getAsset(
76 className, classPK);
77
78 PortletPreferences prefs =
79 PortletPreferencesFactoryUtil.getPortletSetup(
80 req, referringPortletResource);
81
82 addSelection(className, asset.getAssetId(), assetOrder, prefs);
83
84 prefs.store();
85 }
86
87 public static void addSelection(ActionRequest req, PortletPreferences prefs)
88 throws Exception {
89
90 String assetType = ParamUtil.getString(req, "assetType");
91 long assetId = ParamUtil.getLong(req, "assetId");
92 int assetOrder = ParamUtil.getInteger(req, "assetOrder");
93
94 addSelection(assetType, assetId, assetOrder, prefs);
95 }
96
97 public static void addSelection(
98 String assetType, long assetId, int assetOrder,
99 PortletPreferences prefs)
100 throws Exception {
101
102 String[] manualEntries = prefs.getValues(
103 "manual-entries", new String[0]);
104
105 String assetConfig = _assetConfiguration(assetType, assetId);
106
107 if (assetOrder > -1) {
108 manualEntries[assetOrder] = assetConfig;
109 }
110 else {
111 manualEntries = ArrayUtil.append(manualEntries, assetConfig);
112 }
113
114 prefs.setValues("manual-entries", manualEntries);
115 }
116
117 public static void addRecentFolderId(
118 PortletRequest req, String className, long classPK) {
119
120 _getRecentFolderIds(req).put(className, classPK);
121 }
122
123 public static long getRecentFolderId(PortletRequest req, String className) {
124 Long classPK = _getRecentFolderIds(req).get(className);
125
126 if (classPK == null) {
127 return 0;
128 }
129 else {
130 return classPK.longValue();
131 }
132 }
133
134 private static String _assetConfiguration(String assetType, long assetId) {
135 String xml = null;
136
137 try {
138 DocumentFactory docFactory = DocumentFactory.getInstance();
139
140 Document doc = docFactory.createDocument(StringPool.UTF8);
141
142 Element asset = doc.addElement("asset");
143
144 asset.addElement("asset-type").addText(assetType);
145 asset.addElement("asset-id").addText(String.valueOf(assetId));
146
147 xml = XMLFormatter.toString(doc, StringPool.BLANK);
148 }
149 catch (IOException ioe) {
150 if (_log.isWarnEnabled()) {
151 _log.warn(ioe);
152 }
153 }
154
155 return xml;
156 }
157
158 private static Map<String, Long> _getRecentFolderIds(PortletRequest req) {
159 HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
160
161 HttpSession ses = httpReq.getSession();
162
163 ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
164 WebKeys.THEME_DISPLAY);
165
166 String key =
167 AssetPublisherUtil.class + "_" + themeDisplay.getPortletGroupId();
168
169 Map<String, Long> recentFolderIds =
170 (Map<String, Long>)ses.getAttribute(key);
171
172 if (recentFolderIds == null) {
173 recentFolderIds = new HashMap<String, Long>();
174 }
175
176 ses.setAttribute(key, recentFolderIds);
177
178 return recentFolderIds;
179 }
180
181 private static Log _log = LogFactory.getLog(AssetPublisherUtil.class);
182
183 }