Re: WORepetition Only Updates on ec.saveChages()
Re: WORepetition Only Updates on ec.saveChages()
- Subject: Re: WORepetition Only Updates on ec.saveChages()
- From: Drew Thoeni <email@hidden>
- Date: Tue, 6 Jan 2004 06:58:00 -0500
Chuck, Thanks. It was the SQL-like method I had created. See below...
(a couple of clarifying questions).
Drew
On Jan 5, 2004, at 11:52 PM, Chuck Hill wrote:
Drew,
As far as I can see your isInGame variable never gets set. I'm not
clear
what you think it should be. There is also a method isInGame() which
should return the correct value, but only *during* the loops of the
WORepetition over (I'm assuming) session.questionList.
More below.
At 10:48 PM 05/01/2004 -0500, Drew Thoeni wrote:
I have an "Add/Remove" column in a WORepetition that contains two
WOConditionals (one for add, one for remove). The conditionals are
bound to a protected boolean called isInGame.
Well, no. They are actually bound to the method protected boolean
isInGame(). Which, BTW, should be public or when you move to WO 5.2.2
and
JDK 1.4 things will stop working.
See the docs for NSKeyValueCoding as to why it is using the method.
Sleepy-head me missed that.
The data structure represents "games" which contain many questions.
The
WORep is displaying questions both already in aGame and questions not
currently assigned to any game. The user can simply click the "Add"
hyperlink and viola, or similarly the "Remove" link (shown per the
isInGame variable) and the question is removed from being assigned to
this game.
So, all this works but the isInGame variable does not get set
correctly
*unless* the user clicks the add or remove link. Each of these (see
below) update the cache and save changes. For any item in the WORep
that has been clicked add or remove, the isInGame is set to a correct
value. For others, even those that should be true, false is the value.
What am I missing here?
Also, possibly
Thanks,
Drew
PS. Comments on my coding style are also very welcome. I'm new at WO
and Java.
import com.webobjects.foundation.*;
import com.webobjects.appserver.*;
import com.webobjects.eocontrol.*;
import com.webobjects.eoaccess.*;
public class EditGame extends WOComponent {
protected String pageTitle;
protected Game aGame;
protected Question aQuestion;
private EOEditingContext ec = session().defaultEditingContext();
private EOFetchSpecification fetchSpec;
Session session = (Session)session();
protected boolean isInGame;
public EditGame(WOContext context) {
super(context);
pageTitle = "Edit Game";
}
public void setAGame(Game inGame) {
aGame = inGame; // set in another page
// get questions
NSDictionary bindings = new NSDictionary(aGame.gameId(),
"currentGame");
eew! gameID == SQL thinking. Think objects or foreever fight WO:
NSDictionary bindings = new NSDictionary(aGame, "currentGame");
And update your fetchspec to be game =
Thanks. I get this (since you pointed it out). It appears that it is
obvious that I have a SQL and procedural background. I'll continue to
try and reorient my thinking to objects.
NSArray temp = EOUtilities.objectsWithFetchSpecificationAndBindings(
ec, "Question", "FetchQuestionsOpenOrAssignedThisGame", bindings);
Is that different from fetch all questions?
Yes. Questions can be assigned to other games and I would not want them
displayed here. Once a question is used (i.e. assigned to a game that
becomes active), it can't be reused.
session.questionList = (NSMutableArray)temp;
}
If this is game specific it would be better managed on this page. If
it is
global, why not just fetch it once per session instead of once per
game?
I get this. I had read it was better to use a single ec (I supposed for
memory reasons). I have run into problems like you described (e.g.
saving the context where unwanted changes were saved due to fetches to
display data) and having a context per page would make those problems
go away.
protected WOComponent addQuestonToGame() {
aGame.addToQuestions(aQuestion);
aQuestion.setGame(aGame);
ec.saveChanges();
return null;
}
return context().page(); is slightly more efficient in all cases and
much
more efficient in some (large numeber of components, target at end of
page).
Good tip. I'll have to research this to understand.
protected WOComponent removeQuestionFromGame() {
aGame.removeFromQuestions(aQuestion);
aQuestion.setGame(null);
ec.saveChanges();
return null;
}
public ManageGames cancel() {
ManageGames nextPage =
(ManageGames)pageWithName("ManageGames");
ec.revert();
return nextPage;
}
You are probably better off creating a new ec for each page than
relying on
this to work. Users will back track, open multiple windows, they have
many
bad habits.
protected boolean isInGame() {
if (aQuestion.gameId() == aGame.gameId()) {
return true;
}
else {
return false;
}
}
urk. More of them ID things.
public boolean isInGame() {
return aQuestion.game() == aGame;
}
Changed this and it solved the problem. isInGame is part of the WORep.
I don't understand why my code didn't fire correctly, but your method
makes a lot more sense.
Thanks again,
Drew
Chuck
--
Chuck Hill email@hidden
Global Village Consulting Inc.
http://www.global-village.net
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.