use of com.axelor.apps.base.db.FileTab in project axelor-open-suite by axelor.
the class DataImportServiceImpl method process.
private List<CSVInput> process(DataReaderService reader, AdvancedImport advancedImport) throws AxelorException, IOException, ClassNotFoundException {
String[] sheets = reader.getSheetNames();
boolean isConfig = advancedImport.getIsConfigInFile();
int linesToIgnore = advancedImport.getNbOfFirstLineIgnore();
boolean isTabConfig = advancedImport.getIsFileTabConfigAdded();
List<CSVInput> inputList = new ArrayList<CSVInput>();
validatorService.sortFileTabList(advancedImport.getFileTabList());
for (FileTab fileTab : advancedImport.getFileTabList()) {
if (!Arrays.stream(sheets).anyMatch(sheet -> sheet.equals(fileTab.getName()))) {
continue;
}
this.initializeVariables();
String fileName = createDataFileName(fileTab);
csvInput = this.createCSVInput(fileTab, fileName);
ifList = new ArrayList<String>();
try (CSVWriter csvWriter = new CSVWriter(new FileWriter(new File(dataDir, fileName)), CSV_SEPRATOR)) {
int totalLines = reader.getTotalLines(fileTab.getName());
if (totalLines == 0) {
continue;
}
Mapper mapper = advancedImportService.getMapper(fileTab.getMetaModel().getFullName());
List<String[]> allLines = new ArrayList<String[]>();
int startIndex = isConfig ? 1 : linesToIgnore;
String[] row = reader.read(fileTab.getName(), startIndex, 0);
String[] headers = this.createHeader(row, fileTab, isConfig, mapper);
allLines.add(headers);
int tabConfigRowCount = 0;
if (isTabConfig) {
String[] objectRow = reader.read(fileTab.getName(), 0, 0);
tabConfigRowCount = advancedImportService.getTabConfigRowCount(fileTab.getName(), reader, totalLines, objectRow);
}
startIndex = isConfig ? tabConfigRowCount + 3 : fileTab.getAdvancedImport().getIsHeader() ? linesToIgnore + 1 : linesToIgnore;
for (int line = startIndex; line < totalLines; line++) {
String[] dataRow = reader.read(fileTab.getName(), line, row.length);
if (dataRow == null || Arrays.stream(dataRow).allMatch(StringUtils::isBlank)) {
continue;
}
String[] data = this.createData(dataRow, fileTab, isConfig, mapper);
allLines.add(data);
}
csvWriter.writeAll(allLines);
csvWriter.flush();
}
inputList.add(csvInput);
importContext.put("ifConditions" + fileTab.getId(), ifList);
importContext.put("jsonContextValues" + fileTab.getId(), createJsonContext(fileTab));
importContext.put("actionsToApply" + fileTab.getId(), fileTab.getActions());
XStream stream = XStreamUtils.createXStream();
stream.processAnnotations(CSVConfig.class);
LOG.debug("CSV Config created :" + "\n" + stream.toXML(csvInput));
}
return inputList;
}
use of com.axelor.apps.base.db.FileTab in project axelor-open-suite by axelor.
the class AdvancedImportServiceImpl method removeRecords.
@SuppressWarnings("unchecked")
public void removeRecords(List<FileTab> fileTabList) throws ClassNotFoundException {
for (FileTab fileTab : fileTabList) {
String targetModelName = fileTab.getMetaModel().getFullName();
Map<String, Object> jsonContextMap = dataImportService.createJsonContext(fileTab);
JsonContext jsonContext = (JsonContext) jsonContextMap.get("jsonContext");
String fieldName = inflector.camelize(fileTab.getMetaModel().getName(), true) + "Set";
List<Object> recordList = (List<Object>) jsonContext.get(fieldName);
if (CollectionUtils.isEmpty(recordList)) {
continue;
}
Class<? extends Model> modelKlass = (Class<? extends Model>) Class.forName(targetModelName);
removeRecord(fileTab, modelKlass, recordList, fileTabList);
removeSubRecords(modelKlass, jsonContext);
}
}
use of com.axelor.apps.base.db.FileTab in project axelor-open-suite by axelor.
the class AdvancedImportServiceImpl method removeRecord.
@SuppressWarnings("unchecked")
@Transactional
public void removeRecord(FileTab fileTab, Class<? extends Model> modelKlass, List<Object> recordList, List<FileTab> fileTabList) throws ClassNotFoundException {
JpaRepository<? extends Model> modelRepo = JpaRepository.of(modelKlass);
for (FileTab tab : fileTabList) {
Map<String, Object> jsonContextMap = dataImportService.createJsonContext(tab);
JsonContext jsonContext = (JsonContext) jsonContextMap.get("jsonContext");
String fieldName = inflector.camelize(tab.getMetaModel().getName(), true) + "Set";
List<Object> recList = (List<Object>) jsonContext.get(fieldName);
if (CollectionUtils.isEmpty(recList)) {
continue;
}
Class<? extends Model> klass = (Class<? extends Model>) Class.forName(tab.getMetaModel().getFullName());
Property[] props = Mapper.of(klass).getProperties();
for (Property prop : props) {
if (prop.getTarget() != null && prop.getTarget() == modelKlass && prop.isRequired()) {
removeRecord(tab, klass, recList, fileTabList);
}
}
}
String ids = recordList.stream().map(obj -> {
Map<String, Object> recordMap = Mapper.toMap(EntityHelper.getEntity(obj));
return recordMap.get("id").toString();
}).collect(Collectors.joining(","));
modelRepo.all().filter("self.id IN (" + ids + ")").delete();
fileTab.setAttrs(null);
LOG.debug("Reset imported data : {}", modelKlass.getSimpleName());
}
use of com.axelor.apps.base.db.FileTab in project axelor-open-suite by axelor.
the class FileTabServiceImpl method getShowRecordIds.
@SuppressWarnings("unchecked")
@Override
public String getShowRecordIds(FileTab fileTab, String field) throws ClassNotFoundException {
Context context = new Context(fileTab.getClass());
Class<? extends Model> klass = (Class<? extends Model>) Class.forName(fileTab.getClass().getName());
JsonContext jsonContext = new JsonContext(context, Mapper.of(klass).getProperty("attrs"), fileTab.getAttrs());
List<Object> recordList = (List<Object>) jsonContext.get(field);
if (CollectionUtils.isEmpty(recordList)) {
return null;
}
String ids = recordList.stream().map(obj -> {
Map<String, Object> recordMap = Mapper.toMap(EntityHelper.getEntity(obj));
return recordMap.get("id").toString();
}).collect(Collectors.joining(","));
return ids;
}
use of com.axelor.apps.base.db.FileTab in project axelor-open-suite by axelor.
the class FileTabController method showRecord.
public void showRecord(ActionRequest request, ActionResponse response) {
try {
FileTab fileTab = request.getContext().asType(FileTab.class);
fileTab = Beans.get(FileTabRepository.class).find(fileTab.getId());
String btnName = request.getContext().get("_signal").toString();
String fieldName = StringUtils.substringBetween(btnName, "show", "Btn");
MetaJsonField jsonField = Beans.get(MetaJsonFieldRepository.class).all().filter("self.name = ?1 AND self.type = 'many-to-many' AND self.model = ?2 AND self.modelField = 'attrs'", fieldName, fileTab.getClass().getName()).fetchOne();
if (jsonField == null) {
return;
}
String ids = Beans.get(FileTabService.class).getShowRecordIds(fileTab, jsonField.getName());
response.setView(ActionView.define(I18n.get(jsonField.getTitle())).model(jsonField.getTargetModel()).add("grid", jsonField.getGridView()).add("form", jsonField.getFormView()).domain("self.id IN (" + ids + ")").map());
} catch (Exception e) {
TraceBackService.trace(response, e);
}
}
Aggregations