Solved

Java ArrayList inside of CFSCRIPT

Posted on 2014-07-29
17
313 Views
Last Modified: 2014-07-30
I was given some sample java code to use to pass information back to a web service.   We are using ColdFusion and I need to be able to put the ArrayList code inside of the CFSCRIPT to send it back.

List<CheckoutRecord> list = new ArrayList<CheckoutRecord>();
CheckoutRecord record = new CheckoutRecord();
record.setCustomerID=("000123123")
list.add(record);

Open in new window


I get the error message A script statement must end with ";" so I know I am missing something really easy...
0
Comment
Question by:digitalwise
  • 6
  • 5
  • 3
  • +2
17 Comments
 
LVL 6

Expert Comment

by:rjohnsonjr
Comment Utility
line 3 is missing a ; at the end.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
List<CheckoutRecord> list = new ArrayList<CheckoutRecord>();
CheckoutRecord record = new CheckoutRecord();
record.setCustomerID("000123123");
list.add(record);
                                  

Open in new window

Not only that - i think it should read as above
0
 

Author Comment

by:digitalwise
Comment Utility
Good catch both of you.   But I am getting the error on the first line - List<CheckoutRecord> list = new ArrayList<CheckoutRecord>();

<cfscript>

	List<CheckoutRecord> list = new ArrayList<CheckoutRecord>();
CheckoutRecord record = new CheckoutRecord();
record.setCustomerID("000123123");
list.add(record);
	
  

</cfscript>

Open in new window

0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
Check whether its an AWT or Util List - or you have made the right library import.
0
 

Author Comment

by:digitalwise
Comment Utility
So I wasn't creating an object (because clearly I am out of my depth with this).   This isn't working yet but I need to start with that createObject piece - not calling it all correctly though.

myArray = createObject("java", "java.util.ArrayList<CheckoutRecord>").init();
CheckoutRecord record = new CheckoutRecord();
record.setCustomerID("6190");

Open in new window

0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
Prob because you are missing the syntax in line 1 of CEHJ's comment.
0
 

Author Comment

by:digitalwise
Comment Utility
Yep - but I don't know how to write it - that's why it isn't there.   This is way out of what I normally do so I am looking for some help.   I was trying to use what I wrote instead.
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
@digitalwise

These guys are right about the java syntax, but the CF translation is a bit different.

List<CheckoutRecord> list = new ....

a) CF is typeless, so you don't need to declare the variable type as you do in java. It's just:

<cfscript>      
    list = ...something...;
</cfscript>      

Open in new window



b) In CF you must use createObject() to create an instance of an object. Then invoke the psuedo-constructor init() instead of using the keyword new.

c) CF is typeless and doesn't use the java generics syntax.

So get rid of the type declaration, generics, and create the ArrayList like this:

<cfscript>
    list = createObject("java", "java.util.ArrayList").init();
    // you can now add ANY type of object to the list
    // but as a simple test use a string like "foo"
    list.add( "foo" );
</cfscript>

Open in new window



  CheckoutRecord record = new CheckoutRecord();


What type of object CheckoutRecord: CFC, custom java class, ...?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You probably need to invoke createObject on CheckoutRecord too
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Probably. Is CheckoutRecord a class from a custom java jar? If yes

1) Did you add it to the CF class path?
2) What's the full path to that class? ie The full path for ArrayList is java.util.ArrayList
0
 

Author Comment

by:digitalwise
Comment Utility
CheckoutRecord is a class from a custom java jar - we are connected to a webservice

 ws = CreateObject("webservice",
"website?wsdl");
    ws.AnotherCustomClass("username", "password", "systemID");
    req = GetSOAPResponse(ws);

Open in new window


then I am supposed to generate the list of variables to send back to them.
0
 

Author Comment

by:digitalwise
Comment Utility
this is what I am trying to send back:

ws.subscribeNotification("systemid","client trans id", "token", list);

Open in new window


And list is supposed to be all of the variables that I am trying to set with this array.
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
I'm confused. I though you were trying to connect to the web service w/just java, but it looks like you're actually using createObject("webservice").  Did you already try using just CF ... and something didn't work? For lists, you can usually just create a CF array.  Append structures to it (which CF should convert to instances of the custom class). Then pass the list into the subscribeNotification() method.

Did you try that and it didn't work?
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Also, is there a public wsdl we can check out?
0
 

Author Comment

by:digitalwise
Comment Utility
No public WSDL.   I am connecting to their web service, getting a key, and then sending information back to them that is supposed to be a list of variables.  

So connect.

ws = CreateObject("webservice",
"website?wsdl");
    ws.AnotherCustomClass("username", "password", "systemID");
    req = GetSOAPResponse(ws);

Open in new window


Get Key

Make my List

They sent me the sample code to create the list

List<CheckoutRecord> list = new ArrayList<CheckoutRecord>();
CheckoutRecord record = new CheckoutRecord();
record.setCustomerID("00000123132");
record.setProductID("09123");
list.add(record);

Open in new window




Send List back to them.

ws.subscribeNotification("systemid","client trans id", "token", list);

Open in new window


Somewhere I am not doing something properly...
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Please start posting error output - there's got to be some ..?
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
Comment Utility
The reason I ask is that java does things differently, so the code you'd use there is often different than what you'd need in CF.

Typically when you use createObject("webservice") you are only work with simple values like strings, dates, arrays (ie what java calls a List) and structures (a Map in java). Java uses classes like "CheckoutRecord".to represent complex objects in WSDL. Whereas CF uses structures to represent complex objects - not classes.  

Translated that means you normally create a structure with the right properties to represent "CheckoutRecord" instead of using createObject("java", "CheckoutRecord"). Then append the structures to your "list".  In cfscript something like this.  

Note - I'm just guessing at the property names.  As mentioned, we need to see error messages - and the WSDL snippet for the CheckoutRecord to be more specific.

         // create a new structure
         record = {};
         // create a structure to represent the CheckoutRecord
         record.customerID = "00000123132";
         record.productID = "09123";
         // append the record to your list
         list = [ record ];

         // create the web service
        ws = createObject("webservice", "the url here....");
        ws.subscribeNotification("systemid","client trans id", "token", list);

Open in new window


Did you try something like that?


> createObject("java", "CheckoutRecord").

In order to create an instance of the java class "CheckoutRecord" you need it's full package path, not just the name ie Like "java.util.ArrayList" not just "ArrayList". Also, the jar containing that class needs to be in your class path.  I don't if you even have a separate jar containing that class...  Honestly I'd try the pure CF approach first..
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now