Creating a DAO for Deployment
To access business objects in .NET and Java environments, you
must write a data access class or classes.
The code in these examples represents what exists within the
data access section of an application since it bridges across MATLAB data
and data types and Java and .NET data types.
Note
In these examples, a fake component generated using the MATLAB builder
products called deploymentExamples is used. Assume
it has been imported. |
Initializing a Component
Use these examples as a framework for initializing a component.
Java
DeploymentExamples deployment = null;
try
{
deployment = new DeploymentExamples ();
//**************
//Use the deployment code here
// (see examples below)
//**************
}
catch(MWException mw_ex)
{
mw_ex.printStackTrace();
}
finally
{
deployment.dispose();
}
.NET
DeploymentExamples.DeploymentExamples deployment = null;
try
{
deployment = new DeploymentExamples.DeploymentExamples();
//******************************
//**Use your deployment code here
//** (See examples below)
//******************************
}
finally
{
deployment.Dispose();
}
Back to Top
Interacting with a Component
You interact with a component by passing inputs to a deployed
application or producing MATLAB output from a deployed application.
All of these examples fit where the comment block resides in Initializing a Componentand the same
component class is used. The Java and .NE Builder infrastructure handles
data marshalling when passing parameters to a component. Data conversion
rules can be found in the MATLAB builder documentation. If a
specific data type is required, you can use the MWArray objects
and pass in the appropriate data type.
Converting an Integer to a MATLAB Data Type
Some of the ways to pass inputs to a deployed applications are
demonstrated in these examples:
Java
int n = 3;
MWNumericArray x = new MWNumericArray(n, MWClassID.DOUBLE);
.NET
int n = 3;
MWNumericArray x = new MWNumericArray(n, true);
Converting Array Data to a MATLAB Data Type
Arrays can be converted to several different MATLAB data
types. An example of converting a String array into a cell array follows:
Java
//Create the array of data…
String[] friendsArray1 =
{
"Jordan Robert",
"Mary Smith",
"Stacy Flora",
"Harry Alpert"
};
int numberOfArrayElements1 = friendsArray1.length;
int numberOfArrayColumns1 = 1;
//Create the MWCellArray to store the data
MWCellArray cellArray1 =
new MWCellArray(
numberOfArrayColumns1,
numberOfArrayElements1);
//Iterate through the array and add the elements to
// the cell array.
for(int i = 1; i<friendsArray1.length+1; i++)
{
cellArray1.set(i, friendsArray1[i-1]);
}
.NET
String[] array =
{
"Jordan Robert",
"Mary Smith",
"Stacy Flora",
"Harry Alpert"
};
int numberOfArrayElements = array.Length;
int numberOfArrayColumns = 1;
MWCellArray cellArray =
new MWCellArray(
numberOfArrayColumns,
numberOfArrayElements);
for (int i = 1; i < array.Length + 1; i++)
{
cellArray[i] = array[i - 1];
}
Converting a List to a MATLAB Data Type
A list can be converted to several different MATLAB data
types. An example of converting a List of Strings into a cell array
follows:
Java
//Create a list of data…
List friendsList = new LinkedList();
friendsList.add("Jordan Robert");
friendsList.add("Mary Smith");
friendsList.add("Stacy Flora");
friendsList.add("Harry Alpert");
int numberOfListElements = friendsList.size();
int numberOfListColumns = 1;
//Create a MWCellArray to store the data
MWCellArray cellArray2 =
new MWCellArray(
numberOfListColumns,
numberOfListElements);
//Iterate through the list adding the elements
// to the cell array.
Iterator friendsListItr = friendsList.iterator();
for(int i = 1;friendsListItr.hasNext(); i++)
{
String currentFriend = (String)friendsListItr.next();
cellArray2.set(i, currentFriend);
}
.NET
List<String> list = new List<String>();
list.Add("Jordan Robert");
list.Add("Mary Smith");
list.Add("Stacy Flora");
list.Add("Harry Alpert");
int numberOfArrayElements = list.Count;
int numberOfArrayColumns = 1;
MWCellArray cellArray =
new MWCellArray(
numberOfArrayColumns,
numberOfArrayElements);
int i = 1;
foreach (String currentElement in list)
{
cellArray[i] = currentElement;
i++;
}
Converting Name Value Pairs to a MATLAB Data Type
Java (Maps)
It is common to have maps of data (name value pairs). The corresponding
.NET data type is Dictionary. The most similar data type in MATLAB is
the structure. Here is an example where you convert a map of people's
names into a MATLAB structure.
//First we create a Java HashMap (java.util.HashMap).
Map firendsMap = new HashMap();
friendsList.put("Jordan Robert", new Integer(3386));
friendsList.put("Mary Smith", new Integer(3912));
friendsList.put("Stacy Flora", new Integer(3238));
friendsList.put("Harry Alpert", new Integer(3077));
//Now we set up the MATLAB Structure that we will fill
// with this data.
int numberOfElements = firendsMap.size();
int numberOfColumns = 1;
String[] fieldnames = {"name", "phone"};
MWStructArray friendsStruct =
new MWStructArray(
numberOfElements,
numberOfColumns,
fieldnames);
//Now we iterate through our map, filling in the structure
Iterator friendsMapItr = friendsMap.keySet().iterator();
for(int i = 1; friendsMapItr.hasNext(); i++)
{
String key = (String)friendsMapItr.next();
friendsStruct.set(fieldnames[0], i,
new MWCharArray(key));
friendsStruct.set(fieldnames[1], i (Integer)
friendsMap.get(key));
}
.NET (Dictionaries)
Dictionary<String, int> dictionary =
new Dictionary<String, int>();
dictionary.Add("Jordan Robert", 3386);
dictionary.Add("Mary Smith", 3912);
dictionary.Add("Stacy Flora", 3238);
dictionary.Add("Harry Alpert", 3077);
int numberOfElements = dictionary.Count;
int numberOfColumns = 1;
String[] fieldnames = { "name", "phone" };
MWStructArray output =
new MWStructArray(
numberOfElements,
numberOfColumns,
fieldnames);
int i = 1;
foreach (String currentKey in dictionary.Keys)
{
output[fieldnames[0], i] = currentKey;
output[fieldnames[1], i] = dictionary[currentKey];
i++;
}
Getting MATLAB Numerics
from a Deployed Application
This code resides in the try block for an
initialized component (see Initializing a Component).
Java
Object[] numericOutput = null;
MWNumericArray numericArray = null;
try
{
numericOutput = deployment.getNumeric(1);
numericArray = (MWNumericArray)numericOutput[0];
int i = numericArray;
}
finally
{
MWArray.disposeArray(numericArray);
}
.NET
MWNumericArray result = (MWNumericArray)deployment.getNumeric();
int resultInt = result.ToScalarInteger();
Getting MATLAB Strings
from a Deployed Application
Java
Object[] stringOutput = null;
MWCharArray stringArray = null;
try
{
stringOutput = deployment.getString(1);
stringArray = (MWCharArray) stringOutput [0];
String s = stringArray;
}
finally
{
MWArray.disposeArray(stringArray);
}
.NET
MWCharArray result = (MWCharArray)deployment.getString();
String resultString = result.ToString();
Getting MATLAB Numeric
Arrays from a Component
Java
Object[] numericArrayOutput = null;
MWNumericArray numericArray1 = null;
try
{
numericArrayOutput = deployment.getNumericArray(1);
numericArray1 = (MWNumericArray)numericArrayOutput[0];
int[] array = numericArray1.getIntData();
}
finally
{
MWArray.disposeArray(numericArray1);
}
.NET
MWNumericArray result =
(MWNumericArray)deployment.getNumericArray();
Double[] doubleArray =
(Double[])result.ToVector(MWArrayComponent.Real);
Getting Character Arrays from a Component
Java
Object[] stringArrayOutput = null
MWCharArray mwCharArray = null;
try
{
stringArrayOutput = deployment.getStringArray(1);
mwCharArray = ((MWCharArray)stringArrayOutput[0];
char[] charArray = new char[mwCharArray.numberOfElements()];
for(int i = 0; i < charArray.length; i++)
{
char currentChar =
((Character)mwCharArray.get(i+1)).charValue();
charArray[i] = currentChar;
}
}
finally
{
MWArray.disposeArray(mwCharArray);
}
.NET
// Note that since MWCharArray doesn't have a
// ToVector method, it is necessary
// to iterate through and get a single
// dimension for the output.
// MWCharArray result =
// (MWCharArray)deployment.getStringArray();
char[,] resultArray = (char[,])result.ToArray();
char[] outputArray = new char[resultArray.GetLength(1)];
for (int i = 0; i < resultArray.GetLength(1); i++)
{
outputArray[i] = resultArray[0, i];
}
Getting Byte Arrays from a Component
Java
Object[] byteOutput = null;
MWNumericArray numericByteArray = null;
try
{
byteOutput = deployment.getByteArray(1);
numericByteArray = (MWNumericArray)byteOutput[0];
byte[] byteArray = numericByteArray.getByteData();
}
finally
{
MWArray.disposeArray(numericByteArray);
}
.NET
MWNumericArray result =
(MWNumericArray)deployment.getByteArray();
byte[] outputByteArray =
(byte[])result.ToVector(MWArrayComponent.Real);
Getting Cell Arrays from a Component
Java
This example shows how to iterate through a cell array and put
the elements into a list or an array:
Object[] cellArrayOutput = null;
MWCellArray cellArray = null;
try
{
cellArrayOutput = deployment.getCellArray();
cellArray = (MWCellArray)cellArrayOutput[0];
List listOfCells = new LinkedList();
Object[] arrayOfCells =
new Object[cellArray.numberOfElements()];
for(int i = 0; i < cellArray.numberOfElements(); i++)
{
Object currentCell = cellArray.getCell(i + 1);
listOfCells.add(currentCell);
arrayOfCells[i] currentCell;
}
}
finally
{
MWArray.disposeArray(cellArray);
}
.NET
MWCellArray result = (MWCellArray)deployment.getCellArray();
List<Object> outputList = new List<Object>();
Object[] outputArray = new Object[result.NumberOfElements];
for (int i = 0; i < result.NumberOfElements; i++)
{
outputArray[i] = result[i + 1];
outputList.Add(result[i + 1]);
}
Getting Structures from a Component
Java
Object[] structureOutput = deployment.getStruct(1);
MWStructArray structureArray =
(MWStructArray)structureOutput[0];
try
{
Object[] structureOutput = deployment.getStruct(1);
structureArray = (MWStructArray)structureOutput[0];
Map mapOfStruct = new HashMap();
for(int i = 0; i < structureArray.fieldName().length(); i++)
{
String keyName = structureArray.fieldNames()[i];
Object value = structureArray.getField(i + 1);
mapOfStruct.put(keyName, value);
}
}
finally
{
MWArray.disposeArray(structureArray);
}
.NET
MWStructArray result = (MWStructArray)deployment.getStruct();
Dictionary<Object, Object> output =
new Dictionary<Object, Object>();
for (int i = 0; i < result.FieldNames.Length; i++)
{
output.Add(result.FieldNames[i],
result.GetField(result.FieldNames[i]));
}
Getting a WebFigure from a Component and Attaching It to a
Page
Java
For more information about WebFigures, see Deploying a Java Component Over the Web in the MATLAB Builder JA User's Guide.
Object[] webFigureOutput = null;
MWJavaObjectRef webFigureReference = null;
try
{
webFigureOutput = deployment.getWebFigure(1);
webFigureReference = (MWJavaObjectRef)webFigureOutput[0];
WebFigure f = (WebFigure)webFigureReference.get();
}
finally
{
MWArray.disposeArray(webFigureOutput);
MWArray.disposeArray(webFigureReference);
}
//forward the request to the View layer (response.jsp)
RequestDispatcher dispatcher =
request.getRequestDispatcher("/response.jsp");
dispatcher.forward(request, response);
Note
This code will not do anything if executed directly. It needs
a response.jsp to produce output. |
.NET
For more information about WebFigures, see Deploying a MATLAB Figure Over the Web Using WebFigures in
the MATLAB Builder NE User's Guide.
The first two lines of code will not do anything if executed directly.
It needs a WebFigureControl on a front end page
that is called WebFigureControl1. If you are not
using a local WebFigureControl and want to simply
return the WebFigure in the current response object, use this code,
for example:
WebFigure webFigure = new WebFigure(deployment.getWebFigure());
WebFigureControl1.WebFigure = webFigure;
//First, attach the webfigure to one of ASP.NET's caches,
// in this case the session cache
Session["SessionStateWebFigure"] = webFigure;
//Now, use a WebFigure Utility to get an HTML String that
// will display this figure, Notice
// how we reference the name we used when attaching it
// to the cache and we indicate
// that the Attach type is session.
String localEmbedString =
WebFigureServiceUtility.GetHTMLEmbedString(
"SessionStateWebFigure",
WebFigureAttachType.session,
300,
300);
Response.Write(localEmbedString);
Getting Encoded Image Bytes from an Image in a Component
Java
public byte[] getByteArrayFromDeployedComponent()
{
Object[] byteImageOutput = null;
MWNumericArray numericImageByteArray = null;
try
{
byteImageOutput =
deployment.getImageDataOrientation(
1, //Number Of Outputs
500, //Height
500, //Width
30, //Elevation
30, //Rotation
"png" //Image Format
);
numericImageByteArray = (MWNumericArray)byteImageOutput[0];
return numericImageByteArray.getByteData();
}
finally
{
MWArray.disposeArray(byteImageOutput);
}
}
.NET
public byte[] getByteArrayFromDeployedComponent()
{
MWArray width = 500;
MWArray height = 500;
MWArray rotation = 30;
MWArray elevation = 30;
MWArray imageFormat = "png";
MWNumericArray result =
(MWNumericArray)deployment.getImageDataOrientation(
height,
width,
elevation,
rotation,
imageFormat);
return (byte[])result.ToVector(MWArrayComponent.Real);
}
Getting a Buffered Image in a Component
Java
public byte[] getByteArrayFromDeployedComponent()
{
Object[] byteImageOutput = null;
MWNumericArray numericImageByteArray = null;
try
{
byteImageOutput =
deployment.getImageDataOrientation(
1, //Number Of Outputs
500, //Height
500, //Width
30, //Elevation
30, //Rotation
"png" //Image Format
);
numericImageByteArray = (MWNumericArray)byteImageOutput[0];
return numericImageByteArray.getByteData();
}
finally
{
MWArray.disposeArray(byteImageOutput);
}
}
public BufferedImage getBufferedImageFromDeployedComponent()
{
try
{
byte[] imageByteArray = getByteArrayFromDeployedComponent()
return ImageIO.read(new ByteArrayInputStream(imageByteArray));
}
catch(IOException io_ex)
{
io_ex.printStackTrace();
}
}
.NET
public byte[] getByteArrayFromDeployedComponent()
{
MWArray width = 500;
MWArray height = 500;
MWArray rotation = 30;
MWArray elevation = 30;
MWArray imageFormat = "png";
MWNumericArray result =
(MWNumericArray)deployment.getImageDataOrientation(
height,
width,
elevation,
rotation,
imageFormat);
return (byte[])result.ToVector(MWArrayComponent.Real);
}
public Image getImageFromDeployedComponent()
{
byte[] byteArray = getByteArrayFromDeployedComponent();
MemoryStream ms = new MemoryStream(myByteArray, 0,
myByteArray.Length);
ms.Write(myByteArray, 0, myByteArray.Length);
return Image.FromStream(ms, true);
}
Getting Image Data from a WebFigure
The following example shows how to get image data from a WebFigure
object. It also shows how to specify the image type and the orientation
of the image.
.NET
WebFigure figure =
new WebFigure(deployment.getWebFigure());
WebFigureRenderer renderer =
new WebFigureRenderer();
//Creates a parameter object that can be changed
// to represent a specific WebFigure and its orientation.
//If you dont set any values it uses the defaults for that
// figure (what they were when the figure was created in M).
WebFigureRenderParameters param =
new WebFigureRenderParameters(figure);
param.Rotation = 30;
param.Elevation = 30;
param.Width = 500;
param.Height = 500;
//If you need a byte array that can be streamed out
// of a web page you can use this:
byte[] outputImageAsBytes =
renderer.RenderToEncodedBytes(param);
//If you need a .NET Image (can't be used on the web)
// you can use this code:
Image outputImageAsImage =
renderer.RenderToImage(param);
Back to Top
 | Working with the Business Service Layer | | Hosting a DAO on a Web Server |  |
Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
Get the Interactive Kit