Asset Control Java API samples
The sample is based on the official asset control document but it can compile and run with the actual result.
1. Runnable Java sample to get the result for ado query and ado browse query.
package section3;
import java.util.ArrayList;
import java.util.List;
import com.ac.api.data.model.AdoId;
import com.ac.api.data.model.AttributeObjectId;
import com.ac.api.data.model.UserId;
import com.ac.api.data.statics.Ado;
import com.ac.api.data.statics.StaticAttributeValue;
import com.ac.api.error.LogonException;
import com.ac.api.error.RemoteException;
import com.ac.api.error.ServerException;
import com.ac.api.factory.ApiFactory;
import com.ac.api.factory.ApiMetaFactory;
import com.ac.api.factory.DefaultApiMetaFactory;
import com.ac.api.query.AdoBrowseQuery;
import com.ac.api.query.AdoBrowseResult;
import com.ac.api.query.AdoQuery;
import com.ac.api.query.AdoQueryOptions;
import com.ac.api.query.AdoResult;
import com.ac.api.query.QueryFactory;
import com.ac.api.server.ConnectionInfo;
import com.ac.api.server.Installation;
import com.ac.api.server.Installations;
import com.ac.api.server.RemoteServer;
import com.ac.api.server.ServerInterface;
public class ConnExample {
static final String HOST = "cpacnvb01";
static final String INSTANCE = "INSTANCE";
static final String USER = "USER";
static final String PASSWD = "PASSWD";
static final String ADO_ID = "ADO_ID";
static final String SYMBOL = "SYMBOL%";
static final Integer TIMEOUT = 120;
private QueryFactory queryFactory;
private ServerInterface serverInterface;
//also use createTimeSeriesQuery,createListQuery
public static void main(String[] args) throws ServerException {
// new ConnExample().adosQuery();
new ConnExample().browseQuery();
}
private void browseQuery() throws ServerException{
AdoBrowseQuery adoBrowseQuery = queryFactory.createAdoBrowseQuery("symbol like '"+SYMBOL+"'", true, true, false);
AdoBrowseResult adoBrowseResult = serverInterface.adoBrowseQuery(adoBrowseQuery);
for(Ado ado:adoBrowseResult){
System.out.println(ado.getId().getValue()+","+ado.getName());
}
}
private void adosQuery() throws ServerException {
AdoQuery adoQuery = queryFactory.createAdoQuery(AdoId.create(ADO_ID),
new ArrayList<AttributeObjectId>(), AdoQueryOptions.AS_OF_NOW, null,
AdoQueryOptions.CodeOptions.NO_CODES);
AdoResult adoResult = serverInterface.adoQuery(adoQuery);
List<StaticAttributeValue> attributeValues = adoResult.getAdo().getAttributeValues();
for (StaticAttributeValue sav : attributeValues){
System.out.println(sav.getAttributeId().getValue()+","+sav.getValue().toString());
}
}
public ConnExample() throws LogonException, RemoteException {
queryFactory = ApiFactory.getInstance().getQueryFactory();
serverInterface = getServerInterface();
}
private static ServerInterface getServerInterface() throws RemoteException,
LogonException {
final ApiMetaFactory apiFactory = DefaultApiMetaFactory.getInstance();
final RemoteServer remoteServer = apiFactory.getServerFactory()
.createRemoteServer();
final Installations installations = remoteServer.getInstallations(HOST,
null);
final Installation installation = installations
.getInstallation(INSTANCE);
final ConnectionInfo connectionInfo = ConnectionInfo.create(TIMEOUT);
final UserId userId = UserId.create(USER);
remoteServer.connect(installation, userId, PASSWD, connectionInfo);
ServerInterface serverInterface = remoteServer.getServerInterface();
return serverInterface;
}
}
2. Runnable Java sample to get the xml result based on the xml query file for ado query and ado browse
package xml;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.Writer;
import com.ac.api.data.model.UserId;
import com.ac.api.error.LogonException;
import com.ac.api.error.RemoteException;
import com.ac.api.error.ServerException;
import com.ac.api.factory.ApiFactory;
import com.ac.api.factory.ApiMetaFactory;
import com.ac.api.factory.DefaultApiMetaFactory;
import com.ac.api.query.QueryResult;
import com.ac.api.query.QuerySpec;
import com.ac.api.server.ConnectionInfo;
import com.ac.api.server.Installation;
import com.ac.api.server.Installations;
import com.ac.api.server.RemoteServer;
import com.ac.api.server.ServerInterface;
import com.ac.api.xml.XmlApiConsumer;
import com.ac.api.xml.XmlApiFormatter;
import com.ac.api.xml.XmlException;
public class XMLExample {
private static final ApiFactory apiFactory = ApiFactory.getInstance();
static final String HOST = "HOST";
static final String INSTANCE = "INSTANCE";
static final String USER = "USER";
static final String PASSWD = "PASSWD";
static final Integer TIMEOUT = 120;
static final String queryFilename = "AdoQuery.xml";
static final String queryBrowseFilename = "AdoBrowseQuery.xml";
static final String queryResultFile = "AdoQueryResult.xml";
public static void main(String[] args) throws XmlException, IOException,
ServerException {
String xmlQuerySpec = getXMLString(new File(queryBrowseFilename));
writeXML(xmlQuerySpec);
}
private static void writeXML(String xmlQuery) throws XmlException,
ServerException, IOException {
StringReader sbReader = new StringReader(xmlQuery);
final XmlApiConsumer consumer = apiFactory.getXmlConsumer();
final QuerySpec querySpec = consumer.getQuerySpec(sbReader);
ServerInterface serverInterface = getServerInterface();
final QueryResult queryResult = querySpec.execute(serverInterface);
final Writer xmlWriter = new FileWriter(queryResultFile);
final XmlApiFormatter xmlFormatter = apiFactory
.getXmlFormatter(xmlWriter);
xmlFormatter.addQueryResult(queryResult);
xmlWriter.close();
}
private static String getXMLString(File file) throws XmlException,
IOException {
final String xmlQuerySpec;
String line = null;
final StringBuffer stringBuffer = new StringBuffer();
final BufferedReader reader = new BufferedReader(new FileReader(
file));
while ((line = reader.readLine()) != null) {
stringBuffer.append(line).append(
System.getProperty("line.separator"));
}
xmlQuerySpec = stringBuffer.toString();
return xmlQuerySpec;
}
private static ServerInterface getServerInterface() throws RemoteException,
LogonException {
final ApiMetaFactory apiFactory = DefaultApiMetaFactory.getInstance();
final RemoteServer remoteServer = apiFactory.getServerFactory()
.createRemoteServer();
final Installations installations = remoteServer.getInstallations(HOST,
null);
final Installation installation = installations
.getInstallation(INSTANCE);
final ConnectionInfo connectionInfo = ConnectionInfo.create(TIMEOUT);
final UserId userId = UserId.create(USER);
remoteServer.connect(installation, userId, PASSWD, connectionInfo);
ServerInterface serverInterface = remoteServer.getServerInterface();
return serverInterface;
}
}
AdoQuery.xml
<?xml version="1.0"?>
<ado-query xmlns="http://www.asset-control.com/xml/api/3.1">
<ado id="ADO.SAMPLEID" />
<attributes default="true" />
<as-of-date timestamp="0" />
<link-options />
</ado-query>
AdoBrowseQuery.xml
<?xml version="1.0"?>
<ado-browse-query xmlns="http://www.asset-control.com/xml/api/3.1">
<query>symbol like 'SYMBOL%'</query>
<options use-inheritance="false" return-name="true" return-details="true" />
</ado-browse-query>
Thursday, September 26, 2013
Wednesday, September 25, 2013
disable scientific notation in Java
disable scientific notation in Java
To print the number without scientific notation (keep all numbers and without additional 0), although it's ugly, maybe the best way is to
double d = -0.0000000000000000001;
System.out.println(new BigDecimal(Double.toString(d)).stripTrailingZeros().toPlainString());
---0.0000000000000000001
To disable scientific notation globally, can use AOP or dynamic proxy to intercept Number/Decimal 's toString
On Swing JTable, it's controlled by NumberRenderer/DoubleRenderer, can provide custom render or convert it from database side:
Oracle pl/sql
FUNCTION get_orig_value(pi_value IN NUMBER)
RETURN VARCHAR2
IS
BEGIN
if pi_value = 0 then
RETURN '0';
end if;
if abs(pi_value) < 1 then
RETURN TO_CHAR(pi_value,'FM0D9999999999999999999999999999999999999999999999999999999999999');
else
RETURN TO_CHAR(pi_value,'TM');
end if;
END get_orig_value;
To print the number without scientific notation (keep all numbers and without additional 0), although it's ugly, maybe the best way is to
double d = -0.0000000000000000001;
System.out.println(new BigDecimal(Double.toString(d)).stripTrailingZeros().toPlainString());
---0.0000000000000000001
To disable scientific notation globally, can use AOP or dynamic proxy to intercept Number/Decimal 's toString
On Swing JTable, it's controlled by NumberRenderer/DoubleRenderer, can provide custom render or convert it from database side:
Oracle pl/sql
FUNCTION get_orig_value(pi_value IN NUMBER)
RETURN VARCHAR2
IS
BEGIN
if pi_value = 0 then
RETURN '0';
end if;
if abs(pi_value) < 1 then
RETURN TO_CHAR(pi_value,'FM0D9999999999999999999999999999999999999999999999999999999999999');
else
RETURN TO_CHAR(pi_value,'TM');
end if;
END get_orig_value;
Sunday, September 22, 2013
English from my son
ribbit
platypus
Emily tigers:
The little girl has a problem with her temper.
Emily pouted.
Emily growled
...she gave her loudest tiger ROAR!
"Not again!" Mom groaned.
"Come here, you rascal!"
Emily bounded around the room, and bounced out of the window.
"Me!" squealed Emily.
Emily stamped her foot and gave a huge ...
Emily snapped, and swishing her long, curly tail.
"Hello, I'm here!" sang Granny as she pranced through the door.
"Gollygumdrops, it looks like a zoo in here. What's been going on?"
"No need to e scared, pumpkin"
That night, Emily dreamt about being a tiger with Granny.
Granny leapt right over the moon.
Emily didn't dare follow her the.re.
You'll learn.
Emily spotted Dylan Jones making a fort.
"Can I help?" asked Emily.
"Hey, give it back!" shouted Emily, and she crouched down, ready to pounce.
Then she glimpsed over at Granny.
"Yippee!" Dylan whooped as he pedaled past. Emily ignored him.
Emily helped Dylan pick himself up.
"Are you OK?"
Granny grinned and gave Emily a big thumbs-up.
She helped Dad wash up, and she brushed her teeth without even the slightest growl.
She showed Emily how to soar over hedges, scale walls and climb trees.
platypus
Emily tigers:
The little girl has a problem with her temper.
Emily pouted.
Emily growled
...she gave her loudest tiger ROAR!
"Not again!" Mom groaned.
"Come here, you rascal!"
Emily bounded around the room, and bounced out of the window.
"Me!" squealed Emily.
Emily stamped her foot and gave a huge ...
Emily snapped, and swishing her long, curly tail.
"Hello, I'm here!" sang Granny as she pranced through the door.
"Gollygumdrops, it looks like a zoo in here. What's been going on?"
"No need to e scared, pumpkin"
That night, Emily dreamt about being a tiger with Granny.
Granny leapt right over the moon.
Emily didn't dare follow her the.re.
You'll learn.
Emily spotted Dylan Jones making a fort.
"Can I help?" asked Emily.
"Hey, give it back!" shouted Emily, and she crouched down, ready to pounce.
Then she glimpsed over at Granny.
"Yippee!" Dylan whooped as he pedaled past. Emily ignored him.
Emily helped Dylan pick himself up.
"Are you OK?"
Granny grinned and gave Emily a big thumbs-up.
She helped Dad wash up, and she brushed her teeth without even the slightest growl.
She showed Emily how to soar over hedges, scale walls and climb trees.
Friday, September 13, 2013
mvn test
maven test
The test scope is different with JUnit (in Eclipse)
The default includes for maven test will be (from http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html)
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
Another option is to add the configuration in build plugins like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<excludes>
<exclude>**/Test*.java</exclude>
</excludes>
</configuration>
</plugin>
The test scope is different with JUnit (in Eclipse)
The default includes for maven test will be (from http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html)
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
Another option is to add the configuration in build plugins like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<excludes>
<exclude>**/Test*.java</exclude>
</excludes>
</configuration>
</plugin>
Thursday, September 12, 2013
F6 S01E15
So much bigger than the cubicle. Oh, this is a cube.
Yes indeedy! With a beautiful view of...
I put a roof over your head! If that means nothing....
http://www.hjenglish.com/new/p528572/
Yes indeedy! With a beautiful view of...
I put a roof over your head! If that means nothing....
http://www.hjenglish.com/new/p528572/
Tuesday, August 27, 2013
Oracle 11g new features
select /*+ result_cache */ state_code,count(*) ...
In PL/SQL:
return sales_tax_rate.tax_rate%type
result_cache
relies_on (sales_tax_rate, customers)
is
l_ret sales_tax_rate.tax_rate%type;
begin
Notice the clause "relies on" in the function code. It tells the function that the cache depends on those two tables: customers and tax_rate.
If the data in those tables change, the cache needs to be refreshed. The refresh happens automatically without your intervention. If the data
does not change, the cache continues to provide the cached values as quickly as possible. (In Oracle Database 11g Release 2, the function
knows the tables it gets the data from and automatically checks for the data change in them; the RELIES_ON clause is not necessary. The syntax
still allows the table name but it is redundant.)
The cache is for a database instance; not for a session. This ability to make caching in one session available in all others is very
different from using package variables that hold the value in memory, which are visible in a session only.
Furthermore, the package variables have no idea about the underlying table being changed. You have to manually refresh them when the data
changes; otherwise the apps will get stale data. Both SQL Result Cache and PL/SQL Function Cache automatically refresh the cache when the
underlying table data changes; no user intervention is required.
Dynamic SQL:
DBMS_SQL.PARSE(v_cursor, v_plsql, DBMS_SQL.NATIVE);
DBMS_SQL.BIND_VARIABLE(v_cursor, 'jobs', v_jobs);
DBMS_SQL.BIND_VARIABLE(v_cursor, 'empnos', v_empnos);
v_execute := DBMS_SQL.EXECUTE(v_cursor);
pivot and unpivot
SELECT deptno
FROM emp
PIVOT (SUM(sal)
FOR deptno IN (10,20,30,40));
In PL/SQL:
return sales_tax_rate.tax_rate%type
result_cache
relies_on (sales_tax_rate, customers)
is
l_ret sales_tax_rate.tax_rate%type;
begin
Notice the clause "relies on" in the function code. It tells the function that the cache depends on those two tables: customers and tax_rate.
If the data in those tables change, the cache needs to be refreshed. The refresh happens automatically without your intervention. If the data
does not change, the cache continues to provide the cached values as quickly as possible. (In Oracle Database 11g Release 2, the function
knows the tables it gets the data from and automatically checks for the data change in them; the RELIES_ON clause is not necessary. The syntax
still allows the table name but it is redundant.)
The cache is for a database instance; not for a session. This ability to make caching in one session available in all others is very
different from using package variables that hold the value in memory, which are visible in a session only.
Furthermore, the package variables have no idea about the underlying table being changed. You have to manually refresh them when the data
changes; otherwise the apps will get stale data. Both SQL Result Cache and PL/SQL Function Cache automatically refresh the cache when the
underlying table data changes; no user intervention is required.
Dynamic SQL:
DBMS_SQL.PARSE(v_cursor, v_plsql, DBMS_SQL.NATIVE);
DBMS_SQL.BIND_VARIABLE(v_cursor, 'jobs', v_jobs);
DBMS_SQL.BIND_VARIABLE(v_cursor, 'empnos', v_empnos);
v_execute := DBMS_SQL.EXECUTE(v_cursor);
pivot and unpivot
SELECT deptno
FROM emp
PIVOT (SUM(sal)
FOR deptno IN (10,20,30,40));
PL/SQL note
An outer join does not require each record in the two joined tables to have a matching record. The joined table retains each record
—even if no other matching record exists. Outer joins subdivide further into left outer joins, right outer joins, and full outer joins,
depending on which table's rows are retained (left, right, or both).
A left outer join returns all the values from an inner join plus all values in the left table that do not match to the right table.
This allows us to find an employee's department, but still shows the employee(s) even when they have not been assigned to a department
SELECT *
FROM employee, department
WHERE employee.DepartmentID = department.DepartmentID(+)
Merge Row:
SELECT DepartmentID,
listagg(LastName, ', ') WITHIN GROUP (ORDER BY LastName) AS LastNames
FROM employee
GROUP BY DepartmentID;
Explain plan is the SQL command that you may use to get Oracle to tell you what the query plan for a given SQL query would be if you
executed it right now in your current environment. The way to read this plan is to turning it into a graph of sorts-an evaluation tree.
From left to right, down to top. Using SQL_TRACE, you can capture the actual plan used at runtime.
AUTOTRACE gives you a look at how much work it actually took to perform your query, providing some important statistics regarding its
actual execution.
TKPROF is real and more powerful.
Currently, there are three transaction isolation levels supported by the Oracle database. There is a fourth transition isolation level, read uncommitted, that is not supported by Oracle.
These transaction isolation levels are:
Read Committed
Serializable
Read Only
The read committed transaction isolation level is the Oracle default. With this setting, each query can see only data committed before before the query (not the transaction) began.
Any transaction that executes a given query more than once can experience non-repeatable reads or phantoms.
Unlike the read committed level transactions, serializable isolation level transactions will not experience non-repeatable reads or phantoms.
All queries in an Oracle serializable transaction see the database as of a single point in time
With the Read Only transaction isolation level, only data that was committed at the start of the transaction can be accessed by a query. No modification to the data is allowed.
—even if no other matching record exists. Outer joins subdivide further into left outer joins, right outer joins, and full outer joins,
depending on which table's rows are retained (left, right, or both).
A left outer join returns all the values from an inner join plus all values in the left table that do not match to the right table.
This allows us to find an employee's department, but still shows the employee(s) even when they have not been assigned to a department
SELECT *
FROM employee, department
WHERE employee.DepartmentID = department.DepartmentID(+)
Merge Row:
SELECT DepartmentID,
listagg(LastName, ', ') WITHIN GROUP (ORDER BY LastName) AS LastNames
FROM employee
GROUP BY DepartmentID;
Explain plan is the SQL command that you may use to get Oracle to tell you what the query plan for a given SQL query would be if you
executed it right now in your current environment. The way to read this plan is to turning it into a graph of sorts-an evaluation tree.
From left to right, down to top. Using SQL_TRACE, you can capture the actual plan used at runtime.
AUTOTRACE gives you a look at how much work it actually took to perform your query, providing some important statistics regarding its
actual execution.
TKPROF is real and more powerful.
Currently, there are three transaction isolation levels supported by the Oracle database. There is a fourth transition isolation level, read uncommitted, that is not supported by Oracle.
These transaction isolation levels are:
Read Committed
Serializable
Read Only
The read committed transaction isolation level is the Oracle default. With this setting, each query can see only data committed before before the query (not the transaction) began.
Any transaction that executes a given query more than once can experience non-repeatable reads or phantoms.
Unlike the read committed level transactions, serializable isolation level transactions will not experience non-repeatable reads or phantoms.
All queries in an Oracle serializable transaction see the database as of a single point in time
With the Read Only transaction isolation level, only data that was committed at the start of the transaction can be accessed by a query. No modification to the data is allowed.
Subscribe to:
Posts (Atom)