Thursday, September 26, 2013

Asset Control Java API samples

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>

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;

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.


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>

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/