Reputation: 355
I am working on a Spring MVC3 application and I have a requirement to enable/disable a link in a page and provide a tool tip on the link (some kind of help), if the link is disabled. The link can be disabled in many scenarios and I would like to know the various layers that should be impacted.
For example, consider a test case where an admin user is trying to renew a subscription. I should be able to renew if
In my case, I have the controller, call a service that determines whether the user is eligible for renewal. If not eligible for renewal, then call another service that provides the help text. The view (JSP) then enables/disables the link and also displays the help text based on modelAttribute.
My question is..
How can I avoid two calls to the service (Which seems to invoke the same methods again). I can add an attribute to the User model to hold the help text but I am not sure if the help text should be in the model
How to pull the renewal help text from a property file instead of hardcoding in the service. I am using messages.properties in the validator but I am not sure how to use it in a @service.
Should I just hard code the renewal help in the View (JSP) instead of over engineering it?
I have some code snippets below to clarify further, on the query..
public class UserController {
@RequestMapping(value="/viewUserDetails", method=RequestMethod.GET)
public String userDetails() {
if(userService.isEligibleToRenew()) {
user.setEligibleToRenew(true);
model.addAttribute("renewalHelp", userService.getRenewalHelp())
}
}
}
@Service
public class UserService {
public boolean isEligibleToRenew (User user) {
if (isAdminUser() &&
isSubscriptionEndingin30days() &&
!isRenewingOwnSubscription()) {
return true;
}
return false;
}
public String renewalHelp(User user) {
if (!isAdminUser ()) {
return "You must be an admin to renew your subscription";
} else if (!isSubscriptionEndingin30days()) {
return "Your subscription is not expiring in the next 30 days. You cannot renew now";
} else if (isRenewingOwnSubscription()) {
return "You cannot renew your own subscription";
}
}
}
Upvotes: 2
Views: 208
Reputation: 29827
Make your service return an object that contains all the information that it requires, in this way you'll only call the service (with all its logic) only once.
Something I've done in the past (in the object that I mentioned above), is to have an enum that describes the message, and then use Spring or any MVC framework to pick a message from a resource bundle with the localized message.
Maybe, if it keeps things simple and this feature to display different messages is not that important.
About 1, I used that in a big ecommerce app in which we had to display a price with a text. The text could have hints related to discounts (e.g. now £1.99 or from £3.99), so I created an object that described a price with an enum + value. The enum later was localized to a message in the correct locale.
Upvotes: 2