Skip to content

Commit

Permalink
#157: Add project names to Activities and adapt features
Browse files Browse the repository at this point in the history
Signed-off-by: Lars <[email protected]>
  • Loading branch information
Lars committed May 1, 2019
1 parent dd4f76e commit 4c4c02e
Show file tree
Hide file tree
Showing 25 changed files with 642 additions and 481 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
*/
package de.lgblaumeiser.ptm.analysis.analyzer;

import static de.lgblaumeiser.ptm.util.Utils.emptyString;
import static de.lgblaumeiser.ptm.util.Utils.getIndexFromCollection;
import static java.util.Arrays.asList;

import java.util.Collection;
import static java.lang.String.format;

import de.lgblaumeiser.ptm.datamanager.model.Activity;
import de.lgblaumeiser.ptm.datamanager.model.Booking;
Expand All @@ -30,31 +26,27 @@ protected String indexGetter(final Activity activity) {
}

@Override
protected Collection<String> getHeadlineActivityElements() {
return asList("Activity", "Project Id", "Activity Id");
protected String getHeadlineNameElement() {
return "Activity";
}

@Override
protected Collection<String> getFootlineActivityElements() {
return asList("Total", emptyString(), emptyString());
protected String getHeadlineIdElement() {
return "Activity Id";
}

@Override
protected Collection<String> getKeyItems(final Activity activity) {
return asList(activity.getActivityName(), activity.getProjectId(), activity.getActivityId());
protected String getElementName(final Activity activity) {
return formatActivityString(activity.getProjectName(), activity.getActivityName());
}

@Override
protected String getSortCriteriaForResultLine(final Collection<String> line) {
return getProjectIdFromResultLine(line) + "_" + getProjectSubidFromResultLine(line);
}

private String getProjectIdFromResultLine(final Collection<String> line) {
return getIndexFromCollection(line, 1);
protected String getElementId(final Activity activity) {
return formatActivityString(activity.getProjectId(), activity.getActivityId());
}

private String getProjectSubidFromResultLine(final Collection<String> line) {
return getIndexFromCollection(line, 2);
private String formatActivityString(final String part1, final String part2) {
return format("%s:%s", part1, part2);
}

public ActivityComputer(final ObjectStore<Booking> bStore, final ObjectStore<Activity> aStore) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static de.lgblaumeiser.ptm.analysis.analyzer.DateFormatterUtil.formatDuration;
import static de.lgblaumeiser.ptm.analysis.analyzer.DateFormatterUtil.formatPercentageString;
import static de.lgblaumeiser.ptm.util.Utils.emptyString;
import static de.lgblaumeiser.ptm.util.Utils.getIndexFromCollection;
import static de.lgblaumeiser.ptm.util.Utils.stringHasContent;
import static java.util.stream.Collectors.toList;

Expand Down Expand Up @@ -43,165 +44,177 @@ public abstract class BaseProjectComputer implements Analysis {
private final ObjectStore<Activity> activityStore;

private static class AnalysisData {
private Map<String, Activity> keyToActivityMap = new HashMap<>();
private Map<String, Duration> keyToMinutesMap = new HashMap<>();
private Map<String, String> keyToCommentMap = new HashMap<>();

void setActivityData(final String key, final Activity activity, final Duration currentLength,
final String comment) {
keyToActivityMap.put(key, activity);
calculateTimeForActivity(key, currentLength);
calculateAccumulatedComment(key, comment);
}

private void calculateAccumulatedComment(final String key, final String newComment) {
String currentComment = getCurrentComment(key);
private Map<String, Activity> keyToActivityMap = new HashMap<>();
private Map<String, Duration> keyToMinutesMap = new HashMap<>();
private Map<String, String> keyToCommentMap = new HashMap<>();

void setActivityData(final String key, final Activity activity, final Duration currentLength,
final String comment) {
keyToActivityMap.put(key, activity);
calculateTimeForActivity(key, currentLength);
calculateAccumulatedComment(key, comment);
}

private void calculateAccumulatedComment(final String key, final String newComment) {
String currentComment = getCurrentComment(key);
if (stringHasContent(newComment) && !currentComment.contains(newComment)) {
currentComment = (stringHasContent(currentComment)
? currentComment + ", "
: emptyString())
+ newComment;
}
keyToCommentMap.put(key, currentComment);
}
}
keyToCommentMap.put(key, currentComment);
}

private String getCurrentComment(final String key) {
return Optional.ofNullable(keyToCommentMap.get(key)).orElse(emptyString());
}
private String getCurrentComment(final String key) {
return Optional.ofNullable(keyToCommentMap.get(key)).orElse(emptyString());
}

private void calculateTimeForActivity(final String key, final Duration currentLength) {
keyToMinutesMap.put(key, getMinutesForKey(key).plus(currentLength));
}
private void calculateTimeForActivity(final String key, final Duration currentLength) {
keyToMinutesMap.put(key, getMinutesForKey(key).plus(currentLength));
}

private Duration getMinutesForKey(final String key) {
return Optional.ofNullable(keyToMinutesMap.get(key)).orElse(Duration.ZERO);
}
private Duration getMinutesForKey(final String key) {
return Optional.ofNullable(keyToMinutesMap.get(key)).orElse(Duration.ZERO);
}

Collection<String> getKeys() {
return keyToActivityMap.keySet();
}
Collection<String> getKeys() {
return keyToActivityMap.keySet();
}

Activity getActivityForKey(final String key) {
return keyToActivityMap.get(key);
}
Activity getActivityForKey(final String key) {
return keyToActivityMap.get(key);
}

Duration getBookedMinutesForKey(final String key) {
return keyToMinutesMap.get(key);
}
Duration getBookedMinutesForKey(final String key) {
return keyToMinutesMap.get(key);
}

String getAccumulatedCommentForKey(final String key) {
return keyToCommentMap.get(key);
}
String getAccumulatedCommentForKey(final String key) {
return keyToCommentMap.get(key);
}
}

@Override
public Collection<Collection<String>> analyze(final CalculationPeriod period, final String user) {
AnalysisData currentAnalysis = new AnalysisData();
AnalysisData currentAnalysis = new AnalysisData();

return createResultCollection(
currentAnalysis,
calculateTimeMapping(getBookingsForPeriod(period, user), currentAnalysis),
period.isDayPeriod());
return createResultCollection(
currentAnalysis,
calculateTimeMapping(getBookingsForPeriod(period, user), currentAnalysis),
period.isDayPeriod());
}

private Collection<Booking> getBookingsForPeriod(final CalculationPeriod period, final String user) {
return bookingStore
.retrieveAll()
.stream()
.filter(b -> b.getUser().equals(user) && period.isInPeriod(b.getBookingday()))
.collect(toList());
return bookingStore
.retrieveAll()
.stream()
.filter(b -> b.getUser().equals(user) && period.isInPeriod(b.getBookingday()))
.collect(toList());
}

private Duration calculateTimeMapping(final Collection<Booking> bookings, final AnalysisData currentAnalysis) {
Duration totalMinutes = Duration.ZERO;
for (Booking booking : bookings) {
if (booking.hasEndtime()) {
Duration totalMinutes = Duration.ZERO;
for (Booking booking : bookings) {
if (booking.hasEndtime()) {
totalMinutes = totalMinutes.plus(calculateBooking(booking, currentAnalysis));
}
}
return totalMinutes;
}
}
return totalMinutes;
}

private Duration calculateBooking(final Booking booking, final AnalysisData currentAnalysis) {
Activity activity = retrieveActivityForBooking(booking);
Duration activityLength = calculateActivityLengthForBooking(booking);
currentAnalysis.setActivityData(indexGetter(activity), activity, activityLength, booking.getComment());
return activityLength;
Activity activity = retrieveActivityForBooking(booking);
Duration activityLength = calculateActivityLengthForBooking(booking);
currentAnalysis.setActivityData(indexGetter(activity), activity, activityLength, booking.getComment());
return activityLength;
}

private Activity retrieveActivityForBooking(final Booking booking) {
return activityStore.retrieveById(booking.getActivity()).orElseThrow(IllegalStateException::new);
return activityStore.retrieveById(booking.getActivity()).orElseThrow(IllegalStateException::new);
}

private Duration calculateActivityLengthForBooking(final Booking booking) {
return TimeSpan.newTimeSpan(booking).getLengthInMinutes();
return TimeSpan.newTimeSpan(booking).getLengthInMinutes();
}

protected abstract String indexGetter(final Activity activity);

private Collection<Collection<String>> createResultCollection(final AnalysisData currentAnalysis,
final Duration totalMinutes, final boolean withComments) {
Collection<Collection<String>> result = new ArrayList<>();
result.add(createLine(getHeadlineActivityElements(), "Hours", "%", "Comments", withComments));
result.addAll(computeResultLines(currentAnalysis, totalMinutes, withComments));
result.add(createLine(getFootlineActivityElements(), formatDuration(totalMinutes), "100.0%", emptyString(), withComments));
return result;
final Duration totalMinutes, final boolean withComments) {
Collection<Collection<String>> result = new ArrayList<>();
result.add(
createLine(getHeadlineNameElement(), getHeadlineIdElement(), "Hours", "%", "Comments", withComments));
result.addAll(computeResultLines(currentAnalysis, totalMinutes, withComments));
result.add(createLine("Total", emptyString(), formatDuration(totalMinutes), "100.0%", emptyString(),
withComments));
return result;
}

protected abstract Collection<String> getHeadlineActivityElements();
protected abstract String getHeadlineNameElement();

protected abstract Collection<String> getFootlineActivityElements();
protected abstract String getHeadlineIdElement();

private Collection<Collection<String>> computeResultLines(final AnalysisData currentAnalysis,
final Duration totalMinutes, final boolean withComments) {
Collection<Collection<String>> valueList = new ArrayList<>();
for (String key : currentAnalysis.getKeys()) {
valueList.add(
calculateResultForActivity(
currentAnalysis.getActivityForKey(key),
currentAnalysis.getBookedMinutesForKey(key),
totalMinutes,
currentAnalysis.getAccumulatedCommentForKey(key),
withComments));
}
return sortResultList(valueList);
final Duration totalMinutes, final boolean withComments) {
Collection<Collection<String>> valueList = new ArrayList<>();
for (String key : currentAnalysis.getKeys()) {
valueList.add(
calculateResultForActivity(
currentAnalysis.getActivityForKey(key),
currentAnalysis.getBookedMinutesForKey(key),
totalMinutes,
currentAnalysis.getAccumulatedCommentForKey(key),
withComments));
}
return sortResultList(valueList);
}

private Collection<String> calculateResultForActivity(final Activity activity, final Duration activityMinutes,
final Duration totalMinutes, final String activityComments, final boolean withComments) {
return createLine(
getKeyItems(activity),
formatDuration(activityMinutes),
formatPercentageString(totalMinutes, activityMinutes),
activityComments,
withComments);
private Collection<String> calculateResultForActivity(final Activity element, final Duration elementMinutes,
final Duration totalMinutes, final String elementComments, final boolean withComments) {
return createLine(
getElementName(element),
getElementId(element),
formatDuration(elementMinutes),
formatPercentageString(totalMinutes, elementMinutes),
elementComments,
withComments);
}

private Collection<String> createLine(final Collection<String> activityinfo, final String activityMinutes,
final String totalMinutes, final String activityComments, final boolean withComments) {
List<String> back = new ArrayList<>(activityinfo);
back.add(activityMinutes);
back.add(totalMinutes);
if (withComments) {
back.add(activityComments);
}
return back;
private Collection<String> createLine(final String elementName, final String elementId, final String elementMinutes,
final String totalMinutes, final String elementComments, final boolean withComments) {
List<String> back = new ArrayList<>();
back.add(elementName);
back.add(elementId);
back.add(elementMinutes);
back.add(totalMinutes);
if (withComments) {
back.add(elementComments);
}
return back;
}

protected abstract Collection<String> getKeyItems(final Activity activity);
protected abstract String getElementName(final Activity activity);

protected abstract String getElementId(final Activity activity);

private List<Collection<String>> sortResultList(final Collection<Collection<String>> valueList) {
return valueList
.stream()
.sorted((line1, line2) -> getSortCriteriaForResultLine(line1)
.compareToIgnoreCase(getSortCriteriaForResultLine(line2)))
.collect(Collectors.toList());
return valueList
.stream()
.sorted((line1, line2) -> compareResultLines(line1, line2))
.collect(Collectors.toList());
}

private int compareResultLines(final Collection<String> line1, final Collection<String> line2) {
return getIdElementFromLine(line1).compareToIgnoreCase(getIdElementFromLine(line2));
}

protected abstract String getSortCriteriaForResultLine(final Collection<String> line);
private String getIdElementFromLine(final Collection<String> line) {
return getIndexFromCollection(line, 1);
}

public BaseProjectComputer(final ObjectStore<Booking> bStore, final ObjectStore<Activity> aStore) {
bookingStore = bStore;
activityStore = aStore;
bookingStore = bStore;
activityStore = aStore;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
*/
package de.lgblaumeiser.ptm.analysis.analyzer;

import static de.lgblaumeiser.ptm.util.Utils.getIndexFromCollection;
import static java.util.Arrays.asList;

import java.util.Collection;

import de.lgblaumeiser.ptm.datamanager.model.Activity;
import de.lgblaumeiser.ptm.datamanager.model.Booking;
import de.lgblaumeiser.ptm.store.ObjectStore;
Expand All @@ -29,23 +24,23 @@ protected String indexGetter(final Activity activity) {
}

@Override
protected Collection<String> getHeadlineActivityElements() {
return asList("Project Id");
protected String getHeadlineNameElement() {
return "Project Name";
}

@Override
protected Collection<String> getFootlineActivityElements() {
return asList("Total");
protected String getHeadlineIdElement() {
return "Project Id";
}

@Override
protected Collection<String> getKeyItems(final Activity activity) {
return asList(activity.getProjectId());
protected String getElementName(Activity activity) {
return activity.getProjectName();
}

@Override
protected String getSortCriteriaForResultLine(final Collection<String> line) {
return getIndexFromCollection(line, 0);
protected String getElementId(final Activity activity) {
return activity.getProjectId();
}

public ProjectComputer(final ObjectStore<Booking> bStore, final ObjectStore<Activity> aStore) {
Expand Down
Loading

0 comments on commit 4c4c02e

Please sign in to comment.