Updating SharedObject Data Value in ActionScript - actionscript-3

I want to update the value of _third.data["#ID"] in this code. What i want to do it to add something with _third.data["#ID"] when storing it in this.ID
What i want to do is:
this.ID = "myvalue" + _third.data["#ID"];
but in output i get myvalueundefined but if i remove "myvalue" from code thn it works fine and gives me the correct value please help me my knowledge of actionscript isn't that much i just learned some of it from internet.
Here is the coding:
public function main(_first:String, _second:Array, _third:SharedObject):Object{
this.ID = _third.data["#ID"];
};
What i want to do is:
public function main(_first:String, _second:Array, _third:SharedObject):Object{
this.ID = "myvalue" + _third.data["#ID"];
};

Using your code, I created the below test case. It's doing the same as your code, and is outputting the expected values.
package kazo
{
import flash.net.SharedObject;
/**
* ...
* #author KM
*/
public class TestCases
{
private var ID :String = null;
public function TestCases() { }
/**
* Start the test case
*/
public function start():void {
trace('Starting test case');
var SO:SharedObject = SharedObject.getLocal('TestCases');
if (!SO.data["#ID"]) {
trace('SO data not found, creating');
SO.data["#ID"] = '123';
SO.flush();
}
getID(SO);
}
/**
*
* #param _SO
*/
public function getID(_SO:SharedObject):void {
this.ID = "myvalue" + _SO.data["#ID"];
// trace = myvalue123
trace(this.ID);
}
}
}
trace:
(fcsh)Build succeeded
Done(0)
[Starting debug session with FDB]
Starting test case
myvalue123
If you're still having problems, I'd suggest trying to call toString() on the value you're pulling, so:
this.ID = "myvalue" + _SO.data["#ID"].toString();
Should that still not work, store it in two separate String variables and then combined them.

Related

In AS3/AIR how can I execute the result method of a responder object?

We are creating an extension for the NetConnection object. When we get an error we will retry “n” times. After “n” times we want to call the responder result method returning an error. The responder object contains two properties, both functions, however we cannot seem to see how to access those functions. Is it possible to execute the “result” function of Responder object?
An example might be;
_rspResponder:Responder = new Responder(someFunction);
// lots of code...
_rspResponder.result("Error: Connection timeout failure.\n A network connection error occurred, if needed please try to save work and restart the application.\n Otherwise just restart the application.\n");
Edit:
Based on comments below I created an extended responder like;
package com.fluorinefx
{
import flash.net.Responder;
public class PublicResponder extends Responder
{
/**
* result - Result handler function
*
* #return result
*/
private var _result:Function = null;
public function get result():Function
{
return this._result;
}
public function set result(value:Function):void
{
this._result = value;
}
/**
* status - Status (error) handler function
*
* #return status
*/
private var _status:Function = null;
public function get status():Function
{
return this._status;
}
public function set status(value:Function):void
{
this._status = value;
}
public function PublicResponder(result:Function, status:Function=null)
{
_result = _result;
_status = status;
super(result, status);
}
}
}
When I attempt to use it in my extended NetConnection like;
import com.fluorinefx.PublicResponder;
private var _rspResponder:PublicResponder;
public override function call(strCommand:String, rspResponder:Responder, ...parameters:Array):void
_rspResponder = rspResponder;
I get an implicit coercion error on the "_rspResponder = rspResponder;" line.
After “n” times we want to call the responder result method returning an error.
Is it possible to execute the result function of the Responder object?
Does something like this setup help you?
//# global vars
public var _rspResponder :Responder;
public var try_count :int = 0;
//# test it...
_rspResponder = new Responder( onResult, onError );
//# Since "result" param can be Object or String... using asterix to allow any data-type
private function onResult( result:* ) :void
{
trace( result ); try_count = 0;
}
//# no comment needed here...
private function onError( error:Object ) :void
{
try_count++;
_rspResponder = new Responder( onResult, onError ); //# retry again
//# after N retries, send custom error String to "onResult" function
if ( try_count == 5)
{
onResult( "Error: Connection timeout failure.\n A network connection error occurred, if needed please try to save work and restart the application.\n Otherwise just restart the application.\n" );
}
}

Show usage of dynamic generated callable function name

