use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.
the class CustomRelationshipTemplateService method create.
@Override
public void create(CustomRelationshipTemplate crt) throws BusinessException {
if (!EntityCustomizationUtils.validateOntologyCode(crt.getCode())) {
throw new IllegalArgumentException("The code of ontology elements must not contain numbers");
}
if (crt.getStartNode() == null) {
throw new IllegalArgumentException("Can't create relation " + crt.getCode() + ": start node can't be null");
}
if (crt.getEndNode() == null) {
throw new IllegalArgumentException("Can't create relation " + crt.getCode() + ": end node can't be null");
}
super.create(crt);
try {
permissionService.createIfAbsent(crt.getModifyPermission(), paramBean.getProperty("role.modifyAllCR", "ModifyAllCR"));
permissionService.createIfAbsent(crt.getReadPermission(), paramBean.getProperty("role.readAllCR", "ReadAllCR"));
if (crt.getAvailableStorages().contains(DBStorageType.SQL)) {
customTableCreatorService.createCrtTable(crt);
}
customFieldsCache.addUpdateCustomRelationshipTemplate(crt);
} catch (Exception e) {
throw new RuntimeException(e);
}
// Synchronize start and end CETs
CustomEntityTemplate startCet = crt.getStartNode();
MeveoModule cetModule = cetService.findModuleOf(startCet);
cetService.addFilesToModule(startCet, cetModule);
CustomEntityTemplate endCrt = crt.getEndNode();
MeveoModule cet2Module = cetService.findModuleOf(endCrt);
cetService.addFilesToModule(endCrt, cet2Module);
}
use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.
the class CustomRelationshipTemplateService method addFilesToModule.
@Override
public void addFilesToModule(CustomRelationshipTemplate entity, MeveoModule module) throws BusinessException {
super.addFilesToModule(entity, module);
File gitDirectory = GitHelper.getRepositoryDir(currentUser, module.getGitRepository().getCode());
String pathJavaFile = "facets/java/org/meveo/model/customEntities/" + entity.getCode() + ".java";
String pathJsonSchemaFile = "facets/json/" + entity.getCode() + "-schema" + ".json";
File newJavaFile = new File(gitDirectory, pathJavaFile);
File newJsonSchemaFile = new File(gitDirectory, pathJsonSchemaFile);
try {
MeveoFileUtils.writeAndPreserveCharset(this.jSONSchemaGenerator.generateSchema(pathJsonSchemaFile, entity), newJsonSchemaFile);
} catch (IOException e) {
throw new BusinessException("File cannot be write", e);
}
gitClient.commitFiles(module.getGitRepository(), Collections.singletonList(newJsonSchemaFile), "Add the crt json schema : " + entity.getCode() + ".json" + " in the module : " + module.getCode());
String schemaLocation = this.cetCompiler.getTemplateSchema(entity);
final CompilationUnit compilationUnit = this.jSONSchemaIntoJavaClassParser.parseJsonContentIntoJavaFile(schemaLocation, entity);
try {
MeveoFileUtils.writeAndPreserveCharset(compilationUnit.toString(), newJavaFile);
} catch (IOException e) {
throw new BusinessException("File cannot be write", e);
}
gitClient.commitFiles(module.getGitRepository(), Collections.singletonList(newJavaFile), "Add the crt java source file : " + entity.getCode() + ".java" + "in the module : " + module.getCode());
}
use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.
the class CustomTableService method importData.
/**
* Import data into custom table
*
* @param cet Custom table definition
* @param inputStream Data stream
* @param append True if data should be appended to the existing data. If false, data will be added in batch. One by one otherwise.
* @return Number of records imported
* @throws BusinessException General business exception
*/
public int importData(String sqlConnectionCode, CustomEntityTemplate cet, InputStream inputStream, boolean append) throws BusinessException {
final String dbTableName = SQLStorageConfiguration.getDbTablename(cet);
// Custom table fields. Fields will be sorted by their GUI 'field' position.
Map<String, CustomFieldTemplate> cfts = customFieldTemplateService.findByAppliesTo(cet.getAppliesTo());
if (cfts == null || cfts.isEmpty()) {
throw new ValidationException("No fields are defined for custom table " + dbTableName, "customTable.noFields");
}
List<CustomFieldTemplate> fields = new ArrayList<>(cfts.values());
fields.sort((cft1, cft2) -> {
int pos1 = cft1.getGUIFieldPosition();
int pos2 = cft2.getGUIFieldPosition();
return pos1 - pos2;
});
int importedLines = 0;
int importedLinesTotal = 0;
List<Map<String, Object>> values = new ArrayList<>();
ObjectReader oReader = getCSVReader(fields);
try (Reader reader = new InputStreamReader(inputStream)) {
// Cache used to avoid fetching multiple time the same data
Map<String, Map<String, String>> entityReferencesCache = new HashMap<>();
MappingIterator<Map<String, Object>> mappingIterator = oReader.readValues(reader);
while (mappingIterator.hasNext()) {
Map<String, Object> lineValues = mappingIterator.next();
lineValues.remove("uuid");
if (append) {
lineValues = convertValue(lineValues, cfts, true, null);
replaceEntityreferences(sqlConnectionCode, fields, entityReferencesCache, lineValues);
String uuid = findIdByUniqueValues(sqlConnectionCode, cet, lineValues, fields);
if (uuid == null) {
final String tablename = SQLStorageConfiguration.getDbTablename(cet);
super.createInNewTx(sqlConnectionCode, tablename, lineValues);
importedLines++;
importedLinesTotal++;
}
} else {
// Save to DB every 500 records
if (importedLines >= 500) {
saveBatch(sqlConnectionCode, cfts, fields, cet.getCode(), values, entityReferencesCache);
values.clear();
importedLines = 0;
}
importedLines++;
importedLinesTotal++;
values.add(lineValues);
}
if (importedLinesTotal % 30000 == 0) {
log.trace("Imported {} lines to {} table", importedLinesTotal, dbTableName);
}
}
if (!append) {
// Save remaining records
saveBatch(sqlConnectionCode, cfts, fields, cet.getCode(), values, entityReferencesCache);
}
// Re-populate ES index
elasticClient.populateAll(currentUser, CustomTableRecord.class, cet.getCode());
log.info("Imported {} lines to {} table", importedLinesTotal, dbTableName);
} catch (RuntimeJsonMappingException e) {
throw new ValidationException("Invalid file format", "message.upload.fail.invalidFormat", e);
} catch (IOException e) {
throw new BusinessException(e);
}
return importedLinesTotal;
}
use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.
the class CustomTableService method importData.
/**
* Import data into custom table
*
* @param customModelObject Custom table definition
* @param values A list of records to import. Each record is a map of values with field name as a map key and field value as a value.
* @param append True if data should be appended to the existing data
* @return Number of records imported
* @throws BusinessException General business exception
*/
public int importData(String sqlConnectionCode, String tableName, CustomModelObject customModelObject, List<Map<String, Object>> values, boolean append) throws BusinessException {
// Custom table fields. Fields will be sorted by their GUI 'field' position.
Map<String, CustomFieldTemplate> cfts = customFieldTemplateService.findByAppliesTo(customModelObject.getAppliesTo());
List<CustomFieldTemplate> fields = new ArrayList<>(cfts.values());
fields.sort((cft1, cft2) -> {
int pos1 = cft1.getGUIFieldPosition();
int pos2 = cft2.getGUIFieldPosition();
return pos1 - pos2;
});
int importedLines = 0;
int importedLinesTotal = 0;
List<Map<String, Object>> valuesPartial = new ArrayList<>();
// Delete current data first if in override mode
if (!append) {
remove(sqlConnectionCode, (CustomEntityTemplate) customModelObject);
}
// By default will update ES immediately. If more than 100 records are being updated, ES will be updated in batch way - reconstructed from a table
boolean updateESImediately = append;
if (values.size() > 100) {
updateESImediately = false;
}
try {
CustomEntityTemplate cet = customEntityTemplateService.findByCodeOrDbTablename(tableName);
for (Map<String, Object> value : values) {
// Save to DB every 1000 records
if (importedLines >= 1000) {
valuesPartial = convertValues(valuesPartial, cfts, false);
customTableService.get().createInNewTx(sqlConnectionCode, cet, updateESImediately, valuesPartial);
valuesPartial.clear();
importedLines = 0;
}
valuesPartial.add(value);
importedLines++;
importedLinesTotal++;
}
// Save to DB remaining records
valuesPartial = convertValues(valuesPartial, cfts, false);
customTableService.get().createInNewTx(sqlConnectionCode, cet, updateESImediately, valuesPartial);
// Repopulate ES index
if (!updateESImediately) {
elasticClient.populateAll(currentUser, CustomTableRecord.class, customModelObject.getCode());
}
} catch (Exception e) {
throw new BusinessException(e);
}
return importedLinesTotal;
}
use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.
the class CustomTableService method replaceEntityreferences.
private void replaceEntityreferences(String sqlConnectionCode, List<CustomFieldTemplate> fields, Map<String, Map<String, String>> entityReferencesCache, Map<String, Object> entityRefValueMap) throws BusinessException {
final HashMap<String, Object> iterationMap = new HashMap<>(entityRefValueMap);
for (Entry<String, Object> entry : iterationMap.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
final Optional<CustomFieldTemplate> templateOptional = fields.stream().filter(f -> f.getDbFieldname().equals(key)).findFirst();
if (templateOptional.isPresent() && templateOptional.get().getFieldType() == CustomFieldTypeEnum.ENTITY) {
CustomEntityTemplate entityRef = customEntityTemplateService.findByCode(templateOptional.get().getEntityClazzCetCode());
// Try to retrieve record first
String uuid = entityReferencesCache.computeIfAbsent(key, k -> new HashMap<>()).computeIfAbsent((String) value, serializedValues -> {
Map<String, Object> entityRefValues = JacksonUtil.fromString(serializedValues, GenericTypeReferences.MAP_STRING_OBJECT);
return findIdByUniqueValues(sqlConnectionCode, entityRef, entityRefValues, fields);
});
// If record is not found, create it
if (uuid == null) {
Map<String, Object> entityRefValues = JacksonUtil.fromString((String) value, GenericTypeReferences.MAP_STRING_OBJECT);
log.info("Creating missing entity reference {}", entityRefValues);
CustomEntityTemplate cet = customFieldsCacheContainerProvider.getCustomEntityTemplate(templateOptional.get().getEntityClazzCetCode());
CustomEntityInstance cei = new CustomEntityInstance();
cei.setCetCode(cet.getCode());
customFieldInstanceService.setCfValues(cei, cei.getCetCode(), entityRefValues);
uuid = create(sqlConnectionCode, cet, cei);
}
entityRefValueMap.put(key, uuid);
}
}
}
Aggregations