AbstractGhantChartManagerImpl.java

  1. /*
  2.  * Copyright 2010-2025 James Pether Sörling
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.web.impl.ui.application.views.common.chartfactory.impl;

  20. import java.util.Collection;
  21. import java.util.Collections;
  22. import java.util.Comparator;
  23. import java.util.Date;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Map.Entry;
  27. import java.util.SortedSet;
  28. import java.util.TreeMap;
  29. import java.util.TreeSet;
  30. import java.util.function.Function;
  31. import java.util.stream.Collectors;

  32. import org.joda.time.DateTime;
  33. import org.tltv.gantt.Gantt;
  34. import org.tltv.gantt.client.shared.AbstractStep.CaptionMode;
  35. import org.tltv.gantt.client.shared.Resolution;
  36. import org.tltv.gantt.client.shared.Step;
  37. import org.tltv.gantt.client.shared.SubStep;

  38. import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
  39. import com.vaadin.server.Sizeable.Unit;
  40. import com.vaadin.ui.AbstractOrderedLayout;

  41. /**
  42.  * The Class AbstractGhantChartManagerImpl.
  43.  *
  44.  * @param <T>
  45.  *            the generic type
  46.  */
  47. public abstract class AbstractGhantChartManagerImpl<T extends Object> {

  48.     /** The Constant CONTENT_SEPARATOR. */
  49.     private static final char CONTENT_SEPARATOR = ' ';

  50.     /** The Constant FILTER_DATA_BEFORE_YEAR. */
  51.     private static final int FILTER_DATA_BEFORE_YEAR = 2000;

  52.     /** The Constant PARTY_END_TAG. */
  53.     private static final char PARTY_END_TAG = ')';

  54.     /** The Constant PARTY_START_TAG. */
  55.     private static final String PARTY_START_TAG = " (";

  56.     /**
  57.      * Instantiates a new abstract ghant chart manager impl.
  58.      */
  59.     public AbstractGhantChartManagerImpl() {
  60.         super();
  61.     }

  62.     /**
  63.      * Creates the gantt.
  64.      *
  65.      * @return the gantt
  66.      */
  67.     private static final Gantt createGantt() {
  68.         final Gantt gantt = new Gantt();
  69.         gantt.setSizeFull();
  70.         gantt.setWidth(100, Unit.PERCENTAGE);
  71.         gantt.setHeight(100, Unit.PERCENTAGE);
  72.         gantt.setResizableSteps(false);
  73.         gantt.setMovableSteps(false);
  74.         gantt.setResolution(Resolution.Week);
  75.         return gantt;
  76.     }

  77.     /**
  78.      * Strip dates after current date.
  79.      *
  80.      * @param toDate
  81.      *            the to date
  82.      * @return the date
  83.      */
  84.     private static final Date stripDatesAfterCurrentDate(final Date toDate) {
  85.         return DateUtils.stripDatesAfterCurrentDate(toDate);
  86.     }

  87.     /**
  88.      * Adds the view generic role member to step.
  89.      *
  90.      * @param roleName
  91.      *            the role name
  92.      * @param step
  93.      *            the step
  94.      * @param roleAssignments
  95.      *            the role assignments
  96.      * @param stepMapping
  97.      *            the step mapping
  98.      */
  99.     private void addViewGenericRoleMemberToStep(final String roleName, final Step step, final List<T> roleAssignments,
  100.             final StepMapping<T> stepMapping) {

  101.         for (final T roleMember : roleAssignments) {

  102.             String subStepName = "";

  103.             if (stepMapping.getRoleCode(roleMember) != null) {
  104.                 subStepName = new StringBuilder().append(stepMapping.getFirstName(roleMember))
  105.                         .append(CONTENT_SEPARATOR).append(stepMapping.getLastName(roleMember))
  106.                         .append(PARTY_START_TAG).append(stepMapping.getParty(roleMember)).append(PARTY_END_TAG)
  107.                         .toString();
  108.             }

  109.             final SubStep sameRoleSubStep = new SubStep(roleName + '.' + subStepName,CaptionMode.HTML);
  110.             sameRoleSubStep.setDescription(roleName + '.' + subStepName);
  111.             sameRoleSubStep.setBackgroundColor(stepMapping.getBackgroundColor(roleMember));

  112.             sameRoleSubStep.setStartDate(stepMapping.getFromDate(roleMember).getTime());
  113.             sameRoleSubStep.setEndDate(stripDatesAfterCurrentDate(stepMapping.getToDate(roleMember)).getTime());

  114.             step.addSubStep(sameRoleSubStep);
  115.         }
  116.     }

  117.     /**
  118.      * Creates the generic gantt.
  119.      *
  120.      * @param assignmentList
  121.      *            the assignment list
  122.      * @param roleMapping
  123.      *            the role mapping
  124.      * @param stepMapping
  125.      *            the step mapping
  126.      * @return the gantt
  127.      */
  128.     private Gantt createGenericGantt(final List<T> assignmentList, final Function<T, String> roleMapping,
  129.             final StepMapping<T> stepMapping) {

  130.         final Map<String, List<T>> assignmentListMap = assignmentList.stream()
  131.                 .collect(Collectors.groupingBy(roleMapping, TreeMap::new, Collectors.toList()));

  132.         final Gantt gantt = createGantt();

  133.         if (!assignmentList.isEmpty()) {

  134.             gantt.setStartDate(stepMapping.getFromDate(assignmentList.get(0)));
  135.             gantt.setEndDate(
  136.                     stripDatesAfterCurrentDate(stepMapping.getToDate(assignmentList.get(assignmentList.size() - 1))));

  137.             for (final Entry<String, List<T>> entry : entriesSortedByValues(assignmentListMap, stepMapping)) {

  138.                 final String stepName = entry.getKey();

  139.                 final Step step = new Step(stepName,CaptionMode.HTML);
  140.                 step.setDescription(stepName);

  141.                 final List<T> assignments = entry.getValue();

  142.                 Collections.sort(assignments, getComparator());

  143.                 addViewGenericRoleMemberToStep(stepName, step, assignments, stepMapping);

  144.                 gantt.addStep(step);
  145.             }
  146.         }

  147.         return gantt;
  148.     }


  149.     /**
  150.      * Creates the role ghant.
  151.      *
  152.      * @param roleSummaryLayoutTabsheet the role summary layout tabsheet
  153.      * @param assignmentList            the assignment list
  154.      */
  155.     public final void createRoleGhant(final AbstractOrderedLayout roleSummaryLayoutTabsheet, final Collection<T> assignmentList) {

  156.         final List<T> list = filterAndSortAssignments(assignmentList);

  157.         final Gantt createGantt = createGenericGantt(list, getRoleMapping(), getStepMapping());
  158.         roleSummaryLayoutTabsheet.addComponent(createGantt);
  159.         roleSummaryLayoutTabsheet.setExpandRatio(createGantt, ContentRatio.GRID);

  160.     }

  161.     /**
  162.      * Filter and sort assignments.
  163.      *
  164.      * @param assignmentList
  165.      *            the assignment list
  166.      * @return the list
  167.      */
  168.     private List<T> filterAndSortAssignments(final Collection<T> assignmentList) {
  169.         final Comparator<T> compare = getComparator();

  170.         final List<T> list = assignmentList.stream().filter(
  171.                 (final T x) -> new DateTime(getStepMapping().getFromDate(x).getTime()).getYear() > FILTER_DATA_BEFORE_YEAR)
  172.                 .collect(Collectors.toList());

  173.         Collections.sort(list, compare);
  174.         return list;
  175.     }

  176.     /**
  177.      * Entries sorted by values.
  178.      *
  179.      * @param map
  180.      *            the map
  181.      * @param stepMapping
  182.      *            the step mapping
  183.      * @return the sorted set
  184.      */
  185.     private SortedSet<Map.Entry<String, List<T>>> entriesSortedByValues(final Map<String, List<T>> map,
  186.             final StepMapping<T> stepMapping) {
  187.         final Comparator<? super Entry<String, List<T>>> compare = (o1, o2) -> {

  188.             final Comparator<T> compare1 = (o11, o21) -> {
  189.                 final int compareDate = stepMapping.getFromDate(o11).compareTo(stepMapping.getFromDate(o21));
  190.                 if (compareDate == 0) {
  191.                     final int compareType = stepMapping.getRoleCode(o11).compareTo(stepMapping.getRoleCode(o21));
  192.                     if (compareType == 0) {
  193.                         return stepMapping.getOrg(o11).compareTo(stepMapping.getOrg(o21));
  194.                     } else {
  195.                         return compareType;
  196.                     }
  197.                 }

  198.                 return compareDate;
  199.             };

  200.             Collections.sort(o1.getValue(), compare1);
  201.             Collections.sort(o2.getValue(), compare1);

  202.             return compare1.compare(o1.getValue().get(0), o2.getValue().get(0));
  203.         };

  204.         final SortedSet<Map.Entry<String, List<T>>> sortedEntries = new TreeSet<>(compare);
  205.         sortedEntries.addAll(map.entrySet());
  206.         return sortedEntries;
  207.     }

  208.     /**
  209.      * Gets the comparator.
  210.      *
  211.      * @return the comparator
  212.      */
  213.     protected abstract Comparator<T> getComparator();

  214.     /**
  215.      * Gets the role mapping.
  216.      *
  217.      * @return the role mapping
  218.      */
  219.     protected abstract Function<T, String> getRoleMapping();

  220.     /**
  221.      * Gets the step mapping.
  222.      *
  223.      * @return the step mapping
  224.      */
  225.     protected abstract StepMapping<T> getStepMapping();

  226.     /**
  227.      * The Interface StepMapping.
  228.      *
  229.      * @param <T>
  230.      *            the generic type
  231.      */
  232.     public interface StepMapping<T> {

  233.         /**
  234.          * Gets the background color.
  235.          *
  236.          * @param t
  237.          *            the t
  238.          * @return the background color
  239.          */
  240.         String getBackgroundColor(T t);

  241.         /**
  242.          * Gets the first name.
  243.          *
  244.          * @param assignmentData
  245.          *            the assignment data
  246.          * @return the first name
  247.          */
  248.         Object getFirstName(T assignmentData);

  249.         /**
  250.          * Gets the from date.
  251.          *
  252.          * @param t
  253.          *            the t
  254.          * @return the from date
  255.          */
  256.         Date getFromDate(T t);

  257.         /**
  258.          * Gets the last name.
  259.          *
  260.          * @param assignmentData
  261.          *            the assignment data
  262.          * @return the last name
  263.          */
  264.         Object getLastName(T assignmentData);

  265.         /**
  266.          * Gets the org.
  267.          *
  268.          * @param t
  269.          *            the t
  270.          * @return the org
  271.          */
  272.         String getOrg(T t);

  273.         /**
  274.          * Gets the party.
  275.          *
  276.          * @param t
  277.          *            the t
  278.          * @return the party
  279.          */
  280.         String getParty(T t);

  281.         /**
  282.          * Gets the role code.
  283.          *
  284.          * @param t
  285.          *            the t
  286.          * @return the role code
  287.          */
  288.         String getRoleCode(T t);

  289.         /**
  290.          * Gets the to date.
  291.          *
  292.          * @param t the t
  293.          * @return the to date
  294.          */
  295.         Date getToDate(T t);

  296.     }

  297. }