Forums

Home » Liferay Portal » English » 2. Using Liferay » General

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Filippo Boatto
get the last article in velocity
June 9, 2009 2:47 AM
Answer

Filippo Boatto

Rank: Junior Member

Posts: 99

Join Date: February 1, 2007

Recent Posts

i would get the last article of type "marketing" in a velocity template.
This is my code:
 1
 2#set($journalArticleLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService"))
 3
 4#set($keywords = "")
 5#set($version = "")
 6#set($type = "marketing")
 7#set($structureIds = "")
 8#set($templateIds = "")
 9#set($displayDateGT = "")
10#set($displayDateLT = "")
11#set($approved = "true")
12#set($expired = "false")
13#set($reviewDate = "")
14#set($start = "")
15#set($end = "")
16#set($obc = "DESC")
17
18#set($journalArticlesList = $journalArticleLocalService.search(
19                                         $companyId, $groupId,
20                                                    $keywords, $type,
21                                            $structureIds, $templateIds,
22                                            $displayDateGT, $displayDateLT,
23                                         $approved, $expired,
24                                         $reviewDate,
25                                         $start, $end, $obc))
26
27#foreach ($journalArticle in $journalArticlesList )
28  $journalArticle.getTitle()<br>
29#end


but doesn't works :-(
any suggestions?
Amos Fong
RE: get the last article in velocity
June 9, 2009 6:40 PM
Answer

Amos Fong

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1912

Join Date: October 7, 2008

Recent Posts

You want to get the orderbycomparator like this:

1#set ($obc = $portal.getClass().forName("com.liferay.portlet.journal.util.comparator.ArticleCreateDateComparator").newInstance()))


Also you want to use $getterUtil.getLong($groupId) and $getterUtil.getLong($companyId) too or it won't work. There might be some other syntax errors in your code because it didn't work for me.

Make sure you have method and types correct in passing to the method.
Filippo Boatto
RE: get the last article in velocity
June 10, 2009 2:43 AM
Answer

Filippo Boatto

Rank: Junior Member

Posts: 99

Join Date: February 1, 2007

Recent Posts

thanks Amos Fong!!
now with thi code:
 1
 2#set($journalArticleLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService"))
 3
 4#set($company = $getterUtil.getLong($companyId))
 5#set($group = $getterUtil.getLong($groupId))
 6#set($keywords = "")
 7#set($version = $null.null)
 8#set($type = "products")
 9#set($structureId = "")
10#set($templateId = "")
11#set($displayDateGT = $getterUtil.getDate("19990101",$dateUtil.getISOFormat("19990101")))
12#set($displayDateLT = $null.null)
13#set($approved = $getterUtil.getBoolean("true"))
14#set($expired = $getterUtil.getBoolean("false"))
15#set($reviewDate = $null.null)
16#set($start = -1)
17#set($end = -1)
18#set ($obc = $portal.getClass().forName("com.liferay.portlet.journal.util.comparator.ArticleDisplayDateComparator").newInstance())
19
20
21#set($journalArticlesList = $journalArticleLocalService.search(
22                                         $company, $group,
23                                                    $keywords, $version,
24                                                    $type, $structureId, $templateId,
25                                            $displayDateGT, $displayDateLT, $approved,
26                                            $expired, $reviewDate,$start, $end,
27                                            $obc))
28
29#set($debug=$getterUtil.getBoolean("true"))
30#if ($debug)
31    @company_id@<br>
32    @group_id@<br>
33    portletgrp: $scopeGroupId<br>
34    $company  <br>
35    $group<br>
36   
37    $keywords<br>
38    $version<br>
39    $type<br>
40    $structureIds<br>
41    $templateIds<br>
42    $displayDateGT<br>
43    $displayDateLT<br>
44    $approved<br>
45    $expired<br>
46    $reviewDate<br>
47    $start<br>
48    $end<br>
49    $obc<br>
50   
51    journalListsize: $journalArticlesList.size()<br>
52   
53#end
54
55#foreach ($article in $journalArticlesList )
56  $article.***
57#end


i have all articles that i need.
But the articles has a structure...
How to access the fields of the structure in the cycle "#foreach ($article in $journalArticlesList )" ?

for example my structure "article-news" has a field "article-big-image" and $article is created with this structure.
How could i access the propertie "article-big-image" of the $article?
Amos Fong
RE: get the last article in velocity
June 10, 2009 5:14 PM
Answer

Amos Fong

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1912

Join Date: October 7, 2008

Recent Posts

Not sure if this translates to velocity well or not, but you can try this:

http://www.liferay.com/web/guest/community/forums/-/message_boards/message/2352114

Check this wiki for any util classes you need.

