office-alexander-logistics/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/zoho/ZohoExportAdapter.java
2025-05-08 23:10:57 +02:00

134 lines
No EOL
4.4 KiB
Java

package com.alexanderlogistics.zoho;
import java.util.List;
import java.util.logging.Logger;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.imixs.archive.core.SnapshotService;
import org.imixs.workflow.FileData;
import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.SignalAdapter;
import org.imixs.workflow.engine.WorkflowService;
import org.imixs.workflow.exceptions.AdapterException;
import org.imixs.workflow.exceptions.PluginException;
import com.alexanderlogistics.zoho.dto.ZohoBill;
import com.alexanderlogistics.zoho.dto.ZohoInvoice;
import jakarta.inject.Inject;
/**
* This adapter exports the invoice data to zoho
*
* @version 1.0
* @author rsoika
*/
public class ZohoExportAdapter implements SignalAdapter {
public static final String CONFIG_ERROR = "CONFIG_ERROR";
public static final String ENV_ZOHO_EXPORT_MAX_ATTACHMENT_SIZE = "zoho.export.maxattachmentsize";
private static Logger logger = Logger.getLogger(ZohoExportAdapter.class.getName());
@Inject
ZohoAPIService zohoAPIService;
@Inject
WorkflowService workflowService;
@Inject
SnapshotService snapshotService;
@Inject
ZohoOAuthManager zohoOAuthManager;
@Inject
@ConfigProperty(name = ENV_ZOHO_EXPORT_MAX_ATTACHMENT_SIZE, defaultValue = "10485760")
long maxAttachmentSize;
/**
* This method calls the zoho api
*
* @throws PluginException
*/
@Override
public ItemCollection execute(ItemCollection document, ItemCollection event)
throws AdapterException, PluginException {
// read the cargosoft export options
ItemCollection evalItemCollection = workflowService.evalWorkflowResult(event, "zoho", document, false);
// get access token
String accessToken = zohoOAuthManager.getValidAccessToken();
// Rechnungsausgang
if (document.getModelVersion().startsWith("rechnungsausgang-")) {
logger.info("├── Invoice Export");
exportRechnungsausgang(document, accessToken);
}
// Rechnungseingang
if (document.getModelVersion().startsWith("rechnungseingang-")) {
logger.info("├── Bill Export");
exportRechnungseingang(document, accessToken);
}
return document;
}
/**
* Help method, - finds/updates - contact, - export invoice, - change state,
* -attache files
*
* @param document
* @param accessToken
* @throws PluginException
*/
private void exportRechnungsausgang(ItemCollection document, String accessToken) throws PluginException {
// find companyID
String contactID = zohoAPIService.lookupContact(document.getItemValueString("dbtr.name"), "customer",
accessToken);
ZohoInvoice zohoInvoice = zohoAPIService.exportInvoice(document, contactID, accessToken);
// Attache Files....
List<FileData> files = null;
ItemCollection snapshot = snapshotService.findSnapshot(document);
if (snapshot != null) {
files = snapshot.getFileData();
if (files != null) {
for (FileData file : files) {
zohoAPIService.attacheFileData(zohoInvoice.getInvoiceId(), file, "invoices", accessToken);
}
}
}
}
/**
* Help method, - finds/updates - contact, - export invoice, - change state,
* -attache files
*
* @param document
* @param accessToken
* @throws PluginException
*/
private void exportRechnungseingang(ItemCollection document, String accessToken) throws PluginException {
// find companyID
String contactID = zohoAPIService.lookupContact(document.getItemValueString("cdtr.name"), "vendor",
accessToken);
String accountID = zohoOAuthManager.getAccountId();
ZohoBill zohoBill = zohoAPIService.exportBill(document, contactID, accountID, accessToken);
// Attache Files....
List<FileData> files = null;
ItemCollection snapshot = snapshotService.findSnapshot(document);
if (snapshot != null) {
files = snapshot.getFileData();
if (files != null) {
for (FileData file : files) {
zohoAPIService.attacheFileData(zohoBill.getBillId(), file, "bills", accessToken);
}
}
}
}
}