Correct usage of beforeCreate event handler in Data API

I have the following table beforeCreate event handler setup for the Subscriptions table.









@Asset( "Subscriptions" )

public class SubscriptionsTableEventHandler extends com.backendless.servercode.extension.PersistenceExtender<Subscriptions>

{

	Logger logger;




	@Override

	public void beforeCreate( RunnerContext context, Subscriptions subscription) throws Exception

	{

		

	}

When I run a create request via the REST API I get the following error in CodeRunner:

Dec 09, 2014 5:51:23 AM com.backendless.coderunner.runtime.InvocationTask runImpl

SEVERE: argument type mismatchjava.lang.IllegalArgumentException: argument type mismatch

Running it in Debug mode it seems the 2nd argument subscriptions is a HashMap containing the same key/value members as the actual Subscriptions object properties would contain. What do I need in order to have it actually presented to be in a Subscriptions type Java object?

By the way I have the following setup in Bootstrap.java:


Backendless.Persistence.mapTableToClass( "Subscriptions", Subscriptions.class );

Hi Simon.
Do you have

Backendless.Persistence.mapTableToClass( "Devices", Devices.class );

in Bootstrap.java?

Regards,
Kate.

Yes I do, and just to be sure I re-exported all the table Java classes and Bootstrap.java content from the console to make sure all the table columns and corresponding Java object setter/getter methods were matching.

Hi, Simon,

We’ve made some updates to CodeRunner, could you please download it again and check the problem? Thanks in advance.

Hi,
I still get the following error:

Error updating subscription with backend: Code = 0, Message = java.util.HashMap cannot be cast to com.backendless.digitalcopel.models.Subscriptions

I assign task to dev team. They will check this issue again.
Sorry for this inconvenience.

Regards,
Kate.

Hi, Simon,
Could you please provide the implementation of Subscriptions class you are getting error with?

Just checked it using your application and Data Service’s REST console with CodeRunner in debug mode and request body {“Classroom”:“XXXX”} - Subscriptions object was created successfully. Could you please try to do the same and tell me what result you get? Or tell me your own steps to reproduce the problem.

Hi Sergey,

Here’s how you can reproduce my issue:

  1. Run the following beforeCreate handler on the Subscriptions table.
 
 
 
 
 
 
 
 
@Asset( "Subscriptions" ) 
 
public class SubscriptionsTableEventHandler extends com.backendless.servercode.extension.PersistenceExtender<Subscriptions> 
 
{ 
 
Logger logger; 
 
 
 
 
@Override 
 
public void beforeCreate( RunnerContext context, Subscriptions subscription) throws Exception 
 
{ 
 
logger = Logger.getLogger(SubscriptionsTableEventHandler.class); 
 
debug("SubscriptionsTableEventHandler#beforeCreate called"); 
 
super.beforeCreate(context, subscription);


}

void info(String message) { 
System.out.println(message); 
logger.info(message); 
} 
 
 
void debug(String message) { 
System.out.println(message); 
logger.debug(message); 
} 
 
 
void error(String message) { 
System.out.println(message); 
logger.error(message); 
} 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
}


