Tribune

Home » Liferay Portal » English » 3. Development

Vista Combinata Vista Piatta Vista ad Albero
Discussioni [ Precedente | Successivo ]
toggle
Diego Pietralunga
[Velocity] Journal/WCD file properties from document library structure fiel
26 ottobre 2010 11.17
Risposta

Diego Pietralunga

Punteggio: New Member

Messaggi: 17

Data di Iscrizione: 4 agosto 2009

Messaggi recenti

Hello everybody.

I'm stuck with this seemingly simple task:

Get file names (or size or owner or ID or else) from a WCD/Journal portlet using a document_library (repeatable) field.

I tried search around quite a lot but couldn't find a suitable and ready-to-go solution.

Here's my WCD structure:

<root>
<dynamic-element name='content' type='text_area' index-type='' repeatable='false'>
<meta-data>
<entry name="displayAsTooltip"><![CDATA]></entry>
<entry name="required"><![CDATA]></entry>
<entry name="instructions"><![CDATA[]]></entry>
<entry name="label"><![CDATA]></entry>
<entry name="predefinedValue"><![CDATA[]]></entry>
</meta-data>
</dynamic-element>
<dynamic-element name='files' type='document_library' index-type='' repeatable='true'>
<meta-data>
<entry name="displayAsTooltip"><![CDATA]></entry>
<entry name="required"><![CDATA]></entry>
<entry name="instructions"><![CDATA[]]></entry>
<entry name="label"><![CDATA[DocumentLibraryField]]></entry>
<entry name="predefinedValue"><![CDATA[]]></entry>
</meta-data>
</dynamic-element>
</root>

and the sample template trying to list link and file names:

Content is:<br/>
$content.getData()
<br/>
Files are:
<br/>
<ul>
#foreach ($ListItem in $files.getSiblings())

<li><a href='$ListItem.getData()'> ??</a> </li>

#end
</ul>


Content data is showing up and I can get the "files" fields contents (which are document library links without the host name, as per selection fron WCD editor)
I get something like this:

Content is:
Yocontent


Files are:
?? (<-- Note: link IS working here with url: http://myLiferayHost.here/documents/16292/18041/myUploadedFilename.here.pdf
??

I guess I need some service here, somehing like (velocity):

#set($dlFileEntryUtil = $serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLFileEntryLocalService"))
#set ($longGroupId = $getterUtil.getLong($groupId))
#set($fileEntry = $dlFileEntryUtil.getFileEntryByUuidAndGroupId($uuid,$longGroupId))
(from here )

but the question is:

What is the correct way to create a FileEntryXXX velocity object with the correct parameters if I (apparently) have only the (web) file path (such as /documents/16292/18041/myUploadedFilename.here.pdf) ?

Do I need some URL parsing? Or there's a dedicated Liferay way ?
What class and method(s) to use?

Config:

Liferay 6.0.10 from Trunk (30 sept 2010 build)
Java 6
Linux CentOS

P.S= I put an empty property journal.template.velocity.restricted.variables=
in portal-ext.properties


Thanks in advance!

Diego
Diego Pietralunga
RE: [Velocity] Journal/WCD file properties from document library structure
28 ottobre 2010 10.40
Risposta

Diego Pietralunga

Punteggio: New Member

Messaggi: 17

Data di Iscrizione: 4 agosto 2009

Messaggi recenti

Nobody has a little hint? ... I guess it's a common task... :-(((((


While searching meanwhile, I made up my mind that you need to parse the journal document library fields' URLs according to the FriendlyUrlMapping naming strategy. Then there's a possibility to che a FileEntry handle (still wondering if there's a "standard" LR way...)

Supposing I am right, can somebody please post a quick and tested LR/Velocity code snippet to parse a document library URL?
Francesco Marangi
RE: [Velocity] Journal/WCD file properties from document library structure
3 novembre 2011 10.47
Risposta

Francesco Marangi

Punteggio: New Member

Messaggi: 9

Data di Iscrizione: 6 febbraio 2009

Messaggi recenti

Even if this thread is quite "ancient", I'd love to contribute the solution I used for it.

First off, make sure you have the serviceLocator enabled in your portal-ext.properties:
1journal.template.velocity.restricted.variables=


Suppose the document library link is stored in a field called 'doc'.

 1## Get the group ID of the document library you are referring to
 2#set ($gid = $getterUtil.getLong($doc.getData().split("/").get(2)))
 3
 4## Get the folder ID of the DL
 5#set ($fid = $getterUtil.getLong($doc.getData().split("/").get(3)))
 6
 7## Get the file title
 8#set ($fileTitle = $doc.getData().split("/").get(4))
 9
10## Since the file title is URL encoded, we need to decode it first
11## (I assume you are using UTF-8 as charset)
12#set ($tmp = {})
13#set ($decoder = $tmp.class.forName("java.net.URLDecoder").newInstance())
14#set ($fileTitle = $decoder.decode($fileTitle, "UTF-8"))
15
16## Now we can retrieve the FileEntry from the DL
17#set ($dlService = $serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLFileEntryLocalService"))
18#set ($fileEntry = $dlService.getFileEntryByTitle($gid, $fid, $fileTitle))


That's it, hope this helps other developers!
Jody Braaten
RE: [Velocity] Journal/WCD file properties from document library structure
26 febbraio 2013 10.37
Risposta

Jody Braaten

Punteggio: New Member

Messaggi: 5

Data di Iscrizione: 13 novembre 2008

Messaggi recenti

Thanks for posting your solution to the 'ancient' thread - it saved me tons of time!! emoticon
Nicolas Lafaury
RE: [Velocity] Journal/WCD file properties from document library structure
23 luglio 2013 1.57
Risposta

Nicolas Lafaury

Punteggio: New Member

Messaggi: 3

Data di Iscrizione: 25 settembre 2012

Messaggi recenti

Hello

Thanks for your solution. Is there any other option that split the URL?

BTW, code was not working for me, set ($fileEntry = $dlService.getFileEntryByTitle($gid, $fid, $fileTitle)) was always null

I had to change

## Get the file uid
#set ($fileUid = $fichier.getData().split("/").get(5))

#set ($fileEntry = $dlService.getFileEntryByUuidAndGroupId($fileUid, $gid))


Many thanks

Nicolas
Enrique Valdes Lacasa
RE: [Velocity] Journal/WCD file properties from document library structure
19 dicembre 2014 12.52
Risposta

Enrique Valdes Lacasa

Punteggio: Junior Member

Messaggi: 29

Data di Iscrizione: 29 luglio 2014

Messaggi recenti

Thanks for the code Francesco. One important thing: in Liferay 6.2 the portal property to enable the $serviceLocator was changed! instead of
1journal.template.velocity.restricted.variables=
it is now:
1velocity.engine.restricted.variables=
. These property changes are scary sometimes, but they are supposed to be for the better. Check out the answer by James Falkner in this thread to know more about this:

6.2 serviceLocator variable

Best regards