Is there any posibility in PhpStorm to map usage of dynamic generated fucntion between it declaration an usage?
Assume I have next code:
<?php
class TestExample {
public function __construct($component) {
$component_parts = $this->get_dynamic_component_part_list($component);
$this->load_component_parts($component, $component_parts);
}
private function get_dynamic_component_part_list($component){
//Complex logic to get attached parts by $component
$component_parts = array('part1', 'part2');
return $component_parts;
}
private function load_component_parts(&$component, $component_parts) {
foreach ($component_parts as $component_part) {
$component[$component_part] = $this->{'load_' . $component_part}($component['id']);
}
}
private function load_part1($id) {
//Loading and prepare condition from one source
$part1 = new Part1($id);
// Complex algorithm
return $part1;
}
private function load_part2($id) {
//Loading and prepare condition from another source
$part2 = new Part2($id);
// Complex algorithm
return $part2;
}
}
class Part1 {
}
class Part2 {
}
I want to see usage of load_part1 and load_part2.
Is there any way to do it by usage phpDoc or in some other way?
At this moment PhpStorm notice me that this function doesn't have usage but realy it used in load_component_parts method.
You can use the phpDoc annotation #see.
For example:
$className = 'SomeClass';
$method = 'methodToCall';
$anArgument = 'bar';
/** #see SomeClass::someMethod() */
$foo = call_user_func([$className, $method], $anArgument);
This annotation will create at least a reference to this code, so that you know to come back here when you review SomeClass::someMethod() before throwing the "unused" method away.

SmartGWT RestDataSource calling an existing REST service

