Discussion:
Need Help on EOModel Auto update of DATE column for DirectToWeb app
BLS
2009-11-09 08:23:56 UTC
Permalink
I have a DirectToWeb application (with EOModels), where in I have a table
with create_date and modify_date columns.

I need the create_date and modify_date of the table to be automatically
updated, whenever the user tries to create a new record for the table or,
when the user tries to edit an existing record of the table.

Any idea how to achieve this?

Any hints will be very helpful..

Thanks a lot in advance,
BLS
--
View this message in context: http://old.nabble.com/Need-Help-on-EOModel-Auto-update-of-DATE-column-for-DirectToWeb-app-tp26261738p26261738.html
Sent from the WebObjects-Dev mailing list archive at Nabble.com.
Pierre Gilquin
2009-11-09 08:52:14 UTC
Permalink
You can use trigger in the database
or with EOF, you can implement validateForInsert() and validateForUpdate()
in the Java class of your entity
----- Original Message -----
From: BLS
To: webobjects-***@omnigroup.com
Sent: Monday, November 09, 2009 9:23 AM
Subject: Need Help on EOModel Auto update of DATE column for DirectToWeb app



I have a DirectToWeb application (with EOModels), where in I have a table
with create_date and modify_date columns.

I need the create_date and modify_date of the table to be automatically
updated, whenever the user tries to create a new record for the table or,
when the user tries to edit an existing record of the table.

Any idea how to achieve this?

Any hints will be very helpful..

Thanks a lot in advance,
BLS
--
View this message in context: http://old.nabble.com/Need-Help-on-EOModel-Auto-update-of-DATE-column-for-DirectToWeb-app-tp26261738p26261738.html
Sent from the WebObjects-Dev mailing list archive at Nabble.com.

_______________________________________________
WebObjects-dev mailing list
WebObjects-***@omnigroup.com
http://www.omnigroup.com/mailman/listinfo/webobjects-dev
Paul Hoadley
2009-11-09 10:05:53 UTC
Permalink
Post by Pierre Gilquin
You can use trigger in the database
or with EOF, you can implement validateForInsert() and
validateForUpdate()
in the Java class of your entity
You don't want to go modifying EO attributes in validateForInsert() or
validateForUpdate():

http://wiki.objectstyle.org/confluence/display/WO/EOF-Using+EOF-The+EOF+Commandments

Depending on the OP's specific requirements, s/he might be able to use
awakeFromInsertion() or awakeFromFetch(), or, better still,
ERXEnterpriseObject's willInsert() or willUpdate(). See which of
those fits the specific requirements.
--
Paul.

w http://logicsquad.net/
h http://paul.hoadley.name/
Pierre Gilquin
2009-11-09 11:00:30 UTC
Permalink
if the last line is not forgotten, I did not have any problem modify an EO :

editingContext().processRecentChanges();

Pierre
----- Original Message -----
From: Paul Hoadley
To: webobjects-***@omnigroup.com
Sent: Monday, November 09, 2009 11:05 AM
Subject: Re: Need Help on EOModel Auto update of DATE column for DirectToWebapp


On 09/11/2009, at 7:22 PM, Pierre Gilquin wrote:


You can use trigger in the database
or with EOF, you can implement validateForInsert() and validateForUpdate()
in the Java class of your entity


You don't want to go modifying EO attributes in validateForInsert() or validateForUpdate():


http://wiki.objectstyle.org/confluence/display/WO/EOF-Using+EOF-The+EOF+Commandments


Depending on the OP's specific requirements, s/he might be able to use awakeFromInsertion() or awakeFromFetch(), or, better still, ERXEnterpriseObject's willInsert() or willUpdate(). See which of those fits the specific requirements.




--
Paul.

w http://logicsquad.net/
h http://paul.hoadley.name/
Chuck Hill
2009-11-09 18:40:10 UTC
Permalink
Post by Pierre Gilquin
if the last line is not forgotten, I did not have any problem
editingContext().processRecentChanges();
You may not have had, or noticed, any problem so far. But this is
NOT safe. This can modify EOs during saveChanges() and EOF is not
expecting that to happen. Both Wonder and Practical WebObjects
provide an implementation of the correct, safe way to make such changes.


Chuck
Post by Pierre Gilquin
Pierre
----- Original Message -----
From: Paul Hoadley
Sent: Monday, November 09, 2009 11:05 AM
Subject: Re: Need Help on EOModel Auto update of DATE column for DirectToWebapp
Post by Pierre Gilquin
You can use trigger in the database
or with EOF, you can implement validateForInsert() and
validateForUpdate()
in the Java class of your entity
http://wiki.objectstyle.org/confluence/display/WO/EOF-Using+EOF-The+EOF+Commandments
Depending on the OP's specific requirements, s/he might be able to
use awakeFromInsertion() or awakeFromFetch(), or, better still,
ERXEnterpriseObject's willInsert() or willUpdate(). See which of
those fits the specific requirements.
--
Paul.
w http://logicsquad.net/
h http://paul.hoadley.name/
_______________________________________________
WebObjects-dev mailing list
http://www.omnigroup.com/mailman/listinfo/webobjects-dev
_______________________________________________
WebObjects-dev mailing list
http://www.omnigroup.com/mailman/listinfo/webobjects-dev
--
Chuck Hill Senior Consultant / VP Development

Practical WebObjects - for developers who want to increase their
overall knowledge of WebObjects or who are trying to solve specific
problems.
http://www.global-village.net/products/practical_webobjects
Pierre Gilquin
2009-11-09 19:38:10 UTC
Permalink
ok, I will read it again.

