掲示板
Get ServiceContext in Freemarker template
9年前 に Beppo Ivel によって更新されました。
Get ServiceContext in Freemarker template
Regular Member 投稿: 112 参加年月日: 14/04/09 最新の投稿
Hi.
I try to run move the file "MyTestFile" from folder Test1 to folder Test2 in my freemarker template:
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.
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.
9年前 に Andew Jardine によって更新されました。
RE: Get ServiceContext in Freemarker template
Liferay Legend 投稿: 2416 参加年月日: 10/12/22 最新の投稿
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.
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.
9年前 に Beppo Ivel によって更新されました。
RE: Get ServiceContext in Freemarker template
Regular Member 投稿: 112 参加年月日: 14/04/09 最新の投稿
Hi I found this command also at this site. But Iam not able to translate it to freemarker:
The last line creates the error:
<#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.
9年前 に Beppo Ivel によって更新されました。
RE: Get ServiceContext in Freemarker template
Regular Member 投稿: 112 参加年月日: 14/04/09 最新の投稿
I also found:
in the util-spring.xml. So is it possible to load the class "com.liferay.portal.service.ServiceContextThreadLocal" in a other way?
<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?
8年前 に s s によって更新されました。
RE: Get ServiceContext in Freemarker template forName
Junior Member 投稿: 28 参加年月日: 10/01/29 最新の投稿
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)..
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.
#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.
8年前 に s s によって更新されました。
RE: Get ServiceContext in Freemarker template forName
Junior Member 投稿: 28 参加年月日: 10/01/29 最新の投稿
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
*UPDATE*
For the bottom, sort of works...if I look at the resulted assetEntryQuery is has this in it;
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.
<#assign PortletPreferencesFactoryUtil = staticUtil["com.liferay.portlet.PortletPreferencesFactoryUtil"] />
<#assign assetEntryQuery = staticUtil["com.liferay.portlet.asset.service.persistence.AssetEntryQuery"] />
*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.
8年前 に James Falkner によって更新されました。
RE: Get ServiceContext in Freemarker template forName
Liferay Legend 投稿: 1399 参加年月日: 10/09/17 最新の投稿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<#assign PortletPreferencesFactoryUtil = staticUtil["com.liferay.portlet.PortletPreferencesFactoryUtil"] /> <#assign assetEntryQuery = staticUtil["com.liferay.portlet.asset.service.persistence.AssetEntryQuery"] />
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
<#assign ppfu = objectUtil("com.liferay.portlet.PortletPreferencesFactoryUtil") />
<#assign aeq = objectUtil("com.liferay.portlet.asset.service.persistence.AssetEntryQuery") />
This will actually instantiate the class. Note the use of parentheses (method call) as opposed to brackets (hash access) when using staticUtil.
8年前 に s s によって更新されました。
RE: Get ServiceContext in Freemarker template forName
Junior Member 投稿: 28 参加年月日: 10/01/29 最新の投稿
Excellent - will try that!
*UPDATE*
Well that is better. Now getting all the "methods"! Tried a simple;
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...
*UPDATE*
Well that is better. Now getting all the "methods"! Tried a simple;
<#assign V = assetEntryQuery.setAllCategoryIds(catId) />
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...
<#assign V = assetEntryQuery.setAllCategoryIds([catId]) />