I have a database back-end that I've thoroughly tested with several unit tests. The controller looks like this:
#RequestMapping(value = "/create", method = RequestMethod.POST, produces = "application/json", headers = "content-type=application/json")
public #ResponseBody UserDTO createUser(#RequestBody UserDTO user)
{
UserEntity userEntity = service.add(user);
return mappingUser(userEntity);
}
The unit test looks like:
#Test
public void testCreateUser() throws Exception
{
UserDTO userDto = createUserDto();
String url = BASE_URL + "/rest/users/create";
UserDTO newUserDto = restTemplate
.postForObject(url, userDto, UserDTO.class, new Object[]{});
}
I have confirmed that the unit test works great, and the actual web-service is called correctly, and data is put into the database.
Now, I am using a SmartGWT RestDataSource, and I am trying to configure the RestDataSource correctly to pass a new user in the request body, and return the new object. I want to send the data as JSON in the body, and return JSON from this call. So, it might be that I need to change the controller itself to match up with the datasource.
Here is the AbstractDataSource which extends RestDataSource:
import java.util.Map;
import com.google.gwt.http.client.URL;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.OperationBinding;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.DSProtocol;
public abstract class AbstractRestDataSource extends RestDataSource
{
public AbstractRestDataSource(String id)
{
setID(id);
setClientOnly(false);
// set up FETCH to use GET requests
OperationBinding fetch = new OperationBinding();
fetch.setOperationType(DSOperationType.FETCH);
fetch.setDataProtocol(DSProtocol.GETPARAMS);
DSRequest fetchProps = new DSRequest();
fetchProps.setHttpMethod("GET");
fetch.setRequestProperties(fetchProps);
// set up ADD to use POST requests
OperationBinding add = new OperationBinding();
add.setOperationType(DSOperationType.ADD);
add.setDataProtocol(DSProtocol.POSTMESSAGE);
DSRequest addProps = new DSRequest();
addProps.setHttpMethod("POST");
addProps.setContentType("application/json");
add.setRequestProperties(addProps);
// set up UPDATE to use PUT
OperationBinding update = new OperationBinding();
update.setOperationType(DSOperationType.UPDATE);
update.setDataProtocol(DSProtocol.POSTMESSAGE);
DSRequest updateProps = new DSRequest();
updateProps.setHttpMethod("PUT");
update.setRequestProperties(updateProps);
// set up REMOVE to use DELETE
OperationBinding remove = new OperationBinding();
remove.setOperationType(DSOperationType.REMOVE);
DSRequest removeProps = new DSRequest();
removeProps.setHttpMethod("DELETE");
remove.setRequestProperties(removeProps);
// apply all the operational bindings
setOperationBindings(fetch, add, update, remove);
init();
}
#Override
protected Object transformRequest(DSRequest request)
{
super.transformRequest(request);
// now post process the request for our own means
postProcessTransform(request);
return request.getData();
}
/*
* Implementers can override this method to create a
* different override.
*/
#SuppressWarnings("rawtypes")
protected void postProcessTransform(DSRequest request)
{
StringBuilder url = new StringBuilder(getServiceRoot());
Map dataMap = request.getAttributeAsMap("data");
if (request.getOperationType() == DSOperationType.REMOVE)
{
// in case of remove, append the primary key
url.append(getPrimaryKeyProperty()).append("/").append(dataMap.get(getPrimaryKeyProperty()));
}
else if (request.getOperationType() == DSOperationType.UPDATE)
{
url.append("update");
appendParameters(url, request);
}
else if (request.getOperationType() == DSOperationType.FETCH && dataMap.size() > 0)
{
url.append(getPrimaryKeyProperty()).append("/").append(dataMap.get(getPrimaryKeyProperty()));
}
else if (request.getOperationType() == DSOperationType.ADD)
{
url.append("create");
}
System.out.println("AbstractRestDataSource: postProcessTransform: url=" + url.toString());
request.setActionURL(URL.encode(url.toString()));
}
/*
* This simply appends parameters that have changed to the URL
* so that PUT requests go through successfully. This is usually
* necessary because when smart GWT updates a row using a form,
* it sends the data as form parameters. Most servers cannot
* understand this and will simply disregard the form data
* sent to the server via PUT. So we need to transform the form
* data into URL parameters.
*/
#SuppressWarnings("rawtypes")
protected void appendParameters(StringBuilder url, DSRequest request)
{
Map dataMap = request.getAttributeAsMap("data");
Record oldValues = request.getOldValues();
boolean paramsAppended = false;
if (!dataMap.isEmpty())
{
url.append("?");
}
for (Object keyObj : dataMap.keySet())
{
String key = (String) keyObj;
if (!dataMap.get(key).equals(oldValues.getAttribute(key)) || isPrimaryKey(key))
{
// only append those values that changed or are primary keys
url.append(key).append('=').append(dataMap.get(key)).append('&');
paramsAppended = true;
}
}
if (paramsAppended)
{
// delete the last '&'
url.deleteCharAt(url.length() - 1);
}
}
private boolean isPrimaryKey(String property)
{
return getPrimaryKeyProperty().equals(property);
}
/*
* The implementer can override this to change the name of the
* primary key property.
*/
protected String getPrimaryKeyProperty()
{
return "id";
}
protected abstract String getServiceRoot();
protected abstract void init();
}
And here is the UserDataSource which extends AbstractRestDataSource:
import java.util.Map;
import com.google.gwt.http.client.URL;
import com.opensource.restful.shared.Constants;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.fields.DataSourceBooleanField;
import com.smartgwt.client.data.fields.DataSourceDateField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSOperationType;
public class UserDataSource extends AbstractRestDataSource
{
private static UserDataSource instance = null;
public static UserDataSource getInstance()
{
if (instance == null)
{
instance = new UserDataSource("restUserDS");
}
return instance;
}
private UserDataSource(String id)
{
super(id);
}
private DataSourceIntegerField userIdField;
private DataSourceBooleanField userActiveField;
private DataSourceTextField usernameField;
private DataSourceTextField passwordField;
private DataSourceTextField firstnameField;
private DataSourceTextField lastnameField;
private DataSourceTextField emailField;
private DataSourceTextField securityQuestion1Field;
private DataSourceTextField securityAnswer1Field;
private DataSourceTextField securityQuestion2Field;
private DataSourceTextField securityAnswer2Field;
private DataSourceDateField birthdateField;
private DataSourceIntegerField positionIdField;
protected void init()
{
setDataFormat(DSDataFormat.JSON);
setJsonRecordXPath("/");
// set the values for the datasource
userIdField = new DataSourceIntegerField(Constants.USER_ID, Constants.TITLE_USER_ID);
userIdField.setPrimaryKey(true);
userIdField.setCanEdit(false);
userActiveField = new DataSourceBooleanField(Constants.USER_ACTIVE, Constants.TITLE_USER_ACTIVE);
usernameField = new DataSourceTextField(Constants.USER_USERNAME, Constants.TITLE_USER_USERNAME);
passwordField = new DataSourceTextField(Constants.USER_PASSWORD, Constants.TITLE_USER_PASSWORD);
firstnameField = new DataSourceTextField(Constants.USER_FIRST_NAME, Constants.TITLE_USER_FIRST_NAME);
lastnameField = new DataSourceTextField(Constants.USER_LAST_NAME, Constants.TITLE_USER_LAST_NAME);
emailField = new DataSourceTextField(Constants.USER_EMAIL, Constants.TITLE_USER_EMAIL);
securityQuestion1Field =
new DataSourceTextField(Constants.USER_SECURITY_QUESTION_1, Constants.TITLE_USER_SECURITY_QUESTION_1);
securityAnswer1Field =
new DataSourceTextField(Constants.USER_SECURITY_ANSWER_1, Constants.TITLE_USER_SECURITY_ANSWER_1);
securityQuestion2Field =
new DataSourceTextField(Constants.USER_SECURITY_QUESTION_2, Constants.TITLE_USER_SECURITY_QUESTION_2);
securityAnswer2Field =
new DataSourceTextField(Constants.USER_SECURITY_ANSWER_2, Constants.TITLE_USER_SECURITY_ANSWER_2);
birthdateField = new DataSourceDateField(Constants.USER_BIRTHDATE, Constants.TITLE_USER_BIRTHDATE);
positionIdField = new DataSourceIntegerField(Constants.USER_POSITION_ID, Constants.TITLE_USER_POSITION_ID);
// positionActiveField = new DataSourceBooleanField(Constants.USER_ACTIVE, Constants.TITLE_USER_ACTIVE);
// positionCodeField;
// positionDescriptionField;
setFields(userIdField, userActiveField, usernameField, passwordField, firstnameField, lastnameField,
emailField, birthdateField, securityQuestion1Field, securityAnswer1Field, securityQuestion2Field,
securityAnswer2Field, positionIdField);
}
protected String getServiceRoot()
{
return "rest/users/";
}
protected String getPrimaryKeyProperty()
{
return "userId";
}
/*
* Implementers can override this method to create a
* different override.
*/
#SuppressWarnings("rawtypes")
protected void postProcessTransform(DSRequest request)
{
// request.setContentType("application/json");
StringBuilder url = new StringBuilder(getServiceRoot());
Map dataMap = request.getAttributeAsMap("data");
if (request.getOperationType() == DSOperationType.REMOVE)
{
// in case of remove, append the primary key
url.append(getPrimaryKeyProperty()).append("/").append(dataMap.get(getPrimaryKeyProperty()));
}
else if (request.getOperationType() == DSOperationType.UPDATE)
{
url.append("update");
System.out.println("UserDataSource: postProcessTransform: update: url=" + url.toString());
}
else if (request.getOperationType() == DSOperationType.FETCH && dataMap.size() > 0)
{
url.append(getPrimaryKeyProperty()).append("/").append(dataMap.get(getPrimaryKeyProperty()));
}
else if (request.getOperationType() == DSOperationType.ADD)
{
url.append("create");
}
System.out.println("UserDataSource: postProcessTransform: url=" + url.toString());
request.setActionURL(URL.encode(url.toString()));
}
}
If I can find out how to get the UserDTO as JSON into the requestBody, I think I will have solved all my issues. For extra information you should know, I am using Spring 3.2 and the Jackson Message Converters configured in the springmvc-servlet.xml file.
At one point I did see all the data getting appended to the URL, but I would prefer if the data was not in the URL as parameters, but rather in the request body. SO, I need to know if this is possible, and how to do it.
Thanks for any help!!!
You probably want to undo all of these modifications and just implement the default RestDataSource protocol, which already passes the request body as JSON if you just call RestDataSource.setDataFormat(). There are sample JSON messages in the docs:
http://www.smartclient.com/smartgwtee/javadoc/com/smartgwt/client/data/RestDataSource.html
Among other problems you've created:
the different CRUD operations now go to distinct URLs and use different HTTP verbs, so they can no longer be combined into a single queue and sent together. This means you can't do basic things like perform a mixture of create and update operations together in a transaction, save a new Order along with it's OrderItems, or save data and also fetch dependent data needed to transition to a new screen.
you're assuming the "fetch" will be based on HTTP GET, but this requires awkward encoding of nested criteria structures (AdvancedCriteria) into URL parameters, which can easily hit the maximum URL length
For these and other reasons, most generated Java services do not meet the needs of a modern UI, and the auto-generation approach should not be used. Deeper explanation in the FAQ:
http://forums.smartclient.com/showthread.php?t=8159#aExistingRest

