Skip to content

Commit

Permalink
Master external system window (#10776)
Browse files Browse the repository at this point in the history
* External logs view enhancement
* External logs view fixes

Co-authored-by: cif-ps <ci-ps@pevesoft.ro>
#10766
  • Loading branch information
cp-ps committed Mar 10, 2021
1 parent 623cd3b commit 20c3f08
Show file tree
Hide file tree
Showing 11 changed files with 745 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public ProcessExecutionResult invokeExternalSystem(@NonNull final InvokeExternal
processInfoBuilder.addParameter(PARAM_EXTERNAL_REQUEST, invokeExternalSystemProcessRequest.getRequest());
processInfoBuilder.addParameter(PARAM_CHILD_CONFIG_ID, configIdAsString);

processInfoBuilder.setRecord(externalSystemParentConfig.getTableRecordReference());

final ProcessExecutionResult processExecutionResult = processInfoBuilder
.buildAndPrepareExecution()
.executeSync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ public Optional<ExternalSystemParentConfig> getByTypeAndValue(@NonNull final Ext
}
}

public Optional<IExternalSystemChildConfig> getChildByParentIdAndType(@NonNull final ExternalSystemParentConfigId id, @NonNull final ExternalSystemType externalSystemType)
{
switch (externalSystemType)
{
case Alberta:
return getAlbertaConfigByParentId(id);
case Shopware6:
return getShopware6ConfigByParentId(id);
default:
throw Check.fail("Unsupported IExternalSystemChildConfigId.type={}", externalSystemType);
}
}

@NonNull
private Optional<I_ExternalSystem_Config_Alberta> getAlbertaConfigByValue(@NonNull final String value)
{
Expand All @@ -94,6 +107,28 @@ private Optional<I_ExternalSystem_Config_Shopware6> getShopware6ConfigByValue(@N
.firstOnlyOptional(I_ExternalSystem_Config_Shopware6.class);
}

@NonNull
private Optional<IExternalSystemChildConfig> getAlbertaConfigByParentId(@NonNull final ExternalSystemParentConfigId id)
{
return queryBL.createQueryBuilder(I_ExternalSystem_Config_Alberta.class)
.addOnlyActiveRecordsFilter()
.addEqualsFilter(I_ExternalSystem_Config_Alberta.COLUMNNAME_ExternalSystem_Config_ID, id.getRepoId())
.create()
.firstOnlyOptional(I_ExternalSystem_Config_Alberta.class)
.map(ex -> buildExternalSystemAlbertaConfig(ex, id));
}

@NonNull
private Optional<IExternalSystemChildConfig> getShopware6ConfigByParentId(@NonNull final ExternalSystemParentConfigId id)
{
return queryBL.createQueryBuilder(I_ExternalSystem_Config_Shopware6.class)
.addOnlyActiveRecordsFilter()
.addEqualsFilter(I_ExternalSystem_Config_Shopware6.COLUMNNAME_ExternalSystem_Config_ID, id.getRepoId())
.create()
.firstOnlyOptional(I_ExternalSystem_Config_Shopware6.class)
.map(ex -> buildExternalSystemShopware6Config(ex, id));
}

private ExternalSystemParentConfig getById(@NonNull final ExternalSystemAlbertaConfigId id)
{
final I_ExternalSystem_Config_Alberta config = InterfaceWrapperHelper.load(id, I_ExternalSystem_Config_Alberta.class);
Expand All @@ -105,18 +140,25 @@ private ExternalSystemParentConfig getExternalSystemParentConfig(@NonNull final
{
final ExternalSystemParentConfigId parentConfigId = ExternalSystemParentConfigId.ofRepoId(config.getExternalSystem_Config_ID());

final ExternalSystemAlbertaConfig child = ExternalSystemAlbertaConfig.builder()
final ExternalSystemAlbertaConfig child = buildExternalSystemAlbertaConfig(config, parentConfigId);

return getById(parentConfigId)
.childConfig(child)
.build();
}

@NonNull
private ExternalSystemAlbertaConfig buildExternalSystemAlbertaConfig(final @NonNull I_ExternalSystem_Config_Alberta config,
@NonNull final ExternalSystemParentConfigId parentConfigId)
{
return ExternalSystemAlbertaConfig.builder()
.id(ExternalSystemAlbertaConfigId.ofRepoId(config.getExternalSystem_Config_Alberta_ID()))
.parentId(parentConfigId)
.apiKey(config.getApiKey())
.baseUrl(config.getBaseURL())
.name(config.getName())
.tenant(config.getTenant())
.build();

return getById(parentConfigId)
.childConfig(child)
.build();
}

private ExternalSystemParentConfig getById(@NonNull final ExternalSystemShopware6ConfigId id)
Expand All @@ -130,17 +172,24 @@ private ExternalSystemParentConfig getExternalSystemParentConfig(@NonNull final
{
final ExternalSystemParentConfigId parentConfigId = ExternalSystemParentConfigId.ofRepoId(config.getExternalSystem_Config_ID());

final ExternalSystemShopware6Config child = ExternalSystemShopware6Config.builder()
final ExternalSystemShopware6Config child = buildExternalSystemShopware6Config(config, parentConfigId);

return getById(parentConfigId)
.childConfig(child)
.build();
}

@NonNull
private ExternalSystemShopware6Config buildExternalSystemShopware6Config(final @NonNull I_ExternalSystem_Config_Shopware6 config,
@NonNull final ExternalSystemParentConfigId parentConfigId)
{
return ExternalSystemShopware6Config.builder()
.id(ExternalSystemShopware6ConfigId.ofRepoId(config.getExternalSystem_Config_Shopware6_ID()))
.parentId(parentConfigId)
.baseUrl(config.getBaseURL())
.clientSecret(config.getClient_Secret())
.clientId(config.getClient_Id())
.build();

return getById(parentConfigId)
.childConfig(child)
.build();
}

private ExternalSystemParentConfig.ExternalSystemParentConfigBuilder getById(final @NonNull ExternalSystemParentConfigId id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@

package de.metas.externalsystem;

import de.metas.externalsystem.model.I_ExternalSystem_Config;
import lombok.Builder;
import lombok.NonNull;
import lombok.Value;
import org.adempiere.util.lang.ITableRecordReference;
import org.adempiere.util.lang.impl.TableRecordReference;

@Value
public class ExternalSystemParentConfig
Expand All @@ -49,4 +52,9 @@ public ExternalSystemParentConfig(
this.name = name;
this.childConfig = childConfig;
}

public ITableRecordReference getTableRecordReference()
{
return TableRecordReference.of(I_ExternalSystem_Config.Table_Name, this.id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import de.metas.common.externalsystem.ExternalSystemConstants;
import de.metas.externalsystem.ExternalSystemParentConfig;
import de.metas.externalsystem.ExternalSystemParentConfigId;
import de.metas.externalsystem.ExternalSystemType;
import de.metas.externalsystem.IExternalSystemChildConfigId;
import de.metas.externalsystem.alberta.ExternalSystemAlbertaConfig;
Expand Down Expand Up @@ -56,9 +57,10 @@ protected IExternalSystemChildConfigId getExternalChildConfigId()
}
else
{
// note: we can blindly get the first I_ExternalSystem_Config_Alberta, because the checkPreconditionsApplicable impl made sure there is exactly one.
id = getSelectedIncludedRecordIds(I_ExternalSystem_Config_Alberta.class).stream().findFirst().get();
id = externalSystemConfigDAO.getChildByParentIdAndType(ExternalSystemParentConfigId.ofRepoId(getRecord_ID()), getExternalSystemType())
.get().getId().getRepoId();
}

return ExternalSystemAlbertaConfigId.ofRepoId(id);
}

Expand All @@ -81,4 +83,10 @@ protected String getTabName()
{
return ExternalSystemType.Alberta.getName();
}

@NonNull
protected ExternalSystemType getExternalSystemType()
{
return ExternalSystemType.Alberta;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import de.metas.common.util.CoalesceUtil;
import de.metas.externalsystem.ExternalSystemConfigRepo;
import de.metas.externalsystem.ExternalSystemParentConfig;
import de.metas.externalsystem.ExternalSystemParentConfigId;
import de.metas.externalsystem.ExternalSystemType;
import de.metas.externalsystem.IExternalSystemChildConfig;
import de.metas.externalsystem.IExternalSystemChildConfigId;
import de.metas.i18n.AdMessageKey;
import de.metas.i18n.IMsgBL;
Expand Down Expand Up @@ -57,6 +60,7 @@
import java.sql.Timestamp;
import java.time.Instant;
import java.util.Map;
import java.util.Optional;

public abstract class InvokeExternalSystemProcess extends JavaProcess implements IProcessPrecondition, IProcessDefaultParametersProvider
{
Expand Down Expand Up @@ -132,14 +136,20 @@ public ProcessPreconditionsResolution checkPreconditionsApplicable(final @NonNul
}

final long selectedRecordsCount = getSelectedRecordCount(context);
if (selectedRecordsCount == 0)
{
return ProcessPreconditionsResolution.reject(Services.get(IMsgBL.class).getTranslatableMsgText(MSG_ERR_NO_EXTERNAL_SELECTION, getTabName()));
}
if (selectedRecordsCount > 1)
{
return ProcessPreconditionsResolution.reject(Services.get(IMsgBL.class).getTranslatableMsgText(MSG_ERR_MULTIPLE_EXTERNAL_SELECTION, getTabName()));
}
else if (selectedRecordsCount == 0)
{
final Optional<IExternalSystemChildConfig> childConfig =
externalSystemConfigDAO.getChildByParentIdAndType(ExternalSystemParentConfigId.ofRepoId(context.getSingleSelectedRecordId()), getExternalSystemType());

if (!childConfig.isPresent())
{
return ProcessPreconditionsResolution.reject(Services.get(IMsgBL.class).getTranslatableMsgText(MSG_ERR_NO_EXTERNAL_SELECTION, getTabName()));
}
}

return ProcessPreconditionsResolution.accept();
}
Expand Down Expand Up @@ -173,6 +183,8 @@ private Timestamp retrieveSinceValue()

protected abstract String getTabName();

protected abstract ExternalSystemType getExternalSystemType();

protected abstract long getSelectedRecordCount(final IProcessPreconditionsContext context);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import de.metas.common.externalsystem.ExternalSystemConstants;
import de.metas.externalsystem.ExternalSystemParentConfig;
import de.metas.externalsystem.ExternalSystemParentConfigId;
import de.metas.externalsystem.ExternalSystemType;
import de.metas.externalsystem.IExternalSystemChildConfigId;
import de.metas.externalsystem.model.I_ExternalSystem_Config_Shopware6;
Expand All @@ -35,7 +36,8 @@
import java.util.HashMap;
import java.util.Map;

public class InvokeShopware6Action extends InvokeExternalSystemProcess {
public class InvokeShopware6Action extends InvokeExternalSystemProcess
{
@Override
protected IExternalSystemChildConfigId getExternalChildConfigId()
{
Expand All @@ -47,9 +49,10 @@ protected IExternalSystemChildConfigId getExternalChildConfigId()
}
else
{
// note: we can blindly get the first I_ExternalSystem_Config_Shopware6, because the checkPreconditionsApplicable impl made sure there is exactly one.
id = getSelectedIncludedRecordIds(I_ExternalSystem_Config_Shopware6.class).stream().findFirst().get();
id = externalSystemConfigDAO.getChildByParentIdAndType(ExternalSystemParentConfigId.ofRepoId(getRecord_ID()), getExternalSystemType())
.get().getId().getRepoId();
}

return ExternalSystemShopware6ConfigId.ofRepoId(id);
}

Expand Down Expand Up @@ -81,4 +84,10 @@ protected long getSelectedRecordCount(final IProcessPreconditionsContext context
.filter(recordRef -> I_ExternalSystem_Config_Shopware6.Table_Name.equals(recordRef.getTableName()))
.count();
}

@NonNull
protected ExternalSystemType getExternalSystemType()
{
return ExternalSystemType.Shopware6;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
DROP VIEW IF EXISTS ExternalSystem_Config_PInstance_Log_v
;

CREATE OR REPLACE VIEW ExternalSystem_Config_PInstance_Log_v AS
SELECT pi.ad_process_id,
pil.AD_PInstance_Log_ID AS ExternalSystem_Config_PInstance_Log_v_ID,
pip_request.info AS External_Request,
pil.p_msg,
pil.p_date,
SELECT (i.ad_issue_id * 10) + 2 AS ExternalSystem_Config_PInstance_Log_v_ID,
pi.ad_process_id,
pip_request.info AS External_Request,
null as p_msg,
i.created as p_date,
pi.ad_client_id,
COALESCE(
NULLIF(pi.record_id, '0'), /*Record_ID is not set then the process was called from AD_Scheduler */
Expand All @@ -12,15 +15,48 @@ SELECT pi.ad_process_id,
pi.ad_pinstance_id,
pi.isprocessing,
pi.errormsg,
now() as created,
100 as createdby,
now() as updated,
100 as updatedby,
'Y' as isactive
FROM ad_pinstance pi
i.ad_issue_id as ad_issue_id,
i.issuesummary as issuesummary,
'Error' as type,
now() as created,
100 as createdby,
now() as updated,
100 as updatedby,
'Y' as isactive
from ad_pinstance pi
JOIN ad_pinstance_para pip_request ON pi.ad_pinstance_id = pip_request.ad_pinstance_id AND pip_request.parametername = 'External_Request'
JOIN ad_issue i on i.ad_pinstance_id = pi.ad_pinstance_id
LEFT JOIN ad_pinstance_para pip_config ON pi.ad_pinstance_id = pip_config.ad_pinstance_id AND pip_config.parametername = 'ChildConfigId'
JOIN ad_pinstance_log pil ON pil.ad_pinstance_id = pi.ad_pinstance_id
WHERE pi.AD_Table_ID = get_table_id('ExternalSystem_Config') /*AD_Table_ID is not set then the process was called from AD_Scheduler */
OR pip_config.parametername = 'ChildConfigId'
;

UNION
SELECT (pil.ad_pinstance_log_id * 10) + 1 AS ExternalSystem_Config_PInstance_Log_v_ID,
pinstance.ad_process_id,
pip_request.info AS External_Request,
pil.p_msg,
pil.p_date,
pinstance.ad_client_id,
COALESCE(
NULLIF(pinstance.record_id, '0'), /*Record_ID is not set then the process was called from AD_Scheduler */
cast_to_numeric_or_null(pip_config.p_string)) AS ExternalSystem_Config_ID,
pinstance.ad_org_id,
pinstance.ad_pinstance_id,
pinstance.isprocessing,
pinstance.errormsg,
null as ad_issue_id,
null as issuesummary,
'Info' as type,
now() as created,
100 as createdby,
now() as updated,
100 as updatedby,
'Y' as isactive

from ad_pinstance pinstance
JOIN ad_pinstance_para pip_request ON pinstance.ad_pinstance_id = pip_request.ad_pinstance_id AND pip_request.parametername = 'External_Request'
JOIN ad_pinstance_log pil ON pil.ad_pinstance_id = pinstance.ad_pinstance_id
LEFT JOIN ad_pinstance_para pip_config ON pinstance.ad_pinstance_id = pip_config.ad_pinstance_id AND pip_config.parametername = 'ChildConfigId'
WHERE pinstance.AD_Table_ID = get_table_id('ExternalSystem_Config') /*AD_Table_ID is not set then the process was called from AD_Scheduler */
OR pip_config.parametername = 'ChildConfigId'
;

0 comments on commit 20c3f08

Please sign in to comment.