Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#4703-app - Support forum-datenaustausch.ch XML dunning
* add API and SPI analog to invoice exporting #4703
- Loading branch information
Showing
36 changed files
with
1,540 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
de.metas.dunning/src/main/java/de/metas/dunning/export/DunningExportService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package de.metas.dunning.export; | ||
|
||
import lombok.NonNull; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.adempiere.exceptions.AdempiereException; | ||
import org.adempiere.util.lang.impl.TableRecordReference; | ||
import org.slf4j.Logger; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.io.ByteStreams; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import de.metas.attachments.AttachmentConstants; | ||
import de.metas.attachments.AttachmentEntryCreateRequest; | ||
import de.metas.attachments.AttachmentEntryService; | ||
import de.metas.dunning.DunningDocId; | ||
import de.metas.dunning.model.I_C_DunningDoc; | ||
import de.metas.dunning_gateway.api.DunningExportServiceRegistry; | ||
import de.metas.dunning_gateway.spi.DunningExportClient; | ||
import de.metas.dunning_gateway.spi.DunningExportClientFactory; | ||
import de.metas.dunning_gateway.spi.model.DunningExportResult; | ||
import de.metas.dunning_gateway.spi.model.DunningToExport; | ||
import de.metas.logging.LogManager; | ||
import de.metas.util.ILoggable; | ||
import de.metas.util.Loggables; | ||
import de.metas.util.StringUtils; | ||
|
||
/* | ||
* #%L | ||
* de.metas.dunning | ||
* %% | ||
* Copyright (C) 2018 metas GmbH | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 2 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-2.0.html>. | ||
* #L% | ||
*/ | ||
|
||
@Service | ||
public class DunningExportService | ||
{ | ||
|
||
private static final Logger logger = LogManager.getLogger(DunningExportService.class); | ||
|
||
private final DunningToExportFactory dunningToExportFactory; | ||
private final AttachmentEntryService attachmentEntryService; | ||
private final DunningExportServiceRegistry dunningExportServiceRegistry; | ||
|
||
private DunningExportService( | ||
@NonNull final DunningToExportFactory dunningToExportFactory, | ||
@NonNull final DunningExportServiceRegistry dunningExportServiceRegistry, | ||
@NonNull final AttachmentEntryService attachmentEntryService) | ||
{ | ||
this.attachmentEntryService = attachmentEntryService; | ||
this.dunningToExportFactory = dunningToExportFactory; | ||
this.dunningExportServiceRegistry = dunningExportServiceRegistry; | ||
} | ||
|
||
public void exportDunnings(@NonNull final ImmutableList<DunningDocId> dunningDocIds) | ||
{ | ||
for (final DunningDocId dunningDocId : dunningDocIds) | ||
{ | ||
final List<DunningToExport> dunningsToExport = dunningToExportFactory.getCreateForId(dunningDocId); | ||
for (final DunningToExport dunningToExport : dunningsToExport) | ||
{ | ||
exportDunning(dunningToExport); | ||
} | ||
} | ||
} | ||
|
||
private void exportDunning(@NonNull final DunningToExport dunningToExport) | ||
{ | ||
final ILoggable loggable = Loggables.get().withLogger(logger, Level.DEBUG); | ||
|
||
final List<DunningExportClient> exportClients = dunningExportServiceRegistry.createExportClients(dunningToExport); | ||
if (exportClients.isEmpty()) | ||
{ | ||
loggable.addLog("DunningExportService - Found no DunningExportClient implementors for dunningDocId={}; dunningToExport={}", dunningToExport.getId(), dunningToExport); | ||
return; // nothing more to do | ||
} | ||
|
||
final List<AttachmentEntryCreateRequest> attachmentEntryCreateRequests = new ArrayList<>(); | ||
for (final DunningExportClient exportClient : exportClients) | ||
{ | ||
final List<DunningExportResult> exportResults = exportClient.export(dunningToExport); | ||
for (final DunningExportResult exportResult : exportResults) | ||
{ | ||
attachmentEntryCreateRequests.add(createAttachmentRequest(exportResult)); | ||
} | ||
} | ||
|
||
for (final AttachmentEntryCreateRequest attachmentEntryCreateRequest : attachmentEntryCreateRequests) | ||
{ | ||
attachmentEntryService.createNewAttachment( | ||
TableRecordReference.of(I_C_DunningDoc.Table_Name, dunningToExport.getId()), | ||
attachmentEntryCreateRequest); | ||
loggable.addLog("DunningExportService - Attached export data to dunningDocId={}; attachment={}", dunningToExport.getId(), attachmentEntryCreateRequest); | ||
} | ||
} | ||
|
||
private AttachmentEntryCreateRequest createAttachmentRequest(@NonNull final DunningExportResult exportResult) | ||
{ | ||
byte[] byteArrayData; | ||
try | ||
{ | ||
byteArrayData = ByteStreams.toByteArray(exportResult.getData()); | ||
} | ||
catch (IOException e) | ||
{ | ||
throw AdempiereException.wrapIfNeeded(e); | ||
} | ||
|
||
final AttachmentEntryCreateRequest attachmentEntryCreateRequest = AttachmentEntryCreateRequest | ||
.builderFromByteArray( | ||
exportResult.getFileName(), | ||
byteArrayData) | ||
.tag(AttachmentConstants.TAGNAME_IS_DOCUMENT, StringUtils.ofBoolean(true)) // other than the "input" xml with was more or less just a template, this is a document | ||
.tag(AttachmentConstants.TAGNAME_BPARTNER_RECIPIENT_ID, Integer.toString(exportResult.getRecipientId().getRepoId())) | ||
.tag(DunningExportClientFactory.ATTATCHMENT_TAGNAME_EXPORT_PROVIDER, exportResult.getDunningExportProviderId()) | ||
.build(); | ||
return attachmentEntryCreateRequest; | ||
} | ||
|
||
} |
Oops, something went wrong.