i'm new to Grails i'm using Grails 2.5.1 , i'm creating a service but when i call this service i get the bellow errors :
Class:groovy.lang.MissingPropertyExceptionMessage:No such property: flash for class: com.sherif.UtilsService Possible solutions: class
and
Class:groovy.lang.MissingMethodExceptionMessage:No signature of method: com.sherif.UtilsService.render() is applicable for argument types: (java.util.LinkedHashMap) values: [[view:forget]] Possible solutions: every(), find(), find(groovy.lang.Closure), every(groovy.lang.Closure)
here is my service :
class UtilsService {
def sendPassword(params) {
def enteredCeredintials = User.findByEmail(params?.email)
if (enteredCeredintials?.email==null) {
flash.message = message(code: 'user.email.notfound', args: [params?.email])
}
else {
flash.message = message(code: 'user.passwordSent', args: [params?.email])
}
render (view: "forget")
}
}
what may caused these errors ?
Thanks
flash is temporary storage used in the web tier - it's one of the implicit variables that's available in controllers, along with request, response, session, etc. But it's not available in services - there are no implicit variables in services.
Ideally you should keep each tier separate. Services shouldn't work with or be aware of the web tier - they're helper classes that use business logic to perform various tasks, work with the database, etc. They're called by controllers, but shouldn't be polluted with web tier concepts.
To fix this, extract out the logic that is independent of the web tier and do that work in the service. Then return whatever data you need to render the view in the controller, and in this case to additionally store some data in flash scope.
Related
Usually I have seen in OSGi development that one service binds to another service. However I am trying to inject an OSGi service in a non-service class.
Scenario trying to achieve: I have implemented a MessageBusListener which is an OSGi service and binds to couple of more services like QueueExecutor etc.
Now one of the tasks of the MessageBusListener is to create a FlowListener (non-service class) which would invoke the flows based on the message content. This FlowListener requires OSGi services like QueueExecutor to invoke the flow.
One of the approach I tried was to pass the reference of the services while creating the instance of FlowListener from MessageBusListener. However when the parameterized services are deactivated and activated back, I think OSGi service would create a new instance of a service and bind to MessageBusListener, but FlowListener would still have a stale reference.
#Component
public class MessageBusListener
{
private final AtomicReference<QueueExecutor> queueExecutor = new AtomicReference<>();
#Activate
protected void activate(Map<String, Object> osgiMap)
{
FlowListener f1 = new FlowListener(queueExeciutor)
}
Reference (service = QueueExecutor.class, cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.STATIC)
protected void bindQueueExecutor(QueueExecutor queueExecutor)
{
this.queueExecutor = queueExecutor;
}
}
public class FlowListener
{
private final AtomicReference<QueueExecutor> queueExecutor;
FlowListener(QueueExecutor queueExecutor)
{
this.queueExecutor = queueExecutor;
}
queueExecutor.doSomething() *// This would fail in case the QueueExecutor
service was deactivated and activated again*
}
Looking forward to other approaches which could suffice my requirement.
Your approach is correct you just need to also handle the deactivation if necessary.
If the QueueExecutor disappears the MessageBuslistener will be shut down. You can handle this using a #Deactivate method. In this method you can then also call a sutdown method of FlowListener.
If a new QeueExecutor service comes up then DS will create a new MessageBuslistener so all should be fine.
Btw. you can simply inject the QueueExecutor using:
#Reference
QueueExecutor queueExecutor;
Currently, I created 2 singleton classes one for processing services(e.g WebService.h) and other for handle accessing to database (e.g DatabaseHandler.h).
Do you think i should separate them into specific classes and then very easy to resuse? and if you have any other structure / pattern to apply this case please guide me.
Regards,
Jony
The webservice class should invoke the database broker if the user calls a data persistence action from the web api like GET, PUT, DELETE, etc. So i would tier the structure as follows:
DatabaseHandler(){
// plain old db handler
}
CustomDatabaseHandler extends DatabaseHandler(){
// now we extend the main instance to provide
// more scalability and room
// for custom logic overrides
}
$dbh = new CustomDatabaseHandler()
WebService($dbh){
this.db = $dbh
func create(){
// do stuff here
this.db.create()
}
func read(){
// do stuff here
this.db.read()
}
}
I have a Grails webapp running a Spring Integration inbound-channel-adapter that is configured to receive emails and there is a service activator that processes the message. This service activator pulls out pieces and parts of the email based on business rules and then needs to update Grails domain objects and save those changes to the database.
Spring Integration service activator code snippet:
HashMap<String, Serializable> params = new HashMap<String, Serializable>();
params.put("notification", notification );
params.put("notificationType", notificationType );
params.put("sender", notificationSender );
InvokerHelper.invokeMethod(NotificationCreationService.class, "createNotification", params);
Grails NotificationCreationService action snippet:
def static createNotification() {
if (params.notification != null && params.notificationType != null && params.sender != null) {
String notification = params.get("notification") as String
NotificationType notificationType = params.get("notificationType") as NotificationType
String sender = params.get("sender") as String
def NotificationMessage notificationMessage = null
notificationMessage = new NotificationMessage()
notificationMessage.notification = notification
notificationMessage.save(flush: true)
...
}
}
NotificationMessage is a standard Grails domain class
Error generated:
org.springframework.integration.MessageHandlingException:
groovy.lang.MissingMethodException: No signature of method: mycompany.pyproject.mypackage.NotificationMessage.save() is applicable for argument types: () values: []
Possible solutions: save(), save(boolean), save(java.util.Map), wait(), any(), wait(long)
If I change createNotification() to not be static then I get the following:
org.springframework.integration.MessageHandlingException:
groovy.lang.MissingMethodException: No signature of method: static mycompany.pyproject.mypackage.NotificationCreationService.createNotification() is applicable for argument types: (java.util.HashMap) values: [...]
Possible solutions: createNotification(java.util.HashMap), createNotification(java.lang.String, napa.changedetection.alert.NotificationType, java.lang.String)
I've tried other combinations of InvokeHelper.invokeMethod() and the definitions of the Grails actions with similar results. InvokeHelper has several other methods for invoking methods as well as setting different properties, but I haven't found the magic combination to make this work and any internet searched have turned up very little as far as example code for InvokeHelper.
Ideally, the Grails action would not be static and it would use the standard params mechanism. This would allow me to call the same action directly from the Grails code as well as from the Spring Integration service activator via the InvokeMethod functionality.
Does anyone have any thought on how to tie this together?
You should not use InvokeHelper now, Grails 2 injects signature at compile time for java purposes (except dynamic finders, but you can guess why).
In your Java bean I would inject notificationCreationService using :
resources.groovy/xml + setter
OR
Constructor + applicationContext (implement ApplicationContextAware)
Using the service reference should work, if not there is a bug to analyse there.
Where did you declare your service activator ?
I registered my services.yml file like below :
services:
PMI.form.users_tasks:
class: PMI\UserBundle\Form\UsersTasksType
arguments:
EntityManager: "#doctrine.orm.default_entity_manager"
I can list it by php app/console container:debug, so that mean my service is registered properly.
In my UsersTasksType class I have like below :
class UsersTasksType extends AbstractType
{
protected $ur;
public function __construct(EntityManager $ur )
{
$this->setUr($ur);
}
// Get and setters
}
Does Dependency Injection mean that I don't have to pass the EntityManager to the class constructor anymore? Or what ?
Because when I have to run the code below :
$form = $this->createForm(new UsersTasksType(), $entity);
I get this error:
Catchable Fatal Error: Argument 1 passed to PMI\UserBundle\Form\UsersTasksType::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in C:\wamp\www\PMI_sf2\src\PMI\UserBundle\Controller\UsersTasksController.php on line 74 and defined in C:\wamp\www\PMI_sf2\src\PMI\UserBundle\Form\UsersTasksType.php line 19
And I have to do something below :
$em = $this->container->get('doctrine.orm.entity_manager');
$form = $this->createForm(new UsersTasksType($em), $entity);
So what would be the whole purpose of Dependency Injection ?
Dependency Injection basically gives one service (in this case, your UserTasksType) access to another service (in this case, your the entity manager).
arguments:
EntityManager: "#doctrine.orm.default_entity_manager"
These two lines tell Symfony to expect the entity manager service to be passed into the constructor when you instantiate a new UserTasksType object, which effectively gives your UserTasksType access to the entity manager.
If you aren't using the entity manager in your UserTasksType, there is no need to inject it in the constructor and you could get rid of the two lines above and the __construct() / setUr() methods in your UserTasksType.
A better example to help you understand DIC might be that you have a service that is written specifically to send emails (Swiftmail, for e.g.) and you need to inject it into another service so that service can send emails.
By adding
arguments: [ #mailer ]
to your service definition, your services constructor will expect your mailer service
__construct ($mailer)
{
$this->mailer = $mailer;
}
which will give it access to send emails
someFunction()
{
//do something useful, then send an email using the swift mailer service
$this->mailer->sendEmail();
}
Check out the latest Symfony docs for more of an explanation.
http://symfony.com/doc/current/book/service_container.html
Is it possible to inject a Spring bean into a Grails webflow? I tried the following
class CheckoutController {
ShoppingService shoppingService
def checkoutFlow = {
start {
action {
// This attempt to access the service doesn't work
flow.addresses = shoppingService.getOrder()
}
}
}
}
I can access shoppingService from a regular controller action, but I can't access it from an action of the webflow (see above).
add the following to your controller:
def transient shoppingService
There are issues with dependency injection with webflows in controllers that contain traditional actions plus webflows. It worked for me if the traditional action executed first.
see:
GRAILS-7095
GRAILS-4141
Webflows also break notions of defaultAction in mixed controllers. I have found the first webflow wins and becomes the default action.
separately using transient keeps your service from be serialized between flow states. (e.g. don't have to implement serializable)
At first I thought what you listed was pseudocode but I made a sample app using your example and got the NPE as well. I think it may be your flow structure that is the problem. action blocks should go within a flow state. Your flow definition should look something like:
class CheckoutController {
ShoppingService shoppingService
def checkoutFlow = {
start {
action {
flow.addresses = shoppingService.getOrder()
if(flow.addresses) {
showForm()
}
else {
showError()
}
}
on("showForm").to "showForm"
on("showError").to "showError"
}
showError {
...
}
//etc.
}
}
You can definitely use injected services in your web flows. I am guessing that the problem lies in your flow structure.