JavaEE6 Conversation.end doesn't reset conversation.id to 1 - java-ee-6

Is the conversation.id not reset when conversation.end is called?
Scenario: I have an app that uses conversation scope in CRUD, so when I visit the list page it starts a conversation. Go to the detail and click back will call end conversation and begin conversation again. But while I'm at debug mode I found out that when conversation.end() is called conversation is set to null. Then when I reinvoke conversation.begin() conversation.id is not reset to 1 but rather the last value + 1. Is it correct to behave that way?
What's more puzzling is after logout and login again, the conversation.id pick up the last value + 1.
My environment: Jboss 7.1.3 using javaee-api.
protected void beginConversation() {
if (conversation.isTransient()) {
conversation.begin();
}
}
protected void endConversation() {
if (!conversation.isTransient()) {
conversation.end();
}
}
So basically I have a base entity (where the above code is defined.) extended by all the backing beans. When a list page is render it will call beginConversation. Clicking the back button in detail page will call endConversation.

Why should the conversation id be reset?
The best way to solve those questions is to directly having a look in the specification (in your case CDI 1.0), which states that the container has to generate an id for the conversation, but not how.
Check out this question, which states how it's done in WELD.

Related

Primefaces Push - What are the methods in <p:socket/> client widget

Can anybody point me to any api link which contains the <p:socket/> client widget?
Going through the push showcase I can only see connect method in
requestContext.execute("subscriber.connect('/" + username + "')");
What are the other methods.? Is there any disconnect method as-well.?
Also, how to create separate channel for each user (in case of chat application). I reckon, this <p:socket onMessage="handleMessage" channel="/chat/#{userSession.userId}" autoConnect="false" widgetVar="subscriber"/> will do the trick but apparantly it is not, atleast for me. Because by looking in the Chrome dev console I can see that everytime the page is refreshed it is appending the channel name (/chat/userid/userid...).
Any pointers is highly appreciated.!!!
I think I got the answer for some of the issue I'm facing.
For methods in push widget, push.js is the file to look for.
The appending issue is because of calling
requestContext.execute("subscriber.connect('/" + username + "')");
multiple time. The below code gets called which results in appending of the channel names multiple times.
connect: function (a) {if (a) {
this.cfg.request.url += a // <----
}
this.connection = $.atmosphere.subscribe(this.cfg.request)
Disconnect method is available in PF 4.0. or you can add the following code to push.js.
disconnect: function () {
this.connection.close()
}

silverstripe dopublish function for ModelAdmin managed Pages

in the silverstripe backend I´m managing certain PageTypes via a ModelAdmin. That works fine so far, the only thing i can´t figure out is how to make a Page being "Published" when saving it.
Thats my code:
class ProjectPage extends Page {
public function onAfterWrite() {
$this->doPublish();
parent::onAfterWrite();
}
}
At the moment I can still see the ModelAdmin-created Pages in the Sitetree and I can see they are in Draft Mode. If I use the code above I get this error:
Maximum execution time of 30 seconds exceeded in
.../framework/model/DataList.php
Many thx,
Florian
the reason why you get "Maximum execution time exceeded" is because $this->doPublish(); calls $this->write(); which then calls $this->onAfterWrite();. And there you have your endless loop.
So doing this in onAfterWrite() or write() doesn't really work
you should just display the save & publish button instead of the save button
But I guess its easier said than done.
Well adding a button is actually just a few lines, but we also need a provide the functions that do what the button says it does.
This sounds like the perfect call for creating a new Module that allows proper handling of Pages in model admin. I have done this in SS2.4, and I have a pretty good idea of how to do it in SS3, but no time this week, poke me on the silverstripe irc channel on the weekend, maybe I have time on the weekend.
I found the same need/lack and I built a workaround that seems to work for me, maybe it can be useful.
public function onAfterWrite()
{
if(!$this->isPublished() || $this->getIsModifiedOnStage())
{
$this->publish('Stage', 'Live');
Controller::curr()->redirectBack();
}
parent::onAfterWrite();
}
Create a class that extends ModelAdmin and define an updateEditForm function to add a publish button to the actions in the GridFieldDetailForm component of the GridField.
public function updateEditForm($form) {
if ( ! singleton($this->owner->modelClass)->hasExtension('Versioned') ) return;
$gridField = $form->Fields()->fieldByName($this->owner->modelClass);
$gridField->getConfig()->getComponentByType('GridFieldDetailForm')->setItemEditFormCallback(function ($form) {
$form->Actions()->push(FormAction::create('doPublish', 'Save & Publish'));
});
}
Then create a class that extends GridFieldDetailForm_ItemRequest to provide an action handler for your publish button.
public function doPublish($data, $form) {
$return = $this->owner->doSave($data, $form);
$this->owner->record->publish('Stage', 'Live');
return $return;
}
Make sure the extensions are applied and you're done.
Or, you could just grab all the code you need from GitHub.

pause viewmodel process for user input

I've been looking at a view examples of the typical "raise dialog from viewmodel" problem, noting 3 main solutions:
use attached behaviors
use a mediator pattern
use a service
I'm getting a bit bogged down though and struggling to find a solution that easily fits into my problem space - which is a very simple file copy problem:
My viewmodel is processing a loop (copying a list of files)
When a file already exists at the destination I need to raise a modal dialog to get confirmation to replace
The vm needs to wait for and receive confirmation before continuing
The "modal dialog" is actually not a new window but a hidden overlay in my MainWindow, as per http://www.codeproject.com/KB/WPF/wpfmodaldialog.aspx (thanks Ronald!)
I'm mostly there but the biggest struggles I have are:
- how to pause the loop in the viewmodel while it waits for input
- how to get input back to the viewmodel within the loop so it can carry on
So far I'm leaning towards the service solution because it seems a direct method call with a return that the vm must wait for. However, it does mean the service needs to tie directly to the view in order to make an element visible?
If anyone can post some simple code that deals directly with this problem I (and the net) would be very happy! Thanks!
For example, you have a service called IDialogService with the following interface:
public interface IDialogService
{
bool ConfirmAction(string title, string confirmationText);
}
As you mentioned, in order for the service to be able to show the actual dialog it needs to have a reference to the view that will show the actual overlay element. But instead of directly referencing the view I prefer to reference it via an interface. Lets call it ICanShowDialog and it will have the following members:
public interface ICanShowDialog
{
void ShowDialog(object dialogContent);
void HideDialog();
}
This interface will be implemented by your view that owns the dialog overlay (e.g. your main window).
Now the interesting part: suspending the code execution while the dialog is shown. First of all, I would recommend you not to use overlay elements but use usual windows if possible. Then you will not have that problem. You can style the dialog window so it will look just like the overlay element.
Anyway, if you still want to use overlay elements then you can do the following trick to suspend the code execution:
Here is pseudo code of the ConfirmAction method of the IDialogService inteface:
public bool ConfirmAction(string title, string confirmationText)
{
ConfirmationDialogView dialogView = new ConfirmationDialogView(title, confirmationText);
DialogShower.ShowDialog(dialogView); // DialogShower is of type ICanShowDialog
while (!dialogView.ResultAvailable)
{
DispatcherUtils.DoEvents();
}
DialogShower.HideDialog();
return dialogView.Result;
}
Here is the code of DispatcherUtils.DoEvents() (that was taken from here: http://dedjo.blogspot.com/2007/08/how-to-doevents-in-wpf.html):
public static class DispatcherUtils
{
public static void DoEvents()
{
DispatcherFrame f = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(
DispatcherPriority.Background,
(SendOrPostCallback)delegate(object arg) {
DispatcherFrame fr = arg as DispatcherFrame;
fr.Continue=True;
}, f);
Dispatcher.PushFrame(frame);
}
}
But I must warn you. Using DoEvents can result in some subtle bugs caused by inner dispatcher loops.
As an alternative to suspending the code execution while a dialog is shown you can use callbacks:
public interface IDialogService
{
void ConfirmAction(string title, string confirmationText, Action<bool> dialogResultCallback);
}
But it will not be so convenient to use.

GWT - Trouble with History first token

I have this problem : when i call the Content class (the one who decide which page to view, due to the #param) I do somethings like this :
History.addValueChangeHandler(this);
if(!History.getToken().isEmpty()){
changePage(History.getToken());
} else {
History.newItem("homepage");
}
So, now, if i look at browser's navigation bar, i see http://localhost:8084/GWT/?gwt.codesvr=127.0.0.1:9997#homepage. And that's right. Unfortunatly, If i press Back on my browser, i see that it load the previous address, such as http://localhost:8084/GWT/?gwt.codesvr=127.0.0.1:9997
I have a sort of "fake" page at the beginning.
1 - How can I fix it? And start the application with a default token, or remove it in the history. Or just call the onValueChange method when there is empty token, and after decide the workflow with a sort of switch/if-else.
2 - As related question, when i call History.addValueChangeHandler(this); in the costructor class, netbeans say "Leaking this in constructor". What it means?
Cheers
Maybe you forgot to add History.fireCurrentHistoryState(); to end of onModuleLoad() method?
You need to set a history token and fire the history change event with current token.
Heres how you could do it:
/ If the application starts with no history token, redirect to a new
// 'homepage' state.
String initToken = History.getToken();
if (initToken.length() == 0) {
History.newItem("homepage");
}
// Add widgets etc
// Add history listener
History.addHistoryListener(yourHistoryHandler);
// Fire the initial history state.
History.fireCurrentHistoryState();
IMHO, home url in form of "proto://hostname#homepage" is ugly :)
1. Just a suggestion:
String token = History.getToken();
String page = token.isEmpty() ? "homepage" : token;
changePage(page);
2. Does Your EntryPoint implement ValueChangeHandler<String>?

Problem in Large scale application development and MVP tutorial

I recently tried to follow the Large scale application development and MVP tutorial. The tutorial was great but I am having a hard time with a few things.
If you try and add a contact to the list, the contact is created. If you try and add another contact, you are taken to the edit screen of the last contact you created. No more contacts can be added once you add your first contact. What needs to be changed so you can add more than one contact.
Changes I have made to try and get it to work:
Create a new editContactsView each time the add button is pressed. This brings up a blank edit screen, but the new contact still overwrites the previous addition.
Changed contacts.size() to contacts.size()+1 when determining the ID of the new contact.
Actually, there are a couple of problems (from what I can see):
like Lumpy already mentioned, the new Contact created via EditContactPresenter doesn't get an id assigned (it's null). This is because EditContactPresenter uses the default Contact() constructor which doesn't set the id. There are many possible solutions to this: add setting the id in the default constructor (so that you don't have to keep track of the ids somewhere else in the app), delegate that function to your server (for example, make your DB generate the next available id and send it back) or just add a contact.setId(whatever); in the appropriate place in EditContactsPresenter
AppController.java:134 - this example reuses the view (which is a good idea), but it doesn't clear it if you use it for creating a new Contact. Solution: either disable view reusing (just make a new EditContactsView every time) or add a clear() or sth similar to your Views and make the Presenters call it when they want to create a new entry, instead of editing an exisiting one (in which case, the values from the current entry overwrite the old values, so it's ok).
It's weird that this sample was left with such bugs - although I understand that it's main purpose was to show how MVP and GWT go together, but still :/
When a new contact is added it's id is never set. Because the id field is a string it is stored as "". That is how the first contact is added. Now every time you create a new contact you overwrite the contact with key "". To fix this you need to set the value of the id. I did this by changing the doSave method in EditContactsPresenter.
private void doSave() {
contact.setFirstName(display.getFirstName().getValue());
contact.setLastName(display.getLastName().getValue());
contact.setEmailAddress(display.getEmailAddress().getValue());
if(History.getToken.equals("add")
rpcService.updateContact(contact, new AsyncCallback<Contact>() {
public void onSuccess(Contact result) {
eventBus.fireEvent(new ContactUpdatedEvent(result));
}
public void onFailure(Throwable caught) {
Window.alert("Error updating contact");
}
});
else
rpcService.updateContact(contact, new AsyncCallback<Contact>() {
public void onSuccess(Contact result) {
eventBus.fireEvent(new ContactUpdatedEvent(result));
}
public void onFailure(Throwable caught) {
Window.alert("Error updating contact");
}
});
}