/*
 * Axelor Business Solutions
 *
 * Copyright (C) 2018 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.apps.base.db.repo;

import java.io.IOException;
import java.io.InputStream;

import javax.persistence.PersistenceException;
import javax.validation.ValidationException;

import com.axelor.apps.base.db.Product;
import com.axelor.apps.base.service.BarcodeGeneratorService;
import com.axelor.apps.base.service.ProductService;
import com.axelor.apps.base.service.app.AppBaseService;
import com.axelor.apps.tool.service.TranslationService;
import com.axelor.exception.AxelorException;
import com.axelor.inject.Beans;
import com.axelor.meta.MetaFiles;
import com.axelor.meta.db.MetaFile;
import com.google.common.base.Strings;
import com.google.inject.Inject;

public class ProductBaseRepository extends ProductRepository{
	
	@Inject
	private MetaFiles metaFiles;

	@Inject
	protected AppBaseService appBaseService;

	@Inject
    protected TranslationService translationService;

    protected static final String FULL_NAME_FORMAT = "[%s] %s";

	@Override
	public Product save(Product product){
        Product oldProduct = Beans.get(ProductRepository.class).find(product.getId());
        product.setFullName(String.format(FULL_NAME_FORMAT, product.getCode(), product.getName()));

        translationService.updateFormatedValueTranslations(oldProduct.getFullName(), FULL_NAME_FORMAT,
                product.getCode(), product.getName());

		product = super.save(product);
		if(product.getBarCode() == null && product.getSerialNumber()!=null  &&  appBaseService.getAppBase().getActivateBarCodeGeneration() && product.getBarcodeTypeConfig()!=null) {
			try {
				boolean addPadding=false;
				InputStream inStream = BarcodeGeneratorService.createBarCode(product.getSerialNumber(), product.getBarcodeTypeConfig(), addPadding);
				if (inStream != null) {
			    	MetaFile barcodeFile =  metaFiles.upload(inStream, String.format("ProductBarCode%d.png", product.getId()));
					product.setBarCode(barcodeFile);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			catch (AxelorException  e) {
				throw new ValidationException(e.getMessage());
			}
		}

		try {
			if (Strings.isNullOrEmpty(product.getSequence()) && appBaseService.getAppBase().getGenerateProductSequence()) {
				product.setSequence(Beans.get(ProductService.class).getSequence());
			}

			return super.save(product);
		} catch (Exception e) {
			throw new PersistenceException(e.getLocalizedMessage());
		}
	}
	
	@Override
	public Product copy(Product product, boolean deep) {
		
		Product copy = super.copy(product, deep);
		copy.setBarCode(null);
		
		return copy;
		
	}
}

@@ -1700,16 +1700,160 @@
 oduct)%7B%0A
+        product.setFullName(String.format(FULL_NAME_FORMAT, product.getCode(), product.getName()));%0A%0A        if (product.getId() != null) %7B%0A    
         
@@ -1935,33 +1935,91 @@
 ));%0A        
-p
+    translationService.updateFormatedValueTranslations(oldP
 roduct.
-s
+g
 etFullName(S
@@ -2013,38 +2013,27 @@
 getFullName(
-String.format(
+), 
 FULL_NAME_FO
@@ -2029,32 +2029,52 @@
 ULL_NAME_FORMAT,
+%0A                   
  product.getCode
@@ -2095,28 +2095,45 @@
 tName())
-)
 ;%0A
-%0A
+        %7D else %7B%0A  
         
 translat
@@ -2116,32 +2116,34 @@
 lse %7B%0A          
+  
 translationServi
@@ -2137,35 +2137,35 @@
 nslationService.
-upd
+cre
 ateFormatedValue
@@ -2181,34 +2181,8 @@
 ons(
-oldProduct.getFullName(), 
 FULL
@@ -2194,32 +2194,16 @@
 _FORMAT,
-%0A               
  product
@@ -2233,16 +2233,26 @@
 Name());
+%0A        %7D
 %0A%0A%09%09prod

fix updating/creating value translations when product is new.