use of com.axelor.db.mapper.Mapper 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.mapper.Mapper in project axelor-open-suite by axelor.
the class ConfiguratorServiceImpl method generateProduct.
@Override
@Transactional(rollbackOn = { Exception.class })
public void generateProduct(Configurator configurator, JsonContext jsonAttributes, JsonContext jsonIndicators) throws AxelorException {
cleanIndicators(jsonIndicators);
Mapper mapper = Mapper.of(Product.class);
Product product = new Product();
for (String key : jsonIndicators.keySet()) {
mapper.set(product, key, jsonIndicators.get(key));
}
fixRelationalFields(product);
fetchManyToManyFields(product);
fillOneToManyFields(configurator, product, jsonAttributes);
if (product.getProductTypeSelect() == null) {
product.setProductTypeSelect(ProductRepository.PRODUCT_TYPE_STORABLE);
}
if (product.getCode() == null) {
throw new AxelorException(configurator, TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.CONFIGURATOR_PRODUCT_MISSING_CODE));
}
if (product.getName() == null) {
throw new AxelorException(configurator, TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.CONFIGURATOR_PRODUCT_MISSING_NAME));
}
configurator.setProduct(product);
product.setConfigurator(configurator);
Beans.get(ProductRepository.class).save(product);
}
use of com.axelor.db.mapper.Mapper 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.mapper.Mapper in project axelor-open-suite by axelor.
the class AppLoaderExportServiceImpl method createMetaModelInput.
protected XMLInput createMetaModelInput(AppDataLoader dataLoader, boolean relationalInput) throws ClassNotFoundException {
XMLInput xmlInput = createXmlInput(dataLoader);
Mapper modelMapper = Mapper.of(Class.forName(dataLoader.getMetaModel().getFullName()));
String nodeName = getBindNodeName(xmlInput.getRoot());
XMLBind xmlBind = new XMLBind();
xmlBind.setTypeName(dataLoader.getMetaModel().getFullName());
xmlBind.setNode(nodeName);
if (!CollectionUtils.isEmpty(dataLoader.getSearchMetaFieldSet())) {
xmlBind.setSearch(getMetaSearchFields(modelMapper, dataLoader));
xmlBind.setUpdate(true);
}
if (relationalInput) {
xmlBind.setCreate(false);
}
xmlBind.setBindings(getMetaFieldBinding(modelMapper, dataLoader, relationalInput));
List<XMLBind> rootBindings = new ArrayList<XMLBind>();
rootBindings.add(xmlBind);
xmlInput.setBindings(rootBindings);
return xmlInput;
}
use of com.axelor.db.mapper.Mapper in project axelor-open-suite by axelor.
the class AppLoaderExportServiceImpl method addMetaModelData.
protected void addMetaModelData(AppDataLoader dataLoader, File parentDir) {
try {
Class klass = Class.forName(dataLoader.getMetaModel().getFullName());
if (!allowRead(klass)) {
return;
}
Mapper modelMapper = Mapper.of(klass);
String modelName = dataLoader.getMetaModel().getName();
String dasherizeModel = Inflector.getInstance().dasherize(modelName);
File dataFile = new File(parentDir, modelName + ".xml");
FileWriter fileWriter = createHeader(dasherizeModel, dataFile);
List<FullContext> records = FullContextHelper.filter(modelName, dataLoader.getFilterQuery());
for (FullContext record : records) {
if (!allowRead(klass, (Long) record.get("id"))) {
continue;
}
fileWriter.write("<" + dasherizeModel + ">\n");
for (MetaField metaField : dataLoader.getMetaFieldSet()) {
String field = metaField.getName();
fileWriter.write("\t<" + field + ">" + extractMetaFieldValue(record, modelMapper.getProperty(field)) + "</" + field + ">\n");
}
fileWriter.write("</" + dasherizeModel + ">\n\n");
}
fileWriter.write("</" + dasherizeModel + "s>\n");
fileWriter.close();
} catch (IOException | ClassNotFoundException e) {
TraceBackService.trace(e);
}
}
Aggregations