Difference between revisions of "API v1.0"
old>Admin |
old>Tvi |
||
Line 13: | Line 13: | ||
First of create a query object | First of create a query object | ||
<syntaxhighlight lang="java"> | |||
SolutionQuery myQuery = session.getSolutionQuery( "mynewsolution" ); | |||
</syntaxhighlight> | |||
A query is built much like an SQL query by appending different fields to be SELECT'ed | A query is built much like an SQL query by appending different fields to be SELECT'ed | ||
<syntaxhighlight lang="java"> | |||
myQuery.addSelectField("MYNUMBER"); | |||
</syntaxhighlight> | |||
Additionally WHERE components are added | Additionally WHERE components are added | ||
<syntaxhighlight lang="java"> | |||
myQuery.addWhereCriterion("StatusID", "In progress"); | |||
myQuery.addWhereCriterion("INCOME", QueryPart.MORE, "22"); | |||
myQuery.addWhereInList("PARENTITEMS", arrayListOfDataID); | |||
</syntaxhighlight> | |||
Likewise operators can be added | Likewise operators can be added | ||
<syntaxhighlight lang="java"> | |||
... | |||
myQuery.addWhereOR(); | |||
... | |||
... | |||
myQuery.addWhereAND(); | |||
... | |||
</syntaxhighlight> | |||
Note that lookup values etc. will be translated silently. | Note that lookup values etc. will be translated silently. | ||
<syntaxhighlight lang="java"> | |||
myQuery.setSortOrder("INCOME", true); | |||
</syntaxhighlight> | |||
Order records by income in descending order (true). | Order records by income in descending order (true). | ||
Line 45: | Line 53: | ||
Finally execute the query and return the results | Finally execute the query and return the results | ||
<syntaxhighlight lang="java"> | |||
SolutionQueryResultSet records = myQuery.executeQuery(); | |||
</syntaxhighlight> | |||
== Retrieve data == | == Retrieve data == |
Revision as of 14:51, 8 March 2021
Sessions and security
For interacting with Tempus Serva you need a session
Session session = SessionFactory.getSession(this);
All sessions need to be terminated after use (releasing DB connections etc.)
session.close();
Constructing queryies
First of create a query object
SolutionQuery myQuery = session.getSolutionQuery( "mynewsolution" );
A query is built much like an SQL query by appending different fields to be SELECT'ed
myQuery.addSelectField("MYNUMBER");
Additionally WHERE components are added
myQuery.addWhereCriterion("StatusID", "In progress");
myQuery.addWhereCriterion("INCOME", QueryPart.MORE, "22");
myQuery.addWhereInList("PARENTITEMS", arrayListOfDataID);
Likewise operators can be added
...
myQuery.addWhereOR();
...
...
myQuery.addWhereAND();
...
Note that lookup values etc. will be translated silently.
myQuery.setSortOrder("INCOME", true);
Order records by income in descending order (true).
Finally execute the query and return the results
SolutionQueryResultSet records = myQuery.executeQuery();
Retrieve data
Data are extracted from the query resultset using a relative reference to the record number
for( int i=0; i<3; i++ ) { String text = "Value nr " + i " = " + resultSet.getRecordValue( i, "MYVALUE" ); System.out.println( text ); }
Retriever methods exist for deifferent datatypes
public int getReference( int recordNr )
public Hashtable <String,String> getRecordValueList( int recordNr )
public String getRecordValue( int recordNr, String fieldName )
public int getRecordValueInteger( int recordNr, String fieldName )
private SolutionRecord getRecord(int recordNr, boolean isWrite)
Change / create data
AWAITING REVIEW
Code example
The following example shows how data is retrieved from af collection of templates and used to create a collection of records (with values copied from the template).
int parentTemplateDataID = Parser.getInteger( c.fields.getElementByFieldName(thisKeyToParentTemplate).FieldValue ); Session session = SessionFactory.getSession(this); try { //Get data SolutionQuery opsaetning = session.getSolutionQuery(templateSolutionName); for(int i=0; i<fieldsTemplate.size(); i++ ) opsaetning.addSelectField(fieldsTemplate.get(i)); opsaetning.addWhereCriterion(templateKeyToParentTemplate, parentTemplateDataID); SolutionQueryResultSet recordsToCopy = opsaetning.executeQuery(); int recordCount = recordsToCopy.size(); for( int i=0; i<recordCount; i++ ) { SolutionRecordNew instance = session.getSolutionRecordNew(instanceSolutionName); for(int fi=0; fi<fieldsTemplate.size(); fi++ ) { String value = recordsToCopy.getRecordValue( i, fieldsTemplate.get(fi)); instance.setValue( fieldsInstance.get(fi), value ); } instance.setValueInteger( instanceKeyToParent, c.DataID ); if( ! Parser.isEmpty(instanceKeyToTemplate) ) instance.setValueInteger( instanceKeyToTemplate, recordsToCopy.getReference(i) ); instance.persistChanges(); } } catch(Exception e) { e.printStackTrace(); } finally { session.close(); }