use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class ConfiguratorServiceImpl method fetchManyToManyFields.
/**
* Fix relational fields of a product or a sale order line generated from a configurator. This
* method may become useless on a future ADK update.
*
* @param model
*/
protected void fetchManyToManyFields(Model model) throws AxelorException {
// get all many to many fields
List<MetaField> manyToManyFields = metaFieldRepository.all().filter("self.metaModel.name = :name " + "AND self.relationship = 'ManyToMany'").bind("name", model.getClass().getSimpleName()).fetch();
Mapper mapper = Mapper.of(model.getClass());
for (MetaField manyToManyField : manyToManyFields) {
Set<? extends Model> manyToManyValue = (Set<? extends Model>) mapper.get(model, manyToManyField.getName());
fetchManyToManyField(model, manyToManyValue, manyToManyField);
}
}
use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class ConfiguratorServiceImpl method fillOneToManyFields.
protected void fillOneToManyFields(Configurator configurator, Model model, JsonContext jsonAttributes) throws AxelorException {
try {
ConfiguratorCreator creator = configurator.getConfiguratorCreator();
List<? extends ConfiguratorFormula> configuratorFormulaList;
Class[] methodArg = new Class[1];
if (creator.getGenerateProduct()) {
configuratorFormulaList = creator.getConfiguratorProductFormulaList();
methodArg[0] = Product.class;
} else {
configuratorFormulaList = creator.getConfiguratorSOLineFormulaList();
methodArg[0] = SaleOrderLine.class;
}
configuratorFormulaList = configuratorFormulaList.stream().filter(configuratorFormula -> "OneToMany".equals(configuratorFormula.getMetaField().getRelationship())).collect(Collectors.toList());
for (ConfiguratorFormula formula : configuratorFormulaList) {
List<? extends Model> computedValue = (List<? extends Model>) computeFormula(formula.getFormula(), jsonAttributes);
if (computedValue == null) {
continue;
}
Method setMappedByMethod = computeMappedByMethod(formula);
for (Model listElement : computedValue) {
setMappedByMethod.invoke(listElement, model);
JPA.save(listElement);
}
}
} catch (InvocationTargetException | IllegalAccessException | ClassNotFoundException e) {
throw new AxelorException(e, TraceBackRepository.CATEGORY_INCONSISTENCY);
}
}
use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class ConfiguratorServiceImpl method fixRelationalFields.
protected void fixRelationalFields(Model model) throws AxelorException {
// get all many to one fields
List<MetaField> manyToOneFields = metaFieldRepository.all().filter("self.metaModel.name = :name " + "AND self.relationship = 'ManyToOne'").bind("name", model.getClass().getSimpleName()).fetch();
Mapper mapper = Mapper.of(model.getClass());
for (MetaField manyToOneField : manyToOneFields) {
Model manyToOneValue = (Model) mapper.get(model, manyToOneField.getName());
fixRelationalField(model, manyToOneValue, manyToOneField);
}
}
use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class MessageServiceImpl method regenerateMessage.
@Override
@Transactional(rollbackOn = { Exception.class })
public Message regenerateMessage(Message message) throws Exception {
Preconditions.checkNotNull(message.getTemplate(), I18n.get("Cannot regenerate message without template associated to message."));
Preconditions.checkNotNull(message.getRelatedTo1Select(), I18n.get("Cannot regenerate message without related model."));
Class m = Class.forName(message.getRelatedTo1Select());
Model model = JPA.all(m).filter("self.id = ?", message.getRelatedTo1SelectId()).fetchOne();
Message newMessage = Beans.get(TemplateMessageService.class).generateMessage(model, message.getTemplate());
newMessage.setRelatedTo2Select(message.getRelatedTo2Select());
newMessage.setRelatedTo2SelectId(message.getRelatedTo2SelectId());
message.setArchived(true);
return newMessage;
}
use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class TemplateMessageServiceImpl method computeTemplateContexts.
@SuppressWarnings("unchecked")
@Override
public Map<String, Object> computeTemplateContexts(List<TemplateContext> templateContextList, long objectId, String model, boolean isJson, Map<String, Object> templatesContext) throws ClassNotFoundException {
if (templateContextList == null) {
return templatesContext;
}
Context context = null;
if (isJson) {
context = new com.axelor.rpc.Context(objectId, MetaJsonRecord.class);
} else {
Class<? extends Model> myClass = (Class<? extends Model>) Class.forName(model);
context = new com.axelor.rpc.Context(objectId, myClass);
}
for (TemplateContext templateContext : templateContextList) {
Object result = templateContextService.computeTemplateContext(templateContext, context);
templatesContext.put(templateContext.getName(), result);
}
return templatesContext;
}
Aggregations