use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.
the class ImportAdvancedImport method importPicture.
public Object importPicture(String value, String pathVal) throws IOException {
if (Strings.isNullOrEmpty(value)) {
return null;
}
Path path = Paths.get(pathVal);
if (Strings.isNullOrEmpty(value)) {
return null;
}
File image = path.resolve(value).toFile();
if (!image.exists() || image.isDirectory()) {
return null;
}
MetaFile metaFile = metaFiles.upload(image);
return metaFile;
}
use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.
the class WkfModelServiceImpl method importWkfModels.
@Override
public String importWkfModels(MetaFile metaFile, boolean isTranslate, String sourceLanguage, String targetLanguage) throws AxelorException {
if (metaFile == null) {
return null;
}
String extension = Files.getFileExtension(metaFile.getFileName());
if (extension == null || !extension.equals("xml")) {
throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(ITranslation.INVALID_WKF_MODEL_XML));
}
try {
InputStream inputStream = getClass().getResourceAsStream(IMPORT_CONFIG_PATH);
File configFile = File.createTempFile("config", ".xml");
FileOutputStream fout = new FileOutputStream(configFile);
IOUtil.copyCompletely(inputStream, fout);
File xmlFile = MetaFiles.getPath(metaFile).toFile();
File tempDir = Files.createTempDir();
File importFile = new File(tempDir, "wkfModels.xml");
Files.copy(xmlFile, importFile);
if (isTranslate) {
importFile = this.translateNodeName(importFile, sourceLanguage, targetLanguage);
}
XMLImporter importer = new XMLImporter(configFile.getAbsolutePath(), tempDir.getAbsolutePath());
final StringBuilder log = new StringBuilder();
Listener listner = new Listener() {
@Override
public void imported(Integer imported, Integer total) {
}
@Override
public void imported(Model arg0) {
}
@Override
public void handle(Model arg0, Exception err) {
log.append("Error in import: " + err.getStackTrace().toString());
}
};
importer.addListener(listner);
importer.run();
FileUtils.forceDelete(configFile);
FileUtils.forceDelete(tempDir);
FileUtils.forceDelete(xmlFile);
MetaFileRepository metaFileRepository = Beans.get(MetaFileRepository.class);
metaFileRepository.remove(metaFile);
return log.toString();
} catch (Exception e) {
return e.getMessage();
}
}
use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.
the class ImportProduct method importProduct.
public Object importProduct(Object bean, Map<String, Object> values) {
assert bean instanceof Product;
Product product = (Product) bean;
String fileName = (String) values.get("picture_fileName");
if (!StringUtils.isEmpty(fileName)) {
final Path path = (Path) values.get("__path__");
try {
final File image = path.resolve(fileName).toFile();
if (image != null && image.isFile()) {
final MetaFile metaFile = metaFiles.upload(image);
product.setPicture(metaFile);
} else {
LOG.debug("No image file found: {}", image == null ? path.toAbsolutePath() : image.getAbsolutePath());
}
} catch (Exception e) {
LOG.error("Error when importing product picture : {}", e);
}
}
return productRepo.save(product);
}
use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.
the class HumanResourceMobileController method insertOrUpdateExpenseLine.
/*
* This method is used in mobile application.
* It was in ExpenseServiceImpl
* @param request
* @param response
*
* POST /open-suite-webapp/ws/action/com.axelor.apps.hr.mobile.HumanResourceMobileController:insertOrUpdateExpenseLine
* Content-Type: application/json
*
* URL: com.axelor.apps.hr.mobile.HumanResourceMobileController:insertOrUpdateExpenseLine
* fields: (id,) project, expenseType, date, comments, toInvoice, unTaxTotal, taxTotal, justification
*
* payload:
* { "data": {
* "action": "com.axelor.apps.hr.mobile.HumanResourceMobileController:insertOrUpdateExpenseLine",
* "id": 1,
* "project": 2,
* "expenseType": 10,
* "date": "2018-02-22",
* "comments": "No",
* "toInvoice": "no",
* "unTaxTotal": 100,
* "taxTotal": 2,
* "justification": "no"
* } }
*/
@Transactional
public void insertOrUpdateExpenseLine(ActionRequest request, ActionResponse response) {
try {
User user = AuthUtils.getUser();
Map<String, Object> requestData = request.getData();
Project project = Beans.get(ProjectRepository.class).find(Long.valueOf(requestData.get("project").toString()));
Product product = Beans.get(ProductRepository.class).find(Long.valueOf(requestData.get("expenseType").toString()));
if (user != null) {
ExpenseService expenseService = Beans.get(ExpenseService.class);
Expense expense = expenseService.getOrCreateExpense(user);
ExpenseLine expenseLine;
Object idO = requestData.get("id");
if (idO != null) {
expenseLine = Beans.get(ExpenseLineRepository.class).find(Long.valueOf(idO.toString()));
} else {
expenseLine = new ExpenseLine();
}
expenseLine.setExpenseDate(LocalDate.parse(requestData.get("date").toString(), DateTimeFormatter.ISO_DATE));
expenseLine.setComments(requestData.get("comments").toString());
expenseLine.setExpenseProduct(product);
expenseLine.setProject(project);
expenseLine.setUser(user);
expenseLine.setTotalAmount(new BigDecimal(requestData.get("unTaxTotal").toString()));
expenseLine.setTotalTax(new BigDecimal(requestData.get("taxTotal").toString()));
expenseLine.setUntaxedAmount(expenseLine.getTotalAmount().subtract(expenseLine.getTotalTax()));
expenseLine.setToInvoice(new Boolean(requestData.get("toInvoice").toString()));
String justification = (String) requestData.get("justification");
if (!Strings.isNullOrEmpty(justification)) {
String MIME_IMAGE_X_ICON = "image/x-icon";
String MIME_IMAGE_SVG_XML = "image/svg+xml";
String MIME_IMAGE_BMP = "image/bmp";
String MIME_IMAGE_GIF = "image/gif";
String MIME_IMAGE_JPEG = "image/jpeg";
String MIME_IMAGE_TIFF = "image/tiff";
String MIME_IMAGE_PNG = "image/png";
Map<String, String> mimeTypeMapping = new HashMap<>(200);
mimeTypeMapping.put(MIME_IMAGE_X_ICON, "ico");
mimeTypeMapping.put(MIME_IMAGE_SVG_XML, "svg");
mimeTypeMapping.put(MIME_IMAGE_BMP, "bmp");
mimeTypeMapping.put(MIME_IMAGE_GIF, "gif");
mimeTypeMapping.put(MIME_IMAGE_JPEG, "jpg");
mimeTypeMapping.put(MIME_IMAGE_TIFF, "tif");
mimeTypeMapping.put(MIME_IMAGE_PNG, "png");
byte[] decodedFile = Base64.getDecoder().decode(justification);
String formatName = URLConnection.guessContentTypeFromStream(new ByteArrayInputStream(decodedFile));
String extension = "";
if (mimeTypeMapping.containsKey(formatName)) {
extension = "." + mimeTypeMapping.get(formatName);
File file = MetaFiles.createTempFile("justification", extension).toFile();
Files.write(decodedFile, file);
MetaFile metaFile = Beans.get(MetaFiles.class).upload(file);
expenseLine.setJustificationMetaFile(metaFile);
}
}
expense.addGeneralExpenseLineListItem(expenseLine);
expense = expenseService.compute(expense);
Beans.get(ExpenseRepository.class).save(expense);
HashMap<String, Object> data = new HashMap<>();
data.put("id", expenseLine.getId());
response.setData(data);
response.setTotal(1);
}
} catch (Exception e) {
TraceBackService.trace(response, e);
}
}
use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.
the class ImportVehicle method importVehicle.
public Object importVehicle(Object bean, Map<String, Object> values) {
assert bean instanceof Vehicle;
Vehicle vehicle = (Vehicle) bean;
final Path path = (Path) values.get("__path__");
String fileName = (String) values.get("image_fileName");
if (Strings.isNullOrEmpty(fileName)) {
return bean;
}
final File image = path.resolve(fileName).toFile();
try {
final MetaFile metaFile = metaFiles.upload(image);
vehicle.setImage(metaFile);
} catch (IOException e) {
e.printStackTrace();
}
return bean;
}
Aggregations