Skip to content

Commit

Permalink
#10325 Create proper selection
Browse files Browse the repository at this point in the history
  • Loading branch information
cristinamghita committed Dec 8, 2020
1 parent 500a4ab commit a1951f0
Showing 1 changed file with 44 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import org.adempiere.ad.trx.api.ITrx;
Expand All @@ -35,11 +33,9 @@
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;

import de.metas.handlingunits.HuId;
import de.metas.i18n.AdMessageKey;
import de.metas.i18n.IMsgBL;
import de.metas.process.AdProcessId;
import de.metas.process.IADPInstanceDAO;
import de.metas.process.IProcessPrecondition;
Expand All @@ -57,36 +53,49 @@
import de.metas.ui.web.window.datatypes.DocumentIdsSelection;
import de.metas.util.Services;

public class WEBUI_PP_Order_PrintLabel extends WEBUI_PP_Order_Template implements IProcessPrecondition {
public class WEBUI_PP_Order_PrintLabel extends WEBUI_PP_Order_Template implements IProcessPrecondition
{
final private static AdProcessId LabelPdf_AD_Process_ID = AdProcessId.ofRepoId(584768);
protected static final AdMessageKey MSG_MustBe_TopLevel_HU = AdMessageKey
.of("WEBUI_PP_Order_PrintLabel.MustBe_TopLevel_HU");

final private IADPInstanceDAO adPInstanceDAO = Services.get(IADPInstanceDAO.class);
final private IMsgBL msgBL = Services.get(IMsgBL.class);

@Override
protected ProcessPreconditionsResolution checkPreconditionsApplicable() {
// if (!getSelectedRowIds().isSingleDocumentId())
// {
// return ProcessPreconditionsResolution.rejectBecauseNotSingleSelection();
// }
//
// if (!getSingleSelectedRow().isTopLevelHU())
// {
// return ProcessPreconditionsResolution.reject(msgBL.getTranslatableMsgText(MSG_MustBe_TopLevel_HU));
// }
protected ProcessPreconditionsResolution checkPreconditionsApplicable()
{

final ImmutableSet<HuId> distinctHuIds = retrieveSelectedHuIds();
if (distinctHuIds.isEmpty())
{
return ProcessPreconditionsResolution.rejectBecauseNoSelection();
}

return ProcessPreconditionsResolution.accept();
}

@Override
@RunOutOfTrx
protected String doIt() {
protected String doIt()
{

final DocumentIdsSelection selectedRowIds = getSelectedRowIds();
// create selection
final ImmutableSet<HuId> distinctHuIds = retrieveSelectedHuIds();
DB.createT_Selection(getPinstanceId(), distinctHuIds, ITrx.TRXNAME_None);

// print
final ReportResult label = printLabel();

// preview
getResult().setReportData(label.getReportContent(), buildFilename(), OutputType.PDF.getContentType());

return MSG_OK;

getSingleSelectedRow();
}

private ImmutableSet<HuId> retrieveSelectedHuIds()
{
final DocumentIdsSelection selectedRowIds = getSelectedRowIds();

final ImmutableList<PPOrderLineRow> selectedRows = getView().streamByIds(selectedRowIds)
.collect(ImmutableList.toImmutableList());
Expand All @@ -95,29 +104,24 @@ protected String doIt() {

selectedRows.forEach(row -> {
final PPOrderLineType type = row.getType();
if (type.isBOMLine()) {
huIds.add(row.getHuId());
} else if (type.isMainProduct()) {
if (type.isMainProduct())
{
final ImmutableList<PPOrderLineRow> includedRows = row.getIncludedRows();
includedRows.stream().map(PPOrderLineRow::getHuId).collect(Collectors.toCollection(() -> huIds));
}
else
{
huIds.add(row.getHuId());
}
});

final ImmutableSet<HuId> distinctHuIds = huIds.stream().distinct().collect(ImmutableSet.toImmutableSet());

DB.createT_Selection(getPinstanceId(), distinctHuIds, ITrx.TRXNAME_None);

// print
final ReportResult label = printLabel();

// preview
getResult().setReportData(label.getReportContent(), buildFilename(), OutputType.PDF.getContentType());

return MSG_OK;

final ImmutableSet<HuId> distinctHuIds = huIds.stream().filter(Objects::nonNull).distinct()
.collect(ImmutableSet.toImmutableSet());
return distinctHuIds;
}

private ReportResult printLabel() {
private ReportResult printLabel()
{
final PInstanceRequest pinstanceRequest = createPInstanceRequest();
final PInstanceId pinstanceId = adPInstanceDAO.createADPinstanceAndADPInstancePara(pinstanceRequest);

Expand All @@ -129,17 +133,19 @@ private ReportResult printLabel() {
return reportsClient.report(jasperProcessInfo);
}

private PInstanceRequest createPInstanceRequest() {
private PInstanceRequest createPInstanceRequest()
{

return PInstanceRequest.builder().processId(LabelPdf_AD_Process_ID)
.processParams(ImmutableList.of(ProcessInfoParameter.of("AD_PInstance_ID", getPinstanceId()))).build();
}

private String buildFilename() {
private String buildFilename()
{
final String instance = String.valueOf(getPinstanceId().getRepoId());
final String title = getProcessInfo().getTitle();

return Joiner.on("_").skipNulls().join(instance, title) + ".pdf";
}

}
}

0 comments on commit a1951f0

Please sign in to comment.