Foren

Home » Liferay Portal » English » 3. Development

Kombinierte Ansicht Flache Ansicht Baumansicht
Threads [ Zurück | Nächste ]
toggle
Diego Pietralunga
[Velocity] Journal/WCD file properties from document library structure fiel
26. Oktober 2010 11:17
Antwort

Diego Pietralunga

Rang: New Member

Nachrichten: 17

Eintrittsdatum: 4. August 2009

Neue Beiträge

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. Oktober 2010 10:40
Antwort

Diego Pietralunga

Rang: New Member

Nachrichten: 17

Eintrittsdatum: 4. August 2009

Neue Beiträge

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. November 2011 10:47
Antwort

Francesco Marangi

Rang: New Member

Nachrichten: 9

Eintrittsdatum: 6. Februar 2009

Neue Beiträge

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. Februar 2013 10:37
Antwort

Jody Braaten

Rang: New Member

Nachrichten: 5

Eintrittsdatum: 13. November 2008

Neue Beiträge

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. Juli 2013 01:57
Antwort

Nicolas Lafaury

Rang: New Member

Nachrichten: 3

Eintrittsdatum: 25. September 2012

Neue Beiträge

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. Dezember 2014 12:52
Antwort

Enrique Valdes Lacasa

Rang: Junior Member

Nachrichten: 70

Eintrittsdatum: 29. Juli 2014

Neue Beiträge

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
juan meza
RE: [Velocity] Journal/WCD file properties from document library structure
22. Juni 2015 21:29
Antwort

juan meza

Rang: Regular Member

Nachrichten: 212

Eintrittsdatum: 6. Januar 2014

Neue Beiträge

in the new version of liferay (6.2 GA4)

1#set ($fileEntry = $dlService.getFileEntryByTitle($gid, $fid, $fileTitle))


changed to this:

1#set ($fileEntry = $dlService.getFileEntry($gid, $fid, $fileTitle))