Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » Sapphire » Pass values to custom validation service
Pass values to custom validation service [message #1064466] Wed, 19 June 2013 12:18 Go to next message
kon f is currently offline kon fFriend
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 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
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:

params().get( "A" )


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 Go to previous messageGo to next message
kon f is currently offline kon fFriend
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
Re: Pass values to custom validation service [message #1064713 is a reply to message #1064596] Thu, 20 June 2013 15:54 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 1077
Registered: July 2009
Senior Member
In 0.7, you are can express your constraint by using @Validation annotation, which supports EL.

@Validation( rule = "${ FirstName != null || LastName != null }", message = "First name or last name must be specified." )
Re: Pass values to custom validation service [message #1075309 is a reply to message #1064713] Mon, 29 July 2013 08:05 Go to previous message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
Hey Konstantin,

thank you for the suggestion Smile

Kon

[Updated on: Mon, 29 July 2013 08:33]

Report message to a moderator

Previous Topic:Customized properties displayed in ListProperty
Next Topic:How to programmatically call "Sapphire.Add" or "Sapphire.Browse"
Goto Forum:
  


Current Time: Sat Oct 05 09:41:59 GMT 2024

Powered by FUDForum. Page generated in 0.03444 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top