Using TestNG, is it possible to dynamically change the test name?

Using TestNG, is it possible to dynamically change the test name with a method such as this one below?
#Test(testName = "defaultName", dataProvider="tests")
public void testLogin( int num, String reportName )
{
System.out.println("Starting " + num + ": " + reportName);
changeTestName("Test" + num);
}
No, but your test class can implement org.testng.ITest and override getTestName() to return the name of your test.
For anybody still facing this.
This can be done by implementing the org.testng.ITest class and overriding the getTestName() method just like as #Cedric mentions.
To make the test name dynamic you can use a locally created testName variable In addition. Below is all you need to do
import java.lang.reflect.Method;
import org.testng.ITest;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
public class MyTestClass implements ITest {
#Test(dataProvider = "/* yourDataProvider */")
public void myTestMethod() {
//Test method body
}
#BeforeMethod(alwaysRun = true)
public void setTestName(Method method, Object[] row) {
//You have the test data received through dataProvider delivered here in row
String name = resolveTestName(row);
testName.set(name);
}
#Override
public String getTestName() {
return testName.get();
}
private ThreadLocal<String> testName = new ThreadLocal<>();
}
This way you should be able to generate the testName dynamically
I'm getting the error below, about the two parameters of BeforeMethod, what is wrong?
Message:
org.testng.TestNGException:
Method initLog requires 2 parameters but 2 were supplied in the BeforeMethod annotation.
#Parameters({"method", "FinancialKeys"})
#BeforeMethod(alwaysRun = true)
public void initLog(Method method, Object[] FinancialKeys) {...}

