Solved

Java ArrayList inside of CFSCRIPT

Posted on 2014-07-29
17
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
  • +2
17 Comments
 
LVL 6

Expert Comment

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

Expert Comment

by:CEHJ
ID: 40227568
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
ID: 40227610
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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
LVL 16

Expert Comment

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

Author Comment

by:digitalwise
ID: 40227734
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
ID: 40227756
Prob because you are missing the syntax in line 1 of CEHJ's comment.
0
 

Author Comment

by:digitalwise
ID: 40227813
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_
ID: 40227853
@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
 
LVL 86

Expert Comment

by:CEHJ
ID: 40227860
You probably need to invoke createObject on CheckoutRecord too
0
 
LVL 52

Expert Comment

by:_agx_
ID: 40227865
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
ID: 40228123
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
ID: 40228133
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_
ID: 40228171
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_
ID: 40228268
Also, is there a public wsdl we can check out?
0
 

Author Comment

by:digitalwise
ID: 40229116
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
ID: 40229160
Please start posting error output - there's got to be some ..?
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 40229173
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java: The Public Class Main 4 66
how to debug htl and js pages 8 80
Find out all repos that a user is most active on Github 1 58
Read CLOB data from Oracle using JAVA 3 43
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses

751 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