Search in sources :

Example 46 with Model

use of com.axelor.db.Model in project axelor-open-suite by axelor.

the class PrintTemplateController method openPrint.

public void openPrint(ActionRequest request, ActionResponse response) {
    Model context = request.getContext().asType(Model.class);
    String model = request.getModel();
    LOG.debug("Print template wizard call for model : {}", model);
    Query<PrintTemplate> printTemplateQuery = Beans.get(PrintTemplateRepository.class).all().filter("self.metaModel.fullName = ?", model);
    try {
        long templatesCount = printTemplateQuery.count();
        LOG.debug("Print templates count : {} ", templatesCount);
        if (templatesCount == 0) {
            response.setError(I18n.get("Please define a print template for the model :" + model));
        } else if (templatesCount == 1) {
            Print print = Beans.get(PrintTemplateService.class).generatePrint(context.getId(), printTemplateQuery.fetchOne());
            response.setView(getPrintView(print));
        } else if (templatesCount >= 2) {
            response.setView(ActionView.define(I18n.get("Select template")).model(Wizard.class.getName()).add("form", "select-print-template-wizard-form").param("show-confirm", "false").context("_objectId", context.getId().toString()).context("_templateContextModel", model).map());
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : Print(com.axelor.apps.base.db.Print) PrintTemplate(com.axelor.apps.base.db.PrintTemplate) Model(com.axelor.db.Model) Wizard(com.axelor.apps.base.db.Wizard) AxelorException(com.axelor.exception.AxelorException)

Example 47 with Model

use of com.axelor.db.Model in project axelor-open-suite by axelor.

the class QuerieService method runJpqlRequest.

@SuppressWarnings({ "rawtypes", "unchecked" })
public List<Long> runJpqlRequest(String filter, Class<?> klass) {
    List<Long> idLists = Lists.newArrayList();
    List<Map> result = Query.of((Class<? extends Model>) klass).filter(filter).select("id").fetch(0, 0);
    for (Map map : result) {
        idLists.add(Long.valueOf(map.get("id").toString()));
    }
    return idLists;
}
Also used : Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) Map(java.util.Map)

Example 48 with Model

use of com.axelor.db.Model in project axelor-open-suite by axelor.

the class DataBackupRestoreService method importObjectWithByteArray.

public Object importObjectWithByteArray(Object bean, Map<String, Object> values) throws IOException {
    assert bean instanceof Model;
    final Path path = (Path) values.get("__path__");
    Mapper mapper = Mapper.of(bean.getClass());
    for (String fieldName : values.keySet()) {
        if (fieldName.startsWith("byte_")) {
            String fileName = (String) values.get(fieldName);
            if (Strings.isNullOrEmpty((fileName))) {
                return bean;
            }
            try {
                final File image = path.resolve(fileName).toFile();
                byte[] bytes = new byte[(int) image.length()];
                bytes = java.nio.file.Files.readAllBytes(image.toPath());
                mapper.set(bean, fieldName.substring(5), bytes);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return bean;
}
Also used : Path(java.nio.file.Path) Mapper(com.axelor.db.mapper.Mapper) Model(com.axelor.db.Model) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) IOException(java.io.IOException)

Example 49 with Model

use of com.axelor.db.Model in project axelor-open-suite by axelor.

the class ObjectDataConfigExportManagementRepository method save.

@Override
public ObjectDataConfigExport save(ObjectDataConfigExport entity) {
    try {
        Class<? extends Model> klass = (Class<? extends Model>) Class.forName(entity.getModelSelect());
        JpaRepository<? extends Model> repo = JpaRepository.of(klass);
        Object obj = repo.all().filter("self.id = ?", entity.getModelSelectId()).fetchOne();
        if (obj != null) {
            Mapper mapper = Mapper.of(obj.getClass());
            if (mapper.getNameField() != null && mapper.getNameField().get(obj) != null) {
                entity.setRecordName(mapper.getNameField().get(obj).toString());
            } else {
                entity.setRecordName(mapper.get(obj, "id").toString());
            }
        }
    } catch (ClassNotFoundException e) {
        TraceBackService.trace(e);
    }
    return super.save(entity);
}
Also used : Mapper(com.axelor.db.mapper.Mapper) Model(com.axelor.db.Model)

Example 50 with Model

use of com.axelor.db.Model in project axelor-open-suite by axelor.

the class ObjectDataAnonymizeServiceImpl method deleteFields.

@Transactional
public void deleteFields(Set<MetaField> fields, Mapper mapper, List<? extends Model> data) {
    if (fields.isEmpty()) {
        return;
    }
    Map<String, Object> defaultValues = getDefaultValues(mapper, fields);
    for (Model model : data) {
        for (Entry<String, Object> fieldEntry : defaultValues.entrySet()) {
            mapper.set(model, fieldEntry.getKey(), fieldEntry.getValue());
        }
        JPA.save(model);
        mailMessageRepo.all().filter("self.relatedId = ?1 AND self.relatedModel = ?2", model.getId(), mapper.getBeanClass().getName()).delete();
    }
}
Also used : Model(com.axelor.db.Model) Transactional(com.google.inject.persist.Transactional)

Aggregations

Model (com.axelor.db.Model)77 MetaModel (com.axelor.meta.db.MetaModel)22 AxelorException (com.axelor.exception.AxelorException)19 ArrayList (java.util.ArrayList)18 HashMap (java.util.HashMap)16 Context (com.axelor.rpc.Context)15 Transactional (com.google.inject.persist.Transactional)15 List (java.util.List)14 Mapper (com.axelor.db.mapper.Mapper)13 IOException (java.io.IOException)13 File (java.io.File)12 Map (java.util.Map)12 Property (com.axelor.db.mapper.Property)11 MetaJsonRecord (com.axelor.meta.db.MetaJsonRecord)9 JsonContext (com.axelor.rpc.JsonContext)8 MetaModelRepository (com.axelor.meta.db.repo.MetaModelRepository)7 Strings (com.google.common.base.Strings)7 HashSet (java.util.HashSet)7 FullContext (com.axelor.apps.tool.context.FullContext)6 Beans (com.axelor.inject.Beans)6