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.exceptions.ProductBrandPairExistsException;
import devopsdistilled.operp.client.items.models.BrandModel;
import devopsdistilled.operp.client.items.models.ItemModel;
import devopsdistilled.operp.client.items.models.ProductModel;
import devopsdistilled.operp.client.items.panes.EditItemPane;
import devopsdistilled.operp.client.items.panes.controllers.EditItemPaneController;
import devopsdistilled.operp.client.items.panes.models.EditItemPaneModel;
import devopsdistilled.operp.server.data.entity.items.Item;

public class EditItemPaneControllerImpl implements EditItemPaneController {

	@Inject
	private EditItemPane view;

	@Inject
	private EditItemPaneModel model;

	@Inject
	private ItemModel itemModel;

	@Inject
	private ProductModel productModel;

	@Inject
	private BrandModel brandModel;

	@Override
	public void init() {

	}

	@Override
	public void init(Item item) {
		view.init();
		model.setItem(item);
		model.registerObserver(view);
		productModel.registerObserver(view);
		brandModel.registerObserver(view);
	}

	@Override
	public Item save(Item item) {
		return itemModel.saveAndUpdateModel(item);
	}

	@Override
	public void validate(Item item) throws ProductBrandPairExistsException,
			EntityNameExistsException, NullFieldException {

		if (item.getItemName().equalsIgnoreCase("")
				|| item.getProduct() == null || item.getBrand() == null
				|| item.getPrice() == null) {

			throw new NullFieldException();
		}

		if (!itemModel.getService().isProductBrandPairValidForItem(
				item.getItemId(), item.getProduct(), item.getBrand())) {

			throw new ProductBrandPairExistsException();
		}

		if (!itemModel.getService().isItemNameValidForItem(item.getItemId(),
				item.getItemName())) {

			throw new EntityNameExistsException();
		}
	}
}

@@ -1085,46 +1085,8 @@
 l;%0A%0A
-%09@Override%0A%09public void init() %7B%0A%0A%09%7D%0A%0A
 %09@Ov
@@ -1144,32 +1144,16 @@
 ;%0A%09%09
-model.setItem(item);%0A%09%09m
+productM
 odel
@@ -1175,31 +1175,29 @@
 er(view);%0A%09%09
-product
+brand
 Model.regist
@@ -1208,38 +1208,56 @@
 server(view);%0A%09%09
-brandM
+model.setItem(item);%0A%09%09m
 odel.registerObs
@@ -1500,16 +1500,130 @@
 tion %7B%0A%0A
+%09%09if (item.getItemId() == null)%0A%09%09%09throw new NullFieldException(%0A%09%09%09%09%09%22Item Id encountered, Contact Developer%22);%0A%0A
 %09%09if (it
@@ -1663,45 +1663,195 @@
 (%22%22)
+)
 %0A%09%09%09
-%09%7C%7C item.getProduct() == null %7C%7C 
+throw new NullFieldException(%22Item Name can't be empty%22);%0A%0A%09%09if (item.getProduct() == null)%0A%09%09%09throw new NullFieldException(%0A%09%09%09%09%09%22Item should be associated with a product%22);%0A%0A%09%09if (
 item
@@ -1873,16 +1873,98 @@
 null
+)
 %0A%09%09%09
-%09%7C%7C 
+throw new NullFieldException(%0A%09%09%09%09%09%22Item should be associated with a brand%22);%0A%0A%09%09if (
 item
@@ -1983,19 +1983,16 @@
 == null)
- %7B%0A
 %0A%09%09%09thro
@@ -2008,38 +2008,63 @@
 lFieldException(
-);%0A%09%09%7D
+%22Item Price Null encountered%22);
 %0A%0A%09%09if (!itemMod
@@ -2165,27 +2165,24 @@
 getBrand()))
- %7B%0A
 %0A%09%09%09throw ne
@@ -2215,22 +2215,63 @@
 ception(
-);%0A%09%09%7D
+%0A%09%09%09%09%09%22Product and Brand Pair already exists%22);
 %0A%0A%09%09if (
@@ -2359,19 +2359,16 @@
 Name()))
- %7B%0A
 %0A%09%09%09thro
@@ -2403,14 +2403,36 @@
 ion(
-);%0A%09%09%7D
+%22Item Name already exists%22);
 %0A%09%7D%0A

Update validate() with exception message