Difference between revisions of "API v1.0"

From TempusServa wiki
Jump to navigation Jump to search
old>Tvi
(Added limit)
 
(14 intermediate revisions by 3 users not shown)
Line 4: Line 4:
For interacting with Tempus Serva you need a session
For interacting with Tempus Serva you need a session


  Session session = SessionFactory.getSession(this);
<syntaxhighlight lang="java">
Session session = SessionFactory.getSession(this);
</syntaxhighlight>


All sessions need to be terminated after use (releasing DB connections etc.)
All sessions need to be terminated after use (releasing DB connections etc.)


  session.close();
<syntaxhighlight lang="java">
session.close();
</syntaxhighlight>


== Constructing queryies ==
== Constructing queryies ==
Line 51: Line 55:
Order records by income in descending order (true).
Order records by income in descending order (true).


Finally execute the query and return the results
By default the API limits solution queries to 100 rows. To increase this:<syntaxhighlight lang="java">
myQuery.setLimit(100000);
</syntaxhighlight>To do pagination use this:<syntaxhighlight lang="java">
myQuery.setLimitAndOffset(100, 100);
</syntaxhighlight>Finally execute the query and return the results


<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
Line 60: Line 68:
Data are extracted from the query resultset using a relative reference to the record number  
Data are extracted from the query resultset using a relative reference to the record number  


  for( int i=0; i<3; i++ ) {
<syntaxhighlight lang="java">
    String text = "Value nr " + i " = " + '''resultSet.getRecordValue( i, "MYVALUE" );'''
for( int i=0; i<resultSet.size(); i++ ) {
    System.out.println( text );   
  String text = "Value nr " + i " = " + '''resultSet.getRecordValue( i, "MYVALUE" );'''
  }
  System.out.println( text );   
}
</syntaxhighlight>


Retriever methods exist for deifferent datatypes
Retriever methods exist for different datatypes


  public int getReference( int recordNr )
<syntaxhighlight lang="java">
 
public int getReference( int recordNr )
  public Hashtable <String,String> getRecordValueList( int recordNr )
public Hashtable <String,String> getRecordValueList( int recordNr )
 
public String getRecordValue( int recordNr, String fieldName )
  public String getRecordValue( int recordNr, String fieldName )
public int getRecordValueInteger( int recordNr, String fieldName )
 
private SolutionRecord getRecord(int recordNr, boolean isWrite)
  public int getRecordValueInteger( int recordNr, String fieldName )
</syntaxhighlight>
 
  private SolutionRecord getRecord(int recordNr, boolean isWrite)


== Change / create data ==
== Change / create data ==
Line 85: Line 93:
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).
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 );
<syntaxhighlight lang="java">
String thisKeyToParentTemplate = "VALGTSKABELON"; //Entity
String templateSolutionName = "tasktemplate"; //Entity
ArrayList fieldsTemplate = new ArrayList(String [] {"TASK","DEADLINE","RESPONSIBLE"}); //list of fields
String templateKeyToParentTemplate = "SKABELON"; //Entity
String instanceSolutionName = "taskinstance";
String instanceKeyToParent = "LIST";
</syntaxhighlight>
 
Now to the code ...
 
<syntaxhighlight lang="java">
int parentTemplateDataID = Parser.getInteger( c.fields.getElementByFieldName(thisKeyToParentTemplate).FieldValue );
          
          
        Session session = SessionFactory.getSession(this);
Session session = SessionFactory.getSession(this);
        try {
try {
           
    //Get data
            //Get data
    SolutionQuery opsaetning = session.getSolutionQuery(templateSolutionName);
            SolutionQuery opsaetning = session.getSolutionQuery(templateSolutionName);
    for(int i=0; i<fieldsTemplate.size(); i++ )
            for(int i=0; i<fieldsTemplate.size(); i++ )
        opsaetning.addSelectField(fieldsTemplate.get(i));
                opsaetning.addSelectField(fieldsTemplate.get(i));
    opsaetning.addWhereCriterion(templateKeyToParentTemplate, parentTemplateDataID);
            opsaetning.addWhereCriterion(templateKeyToParentTemplate, parentTemplateDataID);
    SolutionQueryResultSet recordsToCopy = opsaetning.executeQuery();
            SolutionQueryResultSet recordsToCopy = opsaetning.executeQuery();
   
           
    int recordCount = recordsToCopy.size();
            int recordCount = recordsToCopy.size();
    for( int i=0; i<recordCount; i++ ) {
            for( int i=0; i<recordCount; i++ ) {
        SolutionRecordNew instance = session.getSolutionRecordNew(instanceSolutionName);
                SolutionRecordNew instance = session.getSolutionRecordNew(instanceSolutionName);
       
               
        for(int fi=0; fi<fieldsTemplate.size(); fi++ ) {
                for(int fi=0; fi<fieldsTemplate.size(); fi++ ) {
            String fieldNameToCopy = fieldsTemplate.get(fi);  
                    String value = recordsToCopy.getRecordValue( i, fieldsTemplate.get(fi));
            String value = recordsToCopy.getRecordValue( i, fieldNameToCopy );
                    instance.setValue( fieldsInstance.get(fi), value );
            instance.setValue( fieldNameToCopy, 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();
         }
         }
        instance.setValueInteger( instanceKeyToParent, c.DataID );
        if( ! Parser.isEmpty(instanceKeyToTemplate) )
        instance.setValueInteger( instanceKeyToTemplate, recordsToCopy.getReference(i) );
       
        instance.persistChanges();
    }
}
catch(Exception e) {
    e.printStackTrace();
}
catch (TsCloseObjectRequired ex) {}
finally {
    session.close();
}
</syntaxhighlight>

Latest revision as of 10:21, 22 April 2024

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).

By default the API limits solution queries to 100 rows. To increase this:

myQuery.setLimit(100000);

To do pagination use this:

myQuery.setLimitAndOffset(100, 100);

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<resultSet.size(); i++ ) {
   String text = "Value nr " + i " = " + '''resultSet.getRecordValue( i, "MYVALUE" );'''
   System.out.println( text );  
}

Retriever methods exist for different 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).

String thisKeyToParentTemplate = "VALGTSKABELON"; //Entity
String templateSolutionName = "tasktemplate"; //Entity
ArrayList fieldsTemplate = new ArrayList(String [] {"TASK","DEADLINE","RESPONSIBLE"}); //list of fields
String templateKeyToParentTemplate = "SKABELON"; //Entity
String instanceSolutionName = "taskinstance";
String instanceKeyToParent = "LIST";

Now to the code ...

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 fieldNameToCopy = fieldsTemplate.get(fi); 
            String value = recordsToCopy.getRecordValue( i, fieldNameToCopy );
            instance.setValue( fieldNameToCopy, value );
        }
        instance.setValueInteger( instanceKeyToParent, c.DataID );
        if( ! Parser.isEmpty(instanceKeyToTemplate) )
        instance.setValueInteger( instanceKeyToTemplate, recordsToCopy.getReference(i) );
        
        instance.persistChanges();
    }
}
catch(Exception e) {
    e.printStackTrace();
}
catch (TsCloseObjectRequired ex) {}
finally {
    session.close();
}