Fórum

Get ServiceContext in Freemarker template

Beppo Ivel, modificado 9 Anos atrás.

Get ServiceContext in Freemarker template

Regular Member Postagens: 112 Data de Entrada: 09/04/14 Postagens Recentes
Hi.

I try to run move the file "MyTestFile" from folder Test1 to folder Test2 in my freemarker template:


<#assign DLAppServiceUtil = serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLAppService")>

<#assign TestFolder = DLAppServiceUtil.getFolder(groupId,0,"Test1")>
<#assign UploadFolder = DLAppServiceUtil.getFolder(groupId,0,"Test2")>
                  
<#assign Datei = DLAppServiceUtil.getFileEntry(groupId, TestFolder.getFolderId(), "MyTestFile")>
<#assign File = DLAppServiceUtil.moveFileEntry(Datei.getFileEntryId(), UploadFolder.getFolderId(), serviceContext)>


How to access and use the parameter "serviceContext" for the final call "moveFileEntry"???

I did a research on it and found the classes "com.liferay.portal.service.ServiceContextThreadLocal" and "ServiceContextFactory" which belongs to the parameter serviceContext but I cant load this classes.
thumbnail
Andew Jardine, modificado 9 Anos atrás.

RE: Get ServiceContext in Freemarker template

Liferay Legend Postagens: 2416 Data de Entrada: 22/12/10 Postagens Recentes
Hi Beppo,

I'm not sure how you do it in freemarker as I normally go with velocity for my templates. I have used this in VM templates in the past to get the service context --

#set ($serviceContext = $portal.getClass().forName("com.liferay.portal.service.ServiceContextThreadLocal").getServiceContext())

You could try to figure out the equivalent in freemarker.
Beppo Ivel, modificado 9 Anos atrás.

RE: Get ServiceContext in Freemarker template

Regular Member Postagens: 112 Data de Entrada: 09/04/14 Postagens Recentes
Hi I found this command also at this site. But Iam not able to translate it to freemarker:

<#assign DLAppServiceUtil = serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLAppService")>


<#assign TestFolder = DLAppServiceUtil.getFolder(groupId,0,"Test")>
<#assign UploadFolder = DLAppServiceUtil.getFolder(groupId,0,"Upload")>
<#assign Datei = DLAppServiceUtil.getFileEntry(groupId, TestFolder.getFolderId(), "hulk")> 

<#assign a = portal.getClass()>
${a.getName()}

<#assign b = a.forName("com.liferay.portal.service.ServiceContextThreadLocal")>


The last line creates the error:

Expression a.forName is undefined on line 11, column 14 in 10157#10197#11847.
Beppo Ivel, modificado 9 Anos atrás.

RE: Get ServiceContext in Freemarker template

Regular Member Postagens: 112 Data de Entrada: 09/04/14 Postagens Recentes
I also found:

<bean id="com.liferay.portal.kernel.util.ThreadLocalDistributor" class="com.liferay.portal.kernel.util.ThreadLocalDistributor">
		<property name="threadLocalSources">
			<list>
				<bean class="com.liferay.portal.kernel.util.KeyValuePair">
					<constructor-arg name="key" value="com.liferay.portal.service.ServiceContextThreadLocal" />
					<constructor-arg name="value" value="_serviceContextThreadLocal" />
				</bean>
				<bean class="com.liferay.portal.kernel.util.KeyValuePair">
					<constructor-arg name="key" value="com.liferay.portal.kernel.staging.MergeLayoutPrototypesThreadLocal" />
					<constructor-arg name="value" value="_inProgress" />
				</bean>
			</list>
		</property>
	</bean>


in the util-spring.xml. So is it possible to load the class "com.liferay.portal.service.ServiceContextThreadLocal" in a other way?
thumbnail
s s, modificado 8 Anos atrás.

RE: Get ServiceContext in Freemarker template forName

Junior Member Postagens: 28 Data de Entrada: 29/01/10 Postagens Recentes
I too have need to use these "forName" sort of statements in FreeMarker but cannot figure out how to convert. In velocity they look like this (as examples)..
#set ($portletPreferencesFactoryUtil = $portal.getClass().forName("com.liferay.portlet.PortletPreferencesFactoryUtil"))
#set ( $assetEntryQuery = $portal.getClass().forName( "com.liferay.portlet.asset.service.persistence.AssetEntryQuery" ).newInstance()) 

