package devopsdistilled.operp.client.items.panes.controllers.impl;

import javax.inject.Inject;

import devopsdistilled.operp.client.items.exceptions.EntityNameExistsException;
import devopsdistilled.operp.client.items.exceptions.NullFieldException;
import devopsdistilled.operp.client.items.models.CategoryModel;
import devopsdistilled.operp.client.items.models.ProductModel;
import devopsdistilled.operp.client.items.panes.EditProductPane;
import devopsdistilled.operp.client.items.panes.controllers.EditProductPaneController;
import devopsdistilled.operp.client.items.panes.models.EditProductPaneModel;
import devopsdistilled.operp.server.data.entity.items.Product;

public class EditProductPaneControllerImpl implements EditProductPaneController {

	@Inject
	private EditProductPane view;

	@Inject
	private EditProductPaneModel model;

	@Inject
	private ProductModel productModel;

	@Inject
	private CategoryModel categoryModel;

	@Override
	public void init(Product product) {
		view.init();
		model.setEntity(product);
		model.registerObserver(view);
		categoryModel.registerObserver(view);
	}

	@Override
	public void validate(Product product) throws NullFieldException,
			EntityNameExistsException {

		if (product.getProductName().equalsIgnoreCase(""))
			throw new NullFieldException("Product Name can't be empty");

		if (product.getCategories().size() == 0)
			throw new NullFieldException(
					"Product should be of atleast 1 category");

		if (!productModel.getService().isProductNameValidForProduct(
				product.getProductId(), product.getProductName()))
			throw new EntityNameExistsException("Product Name already exists");

	}

	@Override
	public Product save(Product product) {
		return productModel.saveAndUpdateModel(product);
	}

}

@@ -997,37 +997,17 @@
 ;%0A%09%09
-model.setEntity(product);%0A%09%09m
+categoryM
 odel
@@ -1031,27 +1031,101 @@
 (view);%0A
+%0A
 %09%09
-categoryM
+// Respective Models are to be registered at last.%0A%09%09model.setEntity(product);%0A%09%09m
 odel.reg

Manage registerObserver sequence