Fórumok

RE: Change Structure Value in Database from Velocity Template?

Ed O'Connell, módosítva 11 év-val korábban

Change Structure Value in Database from Velocity Template?

New Member Bejegyzések: 8 Csatlakozás dátuma: 2013.01.18. Legújabb bejegyzések
So I am trying to build a little velocity mini-app that allows users to subscribe to update to content in a Web Content Display portlet. Most of the pieces of it are working. We have a service built using Service Builder that sends emails when the service URL is called, and I can call the URL from the velocity template easily enough.

The problem is I am calling that URL on a conditional where the is a checkbox field in the structure, and if that checkbox value evaluates to "true", the service URL is called. That works, but the things is once the value of the checkbox is set in the structure, I can't seem to clear it using Velocity, so the email service ends up being called every single time the page is refreshed, when I only want it to be called once.

This is the velocity code I had attempting to do that:


#if ($SendEmail.data == "true")
<!--<link href="SERVICE_URL" /> -->


#set ($SendEmail.data = "false")


#end

That does clear the SendEmail.data value at the template level, but not from the structure. So when the page is refreshed the service gets called again, and if I go to edit the content the checkbox still shows as checked.

Is there a way to actually clear or change the value that the structure passes to the database, so that after the service URL is called once it is not called again on page refresh?

Thanks a lot.
thumbnail
James Falkner, módosítva 11 év-val korábban

RE: Change Structure Value in Database from Velocity Template?

Liferay Legend Bejegyzések: 1399 Csatlakozás dátuma: 2010.09.17. Legújabb bejegyzések
Ed O'Connell:
So I am trying to build a little velocity mini-app that allows users to subscribe to update to content in a Web Content Display portlet. Most of the pieces of it are working. We have a service built using Service Builder that sends emails when the service URL is called, and I can call the URL from the velocity template easily enough.

The problem is I am calling that URL on a conditional where the is a checkbox field in the structure, and if that checkbox value evaluates to "true", the service URL is called. That works, but the things is once the value of the checkbox is set in the structure, I can't seem to clear it using Velocity, so the email service ends up being called every single time the page is refreshed, when I only want it to be called once.

This is the velocity code I had attempting to do that:


#if ($SendEmail.data == "true")
<!--<link href="SERVICE_URL" /> -->


#set ($SendEmail.data = "false")


#end

That does clear the SendEmail.data value at the template level, but not from the structure. So when the page is refreshed the service gets called again, and if I go to edit the content the checkbox still shows as checked.

Is there a way to actually clear or change the value that the structure passes to the database, so that after the service URL is called once it is not called again on page refresh?

Thanks a lot.


You actually have to update the article itself if you want to clear this field. This requires parsing the XML of the article, updating the XML, and setting it back via the Journal API. So, something like (note I did not test this, but it's from another similar mini-app used on liferay.com so it's close, but it was based on text values, not booleans, so some changes may be needed):

#set ($journalArticleLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService"))
#set ($curArticle = $journalArticleLocalService.getArticle($groupId, $reserved-article-id.data))

## get current value
#set ($document = $saxReaderUtil.read($curArticle.getContent()))
#set ($node = $document.selectSingleNode("//root/dynamic-element[@name='structure-field-name'][dynamic-content='current-structure-field-value']/dynamic-element/dynamic-content"))
#set ($currValue = $node.getText())

## update the current value
#set ($newValue = $currValue + $someChange)

## update the article with the new value
#set ($V = $node.setText($newValue))
#set ($V = $curArticle.setContent($document.asXML()))
#set ($V = $journalArticleLocalService.updateJournalArticle($curArticle, true))
Ed O'Connell, módosítva 11 év-val korábban

RE: Change Structure Value in Database from Velocity Template?

New Member Bejegyzések: 8 Csatlakozás dátuma: 2013.01.18. Legújabb bejegyzések
Thanks a lot! . . . that was an incredibly quick reply. I will let you know how it goes.
Ed O'Connell, módosítva 11 év-val korábban

RE: Change Structure Value in Database from Velocity Template?

New Member Bejegyzések: 8 Csatlakozás dátuma: 2013.01.18. Legújabb bejegyzések
I do have one question actually:

