First Service - Get Activities
The first service fetches Activities
from local database and returns them as EXB - Data Table
.
Steps
1. Create the package com.smeup.kokos.service
2. Create new class X1_X01_01
package com.smeup.kokos.service;
import com.smeup.kokos.sdk.annotations.DocsService;
import com.smeup.kokos.sdk.annotations.DocsServiceMethod;
import com.smeup.kokos.sdk.caller.KokosService;
@DocsService(name = "X1_X01_01", description = "Get activities in the form of a data table")
public class X1_X01_01 extends KokosService{}
This class extends the abstract KokosService
class, which handles the execution of the method
defined in the FUN and inherits methods for creating the necessary Data Structures
Mirco Executor services are documented by specific Java annotations
3. Create GET method
This is the service method that can be called in the FUN
.
@DocsServiceMethod(component = "EXB", function = "GET", description = "Retrive activities from database and create Data Table")
@ServiceMethod("GET")
public void getActivitiesDataTable(Fun fun, ExecutionContext context) throws Exception {}
@ServiceMethod
annotation contains the name that must be used to call the service in the FUN.
4. Create SmeupDataTable
The Data Table is structured with columns (SmeupDataColumn
) and rows (SmeupDataRow
), kokos-sdk
provides builder classes to simplify the creation process.
It displays all details of the Activities
(excluding the ID) and the total hours calculated from the start
and end
times.
-
4.1 Define the variables for the column IDs
public static final String DATE_ID = "X1DATE";
public static final String DESCRIPTION_ID = "X1DESC";
public static final String START_ID = "X1STIM";
public static final String END_ID = "X1ETIM";
public static final String STYLE_CATEGORY_ID = "X1STCT";
public static final String CATEGORY_ID = "X1CTGR";
public static final String TOT_HOURS_ID = "X1HOUR"; -
4.2 Define the
Data Objects
They represent the data types of the fields in the table.private final SmeupDataObj dateObj = new SmeupDataObj("D8", "", "");
private final SmeupDataObj timeObj = new SmeupDataObj("I1", "2", "");
private final SmeupDataObj numberObj = new SmeupDataObj("NR", "", "");
private final SmeupDataObj stringObj = new SmeupDataObj("", "", ""); -
4.3 Define the activities controller to interact with database
private Controller<Activity> activitiesController = null;
-
4.4 Create the method to retrive activities from database
IfactivitiesController
is null, create new instance to interact with MongoDB (or inject your DAO to interact with another database)private List<Activity> getActivities() {
if (activitiesController == null) {
activitiesController = new Controller<Activity>(new ActivitiesMongoDAO());
}
return new ArrayList<Activity>(activitiesController.getAll().values());
} -
4.5 Create the method to define Data Columns
UseSmeupDataColumnBuilder
to create newSmeupDataColumn
private void writeColumns() throws Exception {
// 1
this.writeDataColumn(SmeupDataColumnBuilder.builder()
.name(DATE_ID)
.title("Date")
.build());
// 2
this.writeDataColumn(SmeupDataColumnBuilder.builder()
.name(DESCRIPTION_ID)
.title("Description")
.build());
// 3
this.writeDataColumn(SmeupDataColumnBuilder.builder()
.name(START_ID)
.title("Start")
.build());
// 4
this.writeDataColumn(SmeupDataColumnBuilder.builder()
.name(END_ID)
.title("End")
.build());
// 5
this.writeDataColumn(SmeupDataColumnBuilder.builder()
.name(STYLE_CATEGORY_ID)
.title("Style category")
.build());
// 6
this.writeDataColumn(SmeupDataColumnBuilder.builder()
.name(CATEGORY_ID)
.title("Category")
.build());
// 7
this.writeDataColumn(SmeupDataColumnBuilder.builder()
.name(TOT_HOURS_ID)
.title("Total hours")
.build());
} -
4.6 Create the method to define Data Rows
For each activities create a Data Row. UseSmeupDataRowBuilder
to create newSmeupDataRow
.
The field ofHours
is calculated using the utility classTimeUtil
private void writeRows() throws Exception {
List<Activity> activities = getActivities();
for (Activity activity : activities) {
this.writeDataRow(SmeupDataRowBuilder.builder()
// Field 1
.cell(DATE_ID, SmeupDataCellBuilder.builder()
.obj(dateObj)
.value(activity.getDate().toString())
.build())
// Field 2
.cell(DESCRIPTION_ID, SmeupDataCellBuilder.builder()
.obj(stringObj)
.value(activity.getDescription())
.build())
// Field 3
.cell(START_ID, SmeupDataCellBuilder.builder()
.obj(timeObj)
.value(activity.getStart().toString())
.build())
// Field 4
.cell(END_ID, SmeupDataCellBuilder.builder()
.obj(timeObj)
.value(activity.getEnd().toString())
.build())
// Field 5
.cell(STYLE_CATEGORY_ID, SmeupDataCellBuilder.builder()
.obj(stringObj)
.value(activity.getStyleCategory())
.build())
// Field 6
.cell(CATEGORY_ID, SmeupDataCellBuilder.builder()
.obj(stringObj)
.value(activity.getCategory())
.build())
// Field 7
.cell(TOT_HOURS_ID, SmeupDataCellBuilder.builder()
.obj(numberObj)
.value(String.valueOf(TimeUtil.getTotalHours(activity.getStart(), activity.getEnd())))
.build())
.build());
}
} -
4.7 Call
writeColumns
andwriteRows
methods
Inside the createdGET
service method, callwriteColumns
andwriteRows
to create theSmeupDataTable
with the fetchedActivities
@DocsServiceMethod(component = "EXB", function = "GET", description = "Retrive activities from database and create Data Table")
@ServiceMethod("GET")
public void getActivitiesDataTable(Fun fun, ExecutionContext context) throws Exception {
// Columns
writeColumns();
// Rows
writeRows();
}
Final result
X1_X01_01
package com.smeup.kokos.service;
import java.util.ArrayList;
import java.util.List;
import com.smeup.kokos.controller.Controller;
import com.smeup.kokos.entity.activity.Activity;
import com.smeup.kokos.repository.mongo.ActivitiesMongoDAO;
import com.smeup.kokos.sdk.annotations.DocsService;
import com.smeup.kokos.sdk.annotations.DocsServiceMethod;
import com.smeup.kokos.sdk.caller.KokosService;
import com.smeup.kokos.sdk.fun.Fun;
import com.smeup.kokos.sdk.model.ExecutionContext;
import com.smeup.kokos.sdk.model.ServiceMethod;
import com.smeup.kokos.sdk.model.data.SmeupDataObj;
import com.smeup.kokos.sdk.util.SmeupDataCellBuilder;
import com.smeup.kokos.sdk.util.SmeupDataColumnBuilder;
import com.smeup.kokos.sdk.util.SmeupDataRowBuilder;
import com.smeup.kokos.util.TimeUtil;
@DocsService(name = "X1_X01_01", description = "Get activities in the form of a data table")
public class X1_X01_01 extends KokosService {
public static final String DATE_ID = "X1DATE";
public static final String DESCRIPTION_ID = "X1DESC";
public static final String START_ID = "X1STIM";
public static final String END_ID = "X1ETIM";
public static final String STYLE_CATEGORY_ID = "X1STCT";
public static final String CATEGORY_ID = "X1CTGR";
public static final String TOT_HOURS_ID = "X1HOUR";
private Controller<Activity> activitiesController = null;
private final SmeupDataObj dateObj = new SmeupDataObj("D8", "", "");
private final SmeupDataObj timeObj = new SmeupDataObj("I1", "2", "");
private final SmeupDataObj numberObj = new SmeupDataObj("NR", "", "");
private final SmeupDataObj stringObj = new SmeupDataObj("", "", "");
@DocsServiceMethod(component = "EXB", function = "GET", description = "Retrive activities from database and create Data Table")
@ServiceMethod("GET")
public void getActivitiesDataTable(Fun fun, ExecutionContext context) throws Exception {
// Columns
writeColumns();
// Rows
writeRows();
}
private void writeColumns() throws Exception {
// 1
this.writeDataColumn(SmeupDataColumnBuilder.builder()
.name(DATE_ID)
.title("Date")
.build());
// 2
this.writeDataColumn(SmeupDataColumnBuilder.builder()
.name(DESCRIPTION_ID)
.title("Description")
.build());
// 3
this.writeDataColumn(SmeupDataColumnBuilder.builder()
.name(START_ID)
.title("Start")
.build());
// 4
this.writeDataColumn(SmeupDataColumnBuilder.builder()
.name(END_ID)
.title("End")
.build());
// 5
this.writeDataColumn(SmeupDataColumnBuilder.builder()
.name(STYLE_CATEGORY_ID)
.title("Style category")
.build());
// 6
this.writeDataColumn(SmeupDataColumnBuilder.builder()
.name(CATEGORY_ID)
.title("Category")
.build());
// 7
this.writeDataColumn(SmeupDataColumnBuilder.builder()
.name(TOT_HOURS_ID)
.title("Total hours")
.build());
}
private void writeRows() throws Exception {
List<Activity> activities = getActivities();
for (Activity activity : activities) {
this.writeDataRow(SmeupDataRowBuilder.builder()
// Field 1
.cell(DATE_ID, SmeupDataCellBuilder.builder()
.obj(dateObj)
.value(activity.getDate().toString())
.build())
// Field 2
.cell(DESCRIPTION_ID, SmeupDataCellBuilder.builder()
.obj(stringObj)
.value(activity.getDescription())
.build())
// Field 3
.cell(START_ID, SmeupDataCellBuilder.builder()
.obj(timeObj)
.value(activity.getStart().toString())
.build())
// Field 4
.cell(END_ID, SmeupDataCellBuilder.builder()
.obj(timeObj)
.value(activity.getEnd().toString())
.build())
// Field 5
.cell(STYLE_CATEGORY_ID, SmeupDataCellBuilder.builder()
.obj(stringObj)
.value(activity.getStyleCategory())
.build())
// Field 6
.cell(CATEGORY_ID, SmeupDataCellBuilder.builder()
.obj(stringObj)
.value(activity.getCategory())
.build())
// Field 7
.cell(TOT_HOURS_ID, SmeupDataCellBuilder.builder()
.obj(numberObj)
.value(String.valueOf(TimeUtil.getTotalHours(activity.getStart(), activity.getEnd())))
.build())
.build());
}
}
private List<Activity> getActivities() {
if (activitiesController == null) {
activitiesController = new Controller<Activity>(new ActivitiesMongoDAO());
}
return new ArrayList<Activity>(activitiesController.getAll().values());
}
}
Test it with FUN
Check if the JSON response is the same
FUN payload:
{
"fun": {
"component": "EXB",
"service": "X1_X01_01",
"function": "GET"
},
"context": {
"user": {
"environment": "standard"
}
}
}
JSON response:
{
"type": "SmeupDataTable",
"serviceInfo": {
"fun": "F(EXB;X1_X01_01;GET)",
"serviceName": "X1_X01_01"
},
"columns": [
{
"name": "X1DATE",
"title": "Date",
"visible": true,
"isEditable": false,
"tooltip": false
},
{
"name": "X1DESC",
"title": "Description",
"visible": true,
"isEditable": false,
"tooltip": false
},
{
"name": "X1STIM",
"title": "Start",
"visible": true,
"isEditable": false,
"tooltip": false
},
{
"name": "X1ETIM",
"title": "End",
"visible": true,
"isEditable": false,
"tooltip": false
},
{
"name": "X1STCT",
"title": "Style category",
"visible": true,
"isEditable": false,
"tooltip": false
},
{
"name": "X1CTGR",
"title": "Category",
"visible": true,
"isEditable": false,
"tooltip": false
},
{
"name": "X1HOUR",
"title": "Total hours",
"visible": true,
"isEditable": false,
"tooltip": false
}
],
"rows": [],
}