Skip to content

Commit

Permalink
department chart - web tier
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrenn committed Jun 22, 2024
1 parent 910f093 commit 13db233
Show file tree
Hide file tree
Showing 13 changed files with 433 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public Collection<IDVO> getProbandListStatus(ProbandOutVO proband) {
if (!probandListStatusCache.containsKey(proband.getId())) {
Collection probandListStatus = null;
try {
probandListStatus = WebUtil.getServiceLocator().getTrialService().getProbandListStatus(WebUtil.getAuthentication(), null, proband.getId(), true, null);
probandListStatus = WebUtil.getServiceLocator().getTrialService().getProbandListStatus(WebUtil.getAuthentication(), null, proband.getId(), true, null, null);
} catch (ServiceException | AuthorisationException | IllegalArgumentException e) {
} catch (AuthenticationException e) {
WebUtil.publishException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,300 @@
package org.phoenixctms.ctsms.web.model.trial;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.TreeSet;

import org.phoenixctms.ctsms.compare.ProbandListStatusEntryOutVOComparator;
import org.phoenixctms.ctsms.enumeration.Color;
import org.phoenixctms.ctsms.enumeration.ProbandListStatusLogLevel;
import org.phoenixctms.ctsms.enumeration.VariablePeriod;
import org.phoenixctms.ctsms.exception.AuthenticationException;
import org.phoenixctms.ctsms.exception.AuthorisationException;
import org.phoenixctms.ctsms.exception.ServiceException;
import org.phoenixctms.ctsms.util.CommonUtil;
import org.phoenixctms.ctsms.vo.DepartmentVO;
import org.phoenixctms.ctsms.vo.ProbandListExcelVO;
import org.phoenixctms.ctsms.vo.ProbandListStatusEntryOutVO;
import org.phoenixctms.ctsms.web.model.CartesianChartModel;
import org.phoenixctms.ctsms.web.model.ManagedBeanBase;
import org.phoenixctms.ctsms.web.util.DateUtil;
import org.phoenixctms.ctsms.web.util.DefaultSettings;
import org.phoenixctms.ctsms.web.util.MessageCodes;
import org.phoenixctms.ctsms.web.util.Messages;
import org.phoenixctms.ctsms.web.util.SettingCodes;
import org.phoenixctms.ctsms.web.util.Settings;
import org.phoenixctms.ctsms.web.util.Settings.Bundle;
import org.phoenixctms.ctsms.web.util.WebUtil;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.chart.ChartSeries;

public class DepartmentChartBean extends ManagedBeanBase {

private Long trialId;
//private TrialOutVO trial;
private CartesianChartModel chartModel;
private List<Color> departmentColors;
private ArrayList<Color> seriesColors;
private Date minX;
private Date maxX;
private Integer maxY;
private boolean stacked;

public DepartmentChartBean() {
super();
chartModel = new CartesianChartModel();
departmentColors = Settings.getColorList(SettingCodes.DEPARTMENT_CHART_DEPARTMENT_COLORS, Bundle.SETTINGS, DefaultSettings.DEPARTMENT_CHART_DEPARTMENT_COLORS);
seriesColors = new ArrayList<Color>();
}

@Override
protected String changeAction(Long id) {
this.trialId = id;
initIn();
initSets();
return CHANGE_OUTCOME;
}

private String dateToString(Date date) {
return WebUtil.quoteJSString(DateUtil.getJqPlotJsDateFormat().format(date), true);
}

public CartesianChartModel getChartModel() {
if (isChartEmpty()) {
CartesianChartModel emptyChartModel = new CartesianChartModel();
ChartSeries emptyChartSeries = new ChartSeries();
emptyChartSeries.setLabel(Messages.getString(MessageCodes.EMPTY_CHART_LABEL));
emptyChartSeries.set(dateToString(new Date()), 0);
emptyChartModel.addSeries(emptyChartSeries);
return emptyChartModel;
}
return chartModel;
}

public boolean getEnableExports() {
return Settings.getBoolean(SettingCodes.ENABLE_PROBAND_LIST_EXPORTS, Bundle.SETTINGS, DefaultSettings.ENABLE_PROBAND_LIST_EXPORTS);
}

public StreamedContent getEnrollmentLogExcelStreamedContent() throws Exception {
try {
ProbandListExcelVO excel = WebUtil.getServiceLocator().getTrialService().exportProbandList(WebUtil.getAuthentication(), trialId, ProbandListStatusLogLevel.ENROLLMENT);
return new DefaultStreamedContent(new ByteArrayInputStream(excel.getDocumentDatas()), excel.getContentType().getMimeType(), excel.getFileName());
} catch (AuthenticationException e) {
WebUtil.publishException(e);
throw e;
} catch (AuthorisationException | ServiceException | IllegalArgumentException e) {
throw e;
}
}

public long getMaxX() {
return (maxX != null ? maxX : WebUtil.subIntervals(new Date(), VariablePeriod.MONTH, null, 1)).getTime();
}

public Integer getMaxY() {
return maxY;
}

public long getMinX() {
return (minX != null ? minX : new Date()).getTime();
}

public StreamedContent getPreScreeningLogExcelStreamedContent() throws Exception {
try {
ProbandListExcelVO excel = WebUtil.getServiceLocator().getTrialService()
.exportProbandList(WebUtil.getAuthentication(), trialId, ProbandListStatusLogLevel.PRE_SCREENING);
return new DefaultStreamedContent(new ByteArrayInputStream(excel.getDocumentDatas()), excel.getContentType().getMimeType(), excel.getFileName());
} catch (AuthenticationException e) {
WebUtil.publishException(e);
throw e;
} catch (AuthorisationException | ServiceException | IllegalArgumentException e) {
throw e;
}
}

public StreamedContent getScreeningLogExcelStreamedContent() throws Exception {
try {
ProbandListExcelVO excel = WebUtil.getServiceLocator().getTrialService().exportProbandList(WebUtil.getAuthentication(), trialId, ProbandListStatusLogLevel.SCREENING);
return new DefaultStreamedContent(new ByteArrayInputStream(excel.getDocumentDatas()), excel.getContentType().getMimeType(), excel.getFileName());
} catch (AuthenticationException e) {
WebUtil.publishException(e);
throw e;
} catch (AuthorisationException | ServiceException | IllegalArgumentException e) {
throw e;
}
}

public String getSeriesColors() {
return WebUtil.getSeriesColors(seriesColors);
}

public StreamedContent getSiclExcelStreamedContent() throws Exception {
try {
ProbandListExcelVO excel = WebUtil.getServiceLocator().getTrialService().exportProbandList(WebUtil.getAuthentication(), trialId, ProbandListStatusLogLevel.SICL);
return new DefaultStreamedContent(new ByteArrayInputStream(excel.getDocumentDatas()), excel.getContentType().getMimeType(), excel.getFileName());
} catch (AuthenticationException e) {
WebUtil.publishException(e);
throw e;
} catch (AuthorisationException | ServiceException | IllegalArgumentException e) {
throw e;
}
}

private void initIn() {
stacked = true;
}

private void initSets() {
chartModel.clear();
seriesColors.clear();
minX = null;
maxX = null;
maxY = null;
//trial = WebUtil.getTrial(trialId);
Collection<DepartmentVO> departments = null;
try {
departments = WebUtil.getServiceLocator().getSelectionSetService().getAllDepartments(WebUtil.getAuthentication());
} catch (ServiceException | AuthorisationException | IllegalArgumentException e) {
} catch (AuthenticationException e) {
WebUtil.publishException(e);
}
HashMap<Long, DepartmentVO> departmentMap;
if (departments != null) {
departmentMap = new HashMap<Long, DepartmentVO>(departments.size());
Iterator<Color> departmentColorsIt = CommonUtil.getCircularIterator(departmentColors);
Iterator<DepartmentVO> departmentsIt = departments.iterator();
while (departmentsIt.hasNext()) {
DepartmentVO department = departmentsIt.next();
departmentMap.put(department.getId(), department);
seriesColors.add(departmentColorsIt.next());
}
} else {
departmentMap = new HashMap<Long, DepartmentVO>();
}
Collection<ProbandListStatusEntryOutVO> initialProbandListStatus = null;
if (trialId != null) {
try {
initialProbandListStatus = WebUtil.getServiceLocator().getTrialService().getProbandListStatus(WebUtil.getAuthentication(), trialId, null, false, true, null);
} catch (ServiceException | AuthorisationException | IllegalArgumentException e) {
} catch (AuthenticationException e) {
WebUtil.publishException(e);
}
}
if (initialProbandListStatus == null) {
initialProbandListStatus = new ArrayList<ProbandListStatusEntryOutVO>();
} else {
initialProbandListStatus = new ArrayList<ProbandListStatusEntryOutVO>(initialProbandListStatus);
Collections.sort((ArrayList<ProbandListStatusEntryOutVO>) initialProbandListStatus, new ProbandListStatusEntryOutVOComparator());
}
HashMap<Long, TreeMap<Date, Number>> seriesMap = new HashMap<Long, TreeMap<Date, Number>>();
LinkedHashMap<Long, ProbandListStatusEntryOutVO> probandMap = new LinkedHashMap<Long, ProbandListStatusEntryOutVO>();
Iterator<ProbandListStatusEntryOutVO> probandListStatusIt = initialProbandListStatus.iterator();
while (probandListStatusIt.hasNext()) {
ProbandListStatusEntryOutVO probandListStatusEntry = probandListStatusIt.next();
Long probandId = probandListStatusEntry.getListEntry().getProband().getId();
TreeMap<Long, ProbandListStatusEntryOutVO> enrollmentStatusMap;
if (!probandMap.containsKey(probandId)) {
probandMap.put(probandId, probandListStatusEntry);
}
}
maxY = probandMap.size();
TreeSet<Date> dates = new TreeSet<Date>();
probandListStatusIt = probandMap.values().iterator();
while (probandListStatusIt.hasNext()) {
ProbandListStatusEntryOutVO probandListStatusEntry = probandListStatusIt.next();
Long departmentId = probandListStatusEntry.getListEntry().getProband().getDepartment().getId();
//Long statusId = probandListStatusEntry.getStatus().getId();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(probandListStatusEntry.getRealTimestamp());
Date date = (new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0)).getTime();
dates.add(date);
//TreeMap<Long, ProbandListStatusEntryOutVO> enrollmentStatusMap = probandMap.get(probandListStatusEntry.getListEntry().getProband().getId());
//Entry<Long, ProbandListStatusEntryOutVO> previousStatus = enrollmentStatusMap.lowerEntry(probandListStatusEntry.getId());
TreeMap<Date, Number> series;
if (seriesMap.containsKey(departmentId)) {
series = seriesMap.get(departmentId);
} else {
series = new TreeMap<Date, Number>();
seriesMap.put(departmentId, series);
}
if (series.containsKey(date)) {
series.put(date, series.get(date).intValue() + 1);
} else {
if (series.size() > 0) {
series.put(date, series.lastEntry().getValue().intValue() + 1);
} else {
series.put(date, 1);
}
}
// if (previousStatus != null) {
// series = seriesMap.get(previousStatus.getValue().getStatus().getId());
// series.put(date, series.lastEntry().getValue().intValue() - 1);
// }
}
if (dates.size() > 0) {
minX = dates.first();
maxX = dates.last();
}
Iterator<Long> seriesMapIt = seriesMap.keySet().iterator();
while (seriesMapIt.hasNext()) {
Long departmentId = seriesMapIt.next();
DepartmentVO department = departmentMap.get(departmentId);
TreeMap<Date, Number> series = seriesMap.get(departmentId);
if (department != null && series.size() > 0) {
ChartSeries chartLine = new ChartSeries();
chartLine.setLabel(department.getName());
Iterator<Date> datesIt = dates.iterator();
while (datesIt.hasNext()) {
Date date = datesIt.next();
if (series.containsKey(date)) {
chartLine.set(dateToString(date), series.get(date));
} else {
Entry<Date, Number> previousValue = series.lowerEntry(date);
chartLine.set(dateToString(date), previousValue == null ? 0 : previousValue.getValue());
}
}
chartModel.addSeries(chartLine);
//seriesColors.add(department.getColor());
}
}
}

public boolean isChartEmpty() {
return chartModel.getSeries().isEmpty();
}

@Override
public boolean isCreateable() {
return false;
}

@Override
public boolean isCreated() {
return false;
}

public boolean isStacked() {
return stacked;
}

public void setStacked(boolean stacked) {
this.stacked = stacked;
}

public void updateChart() {
initIn();
initSets();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private void initSets() {
Collection<ProbandListStatusEntryOutVO> probandListStatus = null;
if (trialId != null) {
try {
probandListStatus = WebUtil.getServiceLocator().getTrialService().getProbandListStatus(WebUtil.getAuthentication(), trialId, null, false, null);
probandListStatus = WebUtil.getServiceLocator().getTrialService().getProbandListStatus(WebUtil.getAuthentication(), trialId, null, false, null, null);
} catch (ServiceException | AuthorisationException | IllegalArgumentException e) {
} catch (AuthenticationException e) {
WebUtil.publishException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ public Collection<IDVO> getProbandListStatus(ProbandOutVO proband) {
Collection<ProbandListStatusEntryOutVO> probandListStatus = null;
Collection probandListStatusFiltered = new ArrayList<ProbandListStatusEntryOutVO>();
try {
probandListStatus = WebUtil.getServiceLocator().getTrialService().getProbandListStatus(WebUtil.getAuthentication(), trialId, proband.getId(), false, null);
probandListStatus = WebUtil.getServiceLocator().getTrialService().getProbandListStatus(WebUtil.getAuthentication(), trialId, proband.getId(), false, null,
null);
} catch (ServiceException | AuthorisationException | IllegalArgumentException e) {
} catch (AuthenticationException e) {
WebUtil.publishException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class TrialAssociationBean extends ManagedBeanBase {
private TrialMassMailLazyModel trialMassMailModel;
private BookingDurationSummaryModel bookingDurationModel;
private EnrollmentChartBean enrollmentChartBean;
private DepartmentChartBean departmentChartBean;
private RandomizationListCodeLazyModel randomizationListCodeModel;

public TrialAssociationBean() {
Expand All @@ -42,6 +43,7 @@ public TrialAssociationBean() {
trialMassMailModel = new TrialMassMailLazyModel();
bookingDurationModel = new BookingDurationSummaryModel(BookingDurationSummaryType.TRIAL);
enrollmentChartBean = new EnrollmentChartBean();
departmentChartBean = new DepartmentChartBean();
randomizationListCodeModel = new RandomizationListCodeLazyModel();
}

Expand All @@ -64,6 +66,10 @@ public EnrollmentChartBean getEnrollmentChartBean() {
return enrollmentChartBean;
}

public DepartmentChartBean getDepartmentChartBean() {
return departmentChartBean;
}

public String getMassMailProgressLabel(MassMailOutVO massMail) {
return WebUtil.getMassMailProgressLabel(massMail, trialMassMailModel.getMassMailProgress(massMail));
}
Expand Down Expand Up @@ -107,6 +113,10 @@ public void handleEnrollmentChartChange() {
enrollmentChartBean.changeRootEntity(trialId);
}

public void handleDepartmentChartChange() {
departmentChartBean.changeRootEntity(trialId);
}

public void handleShiftDurationSummaryChange() {
now = new Date();
shiftDurationModel.reset(now, ShiftDurationSummaryType.TRIAL, trialId);
Expand Down Expand Up @@ -147,6 +157,7 @@ private void initSets() {
shiftDurationModel.reset(now, ShiftDurationSummaryType.TRIAL, null);
bookingDurationModel.reset(now, BookingDurationSummaryType.TRIAL, null);
enrollmentChartBean.changeRootEntity(null);
departmentChartBean.changeRootEntity(null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ public final class DefaultSettings {
public static final Color DUTY_SELF_ALLOCATION_LOCKED_COLOR = Color.GAINSBORO;
public static final ArrayList<String> INVENTORY_BOOKING_SCHEDULE_TRIAL_COLORS = new ArrayList<String>();
public static final ArrayList<String> DUTY_ROSTER_SCHEDULE_TRIAL_COLORS = new ArrayList<String>();
public static final ArrayList<String> DEPARTMENT_CHART_DEPARTMENT_COLORS = new ArrayList<String>();
public static final Long MAX_COST_TYPES_PER_TRIAL = 10l;
public static final Long MAX_COST_TYPES_COLUMNS = 5l;
public final static Color ENROLLMENT_STATUS_IS_NOT_COUNT_COLOR = Color.GAINSBORO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ public interface SettingCodes {
public final static String DUTY_SELF_ALLOCATION_LOCKED_COLOR = "duty_self_allocation_locked_color";
public final static String INVENTORY_BOOKING_SCHEDULE_TRIAL_COLORS = "inventory_booking_schedule_trial_colors";
public final static String DUTY_ROSTER_SCHEDULE_TRIAL_COLORS = "duty_roster_schedule_trial_colors";
public final static String DEPARTMENT_CHART_DEPARTMENT_COLORS = "department_chart_department_colors";
public final static String MAX_COST_TYPES_PER_TRIAL = "max_cost_types_per_trial";
public final static String MAX_COST_TYPES_COLUMNS = "max_cost_types_columns";
public final static String ENROLLMENT_STATUS_IS_NOT_COUNT_COLOR = "enrollment_status_is_not_count_color";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3580,7 +3580,7 @@ public static Long getSelectionSetValueCount(Long inputFieldId) {
return null;
}

public static String getSeriesColors(ArrayList<Color> colors) {
public static String getSeriesColors(List<Color> colors) {
if (colors != null && colors.size() > 0) {
StringBuilder seriesColors = new StringBuilder();
Iterator<Color> colorsIt = colors.iterator();
Expand Down
Loading

0 comments on commit 13db233

Please sign in to comment.