Re: Data model suggestions...
Re: Data model suggestions...
- Subject: Re: Data model suggestions...
- From: Ramsey Gurley <email@hidden>
- Date: Mon, 06 Oct 2014 11:49:51 -0700
On Oct 6, 2014, at 11:16 AM, Flavio Donadio <email@hidden> wrote:
> Hello, people!
>
>
> This is a not a WO or WOnder question per se, but I think you guys can help me. Since English is not my native language, I couldn't find anything on the web...
>
> I have this feature I want to implement in a website, which I call "configurator".
>
> In a database of products, each product has options, for example: memory capacity (integer), display type (enum: color, monochrome), audio (boolean), WLAN regulatory domain (enum: FCC, EU, Japan), and so on.
>
> My problem is that some options require other specific option(s) to have a specific value, for example: audio is only available in configurations with a color display.
>
> And, to confuse it a little more, different products have different options with their own rules.
>
> How can I create a model in a way I can input these rules for "valid" configurations? Also, when the user browses the website, he/she can configure the products the way they want and be assured that they won't select an invalid configuration.
>
> I am trying to figure this out, but it doesn't look easy.
>
>
> Cheers,
> Flavio
Specifically in WO/Wonder, you can do this using NSValidation. Using your example, you have a Product EO
Product
-capacity
-display
-audio
-domain
On your Product’s EO class, you would then implement validation methods for these attributes like
public Boolean validateAudio(Boolean audio) throws NSValidation.ValidationException {
//TODO validate boolean here
boolean notValid = …;
if(notValid) {
ERXValidationFactory factory = ERXValidationFactory.defaultFactory();
ERXValidationException ex = factory.createException(this, Product.AUDIO_KEY, audio, ERXValidationException.InvalidValueException);
throw ex;
}
return audio;
}
where the method signature is validate<KEY> where <KEY> is the name of your attribute being validated. If you need validation that depends on values set on multiple attributes, you would probably do that in the EO’s validateFor* methods... validateForInsert, validateForUpdate, validateForSave, validateForDelete.
public void validateForSave() throws NSValidation.ValidationException {
if(display() != DisplayType.COLOR && audio() == true) {
ERXValidationFactory factory = ERXValidationFactory.defaultFactory();
ERXValidationException ex = factory.createCustomException(this, “AudioNotAvailableWithMonoChrome”);
throw ex;
}
}
Then in your Resources/English.lproj/ValidationTemplate.strings file, you would have something like
{
“Product.AudioNotAvailableWithMonoChrome” = “Sorry, monochrome products are not available with audio.”;
}
If you’re using D2W, that’s it. You’re done. If you aren’t, then you need to wire up some way to display validation errors in your page component. That means overriding
public void validationFailedWithException(Throwable t, Object value, String keyPath) {
//TODO catch validation errors here to display to the user.
}
along with some other internal array structure to handle multiple validation exceptions, possibly nested ones down in the EO’s relationships, etc.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden