* Recursive function to generate a list of EDBObjects out of a model object.
*/
private String convertSubModel(OpenEngSBModel model, List<EDBObject> objects, ConnectorInformation info) {
String contextId = ContextHolder.get().getCurrentContextId();
String oid = ModelWrapper.wrap(model).getCompleteModelOID();
EDBObject object = new EDBObject(oid);
try {
fillEDBObjectWithEngineeringObjectInformation(object, model);
} catch (IllegalAccessException e) {
LOGGER.warn("Unable to fill completely the EngineeringObjectInformation into the EDBObject", e);
throw new EKBException("Unable to fill completely the EngineeringObjectInformation into the EDBObject", e);
}
for (OpenEngSBModelEntry entry : model.toOpenEngSBModelEntries()) {
if (entry.getValue() == null) {
continue;
} else if (entry.getType().equals(FileWrapper.class)) {
try {
FileWrapper wrapper = (FileWrapper) entry.getValue();
String content = Base64.encodeBase64String(wrapper.getContent());
object.putEDBObjectEntry(entry.getKey(), content, String.class);
object.putEDBObjectEntry(entry.getKey() + FILEWRAPPER_FILENAME_SUFFIX,
wrapper.getFilename(), String.class);
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
} else if (OpenEngSBModel.class.isAssignableFrom(entry.getType())) {
OpenEngSBModel temp = (OpenEngSBModel) entry.getValue();
String subOid = convertSubModel(temp, objects, info);
object.putEDBObjectEntry(entry.getKey(), subOid, String.class);
} else if (List.class.isAssignableFrom(entry.getType())) {
List<?> list = (List<?>) entry.getValue();
if (list == null || list.size() == 0) {
continue;
}
Boolean modelItems = null;
for (int i = 0; i < list.size(); i++) {
Object item = list.get(i);
if (modelItems == null) {
modelItems = OpenEngSBModel.class.isAssignableFrom(item.getClass());
}
if (modelItems) {
item = convertSubModel((OpenEngSBModel) item, objects, info);
}
String entryName = getEntryNameForList(entry.getKey(), i);
object.putEDBObjectEntry(entryName, item, item.getClass());
}
} else if (entry.getType().isArray()) {
Object[] array = (Object[]) entry.getValue();
if (array == null || array.length == 0) {
continue;
}
Boolean modelItems = null;
for (int i = 0; i < array.length; i++) {
Object item = array[i];
if (modelItems == null) {
modelItems = OpenEngSBModel.class.isAssignableFrom(item.getClass());
}
if (modelItems) {
item = convertSubModel((OpenEngSBModel) item, objects, info);
}
String entryName = getEntryNameForList(entry.getKey(), i);
object.putEDBObjectEntry(entryName, item, item.getClass());
}
} else if (Map.class.isAssignableFrom(entry.getType())) {
Map<?, ?> map = (Map<?, ?>) entry.getValue();
if (map == null || map.size() == 0) {
continue;
}
Boolean keyIsModel = null;
Boolean valueIsModel = null;
int i = 0;
for (Map.Entry<?, ?> ent : map.entrySet()) {
if (keyIsModel == null) {
keyIsModel = OpenEngSBModel.class.isAssignableFrom(ent.getKey().getClass());
}
if (valueIsModel == null) {
valueIsModel = OpenEngSBModel.class.isAssignableFrom(ent.getValue().getClass());
}
Object key = ent.getKey();
Object value = ent.getValue();
if (keyIsModel) {
key = convertSubModel((OpenEngSBModel) key, objects, info);
}
if (valueIsModel) {
value = convertSubModel((OpenEngSBModel) value, objects, info);
}
object.putEDBObjectEntry(getEntryNameForMapKey(entry.getKey(), i), key);
object.putEDBObjectEntry(getEntryNameForMapValue(entry.getKey(), i), value);
i++;
}
} else {
object.putEDBObjectEntry(entry.getKey(), entry.getValue(), entry.getType());
}
}
object.putEDBObjectEntry(EDBConstants.MODEL_TYPE, model.retrieveModelName());
object.putEDBObjectEntry(EDBConstants.MODEL_TYPE_VERSION, model.retrieveModelVersion());
object.putEDBObjectEntry("domainId", info.getDomainId());
object.putEDBObjectEntry("connectorId", info.getConnectorId());
object.putEDBObjectEntry("instanceId", info.getInstanceId());
object.putEDBObjectEntry("contextId", contextId);
objects.add(object);
return oid;
}