EldiraSesto

ManagementServer

Jun 9th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.04 KB | None | 0 0
  1. package managementserver;
  2.  
  3. import warehouse.IWarehouseListener;
  4. import java.util.Collection;
  5. import cashregister.ICashRegister;
  6. //import cashregister.CashRegister;
  7. import cashregister.IObserver;
  8. import tree.ITree;
  9. import rbvs.product.CompositeProduct;
  10. import rbvs.product.IProduct;
  11. import tree.ProductTree;
  12. import container.Container;
  13. import domain.product.JointProduct;
  14. import domain.product.Product;
  15. import tree.node.CategoryTreeNode;
  16. //import tree.node.GenericTreeNode;
  17. import tree.node.ProductCategoryTreeNode;
  18. import rbvs.product.ProductCategory;
  19. import cashregister.NotRegisteredException;
  20. import tree.node.ITreeNode;
  21. import util.searchable.ProductNameFilter;
  22. import tree.node.ProductTreeNode;
  23. //import rbvs.product.CompositeProduct;
  24.  
  25. public class ManagementServer implements IManagementServer, IWarehouseListener, ISubjectManagementServer {
  26.  
  27.     private Collection <ICashRegister> cashRegisters;
  28.     private Collection <IObserver> observer;
  29.     private ITree<IProduct> productAssortment = new ProductTree();
  30.     private static ManagementServer INSTANCE = null;
  31.     private ProductCategoryTreeNode<IProduct> food;
  32.     private ProductCategoryTreeNode<IProduct> beverage;
  33.     private ProductCategoryTreeNode<IProduct> def;
  34.  
  35.     private ManagementServer(){
  36.         cashRegisters = new Container<>();
  37.         observer = new Container<>();
  38.  
  39.         this.food = new ProductCategoryTreeNode<>(ProductCategory.FOOD);
  40.         this.beverage = new ProductCategoryTreeNode<>(ProductCategory.BEVERAGE);
  41.         this.def = new ProductCategoryTreeNode<>(ProductCategory.DEFAULT);
  42.  
  43.         ITreeNode<IProduct> root = new CategoryTreeNode<>("PRODUCTS");
  44.         root.getChildren().add(food);
  45.         root.getChildren().add(beverage);
  46.         root.getChildren().add(def);
  47.         productAssortment.setRoot(root);
  48.     }
  49.    
  50.     public static IManagementServer GET_INSTANCE() {
  51.         if(INSTANCE == null){
  52.             INSTANCE = new ManagementServer();
  53.         }
  54.         return INSTANCE;
  55.     }
  56.    
  57.    
  58.     @Override
  59.     public ITree<IProduct> getChanges() {
  60.         // TODO Auto-generated method stub
  61.         return this.productAssortment.deepCopy();
  62.     }
  63.  
  64.     @Override
  65.     public boolean register(IObserver obs) {
  66.         // TODO Auto-generated method stub
  67.         if(obs == null){
  68.             return false;
  69.         }
  70.         if(observer.contains(obs)){
  71.             obs.activateNotifications(INSTANCE);
  72.             obs.notifyChange(INSTANCE);
  73.         }
  74.         observer.add(obs);
  75.         obs.activateNotifications(INSTANCE);
  76.         obs.notifyChange(INSTANCE);
  77.         return true;
  78.     }
  79.  
  80.     @Override
  81.     public boolean unregister(IObserver obs) {
  82.         // TODO Auto-generated method stub
  83.         if(obs !=null && this.observer.contains(obs)) {
  84.             obs.deactivateNotifications(INSTANCE);
  85.             this.observer.remove(obs);
  86.         }
  87.         return false;
  88.     }
  89.    
  90.    
  91.     /*public void notifyChange(IProduct product) {
  92.         Collection<ITreeNode<IProduct>> temp = this.productAssortment.searchByFilter(new ProductNameFilter(), product);
  93.  
  94.         for(ITreeNode<IProduct> p : temp){
  95.             if(p.nodeValue().getCategory() == product.getCategory()){
  96.                 p.nodeValue().update(product);
  97.             }
  98.             if(p.nodeValue().getCategory() == ProductCategory.FOOD) {
  99.                 if(this.food.getChildren().contains(p)){
  100.                     this.food.removeNodeByNode(p);
  101.                     this.productAdded(product);
  102.                 }
  103.             }
  104.             if(p.nodeValue().getCategory() == ProductCategory.BEVERAGE) {
  105.                 if(this.beverage.getChildren().contains(p)){
  106.                     this.beverage.removeNodeByNode(p);
  107.                     this.productAdded(product);
  108.                 }
  109.             }
  110.             if(p.nodeValue().getCategory() == ProductCategory.DEFAULT) {
  111.                 if(this.def.getChildren().contains(p)){
  112.                     this.def.removeNodeByNode(p);
  113.                     this.productAdded(product);
  114.                 }
  115.             }
  116.             if(product instanceof JointProduct){
  117.                 Collection<Product> jointProds = ((JointProduct) product).getProducts();
  118.                 for(Product prod : jointProds){
  119.                     if(prod.getCategory().equals(ProductCategory.FOOD) ){
  120.                         if(this.food.findNodeByValue((IProduct) prod).equals(null)) {
  121.                             this.productAdded((IProduct) prod);
  122.                         }
  123.                     }
  124.                     if(prod.getCategory().equals(ProductCategory.BEVERAGE)) {
  125.                         if(this.beverage.findNodeByValue((IProduct) prod) == null) {
  126.                             this.productAdded((IProduct) prod);
  127.                         }
  128.                     }
  129.                     if(p.nodeValue().getCategory() == ProductCategory.DEFAULT) {
  130.                         if(this.def.findNodeByValue((IProduct) prod) == null) {
  131.                             this.productAdded((IProduct) prod);
  132.                         }
  133.                     }
  134.                 }
  135.             }
  136.         }
  137.     }*/
  138.  
  139.    
  140.     public void notifyChange(IProduct object) {
  141.  
  142.         Collection<ITreeNode<IProduct>> filteredProducts = this.productAssortment.searchByFilter(new ProductNameFilter(), object);
  143.  
  144.         if(!filteredProducts.isEmpty()) {
  145.            
  146.             // At least one ITreeNode<IProduct> with matching name has been found.
  147.             // First update the price of every product with matching name
  148.            
  149.             for(ITreeNode<IProduct> e : filteredProducts)
  150.                 if(e.nodeValue().getPrice() != object.getPrice())
  151.                     e.nodeValue().setPrice(object.getPrice());
  152.            
  153.             // If names are matching, but the categories are different, delete the existing node
  154.             // and create a new node with the category given in object.
  155.            
  156.             for(ITreeNode<IProduct> e : filteredProducts)
  157.                 if(!e.nodeValue().getCategory().getLabel().equals(object.getCategory().getLabel())) {
  158.                     this.findCategoryNode(object.getCategory()).getChildren().add(new ProductTreeNode(object));
  159.                     if(this.isNodeDirectChildOfCategory(e) == true)
  160.                         this.findCategoryNode(e.nodeValue().getCategory()).getChildren().remove(e);
  161.                 }
  162.            
  163.             // Check if filteredProducts are instances of CompositeProduct
  164.            
  165.             if(object instanceof CompositeProduct)
  166.                
  167.                 // In this part we have to check if one or more IProducts have been added to object.
  168.                 // So we have to check if all contained products of object are contained
  169.                 // as children nodes in the corresponding node. If, on the other hand, an existing IProduct
  170.                 // has been REMOVED from the CompositeProduct, the method does nothing. (Removing products from
  171.                 // CompositeProducts is also not supported in the Warehouse GUI)
  172.                
  173.                 for(ITreeNode<IProduct> e : filteredProducts)
  174.                     // For every product that isn't contained as a tree node, add a new tree node with this product
  175.                     for(IProduct containedProduct : ((CompositeProduct) object).getProducts()) {
  176.                         // Start with the assumption that the product is not contained.
  177.                         boolean productContained = false;
  178.                         // Check if product is contained.
  179.                         for(ITreeNode<IProduct> child : e.getChildren())
  180.                             if(child.nodeValue().equals(containedProduct))
  181.                                 productContained = true;
  182.                         // If product is not contained, add a new ProductTreeNode with this product.
  183.                         if (productContained == false)
  184.                             e.getChildren().add(new ProductTreeNode(containedProduct));
  185.                     }
  186.            
  187.         } else
  188.             // No ITreeNode<IProduct> with matching name. In this case, simply create a new
  189.             // ProductTreeNode and place it in the corresponding category.
  190.             // If object's category reference is null, place it in the DEFAULT category.
  191.             this.findCategoryNode(object.getCategory()).getChildren().add(new ProductTreeNode(object));
  192.        
  193.     }
  194.    
  195.     // Finds and returns the matching ProductCategoryTreeNode.
  196.     // If the passed category is null, DEFAULT is returned
  197.    
  198.     private ITreeNode<IProduct> findCategoryNode(ProductCategory category) {
  199.        
  200.         ITreeNode<IProduct> categoryNode = null;
  201.        
  202.         if (category == null) {
  203.             for(ITreeNode<IProduct> e : this.productAssortment.getRoot().getChildren())
  204.                 if(e.getLabel().equals(ProductCategory.DEFAULT.getLabel()))
  205.                     categoryNode = e;
  206.         } else {
  207.             for(ITreeNode<IProduct> e : this.productAssortment.getRoot().getChildren())
  208.                 if(e.getLabel().equals(category.getLabel()))
  209.                     categoryNode = e;
  210.         }
  211.        
  212.         return categoryNode;
  213.     }
  214.    
  215.     private boolean isNodeDirectChildOfCategory(ITreeNode<IProduct> node) {
  216.        
  217.         for(ITreeNode<IProduct> e : this.productAssortment.getRoot().getChildren())
  218.             if(e.getChildren().contains(node))
  219.                 return true;
  220.        
  221.         return false;  
  222.     }
  223.    
  224.     @Override
  225.     public void productAdded(IProduct product) {
  226.         // TODO Auto-generated method stub
  227.  
  228.         ProductTreeNode newNode = new ProductTreeNode(product);
  229.  
  230.         if(product.getCategory() == ProductCategory.FOOD){
  231.             food.getChildren().add(newNode);
  232.         } else if(product.getCategory() == ProductCategory.BEVERAGE){
  233.             beverage.getChildren().add(newNode);
  234.         } else {
  235.             def.getChildren().add(newNode);
  236.         }
  237.  
  238.     }
  239.  
  240.     @Override
  241.     public void productRemoved(IProduct product) {
  242.         // TODO Auto-generated method stub
  243.  
  244.         if(product != null) {
  245.             productAssortment.removeNode(product);
  246.         }
  247.     }
  248.  
  249.        
  250.  
  251.     @Override
  252.     public void addCashRegister(ICashRegister cashRegister) {
  253.         // TODO Auto-generated method stub
  254.         this.cashRegisters.add(cashRegister);
  255.         if(cashRegister instanceof IObserver) {
  256.             this.observer.add((IObserver) cashRegister);
  257.             ((IObserver) cashRegister).notifyChange(INSTANCE);
  258.         }
  259.     }
  260.  
  261.     @Override
  262.     public void propagateProducts() {
  263.         // TODO Auto-generated method stub
  264.         for(IObserver o : this.observer) {
  265.             o.notifyChange(INSTANCE);
  266.         }
  267.     }
  268.  
  269.     @Override
  270.     public ITree<IProduct> retrieveProductSortiment() {
  271.         // TODO Auto-generated method stub
  272.         if(!(productAssortment instanceof IObserver)) {
  273.             return this.productAssortment.deepCopy();
  274.         }
  275.         return this.getChanges();
  276.     }
  277.  
  278.     @Override
  279.     public ICashRegister retrieveRegisteredCashRegister(Long cashRegisterId) throws NotRegisteredException {
  280.         // TODO Auto-generated method stub
  281.         for(ICashRegister c : this.cashRegisters) {
  282.             if(c.getID() == cashRegisterId) {
  283.                 return c;
  284.             }
  285.         }
  286.         throw new NotRegisteredException("no ICashRegister can be found with the passed id.");
  287.     }
  288.  
  289.     @Override
  290.     public Collection<ICashRegister> retrieveRegisteredCashRegisters() {
  291.         // TODO Auto-generated method stub
  292.         return this.cashRegisters;
  293.     }
  294.  
  295.     @Override
  296.     public void unregisterCashRegister(ICashRegister cashRegister) throws NotRegisteredException {
  297.         // TODO Auto-generated method stub
  298.         if(!this.cashRegisters.contains(cashRegister)) {
  299.             throw new NotRegisteredException("Cash register is not registered!");
  300.         }
  301.         this.cashRegisters.remove(cashRegister);
  302.         if(cashRegister instanceof IObserver) {
  303.             ((IObserver) cashRegister).deactivateNotifications(INSTANCE);
  304.             observer.remove((IObserver) cashRegister);
  305.         }
  306.        
  307.     }
  308.  
  309.  
  310.     }
Advertisement
Add Comment
Please, Sign In to add comment