For these lines:


6 #set ($node = $document.selectSingleNode("//root/dynamic-element[@name='structure-field-name'][dynamic-content='current-structure-field-value']/dynamic-element/dynamic-content"))
7#set ($currValue = $node.getText())

I realize these may need to be modified because of the fact that I am dealing with a checkbox rather than a text field. But assuming my "SendEmail" field were a text field, and the value of the current text was "True" . . . how would line 6 be written?
thumbnail
James Falkner, módosítva 11 év-val korábban

RE: Change Structure Value in Database from Velocity Template?

Liferay Legend Bejegyzések: 1399 Csatlakozás dátuma: 2010.09.17. Legújabb bejegyzések
Ed O'Connell:
I do have one question actually:

For these lines:


6 #set ($node = $document.selectSingleNode("//root/dynamic-element[@name='structure-field-name'][dynamic-content='current-structure-field-value']/dynamic-element/dynamic-content"))
7#set ($currValue = $node.getText())

I realize these may need to be modified because of the fact that I am dealing with a checkbox rather than a text field. But assuming my "SendEmail" field were a text field, and the value of the current text was "True" . . . how would line 6 be written?


I am not an XPath expert, but take a look at this thread.

Perhaps something like

#set ($node = $document.selectSingleNode("//root/dynamic-element[@name='my-flag']/dynamic-content"))
#set ($currValue = $node.getText())
Ed O'Connell, módosítva 11 év-val korábban

RE: Change Structure Value in Database from Velocity Template?

New Member Bejegyzések: 8 Csatlakozás dátuma: 2013.01.18. Legújabb bejegyzések
James,

Thank you again. This is all incredibly helpful and educational. That did work, using the Xpath expression you suggested. The last little hurdle I need to clear is this: Updating the content entry in the database via updateJournalArticle works, but only renders after the user goes back into the control panel to republish the page. Even after the code executes (it seems) the values on the rendered page don't change with simple page refreshes. Someone else seems to have brought this up as well, here

In that thread Hitoshi Ozawa suggests that something might need to be done with the status of the article, but I am not sure how to get at that. Does that seems correct.

At any rate, with very few modifications from yours, and those mostly because of your suggestions, this is what I am currently using. It does almost everything I need. I don't mean to keep pestering you (or anyone else who might be in earshot), but do you think there is some way to make it so that on page refresh the new value for my "SendEmail" field ("") renders?


#if ($SendEmail.data == "true")
 <!-- <link href="SERVICE_URL"> --> 
#set ($journalArticleLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService"))
#set ($curArticle = $journalArticleLocalService.getArticle($groupId, $reserved-article-id.data))

#set ($document = $saxReaderUtil.read($curArticle.getContent()))

#set ($node = $document.selectSingleNode("//root/dynamic-element[@name='SendEmail']/dynamic-content"))

#set ($newValue = "")

#set ($V = $node.setText($newValue))

#set ($V = $curArticle.setContent($document.asXML()))

#set ($V = $journalArticleLocalService.updateJournalArticle($curArticle, true))

#end



Ed
Ed O'Connell, módosítva 11 év-val korábban

RE: Change Structure Value in Database from Velocity Template?

New Member Bejegyzések: 8 Csatlakozás dátuma: 2013.01.18. Legújabb bejegyzések
Hmmm . . . not sure yet, but just unchecking "cacheable" for the template might have done it. Testing tomorrow.
thumbnail
James Falkner, módosítva 11 év-val korábban

RE: Change Structure Value in Database from Velocity Template?

Liferay Legend Bejegyzések: 1399 Csatlakozás dátuma: 2010.09.17. Legújabb bejegyzések
Ed O'Connell:
Hmmm . . . not sure yet, but just unchecking "cacheable" for the template might have done it. Testing tomorrow.


Yeah, that should fix it. Congrats on getting it to work though!
Ed O'Connell, módosítva 11 év-val korábban

RE: Change Structure Value in Database from Velocity Template?

New Member Bejegyzések: 8 Csatlakozás dátuma: 2013.01.18. Legújabb bejegyzések
It seems to have done the trick.

Credit where it's due though. That is all because of your help, a bunch of googling, and stuff I found on these forums.

Thanks!