ActionScript - Determine If Value is Class Constant

i'd like to throw an argument error if a particular function doesn't work without a passed value that also happens to be a public constant of the class containing the function.
is there anyway to determine if a class owns a public constant instead of having to iterate thru all of them?
something like this:
public static const HALIFAX:String = "halifax";
public static const MONTREAL:String = "montreal";
public static const TORONTO:String = "toronto";
private var cityProperty:String;
public function set city(value:String):void
{
if (!this.hasConstant(value))
throw new ArgumentError("set city value is not applicable.");
cityProperty = value;
}
public function get city():Strig
{
return cityProperty;
}
currently, for this functionality i have to write the city setter function like this:
public function set city(value:String):void
{
if (value != HALIFAX && value != MONTREAL && value != TORONTO)
throw new ArgumentError("set city value is not applicable.");
cityProperty = value;
}
is this the only way to accomplish this task?
Yes, if you use reflections:
private var type:Class;
private var description:XML;
private function hasConstant (str : String ) : Boolean
{
if (description == null)
{
type = getDefinitionByName (getQualifiedClassName (this)) as Class;
description = describeType (type);
}
for each ( var constant:XML in description.constant)
{
if (type[constant.#name] == str) return true;
}
return false;
}
Note that for this to work, all constants must always be String objects declared public static const.
I was looking for an answer to this question myself and found it annoying that hasOwnProperty() did not work for static properties. Turns out though, that if you cast your class to a Class object, it does work.
Here's an example:
public final class DisplayMode
{
public static const one: String = "one";
public static const two: String = "two";
public static const three: String = "three";
public static function isValid(aDisplayMode: String): Boolean {
return Class(DisplayMode).hasOwnProperty(aDisplayMode);
}
}
I owe this solution to jimmy5804 from this discussion, so hats off to him.
You should be able to use bracket notation to do this. For example:
var foo:Sprite = new Sprite();
foo.rotation = 20;
trace( foo["x"], foo["rotation"]); // traces "0 20"
or more specific to your case:
var bar:String = "rotation";
trace( foo[bar] ); // traces "20"
The only thing you have to look out for here, is that the bracket accessor will throw a ReferenceError if you ask for an object property that isn't there, such as:
trace ( foo["cat"] ); // throws ReferenceError
But it will not throw if you are asking for a static property:
trace ( Sprite["cat"] ); // traces "undefined"
So in your case you might try:
if ( this[value] == undefined ) {
throw new ArgumentError("set city value is not applicable.");
}
EDIT:
Sorry, I was confusing the const's names with their values.
For this to work on your problem you would have to make the String value the same as the const's name, so for example:
public static const HALIFAX:String = "HALIFAX";
then you could use the query as described above and it would give you the desired result.