[opencms-dev] help with contentload: show only files with a property configured

Nigel Kersten nigel at cofa.unsw.edu.au
Tue Feb 28 21:26:03 CET 2006


On 28/02/2006, at 9:36 PM, Villani Marcello wrote:

> I have an XML content that sets a property. How can I show only  
> contents that have that property configured?
> The contentload tag doesn’t allow me to specify any filter…
>
> I tried with something like this:
>
> java.util.List list = cms.getCmsObject().readResourcesWithProperty 
> ("/news","Provincia",provincia);
>
> for (int i=0, n=list.size(); i < n; i++){
>
>   org.opencms.file.CmsResource res = (org.opencms.file.CmsResource) 
> list.get(i);
>
>   elenco += res.getName()+"<br>";
>
>  }
>
> But I am not able to get all the properties and the content…

This might be the long way round, but it might give you a hand... ?  
I'm reading all resources of a certain type out of a folder, checking  
whether a certain property exists (whether it is a 'feature  
article'), and if it does, I'm adding it to a new List of objects,  
then picking a random one out of that list and then displaying the  
content as editable.


<%
cms.editable(true);
int cofaArticleType = 29;

java.util.List newsItems = cmsObject.readResources("/newsevents/ 
articles/",CmsResourceFilter.requireType(cofaArticleType));
java.util.ListIterator iter = newsItems.listIterator();
java.util.List featureItems = new java.util.LinkedList();
java.util.List newsContentItems = new java.util.LinkedList();

while (iter.hasNext())
{
	CmsResource newsItem = (CmsResource)iter.next();
	CmsXmlContent newsXml = CmsXmlContentFactory.unmarshal(cmsObject,  
newsItem, request);
	String featureArticle = newsXml.getStringValue(cmsObject,  
"NewsContent[1]/FeatureArticle[1]", locale);
   if(featureArticle != null)
   {
     featureItems.add(newsItem);
   }
}


java.util.Random myRandom = new java.util.Random();
int randomInt = myRandom.nextInt(featureItems.size());
CmsResource featureItem = (CmsResource)featureItems.get(randomInt);
CmsXmlContent featureXml = CmsXmlContentFactory.unmarshal(cmsObject,  
featureItem, request);

String articleUri = "/newsevents/news/view_news.html?item=" + "/ 
newsevents/articles/" + featureItem.getName();
String articleImage = featureXml.getStringValue(cmsObject,  
"NewsContent[1]/Content[1]/InsertImage[1]/Image", locale);
String articleCaption = featureXml.getStringValue(cmsObject,  
"NewsContent[1]/Content[1]/InsertImage[1]/Caption", locale);
String articleTitle = featureXml.getStringValue(cmsObject,  
"NewsContent[1]/Title", locale);
String articleFeatureSummary = featureXml.getStringValue(cmsObject,  
"NewsContent[1]/FeatureSummary", locale);


cms.include("/newsevents/articles/" + featureItem.getName(),  
featureItem.getName(), true);

out.println("<div class=\"image\">");
   out.println("<img src=\"" + articleImage + "?__scale=w:300,h:300,t: 
3\" />");
   out.println("<p>" + articleCaption + "</p>");
out.println("</div>");
out.println("<div class=\"mainnewssummary\">");
   out.println("<h2>" + articleTitle + "</h2>");
   out.println(articleFeatureSummary);
   out.println("<div style=\"text-align:right; margin-right:20px;  
margin-top: 5px\">");
     out.println("<a href=\"" + articleUri + "\" />Read More...</a>");
   out.println("</div>");	
out.println("</div>");

%>



-- 
Nigel Kersten [Senior Technical Officer]
College of Fine Arts, University of NSW, Australia.
CRICOS Provider Code: 00098G





More information about the opencms-dev mailing list