Search in sources :

Example 6 with BusinessException

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);
}
Also used : CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) MeveoModule(org.meveo.model.module.MeveoModule) NoResultException(javax.persistence.NoResultException) IOException(java.io.IOException) BusinessException(org.meveo.admin.exception.BusinessException)

Example 7 with BusinessException

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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) BusinessException(org.meveo.admin.exception.BusinessException) IOException(java.io.IOException) File(java.io.File)

Example 8 with BusinessException

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;
}
Also used : ValidationException(org.meveo.admin.exception.ValidationException) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) BusinessException(org.meveo.admin.exception.BusinessException) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) CustomModelObject(org.meveo.model.customEntities.CustomModelObject) Map(java.util.Map) HashMap(java.util.HashMap) RuntimeJsonMappingException(com.fasterxml.jackson.databind.RuntimeJsonMappingException)

Example 9 with BusinessException

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;
}
Also used : ArrayList(java.util.ArrayList) EntityDoesNotExistsException(org.meveo.api.exception.EntityDoesNotExistsException) ValidationException(org.meveo.admin.exception.ValidationException) BusinessException(org.meveo.admin.exception.BusinessException) RuntimeJsonMappingException(com.fasterxml.jackson.databind.RuntimeJsonMappingException) IOException(java.io.IOException) BusinessException(org.meveo.admin.exception.BusinessException) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) CustomModelObject(org.meveo.model.customEntities.CustomModelObject) Map(java.util.Map) HashMap(java.util.HashMap)

Example 10 with BusinessException

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);
        }
    }
}
Also used : Date(java.util.Date) MappingIterator(com.fasterxml.jackson.databind.MappingIterator) SQLQuery(org.hibernate.SQLQuery) EntityDoesNotExistsException(org.meveo.api.exception.EntityDoesNotExistsException) GenericTypeReferences(org.meveo.model.typereferences.GenericTypeReferences) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) Future(java.util.concurrent.Future) TransactionAttributeType(javax.ejb.TransactionAttributeType) SQLStorageConfiguration(org.meveo.model.persistence.sql.SQLStorageConfiguration) Asynchronous(javax.ejb.Asynchronous) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) BigInteger(java.math.BigInteger) AsyncResult(javax.ejb.AsyncResult) Instance(javax.enterprise.inject.Instance) TxType(javax.transaction.Transactional.TxType) ValidationException(org.meveo.admin.exception.ValidationException) Transactional(javax.transaction.Transactional) DocumentField(org.elasticsearch.common.document.DocumentField) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance) Collection(java.util.Collection) CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) Set(java.util.Set) Reader(java.io.Reader) Collectors(java.util.stream.Collectors) BusinessException(org.meveo.admin.exception.BusinessException) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) Objects(java.util.Objects) ElasticClient(org.meveo.service.index.ElasticClient) List(java.util.List) CustomFieldStorageTypeEnum(org.meveo.model.crm.custom.CustomFieldStorageTypeEnum) Entry(java.util.Map.Entry) SortOrder(org.elasticsearch.search.sort.SortOrder) CustomFieldsCacheContainerProvider(org.meveo.cache.CustomFieldsCacheContainerProvider) Optional(java.util.Optional) PaginationConfiguration(org.meveo.admin.util.pagination.PaginationConfiguration) SequenceWriter(com.fasterxml.jackson.databind.SequenceWriter) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) JacksonUtil(org.meveo.model.persistence.JacksonUtil) CustomFieldTemplateService(org.meveo.service.crm.impl.CustomFieldTemplateService) TransactionAttribute(javax.ejb.TransactionAttribute) ParamBean(org.meveo.commons.utils.ParamBean) CustomModelObject(org.meveo.model.customEntities.CustomModelObject) RuntimeJsonMappingException(com.fasterxml.jackson.databind.RuntimeJsonMappingException) MapUtils(org.apache.commons.collections4.MapUtils) QueryBuilder(org.meveo.commons.utils.QueryBuilder) NativePersistenceService(org.meveo.service.base.NativePersistenceService) CustomTableRecord(org.meveo.model.customEntities.CustomTableRecord) DBStorageType(org.meveo.model.persistence.DBStorageType) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) FileWriter(java.io.FileWriter) ColumnType(com.fasterxml.jackson.dataformat.csv.CsvSchema.ColumnType) IOException(java.io.IOException) CustomFieldInstanceService(org.meveo.service.crm.impl.CustomFieldInstanceService) FileInputStream(java.io.FileInputStream) ParamBeanFactory(org.meveo.commons.utils.ParamBeanFactory) InputStreamReader(java.io.InputStreamReader) File(java.io.File) CustomFieldTypeEnum(org.meveo.model.crm.custom.CustomFieldTypeEnum) Feature(com.fasterxml.jackson.core.JsonGenerator.Feature) DateUtils(org.meveo.model.shared.DateUtils) ElasticSearchClassInfo(org.meveo.service.index.ElasticSearchClassInfo) Collections(java.util.Collections) InputStream(java.io.InputStream) HashMap(java.util.HashMap) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) CustomModelObject(org.meveo.model.customEntities.CustomModelObject) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance)

Aggregations

BusinessException (org.meveo.admin.exception.BusinessException)229 IOException (java.io.IOException)97 File (java.io.File)59 HashMap (java.util.HashMap)50 EntityDoesNotExistsException (org.meveo.api.exception.EntityDoesNotExistsException)50 ArrayList (java.util.ArrayList)48 MeveoApiException (org.meveo.api.exception.MeveoApiException)39 ELException (org.meveo.elresolver.ELException)39 CustomFieldTemplate (org.meveo.model.crm.CustomFieldTemplate)38 CustomEntityTemplate (org.meveo.model.customEntities.CustomEntityTemplate)37 Map (java.util.Map)34 BundleKey (org.jboss.seam.international.status.builder.BundleKey)30 TransactionAttribute (javax.ejb.TransactionAttribute)28 CustomEntityInstance (org.meveo.model.customEntities.CustomEntityInstance)27 List (java.util.List)25 MeveoModule (org.meveo.model.module.MeveoModule)25 NoResultException (javax.persistence.NoResultException)24 HashSet (java.util.HashSet)22 Response (javax.ws.rs.core.Response)22 Collection (java.util.Collection)20