Pass values to custom validation service [message #1064466] |
Wed, 19 June 2013 12:18 |
kon f Messages: 152 Registered: March 2012 |
Senior Member |
|
|
Hey
I'm using a custom validation service that extends ValidationService and I was wondering if I could pass some parameter? Something straightforward like:
@Service(impl = MyPersonalValidationService.class, values={"A","B,"C"})
Does this work somehow? I would like to generalize my custom validation service being able to use it for several properties.
Thank you!
Kon
[Updated on: Thu, 20 June 2013 06:05] Report message to a moderator
|
|
|
Re: Pass values to custom validation service [message #1064521 is a reply to message #1064466] |
Wed, 19 June 2013 15:59 |
Konstantin Komissarchik Messages: 1077 Registered: July 2009 |
Senior Member |
|
|
@Service annotation has a params attribute that allows you to pass in key value pairs to your service implementation.
@Service
(
impl = MyPersonalValidationService.class,
params =
{
@Service.Param( name = "A", value = "1" },
@Service.Param( name = "B", value = "2" },
@Service.Param( name = "C", value = "3" }
}
)
In your service implementation, you can access parameters like so:
If your service implementation is widely used, even if it is just across your model, you may find it more convenient to define an annotation for your service to use. Once you have an annotation, you can even register your service via Sapphire extension mechanism, so that all you have to do is add the annotation.
|
|
|
Re: Pass values to custom validation service [message #1064596 is a reply to message #1064521] |
Thu, 20 June 2013 08:02 |
kon f Messages: 152 Registered: March 2012 |
Senior Member |
|
|
Hey Konstantin,
I used the parameter to create a custom validation service that spans over two properties. In my example, I need to make sure that at least one property is filled either first name or last name. I'm aware of @Required and @CountConstraint(min = 1) but they do not interact between each other on different properties.
I don't know if Sapphire provides for this uses case something out of the box. I could find anything similar in the example, so I implemented the following:
public class OrValidationService extends ValidationService {
@Override
public Status validate() {
IModelElement element = context(IModelElement.class);
boolean allEmpty = true;
for (String value : params().values()) {
allEmpty = allEmpty && element.empty(value);
}
if (allEmpty) {
return Status.createErrorStatus(getErrorMessage());
}
return Status.createOkStatus();
}
private String getErrorMessage() {
StringBuilder sb = new StringBuilder("At least one property must be filled either ");
for (Iterator<String> it = params().values().iterator(); it.hasNext();) {
String value = it.next();
ModelProperty mp = (ModelProperty) context(IModelElement.class).type().property(value);
String label = mp.getLabel(false, CapitalizationType.FIRST_WORD_ONLY, false);
if (it.hasNext()) {
sb.append(String.format("\"%s\" ", label));
}
else {
sb.append(String.format("or \"%s\"", label));
}
}
return sb.toString();
}
}
And the use of my custom validation service:
@Service
(
impl = OrValidationService.class,
params =
{
@Service.Param( name="property_name1", value="Firstname"),
@Service.Param( name = "property_name2", value = "Lastname" )
}
)
@DependsOn({ "Lastname" })
@LongString
ValueProperty PROP_FIRST_NAME = new ValueProperty(TYPE, "Firstname");
...
@Service
(
impl = OrValidationService.class,
params =
{
@Service.Param( name="property_name1", value="Firstname"),
@Service.Param( name = "property_name2", value = "Lastname" )
}
)
@DependsOn({ "Firstname" })
@LongString
ValueProperty PROP_LAST_NAME = new ValueProperty(TYPE, "Lastname");
Thank you!
Kon
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03444 seconds