use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.
the class FileFieldServiceImpl method getSubFieldType.
private String[] getSubFieldType(String[] subFields, int index, MetaField parentField) {
String[] types = new String[2];
if (index < subFields.length) {
types[0] = parentField.getTypeName();
types[1] = parentField.getRelationship();
MetaModel childModel = metaModelRepo.findByName(parentField.getTypeName());
if (childModel != null) {
MetaField childField = metaFieldRepo.findByModel(subFields[index], childModel);
if (childField == null) {
return types;
}
if (childField.getRelationship() != null) {
if (!types[0].equals("MetaFile")) {
types[0] = childField.getTypeName();
}
types[1] = childField.getRelationship();
types = this.getSubFieldType(subFields, index + 1, childField);
} else {
if (!types[0].equals("MetaFile")) {
types[0] = childField.getTypeName();
}
}
}
} else {
if ((!Strings.isNullOrEmpty(types[0]) && !types[0].equals("MetaFile")) || Strings.isNullOrEmpty(types[0])) {
types[0] = parentField.getTypeName();
}
types[1] = parentField.getRelationship();
}
return types;
}
use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.
the class AdvancedExportServiceImpl method createQueryParts.
/**
* This method create query parts based on field.
*
* @param splitField
* @param parentIndex
* @param metaModel
* @throws ClassNotFoundException
*/
private void createQueryParts(String[] splitField, int parentIndex, MetaModel metaModel) throws ClassNotFoundException {
while (parentIndex <= splitField.length - 1) {
MetaField relationalField = metaFieldRepo.all().filter("self.name = ?1 and self.metaModel = ?2", splitField[parentIndex], metaModel).fetchOne();
MetaModel subMetaModel = metaModelRepo.all().filter("self.name = ?1", relationalField.getTypeName()).fetchOne();
if (!Strings.isNullOrEmpty(relationalField.getRelationship())) {
checkRelationalField(splitField, parentIndex);
} else {
checkSelectionField(splitField, parentIndex, metaModel);
checkNormalField(splitField, parentIndex);
}
parentIndex += 1;
metaModel = subMetaModel;
}
}
use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.
the class ProductStockRepository method save.
public Product save(Product product) {
WeightedAveragePriceService weightedAveragePriceService = Beans.get(WeightedAveragePriceService.class);
Set<MetaField> specificProductFieldSet = appBaseService.getAppBase().getCompanySpecificProductFieldsSet();
if (!specificProductFieldSet.isEmpty() && appBaseService.getAppBase().getEnableMultiCompany()) {
ArrayList<Company> productCompanyList = new ArrayList<>();
if (product.getProductCompanyList() != null) {
for (ProductCompany productCompany : product.getProductCompanyList()) {
productCompanyList.add(productCompany.getCompany());
}
}
Mapper mapper = Mapper.of(Product.class);
List<StockConfig> stockConfigList = Beans.get(StockConfigRepository.class).all().fetch();
for (StockConfig stockConfig : stockConfigList) {
if (stockConfig.getCompany() != null && !productCompanyList.contains(stockConfig.getCompany()) && stockConfig.getReceiptDefaultStockLocation() != null && (stockConfig.getCompany().getArchived() == null || !stockConfig.getCompany().getArchived())) {
ProductCompany productCompany = new ProductCompany();
for (MetaField specificField : specificProductFieldSet) {
mapper.set(productCompany, specificField.getName(), mapper.get(product, specificField.getName()));
}
// specific case for avgPrice per company
productCompany.setAvgPrice(weightedAveragePriceService.computeAvgPriceForCompany(product, stockConfig.getCompany()));
productCompany.setCompany(stockConfig.getCompany());
productCompany.setProduct(product);
product.addProductCompanyListItem(productCompany);
}
}
}
return super.save(product);
}
use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.
the class DuplicateObjectsService method removeDuplicate.
@Transactional
public void removeDuplicate(List<Long> selectedIds, String modelName) {
List<Object> duplicateObjects = getDuplicateObject(selectedIds, modelName);
Object originalObjct = getOriginalObject(selectedIds, modelName);
List<MetaField> allField = metaFieldRepo.all().filter("(relationship = 'ManyToOne' AND typeName = ?1) OR (relationship = 'ManyToMany' AND (typeName = ?1 OR metaModel.name =?1))", modelName).fetch();
for (MetaField metaField : allField) {
if ("ManyToOne".equals(metaField.getRelationship())) {
Query update = JPA.em().createQuery("UPDATE " + metaField.getMetaModel().getFullName() + " self SET self." + metaField.getName() + " = :value WHERE self." + metaField.getName() + " in (:duplicates)");
update.setParameter("value", originalObjct);
update.setParameter("duplicates", duplicateObjects);
update.executeUpdate();
} else if ("ManyToMany".equals(metaField.getRelationship())) {
if (metaField.getTypeName().equals(modelName)) {
Query select = JPA.em().createQuery("select self from " + metaField.getMetaModel().getFullName() + " self LEFT JOIN self." + metaField.getName() + " as x WHERE x IN (:ids)");
select.setParameter("ids", duplicateObjects);
List<?> list = select.getResultList();
for (Object obj : list) {
Set<Object> items = (Set<Object>) Mapper.of(obj.getClass()).get(obj, metaField.getName());
for (Object dupObj : duplicateObjects) {
if (items.contains(dupObj)) {
items.remove(dupObj);
}
}
items.add(originalObjct);
}
}
Mapper mapper = Mapper.of(originalObjct.getClass());
Set<Object> existRelationalObjects = (Set<Object>) mapper.get(originalObjct, metaField.getName());
for (int i = 0; i < duplicateObjects.size(); i++) {
Set<Object> newRelationalObjects = (Set<Object>) mapper.get(duplicateObjects.get(i), metaField.getName());
if (newRelationalObjects != null) {
existRelationalObjects.addAll(newRelationalObjects);
mapper.set(duplicateObjects.get(i), metaField.getName(), new HashSet<>());
}
}
}
}
JPA.em().flush();
JPA.em().clear();
for (Object obj : getDuplicateObject(selectedIds, modelName)) {
JPA.em().remove(obj);
}
}
use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.
the class ChartBuilderService method addSearchField.
/**
* Method set default value for search-fields(parameters). It will add field and expression in
* onNew for chart.
*
* @param fieldName Name of field of search-field.
* @param typeName Type of field.
* @param defaultValue Default value input in chart filter.
* @param modelField It is for relational field. String array with first element as Model name and
* second as its field.
*/
// private void setDefaultValue(String fieldName, String typeName,
// String defaultValue, String[] modelField) {
//
// if (defaultValue == null) {
// return;
// }
//
// RecordField field = new RecordField();
// field.setName(fieldName);
//
// defaultValue = filterCommonService.getTagValue(defaultValue, false);
//
// if (modelField != null) {
// if (typeName.equals("STRING")) {
// defaultValue = "__repo__(" + modelField[0]
// + ").all().filter(\"LOWER(" + modelField[1] + ") LIKE "
// + defaultValue + "\").fetchOne()";
// } else {
// defaultValue = "__repo__(" + modelField[0]
// + ").all().filter(\"" + modelField[1] + " = "
// + defaultValue + "\").fetchOne()";
// }
//
// }
//
// log.debug("Default value: {}", defaultValue);
//
// field.setExpression("eval:" + defaultValue);
//
// onNewFields.add(field);
// }
/**
* It will create onNew action from onNew fields.
*
* @param viewBuilder ViewBuilder use to get model name also used in onNew action name creation.
*/
// private void setOnNewAction(ChartBuilder chartBuilder) {
//
// if (!onNewFields.isEmpty()) {
// onNewAction = new ActionRecord();
// onNewAction.setName("action-" + chartBuilder.getName() + "-default");
// onNewAction.setModel(chartBuilder.getModel());
// onNewAction.setFields(onNewFields);
// }
//
// }
private void addSearchField(List<Filter> filters) throws AxelorException {
if (filters == null) {
return;
}
for (Filter filter : filters) {
if (!filter.getIsParameter()) {
continue;
}
String fieldStr = "param" + filter.getId();
Object object = null;
StringBuilder parent = new StringBuilder("self");
if (filter.getIsJson()) {
object = filterSqlService.parseJsonField(filter.getMetaJsonField(), filter.getTargetField(), null, parent);
} else {
object = filterSqlService.parseMetaField(filter.getMetaField(), filter.getTargetField(), null, parent, true);
}
if (object instanceof MetaField) {
fieldStr = getMetaSearchField(fieldStr, (MetaField) object);
} else {
fieldStr = getJsonSearchField(fieldStr, (MetaJsonField) object);
}
searchFields.add(fieldStr + "\" x-required=\"true\" />");
}
}
Aggregations