AbstractGhantChartManagerImpl.java

  1. /*
  2.  * Copyright 2014 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.Collections;
  21. import java.util.Comparator;
  22. import java.util.Date;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Map.Entry;
  26. import java.util.SortedSet;
  27. import java.util.TreeMap;
  28. import java.util.TreeSet;
  29. import java.util.function.Function;
  30. import java.util.stream.Collectors;

  31. import org.joda.time.DateTime;
  32. import org.tltv.gantt.Gantt;
  33. import org.tltv.gantt.client.shared.Resolution;
  34. import org.tltv.gantt.client.shared.Step;
  35. import org.tltv.gantt.client.shared.SubStep;

  36. import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
  37. import com.vaadin.server.Sizeable.Unit;
  38. import com.vaadin.ui.VerticalLayout;

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

  46.     /** The Constant PARTY_END_TAG. */
  47.     private static final char PARTY_END_TAG = ')';

  48.     /** The Constant PARTY_START_TAG. */
  49.     private static final String PARTY_START_TAG = " (";

  50.     /** The Constant CONTENT_SEPARATOR. */
  51.     private static final char CONTENT_SEPARATOR = ' ';

  52.     /** The Constant FILTER_DATA_BEFORE_YEAR. */
  53.     private static final int FILTER_DATA_BEFORE_YEAR = 2000;

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

  60.     /**
  61.      * Creates the role ghant.
  62.      *
  63.      * @param roleSummaryLayoutTabsheet
  64.      *            the role summary layout tabsheet
  65.      * @param assignmentList
  66.      *            the assignment list
  67.      */
  68.     public final void createRoleGhant(final VerticalLayout roleSummaryLayoutTabsheet, final List<T> assignmentList) {

  69.         final Comparator<T> compare = getComparator();

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

  73.         Collections.sort(list, compare);

  74.         final Gantt createGantt = createGenericGantt(list, getRoleMapping(), getStepMapping());
  75.         roleSummaryLayoutTabsheet.addComponent(createGantt);
  76.         roleSummaryLayoutTabsheet.setExpandRatio(createGantt, ContentRatio.GRID);

  77.     }

  78.     /**
  79.      * Gets the comparator.
  80.      *
  81.      * @return the comparator
  82.      */
  83.     protected abstract Comparator<T> getComparator();

  84.     /**
  85.      * Gets the role mapping.
  86.      *
  87.      * @return the role mapping
  88.      */
  89.     protected abstract Function<T, String> getRoleMapping();

  90.     /**
  91.      * Gets the step mapping.
  92.      *
  93.      * @return the step mapping
  94.      */
  95.     protected abstract StepMapping<T> getStepMapping();

  96.     /**
  97.      * Creates the generic gantt.
  98.      *
  99.      * @param assignmentList
  100.      *            the assignment list
  101.      * @param roleMapping
  102.      *            the role mapping
  103.      * @param stepMapping
  104.      *            the step mapping
  105.      * @return the gantt
  106.      */
  107.     private Gantt createGenericGantt(final List<T> assignmentList, final Function<T, String> roleMapping,
  108.             final StepMapping<T> stepMapping) {

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

  111.         final Gantt gantt = createGantt();

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

  113.             gantt.setStartDate(stepMapping.getFromDate(assignmentList.get(0)));
  114.             gantt.setEndDate(
  115.                     stripDatesAfterCurrentDate(stepMapping.getToDate(assignmentList.get(assignmentList.size() - 1))));

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

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

  118.                 final Step step = new Step();
  119.                 step.setDescription(stepName);

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

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

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

  123.                 gantt.addStep(step);
  124.             }
  125.         }

  126.         return gantt;
  127.     }

  128.     /**
  129.      * Entries sorted by values.
  130.      *
  131.      * @param map
  132.      *            the map
  133.      * @param stepMapping
  134.      *            the step mapping
  135.      * @return the sorted set
  136.      */
  137.     private SortedSet<Map.Entry<String, List<T>>> entriesSortedByValues(final Map<String, List<T>> map,
  138.             final StepMapping<T> stepMapping) {
  139.         final Comparator<? super Entry<String, List<T>>> compare = (o1, o2) -> {

  140.             final Comparator<T> compare1 = (o11, o21) -> {
  141.                 final int compareDate = stepMapping.getFromDate(o11).compareTo(stepMapping.getFromDate(o21));
  142.                 if (compareDate == 0) {
  143.                     final int compareType = stepMapping.getRoleCode(o11).compareTo(stepMapping.getRoleCode(o21));
  144.                     if (compareType == 0) {
  145.                         return stepMapping.getOrg(o11).compareTo(stepMapping.getOrg(o21));
  146.                     } else {
  147.                         return compareType;
  148.                     }
  149.                 }

  150.                 return compareDate;
  151.             };

  152.             Collections.sort(o1.getValue(), compare1);
  153.             Collections.sort(o2.getValue(), compare1);

  154.             return compare1.compare(o1.getValue().get(0), o2.getValue().get(0));
  155.         };

  156.         final SortedSet<Map.Entry<String, List<T>>> sortedEntries = new TreeSet<>(compare);
  157.         sortedEntries.addAll(map.entrySet());
  158.         return sortedEntries;
  159.     }

  160.     /**
  161.      * Adds the view generic role member to step.
  162.      *
  163.      * @param stepName
  164.      *            the step name
  165.      * @param step
  166.      *            the step
  167.      * @param assignments
  168.      *            the assignments
  169.      * @param stepMapping
  170.      *            the step mapping
  171.      */
  172.     private void addViewGenericRoleMemberToStep(final String stepName, final Step step, final List<T> assignments,
  173.             final StepMapping<T> stepMapping) {

  174.         for (final T assignmentData : assignments) {

  175.             String subStepName = "";

  176.             if (stepMapping.getRoleCode(assignmentData) != null) {
  177.                 subStepName = new StringBuilder().append(stepMapping.getFirstName(assignmentData))
  178.                         .append(CONTENT_SEPARATOR).append(stepMapping.getLastName(assignmentData))
  179.                         .append(PARTY_START_TAG).append(stepMapping.getParty(assignmentData)).append(PARTY_END_TAG)
  180.                         .toString();
  181.             }

  182.             final SubStep sameRoleSubStep = new SubStep(stepName + '.' + subStepName);

  183.             sameRoleSubStep.setBackgroundColor(stepMapping.getBackgroundColor(assignmentData));

  184.             sameRoleSubStep.setStartDate(stepMapping.getFromDate(assignmentData).getTime());
  185.             sameRoleSubStep.setEndDate(stripDatesAfterCurrentDate(stepMapping.getToDate(assignmentData)).getTime());

  186.             step.addSubStep(sameRoleSubStep);
  187.         }
  188.     }

  189.     /**
  190.      * Strip dates after current date.
  191.      *
  192.      * @param toDate
  193.      *            the to date
  194.      * @return the date
  195.      */
  196.     private static final Date stripDatesAfterCurrentDate(final Date toDate) {
  197.         final DateTime currentTime = new DateTime();

  198.         if (currentTime.isBefore(toDate.getTime())) {
  199.             return currentTime.plusDays(1).toDate();
  200.         } else {
  201.             return toDate;
  202.         }
  203.     }

  204.     /**
  205.      * Creates the gantt.
  206.      *
  207.      * @return the gantt
  208.      */
  209.     private static final Gantt createGantt() {
  210.         final Gantt gantt = new Gantt();
  211.         gantt.setSizeFull();
  212.         gantt.setWidth(100, Unit.PERCENTAGE);
  213.         gantt.setHeight(100, Unit.PERCENTAGE);
  214.         gantt.setResizableSteps(false);
  215.         gantt.setMovableSteps(false);
  216.         gantt.setResolution(Resolution.Week);
  217.         return gantt;
  218.     }

  219.     /**
  220.      * The Interface StepMapping.
  221.      *
  222.      * @param <T>
  223.      *            the generic type
  224.      */
  225.     public interface StepMapping<T> {

  226.         /**
  227.          * Gets the from date.
  228.          *
  229.          * @param t
  230.          *            the t
  231.          * @return the from date
  232.          */
  233.         Date getFromDate(T t);

  234.         /**
  235.          * Gets the to date.
  236.          *
  237.          * @param t
  238.          *            the t
  239.          * @return the to date
  240.          */
  241.         Date getToDate(T t);

  242.         /**
  243.          * Gets the role code.
  244.          *
  245.          * @param t
  246.          *            the t
  247.          * @return the role code
  248.          */
  249.         String getRoleCode(T t);

  250.         /**
  251.          * Gets the org.
  252.          *
  253.          * @param t
  254.          *            the t
  255.          * @return the org
  256.          */
  257.         String getOrg(T t);

  258.         /**
  259.          * Gets the party.
  260.          *
  261.          * @param t
  262.          *            the t
  263.          * @return the party
  264.          */
  265.         String getParty(T t);

  266.         /**
  267.          * Gets the background color.
  268.          *
  269.          * @param t
  270.          *            the t
  271.          * @return the background color
  272.          */
  273.         String getBackgroundColor(T t);

  274.         /**
  275.          * Gets the first name.
  276.          *
  277.          * @param assignmentData
  278.          *            the assignment data
  279.          * @return the first name
  280.          */
  281.         Object getFirstName(T assignmentData);

  282.         /**
  283.          * Gets the last name.
  284.          *
  285.          * @param assignmentData
  286.          *            the assignment data
  287.          * @return the last name
  288.          */
  289.         Object getLastName(T assignmentData);

  290.     }

  291. }