use of org.meveo.model.crm.CustomFieldTemplate in project meveo by meveo-org.
the class WorkflowJob method getCustomFields.
@Override
public Map<String, CustomFieldTemplate> getCustomFields() {
Map<String, CustomFieldTemplate> result = new HashMap<String, CustomFieldTemplate>();
CustomFieldTemplate filterCF = new CustomFieldTemplate();
filterCF.setCode("wfJob_filter");
filterCF.setAppliesTo("JOB_WorkflowJob");
filterCF.setActive(true);
filterCF.setDescription("Filter");
filterCF.setFieldType(CustomFieldTypeEnum.ENTITY);
filterCF.setEntityClazz(Filter.class.getName());
filterCF.setValueRequired(true);
result.put("wfJob_filter", filterCF);
CustomFieldTemplate worklowCF = new CustomFieldTemplate();
worklowCF.setCode("wfJob_workflow");
worklowCF.setAppliesTo("JOB_WorkflowJob");
worklowCF.setActive(true);
worklowCF.setDescription("Workflow");
worklowCF.setFieldType(CustomFieldTypeEnum.ENTITY);
worklowCF.setEntityClazz(Workflow.class.getName());
worklowCF.setValueRequired(true);
result.put("wfJob_workflow", worklowCF);
CustomFieldTemplate nbRuns = new CustomFieldTemplate();
nbRuns.setCode("wfJob_nbRuns");
nbRuns.setAppliesTo("JOB_WorkflowJob");
nbRuns.setActive(true);
nbRuns.setDescription(resourceMessages.getString("jobExecution.nbRuns"));
nbRuns.setFieldType(CustomFieldTypeEnum.LONG);
nbRuns.setValueRequired(false);
nbRuns.setDefaultValue("1");
result.put("wfJob_nbRuns", nbRuns);
CustomFieldTemplate waitingMillis = new CustomFieldTemplate();
waitingMillis.setCode("wfJob_waitingMillis");
waitingMillis.setAppliesTo("JOB_WorkflowJob");
waitingMillis.setActive(true);
waitingMillis.setDescription(resourceMessages.getString("jobExecution.waitingMillis"));
waitingMillis.setFieldType(CustomFieldTypeEnum.LONG);
waitingMillis.setValueRequired(false);
waitingMillis.setDefaultValue("0");
result.put("wfJob_waitingMillis", waitingMillis);
return result;
}
use of org.meveo.model.crm.CustomFieldTemplate in project meveo by meveo-org.
the class CETUtils method initCustomFieldValues.
/**
* Init a {@link CustomFieldValues} container from values map and fields definitions
*
* @param valuesMap Values map
* @param fields Fields definition
* @return instance of {@link CustomFieldValues}
*/
public static CustomFieldValues initCustomFieldValues(Map<String, Object> valuesMap, Collection<CustomFieldTemplate> fields) {
CustomFieldValues values = new CustomFieldValues();
valuesMap.forEach((k, v) -> {
values.setValue(k, v);
});
fields.stream().filter(f -> CustomFieldStorageTypeEnum.LIST.equals(f.getStorageType())).filter(f -> valuesMap.get(f.getDbFieldname()) == null).forEach(f -> values.setValue(f.getDbFieldname(), f.getNewListValue(), f.getFieldType().getDataClass()));
return values;
}
use of org.meveo.model.crm.CustomFieldTemplate in project meveo by meveo-org.
the class SwaggerHelperService method convertCftsToProperties.
public Map<String, Property> convertCftsToProperties(Map<String, CustomFieldTemplate> cfts) {
Map<String, Property> result = new HashMap<>();
if (!cfts.isEmpty()) {
for (Entry<String, CustomFieldTemplate> entry : cfts.entrySet()) {
Property property = convertToProperty(entry.getValue());
result.put(entry.getKey(), property);
}
}
return result;
}
use of org.meveo.model.crm.CustomFieldTemplate in project meveo by meveo-org.
the class CustomTableCreatorService method addField.
@AccessTimeout(value = 1L, unit = TimeUnit.MINUTES)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void addField(String sqlConnectionCode, String dbTableName, CustomFieldTemplate cft, boolean checkStorage) {
// Don't add field if not stored in sql
if (checkStorage && !cft.getStoragesNullSafe().contains(DBStorageType.SQL)) {
return;
}
String dbFieldname = cft.getDbFieldname();
if ((cft.getFieldType() == CustomFieldTypeEnum.STRING || cft.getFieldType() == CustomFieldTypeEnum.SECRET) && (cft.getMaxValue() == null || cft.getMaxValue() < 1)) {
cft.setMaxValue(CustomFieldTemplate.DEFAULT_MAX_LENGTH_STRING);
}
DatabaseChangeLog dbLog = new DatabaseChangeLog("path");
ChangeSet changeSet = new ChangeSet(dbTableName + "_CT_" + dbFieldname + "_AF_" + System.currentTimeMillis(), "Meveo", false, false, "meveo", "", "", dbLog);
String columnType;
try {
columnType = getColumnType(cft);
} catch (ClassNotFoundException e1) {
throw new IllegalArgumentException("Cannot get field type for entity with class or code " + cft.getEntityClazzCetCode(), e1);
}
AddColumnConfig column = new AddColumnConfig();
// Check if column type is handled
if (columnType != null) {
AddColumnChange addColumnChange = new AddColumnChange();
addColumnChange.setTableName(dbTableName);
column.setName(dbFieldname);
setDefaultValue(cft, column);
column.setType(columnType);
ConstraintsConfig constraints = new ConstraintsConfig();
column.setConstraints(constraints);
if (cft.isValueRequired()) {
constraints.setNullable(false);
}
if (cft.isUnique()) {
constraints.setUnique(true);
constraints.setUniqueConstraintName(getUniqueConstraintName(dbTableName, dbFieldname));
}
addColumnChange.setColumns(Collections.singletonList(column));
changeSet.addChange(addColumnChange);
}
// entity reference
if (cft.getFieldType() == CustomFieldTypeEnum.ENTITY && cft.getStorageType().equals(CustomFieldStorageTypeEnum.SINGLE)) {
addForeignKey(sqlConnectionCode, changeSet, cft, dbTableName, dbFieldname);
}
if (!changeSet.getChanges().isEmpty()) {
dbLog.addChangeSet(changeSet);
try (Session hibernateSession = sqlConnectionProvider.getSession(sqlConnectionCode)) {
try {
CompletableFuture.runAsync(() -> {
hibernateSession.doWork(connection -> {
Database database;
try {
database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
setSchemaName(database);
var meta = connection.getMetaData();
// Check if field already exist in table
try (ResultSet res = meta.getColumns(null, database.getDefaultSchemaName(), dbTableName, dbFieldname)) {
if (res.next()) {
checkTypeMatches(dbTableName, cft, column, res.getString("TYPE_NAME"), res.getInt("COLUMN_SIZE"));
} else {
// Create the field
Liquibase liquibase = new Liquibase(dbLog, new ClassLoaderResourceAccessor(), database);
liquibase.update(new Contexts(), new LabelExpression());
liquibase.forceReleaseLocks();
}
}
} catch (Exception e) {
log.error("Failed to add field {} to custom table {}", dbFieldname, dbTableName, e);
throw new SQLException(String.format("Failed to add field %s to custom table %s : ", dbFieldname, dbTableName) + e.getMessage(), e);
}
});
}).get(1, TimeUnit.MINUTES);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
log.error("Failed to add field {} to custom table {} within 1 minute", dbFieldname, dbTableName, e);
throw new RuntimeException(e);
}
}
}
}
use of org.meveo.model.crm.CustomFieldTemplate in project meveo by meveo-org.
the class CustomEntityTemplateService method remove.
@Override
public void remove(CustomEntityTemplate cet) throws BusinessException {
Map<String, CustomFieldTemplate> fields = customFieldTemplateService.findByAppliesToNoCache(cet.getAppliesTo());
Map<String, EntityCustomAction> customActionMap = entityCustomActionService.findByAppliesTo(cet.getAppliesTo());
for (CustomFieldTemplate cft : fields.values()) {
// Don't remove super-template cfts
if (cft.getAppliesTo().equals(cet.getAppliesTo())) {
customFieldTemplateService.remove(cft);
}
}
for (EntityCustomAction entityCustomAction : customActionMap.values()) {
entityCustomActionService.remove(entityCustomAction.getId());
}
customFieldsCache.removeCustomEntityTemplate(cet);
// Remove permissions
permissionService.removeIfPresent(cet.getModifyPermission());
permissionService.removeIfPresent(cet.getDecrpytPermission());
permissionService.removeIfPresent(cet.getReadPermission());
super.remove(cet);
}
Aggregations