/**
 * Axelor Business Solutions
 *
 * Copyright (C) 2017 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.csv.script;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Map;

import com.axelor.apps.base.db.Company;
import com.axelor.meta.MetaFiles;
import com.axelor.meta.db.MetaFile;
import com.google.inject.Inject;

public class ImportCompany {
	
	@Inject
	MetaFiles metaFiles;
	
	public Object importCompany(Object bean, Map<String,Object> values) {
		
		assert bean instanceof Company;
		
		Company company = (Company) bean;
		
		final Path path = (Path) values.get("__path__");
	    final File image = path.resolve((String) values.get("logo_fileName")).toFile(); 

		try {
			final MetaFile metaFile = metaFiles.upload(image);
			company.setLogo(metaFile);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return bean;
	}

}

@@ -750,36 +750,8 @@
 le;%0A
-import java.io.IOException;%0A
 impo
@@ -796,16 +796,74 @@
 l.Map;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import c
@@ -894,16 +894,54 @@
 ompany;%0A
+import com.axelor.common.StringUtils;%0A
 import c
@@ -1067,16 +1067,83 @@
 any %7B%0A%09%0A
+%09private final Logger LOG = LoggerFactory.getLogger(getClass());%0A%09%0A
 %09@Inject
@@ -1316,17 +1316,115 @@
 bean;%0A%09%09
-%0A
+String fileName = (String) values.get(%22logo_fileName%22);%0A%09%09%0A%09%09if(!StringUtils.isEmpty(fileName)) %7B%0A%09
 %09%09final 
@@ -1471,12 +1471,24 @@
 );%0A%09
-    
+%09%09%0A%09%09%09try %7B%0A%09%09%09%09
 fina
@@ -1519,42 +1519,16 @@
 lve(
-(String) values.get(%22logo_
 fileName
 %22)).
@@ -1523,18 +1523,16 @@
 fileName
-%22)
 ).toFile
@@ -1538,19 +1538,53 @@
 e();
- %0A
 %0A%09%09
-try
+%09%09if(image != null && image.isFile())
  %7B%0A
+%09%09
 %09%09%09f
@@ -1636,16 +1636,18 @@
 ge);%0A%09%09%09
+%09%09
 company.
@@ -1665,16 +1665,23 @@
 aFile);%0A
+%09%09%09%09%7D%0A%09
 %09%09%7D catc
@@ -1687,10 +1687,8 @@
 ch (
-IO
 Exce
@@ -1705,28 +1705,72 @@
 %0A%09%09%09
-e.printStackTrace();
+%09LOG.error(%22Error when importing product picture : %7B%7D%22, e);%0A%09%09%09%7D
 %0A%09%09%7D
@@ -1780,20 +1780,23 @@
 %09return 
-be
+comp
 an
+y
 ;%0A%09%7D%0A%0A%7D%0A

Fix company logo import if missing.