Pierre
----- Original Message -----
From: Chuck Hill
To: Pierre Gilquin
Cc: webobjects-***@omnigroup.com
Sent: Monday, November 09, 2009 7:40 PM
Subject: Re: Need Help on EOModel Auto update of DATE column for DirectToWebapp
Post by Pierre Gilquin
if the last line is not forgotten, I did not have any problem
editingContext().processRecentChanges();
You may not have had, or noticed, any problem so far. But this is
NOT safe. This can modify EOs during saveChanges() and EOF is not
expecting that to happen. Both Wonder and Practical WebObjects
provide an implementation of the correct, safe way to make such changes.


Chuck
Post by Pierre Gilquin
Pierre
----- Original Message -----
From: Paul Hoadley
Sent: Monday, November 09, 2009 11:05 AM
Subject: Re: Need Help on EOModel Auto update of DATE column for DirectToWebapp
Post by Pierre Gilquin
You can use trigger in the database
or with EOF, you can implement validateForInsert() and
validateForUpdate()
in the Java class of your entity
http://wiki.objectstyle.org/confluence/display/WO/EOF-Using+EOF-The+EOF+Commandments
Depending on the OP's specific requirements, s/he might be able to
use awakeFromInsertion() or awakeFromFetch(), or, better still,
ERXEnterpriseObject's willInsert() or willUpdate(). See which of
those fits the specific requirements.
--
Paul.
w http://logicsquad.net/
h http://paul.hoadley.name/
_______________________________________________
WebObjects-dev mailing list
http://www.omnigroup.com/mailman/listinfo/webobjects-dev
_______________________________________________
WebObjects-dev mailing list
http://www.omnigroup.com/mailman/listinfo/webobjects-dev
--
Chuck Hill Senior Consultant / VP Development

Practical WebObjects - for developers who want to increase their
overall knowledge of WebObjects or who are trying to solve specific
problems.
http://www.global-village.net/products/practical_webobjects
Bhagyalata Sahu
2009-11-10 04:47:20 UTC
Permalink
Thank you all for quick response.
Thanks Pierre for your response specifically. That gave me a flash to
do another simpler solution:

I changed the getter and setter methods of the corresponding java
class to automatically update the create date if create date is null.

public NSTimestamp createDate() {
if(storedValueForKey("createDate") == null) {
NSTimestamp currentTime = new NSTimestamp();
setCreateDate(currentTime);
return currentTime;
} else {
return (NSTimestamp)storedValueForKey("createDate");
}
}
Post by Pierre Gilquin
ok, I will read it again.
Pierre
----- Original Message -----
From: Chuck Hill
To: Pierre Gilquin
Sent: Monday, November 09, 2009 7:40 PM
Subject: Re: Need Help on EOModel Auto update of DATE column for DirectToWebapp
Post by Pierre Gilquin
editingContext().processRecentChanges();
You may not have had, or noticed, any problem so far. But this is
NOT safe. This can modify EOs during saveChanges() and EOF is not
expecting that to happen. Both Wonder and Practical WebObjects
provide an implementation of the correct, safe way to make such changes.
Chuck
Post by Pierre Gilquin
Pierre
----- Original Message -----
From: Paul Hoadley
Sent: Monday, November 09, 2009 11:05 AM
Subject: Re: Need Help on EOModel Auto update of DATE column for DirectToWebapp
Post by Pierre Gilquin
You can use trigger in the database
or with EOF, you can implement validateForInsert() and
validateForUpdate()
in the Java class of your entity
You don't want to go modifying EO attributes in validateForInsert()
http://wiki.objectstyle.org/confluence/display/WO/EOF-Using+EOF-The+EOF+Commandments
Depending on the OP's specific requirements, s/he might be able to
use awakeFromInsertion() or awakeFromFetch(), or, better still,
ERXEnterpriseObject's willInsert() or willUpdate(). See which of
those fits the specific requirements.
--
Paul.
w http://logicsquad.net/
h http://paul.hoadley.name/
_______________________________________________
WebObjects-dev mailing list
http://www.omnigroup.com/mailman/listinfo/webobjects-dev
_______________________________________________
WebObjects-dev mailing list
http://www.omnigroup.com/mailman/listinfo/webobjects-dev
--
Chuck Hill Senior Consultant / VP Development
Practical WebObjects - for developers who want to increase their
overall knowledge of WebObjects or who are trying to solve specific
problems.
http://www.global-village.net/products/practical_webobjects
_______________________________________________
WebObjects-dev mailing list
http://www.omnigroup.com/mailman/listinfo/webobjects-dev
Mark Ritchie
2009-11-12 18:54:00 UTC
Permalink
Hey Bhagyalata!
Post by Bhagyalata Sahu
Thank you all for quick response.
Thanks Pierre for your response specifically. That gave me a flash
I changed the getter and setter methods of the corresponding java
class to automatically update the create date if create date is null.
public NSTimestamp createDate() {
if(storedValueForKey("createDate") == null) {
NSTimestamp currentTime = new NSTimestamp();
setCreateDate(currentTime);
return currentTime;
} else {
return (NSTimestamp)storedValueForKey("createDate");
}
}
Yikes! That's called a mutating accessor and it's a bad idea.
What if an administrative application wanted to use the same EOModel
and EO's to examine the records which did not have a createDate set.
With this implementation, the fetch specification would could
correctly find the records however by the time they were reported
there would be values in the createDate field. Just one of the many
reasons that accessor methods should not modify values!

I've updated the EOF Commandments with some more details on this case:
http://wiki.objectstyle.org/confluence/display/WO/EOF-Using+EOF-The+EOF+Commandments

Side note, if you are using D2W then you should really use Wonder as
well!
Wonder has many enhancements for D2W!

Best!
Mark

Loading...