http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/How%20To%20Access%20Objects%20From%20A%20Velocity%20Template
Filippo Boatto
RE: get the last article in velocity
June 17, 2009 8:00 AM
Answer

Filippo Boatto

Rank: Junior Member

Posts: 99

Join Date: February 1, 2007

Recent Posts

Solved using liferay 5.2.3 and velocity template

Structure:
 1
 2<root>
 3  <dynamic-element name='titoloTesto' type='text' repeatable='false'></dynamic-element>
 4  <dynamic-element name='titoloLink' type='document_library' repeatable='false'></dynamic-element>
 5  <dynamic-element name='testoBreve' type='text_box' repeatable='false'></dynamic-element>
 6  <dynamic-element name='testoEsteso' type='text_area' repeatable='false'></dynamic-element>
 7  <dynamic-element name='immaginePiccola' type='image' repeatable='false'></dynamic-element>
 8  <dynamic-element name='immagineDettaglio1' type='image' repeatable='false'></dynamic-element>
 9  <dynamic-element name='testoImmagineDettaglio1' type='text' repeatable='false'></dynamic-element>
10  <dynamic-element name='immagineDettaglio2' type='image' repeatable='false'></dynamic-element>
11  <dynamic-element name='testoImmagineDettaglio2' type='text' repeatable='false'></dynamic-element>
12  <dynamic-element name='immagineDettaglio3' type='image' repeatable='false'></dynamic-element>
13  <dynamic-element name='testoImmagineDettaglio3' type='text' repeatable='false'></dynamic-element>
14  <dynamic-element name='attachment1' type='document_library' repeatable='false'></dynamic-element>
15  <dynamic-element name='descrizioneAttachment1' type='text' repeatable='false'></dynamic-element>
16  <dynamic-element name='attachment2' type='document_library' repeatable='false'></dynamic-element>
17  <dynamic-element name='descrizioneAttachment2' type='text' repeatable='false'></dynamic-element>
18  <dynamic-element name='attachment3' type='document_library' repeatable='false'></dynamic-element>
19  <dynamic-element name='descrizioneAttachment3' type='text' repeatable='false'></dynamic-element>
20  <dynamic-element name='attachment4' type='document_library' repeatable='false'></dynamic-element>
21  <dynamic-element name='descrizioneAttachment4' type='text' repeatable='false'></dynamic-element>
22  <dynamic-element name='attachment5' type='document_library' repeatable='false'></dynamic-element>
23  <dynamic-element name='descrizioneAttachment5' type='text' repeatable='false'></dynamic-element>
24  <dynamic-element name='attachment6' type='document_library' repeatable='false'></dynamic-element>
25  <dynamic-element name='descrizioneAttachment6' type='text' repeatable='false'></dynamic-element>
26</root>


Template (to show the last news of type "marketing"):
 1
 2<!-- Questo template estrae l'ultima news di tipo marketing
 3     e visualizza i campi della propria struttura
 4-->
 5
 6#set ($document = $saxReaderUtil.readURL("http://@portal_url@/c/journal/get_articles?groupId=@group_id@&type=[b]marketing[/b]&delta=[b]1[/b]&approved=true&expired=false&orderBy=display-date"))
 7#set ($root = $document.getRootElement())
 8#set ($articles = $root.selectNodes("/result-set/result/root"))
 9
10#foreach ($article IN $articles)
11  #set ($articleId = $article.selectSingleNode("dynamic-element[@name='reserved-article-id']/dynamic-content"))
12  #set ($articleTitle = $article.selectSingleNode("dynamic-element[@name='reserved-article-title']/dynamic-content"))
13  #set ($articleModifiedDate = $article.selectSingleNode("dynamic-element[@name='reserved-article-modified-date']/dynamic-content"))
14  #set ($articleAuthorName = $article.selectSingleNode("dynamic-element[@name='reserved-article-author-name']/dynamic-content"))
15
16  <!-- recupero i dati della struttura -->
17  #set ($article_TitoloLink= $article.selectSingleNode("dynamic-element[@name='titoloLink']/dynamic-content"))
18  #set ($article_TitoloTesto = $article.selectSingleNode("dynamic-element[@name='titoloTesto']/dynamic-content"))
19  #set ($article_ImmaginePiccola = $article.selectSingleNode("dynamic-element[@name='immaginePiccola']/dynamic-content"))
20  #set ($article_TestoBreve = $article.selectSingleNode("dynamic-element[@name='testoBreve']/dynamic-content"))
21
22<div id="news_sx">
23   <div class="testo">
24       <p>
25       <!-- titolo della news attiva-->
26             #if ($article_TitoloLink.getData()!="") 
27                #set ($articleUrl = "$article_TitoloLink.getData()")
28             #set ($target = "_blank")
29             #else
30             #set ($articleUrl = "http://@portal_url@/web/4ws-trade/news-detail/-/journal_content/56_INSTANCE_8Rjo/@group_id@/$articleId.getData()")
31            #set ($target = "_top")
32             #end
33             <h2 class="titolo_news"><a href="$articleUrl" target="$target">$article_TitoloTesto.getData()</a></h2>
34          <!-- testo della news attiva-->
35          #if ($article_ImmaginePiccola.getData()!="")
36              <h4 style="width:292px">
37                  $article_TestoBreve.getData()
38              </h4>
39          #else
40              <h4 style="width:400px">
41                  $article_TestoBreve.getData()
42              </h4>
43          #end
44       </p>
45   </div>
46   #if ($article_ImmaginePiccola.getData()!="")
47      <div class="immagine">
48         <a href="$articleUrl" target="$target"><img src="$article_ImmaginePiccola.getData()" style="max-width: 120px;    max-height: 120px;"  border="0"/></a>
49      </div>
50   #end
51</div>
52#end
Amos Fong
RE: get the last article in velocity
June 17, 2009 10:19 AM
Answer

Amos Fong

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1912

Join Date: October 7, 2008

Recent Posts

wow nice work Filippo!
willard largueza macay
RE: get the last article in velocity
July 22, 2010 2:48 AM
Answer

willard largueza macay

Rank: New Member

Posts: 9

Join Date: January 13, 2010

Recent Posts

wow.. this is what i need.. i will try this.. emoticon
GALLET Victor
RE: get the last article in velocity
April 5, 2011 1:52 AM
Answer

GALLET Victor

Rank: New Member

Posts: 12

Join Date: March 9, 2011

Recent Posts

Very nice job Filippo. I thank you emoticon
Eduardo Grotteschi
RE: get the last article in velocity
November 28, 2013 9:59 AM
Answer

Eduardo Grotteschi

Rank: New Member

Posts: 22

Join Date: November 12, 2013

Recent Posts

Hi guys, i am having troubles trying to build my asset template using velocity.

I added the portal-ext.properties file to remove the blockers:

velocity.engine.restricted.classes=
velocity.engine.restricted.variables=
freemarker.engine.restricted.classes=
freemarker.engine.restricted.variables=


And then i tried to add this code to my template:

#set ($document = $saxReaderUtil.readURL("http://localhost:8080/c/journal/get_articles?delta=100&approved=true&expired=false&orderBy=display-date"))
#set ($root = $document.getRootElement())
#set ($articles = $root.selectNodes("/result-set/result/root"))

#foreach ($article IN $articles)
#set ($articleId = $article.selectSingleNode("dynamic-element[@name='Course_Title']/dynamic-content"))
Articulo = $articleId
#end

But nothing was printed on my template. This is the structure that i get when i call that url:

<result-set>
<result>
<root available-locales="en_US" default-locale="en_US">
<dynamic-element name="Course_Title" index="0" type="text" index-type="keyword">...</dynamic-element>
<dynamic-element name="Course_Description" index="0" type="text_box" index-type="keyword">...</dynamic-element>
<dynamic-element name="Free_Video_URL" index="0" type="text" index-type="keyword">...</dynamic-element>
<dynamic-element name="Upload_Date" index="0" index-type="keyword">...</dynamic-element>
...

The "Course_Title" is present in the resultset, but never access to the foreach.

Could you help me?
James Falkner
RE: get the last article in velocity
December 2, 2013 3:33 PM
Answer

James Falkner

Community Moderator

Rank: Liferay Legend

Posts: 1406

Join Date: September 17, 2010

Recent Posts

Hey Eduardo, welcome to the community!

I believe you need to add a groupId parameter (since you are requesting all articles, but get_articles won't give you articles across all scopes) (and you should use the portal_url token as well). So can you try
1#set ($document = $saxReaderUtil.readURL("http://@portal_url@/c/journal/get_articles?groupId=${scopeGroupId}&delta=100&approved=true&expired=false&orderBy=display-date"))
Eduardo Grotteschi
RE: get the last article in velocity
December 3, 2013 8:48 AM
Answer

Eduardo Grotteschi

Rank: New Member

Posts: 22

Join Date: November 12, 2013

Recent Posts

Thanks James!! now i can retrieve info for my articles!
Eduardo Grotteschi
RE: get the last article in velocity
December 4, 2013 5:40 AM
Answer

Eduardo Grotteschi

Rank: New Member

Posts: 22

Join Date: November 12, 2013

Recent Posts

James, i need something but couldnt find how to do it, i need to order the articles "descending" by article title, i tried this:

http://localhost:8080/c/journal/get_articles?groupId=${scopeGroupId}&type=Autor&delta=100&approved=true&expired=false&orderBy=article-title&desc

but nothing happend, do you know how to do it?

Regards.