Freemarker does not seem to like the forName (tried just putting it within the .getClass(here) but that doesn't work). Noticed a few other posts with others with the same problem. Any help much appreciated.
thumbnail
s s, modificado 8 Anos atrás.

RE: Get ServiceContext in Freemarker template forName

Junior Member Postagens: 28 Data de Entrada: 29/01/10 Postagens Recentes
sry, kept searching and found this sort of statement on github (from Ray); no idea if they do the same thing yet but they "process" without error so that may be a good sign
&lt;#assign PortletPreferencesFactoryUtil = staticUtil["com.liferay.portlet.PortletPreferencesFactoryUtil"] /&gt;
&lt;#assign assetEntryQuery = staticUtil["com.liferay.portlet.asset.service.persistence.AssetEntryQuery"] /&gt;


*UPDATE*
For the bottom, sort of works...if I look at the resulted assetEntryQuery is has this in it;
key:checkOrderByCol ,type: type="method"
key:checkOrderByType ,type: type="method"
key:ORDER_BY_COLUMNS ,type: type="hash_ex"

But, that isn't the ones I need (setAllCategoryIds for example). The whole list should match what's on this page: https://docs.liferay.com/portal/6.2/javadocs/com/liferay/portlet/asset/service/persistence/AssetEntryQuery.html. Right? Any clues on that one (how to get those other methods) muchly appreciated.
thumbnail
James Falkner, modificado 8 Anos atrás.

RE: Get ServiceContext in Freemarker template forName

Liferay Legend Postagens: 1399 Data de Entrada: 17/09/10 Postagens Recentes
s s:
sry, kept searching and found this sort of statement on github (from Ray); no idea if they do the same thing yet but they "process" without error so that may be a good sign
&lt;#assign PortletPreferencesFactoryUtil = staticUtil["com.liferay.portlet.PortletPreferencesFactoryUtil"] /&gt;
&lt;#assign assetEntryQuery = staticUtil["com.liferay.portlet.asset.service.persistence.AssetEntryQuery"] /&gt;


staticUtil is not the same as Class.forName - staticUtil will just give you a hash map of the static methods of a class, whereas forName gives you an instance of java.lang.Class representing the named java Class.

So to convert a forName in Velocity to FreeMarker, you can use the objectUtil utility. for example


&lt;#assign ppfu = objectUtil("com.liferay.portlet.PortletPreferencesFactoryUtil") /&gt;
&lt;#assign aeq = objectUtil("com.liferay.portlet.asset.service.persistence.AssetEntryQuery") /&gt;


This will actually instantiate the class. Note the use of parentheses (method call) as opposed to brackets (hash access) when using staticUtil.
thumbnail
s s, modificado 8 Anos atrás.

RE: Get ServiceContext in Freemarker template forName

Junior Member Postagens: 28 Data de Entrada: 29/01/10 Postagens Recentes
Excellent - will try that!
*UPDATE*
Well that is better. Now getting all the "methods"! Tried a simple;
&lt;#assign V = assetEntryQuery.setAllCategoryIds(catId) /&gt; 

But hit this...
"Method public void com.liferay.portlet.asset.service.persistence.AssetEntryQuery.setAllCategoryIds(long[]) threw an exception when invoked on {allCategoryIds=, allTagIds=, anyCategoryIds=, anyTagIds=, classNameIds=, classTypeIds=null, description=, end=-1, excludeZeroViewCount=false, expirationDate=Wed Apr 29 22:21:10 GMT 2015, groupIds=, keywords=null, linkedAssetEntryId=0, notAllCategoryIds=, notAllTagIds=, notAnyCategoryIds=, notAnyTagIds=, orderByCol1=null, orderByCol2=null, orderByType1=null, orderByType2=null, paginationType=null, publishDate=Wed Apr 29 22:21:10 GMT 2015, start=-1null, title=, visible=true}"

I noticed that the velocity code I was looking at had .newInstance() at the end of the #set. Could that be it?

*UPDATE* - seems it just needed square brackets, like so...
&lt;#assign V = assetEntryQuery.setAllCategoryIds([catId]) /&gt;