  1. Run the following curl command:
curl -H application-id:xxxxxxx -H secret-key:xxxxx -H Content-Type:application/json -X POST -d '{"NextNormalMemberTopup":"01/23/2015 15:32:00 GMT+0900","Expires":"12/23/2014 15:32:00 GMT+0900","objectId":null,"created":"01/01/0001 00:00:00 GMT+0900","updated":"01/01/0001 00:00:00 GMT+0900","ownerId":null,"Expired":false,"Enabled":true,"Type":"Premium","Device":{"objectId":"3A2681A5-8CF8-470A-FF59-72C6E012BA00","created":"12/22/2014 13:07:44 GMT+0900","updated":"12/23/2014 06:31:36 GMT+0900","ownerId":null,"graphicsShaderLevel":"30","supportsSparseTextures":"False","graphicsDeviceVendorID":"4318","supportsVibration":"False","npotSupport":"Restricted","supportsInstancing":"False","supports3DTextures":"False","systemMemorySize":"16384","supportsGyroscope":"False","supportsShadows":"True","graphicsDeviceID":"4073","supportsLocationService":"False","supportsAccelerometer":"False","supportsStencil":"1","graphicsPixelFillrate":"-1","supportsComputeShaders":"False","deviceModel":"MacBookPro11,3","supportsRenderTextures":"True","supportsRenderToCubemap":"True","graphicsDeviceVersion":"OpenGL ES 2.0 [emulated]","processorCount":"8","deviceName":"Simon\u2019s MacBook Pro","graphicsDeviceName":"Emulated GPU running OpenGL ES 2.0","operatingSystem":"Mac OS X 10.9.5","deviceType":"Desktop","graphicsDeviceVendor":"Emulated","supportedRenderTargetCount":"1","deviceUniqueIdentifier":"BA63F798-85E3-5AC9-98CB-EA31FE9C7F9D","processorType":"Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz","graphicsMemorySize":"2048","supportsImageEffects":"True","___class":"Devices","__meta":"{\"relationRemovalIds\":{},\"selectedProperties\":[\"supportsLocationService\",\"operatingSystem\",\"systemMemorySize\",\"supportsRenderTextures\",\"graphicsDeviceID\",\"ownerId\",\"processorCount\",\"graphicsDeviceVersion\",\"deviceModel\",\"deviceUniqueIdentifier\",\"supportsGyroscope\",\"supportsAccelerometer\",\"__meta\",\"supportsStencil\",\"supportedRenderTargetCount\",\"___class\",\"graphicsDeviceVendor\",\"supports3DTextures\",\"graphicsPixelFillrate\",\"supportsInstancing\",\"supportsShadows\",\"deviceName\",\"created\",\"updated\",\"objectId\",\"deviceType\",\"supportsRenderToCubemap\",\"graphicsDeviceName\",\"graphicsDeviceVendorID\",\"graphicsShaderLevel\",\"graphicsMemorySize\",\"supportsVibration\",\"supportsImageEffects\",\"npotSupport\",\"processorType\",\"supportsSparseTextures\",\"supportsComputeShaders\"],\"relatedObjects\":{}}"},"___class":"Subscriptions","__meta":null}' -v [url=https://api.backendless.com/dev/data/Subscriptions]https://api.backendless.com/dev/data/Subscriptions[/url] 



and the JSON response is:

{“message”:null,“code”:0}

and in CodeRunner I get:

SEVERE: argument type mismatchjava.lang.IllegalArgumentException: argument type mismatch

Thank you, we finally found a problem and are now working on it. We’ll notify you as soon as it is fixed.

The problem is that you’re passing datetime values as string - this is not allowed in REST requests on Backendless. You should use unix timestamps in milliseconds. So the proper request will look like:

curl -H application-id:xxxxxxx -H secret-key:xxxxx -H Content-Type:application/json -X POST -d '{"NextNormalMemberTopup":1422027120000,"Expires":1419348720000,"objectId":null,"created":"01/01/0001 00:00:00 GMT+0900","updated":"01/01/0001 00:00:00 GMT+0900","ownerId":null,"Expired":false,"Enabled":true,"Type":"Premium","Device":{"objectId":"3A2681A5-8CF8-470A-FF59-72C6E012BA00","created":"12/22/2014 13:07:44 GMT+0900","updated":"12/23/2014 06:31:36 GMT+0900","ownerId":null,"graphicsShaderLevel":"30","supportsSparseTextures":"False","graphicsDeviceVendorID":"4318","supportsVibration":"False","npotSupport":"Restricted","supportsInstancing":"False","supports3DTextures":"False","systemMemorySize":"16384","supportsGyroscope":"False","supportsShadows":"True","graphicsDeviceID":"4073","supportsLocationService":"False","supportsAccelerometer":"False","supportsStencil":"1","graphicsPixelFillrate":"-1","supportsComputeShaders":"False","deviceModel":"MacBookPro11,3","supportsRenderTextures":"True","supportsRenderToCubemap":"True","graphicsDeviceVersion":"OpenGL ES 2.0 [emulated]","processorCount":"8","deviceName":"Simon\u2019s MacBook Pro","graphicsDeviceName":"Emulated GPU running OpenGL ES 2.0","operatingSystem":"Mac OS X 10.9.5","deviceType":"Desktop","graphicsDeviceVendor":"Emulated","supportedRenderTargetCount":"1","deviceUniqueIdentifier":"BA63F798-85E3-5AC9-98CB-EA31FE9C7F9D","processorType":"Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz","graphicsMemorySize":"2048","supportsImageEffects":"True","___class":"Devices","__meta":"{\"relationRemovalIds\":{},\"selectedProperties\":[\"supportsLocationService\",\"operatingSystem\",\"systemMemorySize\",\"supportsRenderTextures\",\"graphicsDeviceID\",\"ownerId\",\"processorCount\",\"graphicsDeviceVersion\",\"deviceModel\",\"deviceUniqueIdentifier\",\"supportsGyroscope\",\"supportsAccelerometer\",\"__meta\",\"supportsStencil\",\"supportedRenderTargetCount\",\"___class\",\"graphicsDeviceVendor\",\"supports3DTextures\",\"graphicsPixelFillrate\",\"supportsInstancing\",\"supportsShadows\",\"deviceName\",\"created\",\"updated\",\"objectId\",\"deviceType\",\"supportsRenderToCubemap\",\"graphicsDeviceName\",\"graphicsDeviceVendorID\",\"graphicsShaderLevel\",\"graphicsMemorySize\",\"supportsVibration\",\"supportsImageEffects\",\"npotSupport\",\"processorType\",\"supportsSparseTextures\",\"supportsComputeShaders\"],\"relatedObjects\":{}}"},"___class":"Subscriptions","__meta":null}' -v[url=https://api.backendless.com/dev/data/Subscriptions]https://api.backendless.com/dev/data/Subscriptions[/url]

(NextNormalMemberTopup and Expires values have been changed).

Besides that, we’ve made some fixes to CodeRunner so you need to update it.

Regards,
Sergey