SHARE
TWEET

Enum parent flow

a guest Nov 12th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.tamkeen.shared.model.enums;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5. import java.util.stream.Stream;
  6.  
  7. import static com.tamkeen.shared.model.enums.PartnershipContractStatuses.Flow.EPRO_CANCEL_FLOW;
  8. import static com.tamkeen.shared.model.enums.PartnershipContractStatuses.Flow.PRO_CANCEL_FLOW;
  9. import static com.tamkeen.shared.model.enums.PartnershipContractStatuses.Flow.CANCEL_NEW_FLOW;
  10. import static com.tamkeen.shared.model.enums.PartnershipContractStatuses.Flow.CANCEL_APPROVAL_PENDING_FLOW;
  11.  
  12. public enum PartnershipContractStatuses {
  13.     PROLONGED(0),
  14.     NEW(1),
  15.     APPROVAL_PENDING(2),
  16.     APPROVED(3),
  17.     REJECTED(3, true),
  18.     CANCELLATION_PENDING(4),
  19.     CANCELED(5),
  20.     CANCELLATION_REJECTED(5, true),
  21.  
  22.     EPRO_CANCELLATION_PENDING(CANCELLATION_PENDING, EPRO_CANCEL_FLOW, UserRoles.EPRO),
  23.     PRO_CANCELED(CANCELED, EPRO_CANCEL_FLOW, UserRoles.PRO_OFFICE, UserRoles.PRO_COMPANY),
  24.     PRO_CANCELLATION_REJECTED(CANCELLATION_REJECTED, EPRO_CANCEL_FLOW, UserRoles.PRO_OFFICE, UserRoles.PRO_COMPANY),
  25.  
  26.     PRO_CANCELLATION_PENDING(CANCELLATION_PENDING, PRO_CANCEL_FLOW, UserRoles.PRO_OFFICE, UserRoles.PRO_COMPANY),
  27.     EPRO_CANCELED(CANCELED, PRO_CANCEL_FLOW, UserRoles.EPRO),
  28.     EPRO_CANCELLATION_REJECTED(CANCELLATION_REJECTED, PRO_CANCEL_FLOW, UserRoles.EPRO),
  29.  
  30.     CANCEL_NEW(NEW, 2, CANCEL_NEW_FLOW, UserRoles.PRO_OFFICE, UserRoles.PRO_COMPANY),
  31.     CANCEL_APPROVAL_PENDING(APPROVAL_PENDING, 3, CANCEL_APPROVAL_PENDING_FLOW, UserRoles.PRO_OFFICE, UserRoles.PRO_COMPANY);
  32.  
  33.     private Flow flow;
  34.     private int order;
  35.     private boolean terminal = false;
  36.     private PartnershipContractStatuses parent;
  37.     private List<UserRoles> supportedRoles = new ArrayList<>();
  38.  
  39.     private static List<PartnershipContractStatuses> values;
  40.  
  41.     PartnershipContractStatuses(int order) {
  42.         this(order, false);
  43.     }
  44.  
  45.     PartnershipContractStatuses(int order, boolean isTerminal) {
  46.         this.order = order;
  47.         this.terminal = isTerminal;
  48.     }
  49.  
  50.     PartnershipContractStatuses(PartnershipContractStatuses parent, Flow flow, UserRoles... roles) {
  51.         this(parent, null, flow, roles);
  52.     }
  53.  
  54.     PartnershipContractStatuses(PartnershipContractStatuses parent, Integer order, Flow flow, UserRoles... roles) {
  55.         this(order != null ? order.intValue() : parent.order, parent.terminal);
  56.         this.parent = parent;
  57.         this.flow = flow;
  58.         this.supportedRoles = Arrays.asList(roles);
  59.     }
  60.  
  61.     public PartnershipContractStatuses getParent() {
  62.         return Optional.ofNullable(this.parent).orElse(this);
  63.     }
  64.  
  65.     public boolean isNext(PartnershipContractStatuses newSt) {
  66.         return !this.terminal && newSt.order - this.order == 1 && (Objects.isNull(this.flow) || this.flow == newSt.flow);
  67.     }
  68.  
  69.     public PartnershipContractStatuses getNext() {
  70.         return getValueByFilter(this.order + 1, false, this.flow).orElse(this);
  71.     }
  72.  
  73.     private static Optional<PartnershipContractStatuses> getValueByFilter(int order, boolean rejected, Flow flow) {
  74.         return Stream.of(PartnershipContractStatuses.values())
  75.                 .filter(x -> x.order == order && x.terminal == rejected && x.flow == flow)
  76.                 .findAny();
  77.     }
  78.  
  79.     public static Optional<PartnershipContractStatuses> getValueByParentAndRole(PartnershipContractStatuses parent, UserRoles role) {
  80.         return Stream.of(PartnershipContractStatuses.values())
  81.                 .filter(x -> x.order == parent.order && x.terminal == parent.terminal && x.supportedRoles.contains(role))
  82.                 .findAny();
  83.     }
  84.  
  85.     public static Set<PartnershipContractStatuses> getChildStatusesIfExist(PartnershipContractStatuses statusCheck) {
  86.         return Stream.of(PartnershipContractStatuses.values())
  87.             .filter(x -> x.getParent().name().equals(statusCheck.name()))
  88.             .collect(Collectors.toSet());
  89.     }
  90.  
  91.     public static Set<PartnershipContractStatuses> getExtendedStatuses(Set<PartnershipContractStatuses> statusesSet) {
  92.         Set<PartnershipContractStatuses> resultSet = new HashSet<>();
  93.         statusesSet.forEach(x -> {
  94.             if (Objects.isNull(x.parent)) {
  95.                 resultSet.addAll(getChildStatusesIfExist(x));
  96.             } else {
  97.                 resultSet.add(x);
  98.             }
  99.         });
  100.  
  101.         return resultSet;
  102.     }
  103.  
  104.  
  105.     enum Flow {
  106.         EPRO_CANCEL_FLOW,
  107.         PRO_CANCEL_FLOW,
  108.         CANCEL_NEW_FLOW,
  109.         CANCEL_APPROVAL_PENDING_FLOW;
  110.     }
  111.  
  112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top