Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0000619: Add year in Indicators export monthly table #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,21 @@ public PivotTableElement fixIndicator(int indicatorId) {
pivot.setFilter(filter);
return pivot;
}

public PivotTableElement YearExcel(int indicatorId) {
PivotTableElement pivot = new PivotTableElement();
pivot.setShowEmptyCells(true);
pivot.addRowDimensions(adminDimensions);
pivot.addRowDimension(new Dimension(DimensionType.Site));
pivot.addColDimension(new DateDimension(DateUnit.MONTH));
pivot.addColDimension(new DateDimension(DateUnit.YEAR));
Filter filter = new Filter();
filter.addRestriction(DimensionType.Database, databaseId);
filter.addRestriction(DimensionType.Indicator, indicatorId);
filter.setDateRange(projectDateRange);
pivot.setFilter(filter);
return pivot;
}

public PivotTableElement fixSite(int siteId) {
PivotTableElement pivot = new PivotTableElement();
pivot.setShowEmptyCells(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,73 +22,76 @@
* #L%
*/


import javax.validation.constraints.NotNull;
import org.sigmah.client.ui.view.pivot.LayoutComposer;
import org.sigmah.server.domain.Project;
import org.sigmah.server.servlet.exporter.base.Exporter;
import org.sigmah.server.util.CalendarDates;
import org.sigmah.shared.command.GenerateElement;
import org.sigmah.shared.command.GetIndicators;
import org.sigmah.shared.command.GetProject;
import org.sigmah.shared.command.result.IndicatorListResult;
import org.sigmah.shared.dto.IndicatorDTO;
import org.sigmah.shared.dto.IndicatorGroup;
import org.sigmah.shared.dto.ProjectDTO;
import org.sigmah.shared.dto.pivot.content.PivotContent;
import org.sigmah.shared.dto.pivot.model.PivotTableElement;

public final class SpreadsheetDataUtil {

private SpreadsheetDataUtil() {
// Only provides static methods.
}

@NotNull
public static LogFrameExportData prepareLogFrameData(final Project project, final Exporter exporter) throws Throwable {
// TODO [INDICATORS] A major redesign of the indicators is in progress...

final LogFrameExportData data = new LogFrameExportData(project, exporter);

// Get indicators from CommandHanler (only for the purpose to get aggregated(current) values of indicators).
final IndicatorListResult indicators = exporter.execute(new GetIndicators(project.getId()));
for (final IndicatorDTO dto : indicators.getData()) {
data.getIndMap().put(dto.getId(), dto);
}
return data;
}

@NotNull
public static IndicatorEntryData prepareIndicatorsData(Integer projectId, final Exporter exporter) throws Throwable {
// TODO [INDICATORS] A major redesign of the indicators is in progress...

// Get project
final ProjectDTO project = exporter.execute(new GetProject(projectId, null));
final LayoutComposer composer = new LayoutComposer(new CalendarDates(), project);

// Get indicators
final IndicatorListResult indicators = exporter.execute(new GetIndicators(project));

// Create spreadsheet data
final IndicatorEntryData data = new IndicatorEntryData(exporter, indicators, project.getName());

// Set indicator groups
for(final IndicatorGroup group:indicators.getGroups()) {
data.getGroupMap().put(group.getId(), group.getName());
}

// Put indicator entry
for (final IndicatorDTO indicator : indicators.getData()) {
final PivotTableElement pivot = composer.fixIndicator(indicator.getId());
final PivotContent content = exporter.execute(new GenerateElement<PivotContent>(pivot));
data.getEntryMap().put(indicator.getId(), content.getData());
}

// Put ungrouped indicators
final IndicatorGroup nonGroup = new IndicatorGroup(exporter.localize("unGrouped"));
nonGroup.getIndicators().addAll(indicators.getUngroupedIndicators());
data.getIndicators().getGroups().add(nonGroup);

return data;
}
}

import javax.validation.constraints.NotNull;
import org.sigmah.client.ui.view.pivot.LayoutComposer;
import org.sigmah.server.domain.Project;
import org.sigmah.server.servlet.exporter.base.Exporter;
import org.sigmah.server.util.CalendarDates;
import org.sigmah.shared.command.GenerateElement;
import org.sigmah.shared.command.GetIndicators;
import org.sigmah.shared.command.GetProject;
import org.sigmah.shared.command.result.IndicatorListResult;
import org.sigmah.shared.dto.IndicatorDTO;
import org.sigmah.shared.dto.IndicatorGroup;
import org.sigmah.shared.dto.ProjectDTO;
import org.sigmah.shared.dto.pivot.content.PivotContent;
import org.sigmah.shared.dto.pivot.model.PivotTableElement;

public final class SpreadsheetDataUtil {

private SpreadsheetDataUtil() {
// Only provides static methods.
}

@NotNull
public static LogFrameExportData prepareLogFrameData(final Project project, final Exporter exporter) throws Throwable {
// TODO [INDICATORS] A major redesign of the indicators is in progress...

final LogFrameExportData data = new LogFrameExportData(project, exporter);

// Get indicators from CommandHanler (only for the purpose to get aggregated(current) values of indicators).
final IndicatorListResult indicators = exporter.execute(new GetIndicators(project.getId()));
for (final IndicatorDTO dto : indicators.getData()) {
data.getIndMap().put(dto.getId(), dto);
}
return data;
}

@NotNull
public static IndicatorEntryData prepareIndicatorsData(Integer projectId, final Exporter exporter) throws Throwable {
// TODO [INDICATORS] A major redesign of the indicators is in progress...

// Get project
final ProjectDTO project = exporter.execute(new GetProject(projectId, null));
final LayoutComposer composer = new LayoutComposer(new CalendarDates(), project);

// Get indicators
final IndicatorListResult indicators = exporter.execute(new GetIndicators(project));

// Create spreadsheet data
final IndicatorEntryData data = new IndicatorEntryData(exporter, indicators, project.getName());

// Set indicator groups
for(final IndicatorGroup group:indicators.getGroups()) {
data.getGroupMap().put(group.getId(), group.getName());
}

// Put indicator entry
for (final IndicatorDTO indicator : indicators.getData()) {
final PivotTableElement pivot = composer.fixIndicator(indicator.getId());
final PivotContent content = exporter.execute(new GenerateElement<PivotContent>(pivot));
data.getEntryMap().put(indicator.getId(), content.getData());
final PivotTableElement pivotyear = composer.YearExcel(indicator.getId());
final PivotContent contentyear = exporter.execute(new GenerateElement<PivotContent>(pivotyear));
data.getEntryMap().put(indicator.getId()+1, contentyear.getData());
}

// Put ungrouped indicators
final IndicatorGroup nonGroup = new IndicatorGroup(exporter.localize("unGrouped"));
nonGroup.getIndicators().addAll(indicators.getUngroupedIndicators());
data.getIndicators().getGroups().add(nonGroup);

return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ private void createDetailSheet(final IndicatorDTO indicator) throws Throwable {
int rowIndex = -1;

List<PivotTableData.Axis> leaves = data.getEntryMap().get(indicator.getId()).getRootColumn().getLeaves();
final List<PivotTableData.Axis> leavesyear
= data.getEntryMap().get(indicator.getId() + 1).getRootColumn().getLeaves();
int numbOfLeaves = leaves.size();
int numbOfCols = 4;

Expand Down Expand Up @@ -211,16 +213,76 @@ private void createDetailSheet(final IndicatorDTO indicator) throws Throwable {
row.getCellByIndex(4).setCellStyleName(null);

// data entry
//for year row in sheet
String merge = "hello";
row = tableEx.getRowByIndex(++rowIndex);
int cellIndex = 0;
CalcUtils.putHeader(row, ++cellIndex, "Year");
Map<String, Integer> columnIndexMapyr = new HashMap<String, Integer>();
for (PivotTableData.Axis axis : leavesyear) {
CalcUtils.putHeader(row, ++cellIndex, axis.getLabel());
columnIndexMapyr.put(axis.getLabel(), cellIndex);
if (merge.equals(axis.getLabel()) && cellIndex > 2) {
CalcUtils.mergeCell(tableEx, cellIndex - 1, rowIndex, cellIndex, rowIndex);
} else {
}
merge = axis.getLabel();

}
int month = 0;
String completedate = "hello";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, follow java conventions and use lowerCamelCase for variable names (completedate, leavesyear lacks uppercases)

// header
row = tableEx.getRowByIndex(++rowIndex);
int cellIndex = 0;
CalcUtils.putHeader(row, ++cellIndex, data.getLocalizedVersion("sideAndMonth"));
cellIndex = 0;
CalcUtils.putHeader(row, ++cellIndex, "Month");
Map<String, Integer> columnIndexMap = new HashMap<String, Integer>();
for (PivotTableData.Axis axis : leaves) {
CalcUtils.putHeader(row, ++cellIndex, axis.getLabel());
columnIndexMap.put(axis.getLabel(), cellIndex);
if (cellIndex == 2) {
completedate = axis.getLabel();
}
}

//for date value
if (completedate.equals("Jan")) {
month = 1;
} else if (completedate.equals("Feb")) {
month = 2;
} else if (completedate.equals("Mar")) {
month = 3;
} else if (completedate.equals("Apr")) {
month = 4;
} else if (completedate.equals("May")) {
month = 5;
} else if (completedate.equals("Jun")) {
month = 6;
} else if (completedate.equals("Jul")) {
month = 7;
} else if (completedate.equals("Aug")) {
month = 8;
} else if (completedate.equals("Sep")) {
month = 9;
} else if (completedate.equals("Oct")) {
month = 10;
} else if (completedate.equals("Nov")) {
month = 11;
} else {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do it more safely, like

} else if (completeDate.equals("Dec")) {
    month = 12;
} else {
    throw new IllegalStateException("unable to parse " + completedate);
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it's quite strange you do that manually, you should be using a date manipulation API (I guess, something like java.text.DateFormat).

month = 12;
}
//for year row in sheet
row = tableEx.getRowByIndex(++rowIndex);
cellIndex = 0;
CalcUtils.putHeader(row, ++cellIndex, "Site");
Map<String, Integer> columnIndexMapcompletedate = new HashMap<String, Integer>();
for (PivotTableData.Axis axis : leavesyear) {
CalcUtils.putHeader(row, ++cellIndex, month + "." + axis.getLabel());
columnIndexMapcompletedate.put(axis.getLabel(), cellIndex);
if (month == 12) {
month = 1;
} else {
month++;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A date manipulation API should do that kind of thing for you. You may have a look at something called lenient (see java.text.DateFormat#setLenient for example)

}
// rows
for (PivotTableData.Axis axis : data.getEntryMap().get(indicator.getId()).getRootRow().getChildren()) {
row = tableEx.getRowByIndex(++rowIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ private void createDetailSheet(final IndicatorDTO indicator) {
int rowIndex = -1;

final List<PivotTableData.Axis> leaves = data.getEntryMap().get(indicator.getId()).getRootColumn().getLeaves();
final List<PivotTableData.Axis> leavesyear = data.getEntryMap().get(indicator.getId() + 1).getRootColumn().getLeaves();

int numbOfLeaves = leaves.size();
int numbOfCols = 4;

Expand Down Expand Up @@ -207,17 +209,88 @@ private void createDetailSheet(final IndicatorDTO indicator) {
sheetEx.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 1, numbOfCols));

// data entry
//for year above months in indicators
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manishvishnoi2 : This needs an indentation fix before the pull request can be merged.

int startval = 2;
int endval = 0;
String merge = "hello";
row = sheetEx.createRow(++rowIndex);
row.setHeightInPoints(defHeight);
int cellIndex = 0;
utils.putHeader(row, ++cellIndex, "Year");
Map<String, Integer> columnIndexMapyr = new HashMap<String, Integer>();
for (PivotTableData.Axis axis : leavesyear) {
utils.putHeader(row, ++cellIndex, axis.getLabel());
columnIndexMapyr.put(axis.getLabel(), cellIndex);
if (cellIndex > 3) {
if (merge.equals(axis.getLabel())) {

} else {
endval = cellIndex - 1;
sheetEx.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, startval, endval));
startval = cellIndex;
}
}
sheetEx.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, startval, cellIndex));
merge = axis.getLabel();

}
// header
int month = 0;
String completedate = "hello";
row = sheetEx.createRow(++rowIndex);
row.setHeightInPoints(defHeight);
int cellIndex = 0;
utils.putHeader(row, ++cellIndex, data.getLocalizedVersion("sideAndMonth"));
cellIndex = 0;
utils.putHeader(row, ++cellIndex, "month");
Map<String, Integer> columnIndexMap = new HashMap<String, Integer>();
for (PivotTableData.Axis axis : leaves) {
utils.putHeader(row, ++cellIndex, axis.getLabel());
columnIndexMap.put(axis.getLabel(), cellIndex);
if (cellIndex == 2) {
completedate = axis.getLabel();
}
}


if (completedate.equals("Jan")) {
month = 1;
} else if (completedate.equals("Feb")) {
month = 2;
} else if (completedate.equals("Mar")) {
month = 3;
} else if (completedate.equals("Apr")) {
month = 4;
} else if (completedate.equals("May")) {
month = 5;
} else if (completedate.equals("Jun")) {
month = 6;
} else if (completedate.equals("Jul")) {
month = 7;
} else if (completedate.equals("Aug")) {
month = 8;
} else if (completedate.equals("Sep")) {
month = 9;
} else if (completedate.equals("Oct")) {
month = 10;
} else if (completedate.equals("Nov")) {
month = 11;
} else {
month = 12;
}

//for year row in sheet
row = sheetEx.createRow(++rowIndex);
row.setHeightInPoints(defHeight);
cellIndex = 0;
utils.putHeader(row, ++cellIndex, "Site");
Map<String, Integer> columnIndexMapcompletedate = new HashMap<String, Integer>();
for (PivotTableData.Axis axis : leavesyear) {
utils.putHeader(row, ++cellIndex, month + "." + axis.getLabel());
columnIndexMapcompletedate.put(axis.getLabel(), cellIndex);
if (month == 12) {
month = 1;
} else {
month++;
}
}
// rows
for (PivotTableData.Axis axis : data.getEntryMap().get(indicator.getId()).getRootRow().getChildren()) {
row = sheetEx.createRow(++rowIndex);
Expand Down