content: /*
   * Copyright (c) 2020 Adam Gaj
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     https://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.adgadev.jplusone.test.domain.commerce;
  
  import lombok.AccessLevel;
  import lombok.AllArgsConstructor;
  import lombok.EqualsAndHashCode;
  import lombok.Getter;
  import lombok.NoArgsConstructor;
  
  import jakarta.persistence.CascadeType;
  import jakarta.persistence.Entity;
  import jakarta.persistence.FetchType;
  import jakarta.persistence.Id;
  import jakarta.persistence.OneToMany;
  import java.util.HashSet;
  import java.util.Random;
  import java.util.Set;
  
  @Getter
  @Entity
  @EqualsAndHashCode(onlyExplicitlyIncluded = true)
  @NoArgsConstructor(access = AccessLevel.PACKAGE) // hibernate requires package access
  @AllArgsConstructor(access = AccessLevel.PRIVATE)
  public class Manufacturer {
  
      @Id
      @EqualsAndHashCode.Include
      private Long id;
  
      private String name;
  
      @OneToMany(mappedBy = "manufacturer", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
      private Set<Product> products = new HashSet<>();
  
      public static Manufacturer of(Long id, String name) {
          return new Manufacturer(id, name, new HashSet<>());
      }
  
      public void updateName(String newName) {
          this.name = newName;
      }
  
      public Product addProduct(String productName) {
          Product product = Product.of(new Random().nextLong() % 1000000, productName, this);
          products.add(product);
          return product;
      }
  
      public Product deleteProduct(String productName) {
          Product product = findProductByName(productName);
          products.remove(product);
          return product;
      }
  
      public Product findProductByName(String productName) {
          return products.stream()
                  .filter(product -> product.getName().equals(productName))
                  .findFirst()
                  .get();
      }
  }
  