From: <die...@us...> - 2011-02-07 17:57:01
|
Revision: 3307 http://openutils.svn.sourceforge.net/openutils/?rev=3307&view=rev Author: diego_schivo Date: 2011-02-07 17:56:54 +0000 (Mon, 07 Feb 2011) Log Message: ----------- MEDIA-214 playlist view Modified Paths: -------------- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/playlist/pages/PlaylistView.java Modified: trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/playlist/pages/PlaylistView.java =================================================================== --- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/playlist/pages/PlaylistView.java 2011-02-07 15:05:15 UTC (rev 3306) +++ trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/playlist/pages/PlaylistView.java 2011-02-07 17:56:54 UTC (rev 3307) @@ -20,15 +20,20 @@ package net.sourceforge.openutils.mgnlmedia.playlist.pages; import info.magnolia.cms.core.Content; +import info.magnolia.cms.core.ItemType; import info.magnolia.cms.i18n.I18nContentSupportFactory; import info.magnolia.cms.util.ContentUtil; import info.magnolia.cms.util.NodeDataUtil; import info.magnolia.context.MgnlContext; import java.io.IOException; +import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; import java.util.HashMap; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; @@ -36,15 +41,21 @@ import java.util.Map.Entry; import javax.jcr.RepositoryException; +import javax.jcr.Value; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.AdvancedResult; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.AdvancedResultItem; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.ResultIterator; import net.sourceforge.openutils.mgnlmedia.media.configuration.MediaConfigurationManager; import net.sourceforge.openutils.mgnlmedia.media.configuration.MediaTypeConfiguration; import net.sourceforge.openutils.mgnlmedia.media.lifecycle.MediaModule; import net.sourceforge.openutils.mgnlmedia.media.pages.MediaBean; import net.sourceforge.openutils.mgnlmedia.media.pages.MediaBeanBuilder; import net.sourceforge.openutils.mgnlmedia.media.pages.MessagesTemplatedMVCHandler; +import net.sourceforge.openutils.mgnlmedia.media.pages.SortMode; import net.sourceforge.openutils.mgnlmedia.media.tags.el.MediaEl; import net.sourceforge.openutils.mgnlmedia.playlist.PlaylistConstants; @@ -204,29 +215,8 @@ Content media = ContentUtil.getContentByUUID(MediaModule.REPO, mediaUUID); if (media != null) { - PlaylistEntryBean entry = new PlaylistEntryBean(); + PlaylistEntryBean entry = playlistEntryBean(media, propNames); entry.setHandle(subNode.getHandle()); - entry.setMedia(media.getUUID()); - entry.setMediaHandle(media.getHandle()); - MediaTypeConfiguration typeConf = MediaConfigurationManager - .getInstance() - .getMediaTypeConfigurationFromMedia(media); - if (typeConf != null) - { - entry.setMediaDialog(typeConf.getDialog()); - } - entry.setThumbnail(MediaEl.thumbnail(media)); - entry.setType(NodeDataUtil.getString(media, "type")); - entry.setTitle(I18nContentSupportFactory - .getI18nSupport() - .getNodeData(media, propNames.get("title")) - .getString()); - entry.setDescription(I18nContentSupportFactory - .getI18nSupport() - .getNodeData(media, propNames.get("description")) - .getString()); - entry - .setTags(I18nContentSupportFactory.getI18nSupport().getNodeData(media, propNames.get("tags")).getString()); entries.add(entry); } else @@ -236,6 +226,51 @@ new Object[]{mediaUUID, subNode.getName(), playlist.getHandle() }); } } + + String requestNodeName = "request"; + try + { + if (node.hasContent(requestNodeName)) + { + Content requestNode = node.getContent(requestNodeName); + Collection<Content> paramNodes = requestNode.getChildren(ItemType.CONTENTNODE); + final Map<String, String[]> map = new HashMap<String, String[]>(); + for (Content paramNode : paramNodes) + { + String paramName = NodeDataUtil.getString(paramNode, "name"); + Value[] jcrValues = paramNode.getNodeData("value").getValues(); + String[] paramValues = new String[jcrValues.length]; + for (int i = 0; i < jcrValues.length; i++) + { + paramValues[i] = jcrValues[i].getString(); + } + map.put(paramName, paramValues); + } + AdvancedResult searchResult = MediaEl + .module() + .getSearch() + .search( + new CustomParamsRequest(request, map, false), + null, + null, + true, + SortMode.SCORE, + 0, + 1); + ResultIterator<AdvancedResultItem> items = searchResult.getItems(); + while (items.hasNext()) + { + AdvancedResultItem item = items.next(); + PlaylistEntryBean entry = playlistEntryBean(item, propNames); + entries.add(entry); + } + } + } + catch (RepositoryException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } playlist.setEntries(entries); } else @@ -428,4 +463,139 @@ return metas; } + private PlaylistEntryBean playlistEntryBean(Content media, Map<String, String> propNames) + { + PlaylistEntryBean entry = new PlaylistEntryBean(); + entry.setMedia(media.getUUID()); + entry.setMediaHandle(media.getHandle()); + MediaTypeConfiguration typeConf = MediaConfigurationManager.getInstance().getMediaTypeConfigurationFromMedia( + media); + if (typeConf != null) + { + entry.setMediaDialog(typeConf.getDialog()); + } + entry.setThumbnail(MediaEl.thumbnail(media)); + entry.setType(NodeDataUtil.getString(media, "type")); + entry.setTitle(I18nContentSupportFactory + .getI18nSupport() + .getNodeData(media, propNames.get("title")) + .getString()); + entry.setDescription(I18nContentSupportFactory + .getI18nSupport() + .getNodeData(media, propNames.get("description")) + .getString()); + entry.setTags(I18nContentSupportFactory.getI18nSupport().getNodeData(media, propNames.get("tags")).getString()); + return entry; + + } + + // freemarker.ext.servlet.IncludePage.CustomParamsRequest + private static final class CustomParamsRequest extends HttpServletRequestWrapper + { + + private final HashMap paramsMap; + + private CustomParamsRequest(HttpServletRequest request, Map paramMap, boolean inheritParams) + { + super(request); + paramsMap = inheritParams ? new HashMap(request.getParameterMap()) : new HashMap(); + for (Iterator it = paramMap.entrySet().iterator(); it.hasNext();) + { + Map.Entry entry = (Map.Entry) it.next(); + String name = String.valueOf(entry.getKey()); + Object value = entry.getValue(); + final String[] valueArray; + if (value == null) + { + // Null values are explicitly added (so, among other + // things, we can hide inherited param values). + valueArray = new String[]{null }; + } + else if (value instanceof String[]) + { + // String[] arrays are just passed through + valueArray = (String[]) value; + } + else if (value instanceof Collection) + { + // Collections are converted to String[], with + // String.valueOf() used on elements + Collection col = (Collection) value; + valueArray = new String[col.size()]; + int i = 0; + for (Iterator it2 = col.iterator(); it2.hasNext();) + { + valueArray[i++] = String.valueOf(it2.next()); + } + } + else if (value.getClass().isArray()) + { + // Other array types are too converted to String[], with + // String.valueOf() used on elements + int len = Array.getLength(value); + valueArray = new String[len]; + for (int i = 0; i < len; ++i) + { + valueArray[i] = String.valueOf(Array.get(value, i)); + } + } + else + { + // All other values (including strings) are converted to a + // single-element String[], with String.valueOf applied to + // the value. + valueArray = new String[]{String.valueOf(value) }; + } + String[] existingParams = (String[]) paramsMap.get(name); + int el = existingParams == null ? 0 : existingParams.length; + if (el == 0) + { + // No original params, just put our array + paramsMap.put(name, valueArray); + } + else + { + int vl = valueArray.length; + if (vl > 0) + { + // Both original params and new params, prepend our + // params to original params + String[] newValueArray = new String[el + vl]; + System.arraycopy(valueArray, 0, newValueArray, 0, vl); + System.arraycopy(existingParams, 0, newValueArray, vl, el); + paramsMap.put(name, newValueArray); + } + } + } + } + + public String[] getParameterValues(String name) + { + String[] value = ((String[]) paramsMap.get(name)); + return value != null ? (String[]) value.clone() : null; + } + + public String getParameter(String name) + { + String[] values = (String[]) paramsMap.get(name); + return values != null && values.length > 0 ? values[0] : null; + } + + public Enumeration getParameterNames() + { + return Collections.enumeration(paramsMap.keySet()); + } + + public Map getParameterMap() + { + HashMap clone = (HashMap) paramsMap.clone(); + for (Iterator it = clone.entrySet().iterator(); it.hasNext();) + { + Map.Entry entry = (Map.Entry) it.next(); + entry.setValue(((String[]) entry.getValue()).clone()); + } + return Collections.unmodifiableMap(clone); + } + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |