Update of 
/var/cvs/contributions/CMSContainer/cmsc/portlets/src/java/com/finalist/cmsc/portlets
In directory 
james.mmbase.org:/tmp/cvs-serv15423/cmsc/portlets/src/java/com/finalist/cmsc/portlets

Modified Files:
        RelatedContentPortlet.java 
Log Message:
CMSC-959  - Added two new methods. The first: getElementIdFromContentURL uses a 
regex to try and find a elementId in the request URL generated by a contentURL. 
The second is just a convenience method: getElementId, that has protected 
scope. This allows you to extend and override the portlet class and make your 
own implementation to get an elementId.


See also: 
http://cvs.mmbase.org/viewcvs/contributions/CMSContainer/cmsc/portlets/src/java/com/finalist/cmsc/portlets
See also: http://www.mmbase.org/jira/browse/CMSC-959


Index: RelatedContentPortlet.java
===================================================================
RCS file: 
/var/cvs/contributions/CMSContainer/cmsc/portlets/src/java/com/finalist/cmsc/portlets/RelatedContentPortlet.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -b -r1.6 -r1.7
--- RelatedContentPortlet.java  10 May 2008 16:31:23 -0000      1.6
+++ RelatedContentPortlet.java  12 Jun 2008 13:44:17 -0000      1.7
@@ -4,28 +4,45 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import javax.portlet.PortletException;
 import javax.portlet.RenderRequest;
 import javax.portlet.RenderResponse;
 import javax.servlet.http.HttpServletRequest;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.pluto.core.impl.PortletRequestImpl;
 
-import com.finalist.cmsc.beans.om.*;
+import com.finalist.cmsc.beans.om.NavigationItem;
+import com.finalist.cmsc.beans.om.Page;
+import com.finalist.cmsc.beans.om.Portlet;
 import com.finalist.cmsc.portalImpl.PortalConstants;
 import com.finalist.cmsc.services.sitemanagement.SiteManagement;
 
-import org.apache.commons.lang.StringUtils;
+import net.sf.mmapps.commons.util.StringUtil;
 
 public class RelatedContentPortlet extends AbstractContentPortlet {
 
+       /**
+        * This rexex pattern is uesd to match the elementId from a contentURL.
+        */
+       private static final String CONTENTURL_ELEMENTID_PATTERN = 
"/content/([0-9]+)";
+
+       @Override
    protected void doView(RenderRequest req, RenderResponse res) throws 
PortletException, IOException {
       String window = req.getPreferences().getValue(WINDOW, null);
       if (StringUtils.isNotEmpty(window)) {
          String elementId = getElementIdFromRequestParameters(req, window);
          if (StringUtils.isEmpty(elementId)) {
             elementId = getElementIdFromScreen(req, window);
+                               if (StringUtil.isEmpty(elementId)) {
+                                       elementId = 
getElementIdFromContentURL(req);
+                                       if (StringUtil.isEmpty(elementId)) {
+                                               elementId = getElementId(req);
+                                       }
+                               }
          }
 
          if (StringUtils.isNotEmpty(elementId)) {
@@ -35,6 +52,10 @@
       super.doView(req, res);
    }
 
+       protected String getElementId(RenderRequest req) {
+               // Extension point
+               return null;
+       }
 
    private String getElementIdFromScreen(RenderRequest req, String window) {
       Integer pageId = getCurrentPageId(req);
@@ -50,18 +71,15 @@
       return null;
    }
 
-
     private Integer getCurrentPageId(RenderRequest req) {
         String pageId = (String) 
req.getAttribute(PortalConstants.CMSC_OM_PAGE_ID);
         return Integer.valueOf(pageId);
     }
 
-
    private HttpServletRequest getServletRequest(RenderRequest req) {
       return (HttpServletRequest) ((PortletRequestImpl) req).getRequest();
    }
 
-
    private String getElementIdFromRequestParameters(RenderRequest req, String 
window) {
       String requestURL = getServletRequest(req).getRequestURL().toString();
       String paramName = "/_rp_" + window + "_elementId/1_";
@@ -78,7 +96,6 @@
       return null;
    }
 
-
    @Override
    protected void doEditDefaults(RenderRequest req, RenderResponse res) throws 
IOException, PortletException {
       Integer pageid = getCurrentPageId(req);
@@ -90,9 +107,16 @@
          Collections.sort(orderedPositions);
          setAttribute(req, "pagepositions", new 
ArrayList<String>(orderedPositions));
       }
-
       super.doEditDefaults(req, res);
-
    }
 
+       private String getElementIdFromContentURL(RenderRequest req) {
+               String requestURL = 
getServletRequest(req).getRequestURL().toString();
+               Pattern pattern = Pattern.compile(CONTENTURL_ELEMENTID_PATTERN);
+               Matcher matcher = pattern.matcher(requestURL);
+               if (matcher.find() && matcher.groupCount() >= 1) {
+                       return matcher.group(1);
+               }
+               return null;
+       }
 }
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to