I have a 2-level model form. Both child and parent level have just one NK, which is "autoincrement". The current UI framework treats these as required fields and doesn't pre-populate with values.
What is the best way to auto-populate these fields? Please note that the user need not see them until after the record is created if that makes things easier.
There's a couple of ways to handle it:
I have done it two times using a rest api and synchronous rest call from ui. Basically you need to change field type from textfield to displayfield so user do not and can not type. In model crud load listener, you will go ahead and populate the field value.
The Rest api looks like:
@GET
@Path("generateID")
public JSONObject generateSubscriptionID() {
SqlService sqlService = Services.get(SqlService.class);
SqlResult sqlResult = sqlService.executeQuery("SELECT " + ID_SEQUENCE_NAME + ".NEXTVAL FROM DUAL");
return successJson().put("id", sqlResult.getRows().get(0).getValue("NEXTVAL").toString());
}
And then UI code looks like:
load : function(modelForm, model) {
if (!model.ID) {
if (!modelForm.generatedId) {
modelForm.generatedId = One.IntegrationSubscriptionUtils.idAutoGenerator();
}
modelForm.getForm().findField("ID").setValue(modelForm.generatedId);
}
/**
* Fetches next available id from the db given the url.
* @param {Object} url
*/
idAutoGenerator : function(url) {
var xmlhttp = OneExt.getXmlHttp();
url = '/oms/rest/integrationsubscription/generateID?nocache=' + new Date().getTime();
xmlhttp.open('GET', url, false);
xmlhttp.send(null);
var ret = Ext.decode(xmlhttp.responseText);
return ret.success ? ret.id : '';
},
Thanks everyone for the multiple correct answers, they're all good. I ended up doing it pretty much like Nate's suggestion #2 as it made the most sense for my use case. Here are the details:
I started with a two-level model House with child level Room. On both House and Room, I have one NK “id” field which is required and has Autoincrement set to true.
In my Create action screen, I added only the non-id fields. In the Detail view, I included the ids as well.
Finally, I added a ModelResourceListener which will autoassign the IDs. Please note that I used the “value sequences” from my model file (you can open the .model file with a text editor)
package com.twolvlform.rest;
import org.json.JSONArray;
import org.json.JSONObject;
import com.onenetwork.platform.data.model.Model;
import com.onenetwork.platform.data.sql.SqlService;
import com.onenetwork.platform.env.servicelocator.Services;
import com.onenetwork.platform.integ.rest.model.BaseModelResourceListener;
import com.onenetwork.platform.integ.rest.model.ModelFormJSONObject;
import com.onenetwork.platform.integ.rest.model.ModelResourceListener;
@ModelResourceListener(modelLevelType = "ZTLF.House")
public class HouseModelResourceListener extends BaseModelResourceListener {
@Override
public void beforeActionExecuted(String actionName, JSONObject modelJSON) {
// TODO Auto-generated method stub
SqlService sqlService = Services.get(SqlService.class);
long houseId = sqlService.executeQuery("select ZTLF_HOUSE_HOUSEID_SEQ.nextval from dual").getRows().get(0).getLongValue(1);
modelJSON.put("HouseId", houseId);
JSONArray arr = modelJSON.getJSONArray("Rooms");
for (int i = 0; i < arr.length(); i++) {
long roomId = sqlService.executeQuery("select ZTLF_ROOM_ROOMID_SEQ.nextval from dual").getRows().get(0).getLongValue(1);
arr.getJSONObject(i).put("RoomId", roomId);
}
}
}
Once all of that was in place I got the following result: