use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class DataBackupCreateService method getQuery.
protected Query<Model> getQuery(MetaModel metaModel, List<String> subClasses) throws ClassNotFoundException {
StringBuilder whereStr = new StringBuilder();
if (subClasses != null && !subClasses.isEmpty()) {
for (String subClassName : subClasses) {
whereStr.append(whereStr.length() > 0 ? " AND " : "");
whereStr.append("id NOT IN (select id from ").append(subClassName).append(")");
}
}
@SuppressWarnings("unchecked") Class<Model> klass = (Class<Model>) Class.forName(metaModel.getFullName());
JpaRepository<Model> model = null;
Query<Model> query = null;
try {
model = JpaRepository.of(klass);
} catch (Exception e) {
TraceBackService.trace(e, DataBackupService.class.getName());
}
if (model != null) {
query = JpaRepository.of(klass).all();
if (StringUtils.notEmpty(whereStr.toString())) {
query.filter(whereStr.toString());
}
try {
Connection connection = DBHelper.getConnection();
DatabaseMetaData dbmd = connection.getMetaData();
List<Property> properties = model.fields();
for (Property property : properties) {
if (property.isRequired()) {
ResultSet res = dbmd.getColumns(null, null, metaModel.getTableName().toLowerCase(), property.getName().replaceAll("([^_A-Z])([A-Z])", "$1_$2").toLowerCase());
while (res.next()) {
if (res.getInt("NULLABLE") == 1) {
String strErr = "Required field issue in table: ";
sb.append(strErr).append("Model: " + metaModel.getName() + ", Field: " + property.getName() + "-----------------------------------------\n");
query = null;
}
}
}
}
connection.close();
} catch (SQLException | NamingException e) {
e.printStackTrace();
}
}
return query;
}
use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class DataBackupCreateService method getMetaModelData.
/* Get Data For csv File */
protected String getMetaModelData(String metaModelName, Mapper metaModelMapper, Property property, Object dataObject, String dirPath, boolean isRelativeDate, boolean updateImportId) {
String id = metaModelMapper.get(dataObject, "id").toString();
Object value = metaModelMapper.get(dataObject, property.getName());
if (value == null) {
return "";
}
String propertyTypeStr = property.getType().toString();
switch(propertyTypeStr) {
case "LONG":
if (updateImportId) {
return ((Model) dataObject).getImportId();
}
return value.toString();
case "DATE":
if (isRelativeDate) {
return createRelativeDate((LocalDate) value);
}
return value.toString();
case "DATETIME":
if (isRelativeDate) {
if (property.getJavaType() == ZonedDateTime.class) {
return createRelativeDateTime(((ZonedDateTime) value).toLocalDateTime());
}
return createRelativeDateTime((LocalDateTime) value);
}
return property.getJavaType() == ZonedDateTime.class ? ((ZonedDateTime) value).toLocalDateTime().toString() : value.toString();
case "BINARY":
String fileName = metaModelName + "_" + property.getName() + "_" + id + ".png";
try {
org.apache.commons.io.FileUtils.writeByteArrayToFile(new File(dirPath, fileName), (byte[]) value);
fileNameList.add(fileName);
} catch (IOException e) {
e.printStackTrace();
}
return fileName;
case "ONE_TO_ONE":
case "MANY_TO_ONE":
return getRelationalFieldValue(property, value, updateImportId);
case "ONE_TO_MANY":
case "MANY_TO_MANY":
return getRelationalFieldData(property, value, updateImportId);
default:
return value.toString();
}
}
use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class DataBackupRestoreService method restore.
/* Restore the Data using provided zip File and prepare Log File and Return it*/
public File restore(MetaFile zipedBackupFile) {
Logger LOG = LoggerFactory.getLogger(getClass());
File tempDir = Files.createTempDir();
String dirPath = tempDir.getAbsolutePath();
StringBuilder sb = new StringBuilder();
try {
unZip(zipedBackupFile, dirPath);
String configFName = tempDir.getAbsolutePath() + File.separator + DataBackupServiceImpl.CONFIG_FILE_NAME;
CSVImporter csvImporter = new CSVImporter(configFName, tempDir.getAbsolutePath());
csvImporter.addListener(new Listener() {
String modelName;
StringBuilder sb1 = new StringBuilder();
@Override
public void handle(Model bean, Exception e) {
if (e.getMessage() != null && !e.getMessage().equals("null")) {
if (bean != null) {
sb1.append(bean.getClass().getSimpleName() + " : \n" + e.getMessage() + "\n\n");
} else {
sb1.append(e.getMessage() + "\n\n");
}
}
}
@Override
public void imported(Model model) {
modelName = model.getClass().getSimpleName();
}
@Override
public void imported(Integer total, Integer count) {
String str = "", strError = "";
if (!StringUtils.isBlank(sb1)) {
strError = "Errors : \n" + sb1.toString();
}
str = "Total Records : {" + total + "} - Success Records : {" + count + "} \n";
if (total != 0 && count != 0) {
sb.append(modelName + " : \n");
}
sb.append(strError).append(str + "-----------------------------------------\n");
sb1.setLength(0);
}
});
csvImporter.run();
LOG.info("Data Restore Completed");
FileUtils.cleanDirectory(new File(tempDir.getAbsolutePath()));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmSS");
String logFileName = "DataBackupLog_" + LocalDateTime.now().format(formatter) + ".log";
File file = new File(tempDir.getAbsolutePath(), logFileName);
PrintWriter pw = new PrintWriter(file);
pw.write(sb.toString());
pw.close();
return file;
} catch (IOException e) {
TraceBackService.trace(e);
return null;
}
}
use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class AppController method configure.
public void configure(ActionRequest request, ActionResponse response) {
Context context = request.getContext();
MetaView formView = null;
String code = (String) context.get("code");
String appName = Inflector.getInstance().camelize(code);
Model config = (Model) context.get("app" + appName);
String model = "com.axelor.apps.base.db.App" + appName;
if (config != null) {
formView = Beans.get(MetaViewRepository.class).all().filter("self.type = 'form' AND self.model = ? AND self.name like '%-config-form'", model).fetchOne();
}
if (formView == null) {
response.setFlash(I18n.get(IExceptionMessages.NO_CONFIG_REQUIRED));
} else {
response.setView(ActionView.define(I18n.get("Configure") + ": " + context.get("name")).add("form", formView.getName()).model(model).context("_showRecord", config.getId()).param("forceEdit", "true").map());
}
}
use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class WkfModelServiceImpl method getMetaModelRecordFilter.
@SuppressWarnings("unchecked")
private Object[] getMetaModelRecordFilter(WkfTaskConfig config, String modelName, User user) {
MetaModel metaModel = metaModelRepo.findByName(modelName);
String model = metaModel.getFullName();
String filter = "self.processInstanceId IN (:processInstanceIds)";
Class<Model> klass = null;
try {
klass = (Class<Model>) Class.forName(model);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (user != null) {
String path = config.getUserPath();
Property property = Mapper.of(klass).getProperty(path.split("\\.")[0]);
if (property == null) {
filter += " AND self.attrs." + path + ".id = '" + user.getId() + "'";
} else {
filter += " AND self." + path + ".id = " + user.getId();
}
}
return new Object[] { klass, filter };
}
Aggregations