Tribune

Home » Liferay Portal » English » 3. Development

Vista Combinata Vista Piatta Vista ad Albero
Discussioni [ Precedente | Successivo ]
toggle
Ed O'Connell
Change Structure Value in Database from Velocity Template?
25 gennaio 2013 13.17
Risposta

Ed O'Connell

Punteggio: New Member

Messaggi: 8

Data di Iscrizione: 18 gennaio 2013

Messaggi recenti

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.
James Falkner
RE: Change Structure Value in Database from Velocity Template?
25 gennaio 2013 13.34
Risposta

James Falkner

LIFERAY STAFF

Punteggio: Liferay Legend

Messaggi: 1222

Data di Iscrizione: 17 settembre 2010

Messaggi recenti

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):

 1#set ($journalArticleLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService"))
 2#set ($curArticle = $journalArticleLocalService.getArticle($groupId, $reserved-article-id.data))
 3
 4## get current value
 5#set ($document = $saxReaderUtil.read($curArticle.getContent()))
 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())
 8
 9## update the current value
10#set ($newValue = $currValue + $someChange)
11
12## update the article with the new value
13#set ($V = $node.setText($newValue))
14#set ($V = $curArticle.setContent($document.asXML()))
15#set ($V = $journalArticleLocalService.updateJournalArticle($curArticle, true))
Ed O'Connell
RE: Change Structure Value in Database from Velocity Template?
25 gennaio 2013 13.45
Risposta

Ed O'Connell

Punteggio: New Member

Messaggi: 8

Data di Iscrizione: 18 gennaio 2013

Messaggi recenti

Thanks a lot! . . . that was an incredibly quick reply. I will let you know how it goes.
Ed O'Connell
RE: Change Structure Value in Database from Velocity Template?
25 gennaio 2013 14.22
Risposta

Ed O'Connell

Punteggio: New Member

Messaggi: 8

Data di Iscrizione: 18 gennaio 2013

Messaggi recenti

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?
James Falkner
RE: Change Structure Value in Database from Velocity Template?
25 gennaio 2013 14.54
Risposta

James Falkner

LIFERAY STAFF

Punteggio: Liferay Legend

Messaggi: 1222

Data di Iscrizione: 17 settembre 2010

Messaggi recenti

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

1#set ($node = $document.selectSingleNode("//root/dynamic-element[@name='my-flag']/dynamic-content"))
2#set ($currValue = $node.getText())
Ed O'Connell
RE: Change Structure Value in Database from Velocity Template?
28 gennaio 2013 17.43
Risposta

Ed O'Connell

Punteggio: New Member

Messaggi: 8

Data di Iscrizione: 18 gennaio 2013

Messaggi recenti

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?

 1
 2#if ($SendEmail.data == "true")
 3 <!-- <link href="SERVICE_URL"> -->
 4#set ($journalArticleLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService"))
 5#set ($curArticle = $journalArticleLocalService.getArticle($groupId, $reserved-article-id.data))
 6
 7#set ($document = $saxReaderUtil.read($curArticle.getContent()))
 8
 9#set ($node = $document.selectSingleNode("//root/dynamic-element[@name='SendEmail']/dynamic-content"))
10
11#set ($newValue = "")
12
13#set ($V = $node.setText($newValue))
14
15#set ($V = $curArticle.setContent($document.asXML()))
16
17#set ($V = $journalArticleLocalService.updateJournalArticle($curArticle, true))
18
19#end


Ed
Ed O'Connell
RE: Change Structure Value in Database from Velocity Template?
28 gennaio 2013 18.56
Risposta

Ed O'Connell

Punteggio: New Member

Messaggi: 8

Data di Iscrizione: 18 gennaio 2013

Messaggi recenti

Hmmm . . . not sure yet, but just unchecking "cacheable" for the template might have done it. Testing tomorrow.
James Falkner
RE: Change Structure Value in Database from Velocity Template?
29 gennaio 2013 6.41
Risposta

James Falkner

LIFERAY STAFF

Punteggio: Liferay Legend

Messaggi: 1222

Data di Iscrizione: 17 settembre 2010

Messaggi recenti

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
RE: Change Structure Value in Database from Velocity Template?
29 gennaio 2013 6.51
Risposta

Ed O'Connell

Punteggio: New Member

Messaggi: 8

Data di Iscrizione: 18 gennaio 2013

Messaggi recenti

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!