Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package managementserver;
- import warehouse.IWarehouseListener;
- import java.util.Collection;
- import cashregister.ICashRegister;
- //import cashregister.CashRegister;
- import cashregister.IObserver;
- import tree.ITree;
- import rbvs.product.CompositeProduct;
- import rbvs.product.IProduct;
- import tree.ProductTree;
- import container.Container;
- import domain.product.JointProduct;
- import domain.product.Product;
- import tree.node.CategoryTreeNode;
- //import tree.node.GenericTreeNode;
- import tree.node.ProductCategoryTreeNode;
- import rbvs.product.ProductCategory;
- import cashregister.NotRegisteredException;
- import tree.node.ITreeNode;
- import util.searchable.ProductNameFilter;
- import tree.node.ProductTreeNode;
- //import rbvs.product.CompositeProduct;
- public class ManagementServer implements IManagementServer, IWarehouseListener, ISubjectManagementServer {
- private Collection <ICashRegister> cashRegisters;
- private Collection <IObserver> observer;
- private ITree<IProduct> productAssortment = new ProductTree();
- private static ManagementServer INSTANCE = null;
- private ProductCategoryTreeNode<IProduct> food;
- private ProductCategoryTreeNode<IProduct> beverage;
- private ProductCategoryTreeNode<IProduct> def;
- private ManagementServer(){
- cashRegisters = new Container<>();
- observer = new Container<>();
- this.food = new ProductCategoryTreeNode<>(ProductCategory.FOOD);
- this.beverage = new ProductCategoryTreeNode<>(ProductCategory.BEVERAGE);
- this.def = new ProductCategoryTreeNode<>(ProductCategory.DEFAULT);
- ITreeNode<IProduct> root = new CategoryTreeNode<>("PRODUCTS");
- root.getChildren().add(food);
- root.getChildren().add(beverage);
- root.getChildren().add(def);
- productAssortment.setRoot(root);
- }
- public static IManagementServer GET_INSTANCE() {
- if(INSTANCE == null){
- INSTANCE = new ManagementServer();
- }
- return INSTANCE;
- }
- @Override
- public ITree<IProduct> getChanges() {
- // TODO Auto-generated method stub
- return this.productAssortment.deepCopy();
- }
- @Override
- public boolean register(IObserver obs) {
- // TODO Auto-generated method stub
- if(obs == null){
- return false;
- }
- if(observer.contains(obs)){
- obs.activateNotifications(INSTANCE);
- obs.notifyChange(INSTANCE);
- }
- observer.add(obs);
- obs.activateNotifications(INSTANCE);
- obs.notifyChange(INSTANCE);
- return true;
- }
- @Override
- public boolean unregister(IObserver obs) {
- // TODO Auto-generated method stub
- if(obs !=null && this.observer.contains(obs)) {
- obs.deactivateNotifications(INSTANCE);
- this.observer.remove(obs);
- }
- return false;
- }
- /*public void notifyChange(IProduct product) {
- Collection<ITreeNode<IProduct>> temp = this.productAssortment.searchByFilter(new ProductNameFilter(), product);
- for(ITreeNode<IProduct> p : temp){
- if(p.nodeValue().getCategory() == product.getCategory()){
- p.nodeValue().update(product);
- }
- if(p.nodeValue().getCategory() == ProductCategory.FOOD) {
- if(this.food.getChildren().contains(p)){
- this.food.removeNodeByNode(p);
- this.productAdded(product);
- }
- }
- if(p.nodeValue().getCategory() == ProductCategory.BEVERAGE) {
- if(this.beverage.getChildren().contains(p)){
- this.beverage.removeNodeByNode(p);
- this.productAdded(product);
- }
- }
- if(p.nodeValue().getCategory() == ProductCategory.DEFAULT) {
- if(this.def.getChildren().contains(p)){
- this.def.removeNodeByNode(p);
- this.productAdded(product);
- }
- }
- if(product instanceof JointProduct){
- Collection<Product> jointProds = ((JointProduct) product).getProducts();
- for(Product prod : jointProds){
- if(prod.getCategory().equals(ProductCategory.FOOD) ){
- if(this.food.findNodeByValue((IProduct) prod).equals(null)) {
- this.productAdded((IProduct) prod);
- }
- }
- if(prod.getCategory().equals(ProductCategory.BEVERAGE)) {
- if(this.beverage.findNodeByValue((IProduct) prod) == null) {
- this.productAdded((IProduct) prod);
- }
- }
- if(p.nodeValue().getCategory() == ProductCategory.DEFAULT) {
- if(this.def.findNodeByValue((IProduct) prod) == null) {
- this.productAdded((IProduct) prod);
- }
- }
- }
- }
- }
- }*/
- public void notifyChange(IProduct object) {
- Collection<ITreeNode<IProduct>> filteredProducts = this.productAssortment.searchByFilter(new ProductNameFilter(), object);
- if(!filteredProducts.isEmpty()) {
- // At least one ITreeNode<IProduct> with matching name has been found.
- // First update the price of every product with matching name
- for(ITreeNode<IProduct> e : filteredProducts)
- if(e.nodeValue().getPrice() != object.getPrice())
- e.nodeValue().setPrice(object.getPrice());
- // If names are matching, but the categories are different, delete the existing node
- // and create a new node with the category given in object.
- for(ITreeNode<IProduct> e : filteredProducts)
- if(!e.nodeValue().getCategory().getLabel().equals(object.getCategory().getLabel())) {
- this.findCategoryNode(object.getCategory()).getChildren().add(new ProductTreeNode(object));
- if(this.isNodeDirectChildOfCategory(e) == true)
- this.findCategoryNode(e.nodeValue().getCategory()).getChildren().remove(e);
- }
- // Check if filteredProducts are instances of CompositeProduct
- if(object instanceof CompositeProduct)
- // In this part we have to check if one or more IProducts have been added to object.
- // So we have to check if all contained products of object are contained
- // as children nodes in the corresponding node. If, on the other hand, an existing IProduct
- // has been REMOVED from the CompositeProduct, the method does nothing. (Removing products from
- // CompositeProducts is also not supported in the Warehouse GUI)
- for(ITreeNode<IProduct> e : filteredProducts)
- // For every product that isn't contained as a tree node, add a new tree node with this product
- for(IProduct containedProduct : ((CompositeProduct) object).getProducts()) {
- // Start with the assumption that the product is not contained.
- boolean productContained = false;
- // Check if product is contained.
- for(ITreeNode<IProduct> child : e.getChildren())
- if(child.nodeValue().equals(containedProduct))
- productContained = true;
- // If product is not contained, add a new ProductTreeNode with this product.
- if (productContained == false)
- e.getChildren().add(new ProductTreeNode(containedProduct));
- }
- } else
- // No ITreeNode<IProduct> with matching name. In this case, simply create a new
- // ProductTreeNode and place it in the corresponding category.
- // If object's category reference is null, place it in the DEFAULT category.
- this.findCategoryNode(object.getCategory()).getChildren().add(new ProductTreeNode(object));
- }
- // Finds and returns the matching ProductCategoryTreeNode.
- // If the passed category is null, DEFAULT is returned
- private ITreeNode<IProduct> findCategoryNode(ProductCategory category) {
- ITreeNode<IProduct> categoryNode = null;
- if (category == null) {
- for(ITreeNode<IProduct> e : this.productAssortment.getRoot().getChildren())
- if(e.getLabel().equals(ProductCategory.DEFAULT.getLabel()))
- categoryNode = e;
- } else {
- for(ITreeNode<IProduct> e : this.productAssortment.getRoot().getChildren())
- if(e.getLabel().equals(category.getLabel()))
- categoryNode = e;
- }
- return categoryNode;
- }
- private boolean isNodeDirectChildOfCategory(ITreeNode<IProduct> node) {
- for(ITreeNode<IProduct> e : this.productAssortment.getRoot().getChildren())
- if(e.getChildren().contains(node))
- return true;
- return false;
- }
- @Override
- public void productAdded(IProduct product) {
- // TODO Auto-generated method stub
- ProductTreeNode newNode = new ProductTreeNode(product);
- if(product.getCategory() == ProductCategory.FOOD){
- food.getChildren().add(newNode);
- } else if(product.getCategory() == ProductCategory.BEVERAGE){
- beverage.getChildren().add(newNode);
- } else {
- def.getChildren().add(newNode);
- }
- }
- @Override
- public void productRemoved(IProduct product) {
- // TODO Auto-generated method stub
- if(product != null) {
- productAssortment.removeNode(product);
- }
- }
- @Override
- public void addCashRegister(ICashRegister cashRegister) {
- // TODO Auto-generated method stub
- this.cashRegisters.add(cashRegister);
- if(cashRegister instanceof IObserver) {
- this.observer.add((IObserver) cashRegister);
- ((IObserver) cashRegister).notifyChange(INSTANCE);
- }
- }
- @Override
- public void propagateProducts() {
- // TODO Auto-generated method stub
- for(IObserver o : this.observer) {
- o.notifyChange(INSTANCE);
- }
- }
- @Override
- public ITree<IProduct> retrieveProductSortiment() {
- // TODO Auto-generated method stub
- if(!(productAssortment instanceof IObserver)) {
- return this.productAssortment.deepCopy();
- }
- return this.getChanges();
- }
- @Override
- public ICashRegister retrieveRegisteredCashRegister(Long cashRegisterId) throws NotRegisteredException {
- // TODO Auto-generated method stub
- for(ICashRegister c : this.cashRegisters) {
- if(c.getID() == cashRegisterId) {
- return c;
- }
- }
- throw new NotRegisteredException("no ICashRegister can be found with the passed id.");
- }
- @Override
- public Collection<ICashRegister> retrieveRegisteredCashRegisters() {
- // TODO Auto-generated method stub
- return this.cashRegisters;
- }
- @Override
- public void unregisterCashRegister(ICashRegister cashRegister) throws NotRegisteredException {
- // TODO Auto-generated method stub
- if(!this.cashRegisters.contains(cashRegister)) {
- throw new NotRegisteredException("Cash register is not registered!");
- }
- this.cashRegisters.remove(cashRegister);
- if(cashRegister instanceof IObserver) {
- ((IObserver) cashRegister).deactivateNotifications(INSTANCE);
- observer.remove((IObserver) cashRegister);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment