Skip to content

Commit

Permalink
push products to alberta - next steps (#10857)
Browse files Browse the repository at this point in the history
* - push products to alberta - m_product_id mandatory
* - push products to alberta - camel unit test
* - export audit index

refs: #10818
  • Loading branch information
cp-ps committed Mar 22, 2021
1 parent aff1c1e commit 7cb44f5
Show file tree
Hide file tree
Showing 48 changed files with 1,652 additions and 214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import de.metas.uom.UomId;
import de.metas.util.Check;
import de.metas.vertical.healthcare.alberta.service.AlbertaCompositeProductInfo;
import de.metas.vertical.healthcare.alberta.service.AlbertaPackagingUnit;
import de.metas.vertical.healthcare.alberta.service.AlbertaProductService;
import de.metas.vertical.healthcare.alberta.service.GetAlbertaProductsInfoRequest;
import lombok.Builder;
Expand Down Expand Up @@ -259,14 +260,11 @@ private JsonAlbertaProductInfo mapToJsonAlbertaProductInfo(@NonNull final Albert
? null
: albertaCompositeProductInfo.getAlbertaPackagingUnitList()
.stream()
.map(albertaPackagingUnit ->
JsonAlbertaPackagingUnit.builder()
.quantity(albertaPackagingUnit.getQuantity())
.unit(albertaPackagingUnit.getUnit())
.build())
.map(this::mapToJsonAlbertaPackagingUnit)
.collect(ImmutableList.toImmutableList());

return JsonAlbertaProductInfo.builder()
.albertaProductId(albertaCompositeProductInfo.getAlbertaArticleId())
.additionalDescription(albertaCompositeProductInfo.getAdditionalDescription())
.assortmentType(albertaCompositeProductInfo.getAssortmentType())
.inventoryType(albertaCompositeProductInfo.getInventoryType())
Expand Down Expand Up @@ -330,4 +328,13 @@ private PriceListId getPharmacyPriceListIdOrNull()
.map(ExternalSystemAlbertaConfig::getPharmacyPriceListId)
.orElse(null);
}

@NonNull
private JsonAlbertaPackagingUnit mapToJsonAlbertaPackagingUnit(@NonNull final AlbertaPackagingUnit albertaPackagingUnit)
{
return JsonAlbertaPackagingUnit.builder()
.quantity(albertaPackagingUnit.getQuantity())
.unit(albertaPackagingUnit.getUnit())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public void init()
Env.setLoggedUserId(Env.getCtx(), UserId.METASFRESH);

final ProductsServicesFacade productsServicesFacade = new ProductsServicesFacade();
final AlbertaProductService albertaProductService = new AlbertaProductService(new AlbertaProductDAO());

final ExternalSystemService externalSystemService = new ExternalSystemService(new ExternalSystemConfigRepo(), new ExternalSystemExportAuditRepo());
final ProductRepository productRepository = new ProductRepository();
Expand All @@ -105,6 +104,7 @@ public void init()

final ExternalReferenceRestControllerService externalReferenceRestControllerService =
new ExternalReferenceRestControllerService(externalReferenceRepository, new ExternalSystems(), new ExternalReferenceTypes());
final AlbertaProductService albertaProductService = new AlbertaProductService(new AlbertaProductDAO(), externalReferenceRepository);

final ProductRestService productRestService = new ProductRestService(productRepository, externalReferenceRestControllerService);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public Integer getReferencedRecordIdOrNullBy(final @NonNull ExternalReferenceQue

public ExternalReferenceId save(@NonNull final ExternalReference externalReference)
{
final I_S_ExternalReference record = InterfaceWrapperHelper.newInstance(I_S_ExternalReference.class);
final I_S_ExternalReference record = InterfaceWrapperHelper.loadOrNew(externalReference.getExternalReferenceId(), I_S_ExternalReference.class);

record.setAD_Org_ID(externalReference.getOrgId().getRepoId());
record.setExternalReference(externalReference.getExternalReference());
Expand Down Expand Up @@ -152,6 +152,19 @@ public ImmutableMap<ExternalReferenceQuery, ExternalReference> getExternalRefere
return result.build();
}

@NonNull
public Optional<ExternalReference> getExternalReferenceByMFReference(@NonNull final GetExternalReferenceByRecordIdReq request)
{
return queryBL.createQueryBuilder(I_S_ExternalReference.class)
.addOnlyActiveRecordsFilter()
.addEqualsFilter(I_S_ExternalReference.COLUMNNAME_Record_ID, request.getRecordId())
.addEqualsFilter(I_S_ExternalReference.COLUMNNAME_Type, request.getExternalReferenceType().getCode())
.addEqualsFilter(I_S_ExternalReference.COLUMNNAME_ExternalSystem, request.getExternalSystem().getCode())
.create()
.firstOnlyOptional(I_S_ExternalReference.class)
.map(this::buildExternalReference);
}

private ExternalReferenceQuery buildExternalReferenceQuery(final I_S_ExternalReference record)
{
final IExternalReferenceType type = extractType(record);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* #%L
* de.metas.externalreference
* %%
* Copyright (C) 2021 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%
*/

package de.metas.externalreference;

import lombok.Builder;
import lombok.NonNull;
import lombok.Value;

@Value
@Builder
public class GetExternalReferenceByRecordIdReq
{
@NonNull
Integer recordId;
@NonNull
IExternalReferenceType externalReferenceType;
@NonNull
IExternalSystem externalSystem;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
package de.metas.externalreference.rest;

import de.metas.Profiles;
import de.metas.util.web.MetasfreshRestAPIConstants;
import de.metas.common.externalreference.JsonExternalReferenceCreateRequest;
import de.metas.common.externalreference.JsonExternalReferenceLookupRequest;
import de.metas.common.externalreference.JsonExternalReferenceLookupResponse;
import de.metas.common.externalreference.JsonExternalReferenceCreateRequest;
import de.metas.common.externalreference.JsonRequestExternalReferenceUpsert;
import de.metas.util.web.MetasfreshRestAPIConstants;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import org.springframework.context.annotation.Profile;
Expand All @@ -38,6 +39,8 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Nullable;

@RequestMapping(value = {
MetasfreshRestAPIConstants.ENDPOINT_API_DEPRECATED + "/externalRef",
MetasfreshRestAPIConstants.ENDPOINT_API_V1 + "/externalRef",
Expand Down Expand Up @@ -82,5 +85,17 @@ public ResponseEntity<?> insert(
return ResponseEntity.ok().build();
}

@PutMapping("/upsert/{orgCode}")
public ResponseEntity<?> upsert(
@ApiParam(required = true, value = "`AD_Org.Value` of the external references we are upserting") //
@PathVariable("orgCode") //
@Nullable final String orgCode,

@RequestBody @NonNull final JsonRequestExternalReferenceUpsert request)
{
externalReferenceRestControllerService.performUpsert(request, orgCode);

return ResponseEntity.ok().build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import de.metas.common.externalreference.JsonExternalReferenceLookupItem;
import de.metas.common.externalreference.JsonExternalReferenceLookupRequest;
import de.metas.common.externalreference.JsonExternalReferenceLookupResponse;
import de.metas.common.externalreference.JsonRequestExternalReferenceUpsert;
import de.metas.common.externalreference.JsonSingleExternalReferenceCreateReq;
import de.metas.common.externalsystem.JsonExternalSystemName;
import de.metas.common.rest_api.JsonMetasfreshId;
Expand All @@ -39,11 +40,12 @@
import de.metas.externalreference.ExternalReferenceRepository;
import de.metas.externalreference.ExternalReferenceTypes;
import de.metas.externalreference.ExternalSystems;
import de.metas.externalreference.GetExternalReferenceByRecordIdReq;
import de.metas.externalreference.IExternalReferenceType;
import de.metas.externalreference.IExternalSystem;
import de.metas.logging.LogManager;
import de.metas.organization.OrgId;
import de.metas.organization.impl.OrgDAO;
import de.metas.util.Check;
import de.metas.util.web.exception.InvalidIdentifierException;
import lombok.NonNull;
import org.slf4j.Logger;
Expand All @@ -55,6 +57,8 @@
import java.util.Objects;
import java.util.Optional;

import static de.metas.RestUtils.retrieveOrgIdOrDefault;

@Component
public class ExternalReferenceRestControllerService
{
Expand All @@ -78,7 +82,7 @@ public JsonExternalReferenceLookupResponse performLookup(
@Nullable final String orgCode,
@NonNull final JsonExternalReferenceLookupRequest request)
{
final OrgId orgId = RestUtils.retrieveOrgIdOrDefault(orgCode);
final OrgId orgId = retrieveOrgIdOrDefault(orgCode);

final IExternalSystem externalSystem = externalSystems.ofCode(request.getSystemName().getName())
.orElseThrow(() -> new InvalidIdentifierException("systemName", request));
Expand Down Expand Up @@ -211,7 +215,6 @@ public void performInsertIfMissing(
performInsert(orgCode, jsonExternalReferenceCreateRequest);
}


@NonNull
public Optional<JsonMetasfreshId> getJsonMetasfreshIdFromExternalReference(
@NonNull final String orgCode,
Expand All @@ -235,4 +238,61 @@ public Optional<JsonMetasfreshId> getJsonMetasfreshIdFromExternalReference(
.filter(Objects::nonNull)
.findFirst();
}

public void performUpsert(@NonNull final JsonRequestExternalReferenceUpsert request, @Nullable final String orgCode)
{
final OrgId orgId = retrieveOrgIdOrDefault(orgCode);
final ExternalReference externalReferenceCandidate = mapJsonToExternalReference(request, orgId);

final GetExternalReferenceByRecordIdReq getExternalRefRequest = GetExternalReferenceByRecordIdReq.builder()
.externalReferenceType(externalReferenceCandidate.getExternalReferenceType())
.externalSystem(externalReferenceCandidate.getExternalSystem())
.recordId(externalReferenceCandidate.getRecordId())
.build();

final ExternalReference externalReferenceToUpsert = externalReferenceRepository.getExternalReferenceByMFReference(getExternalRefRequest)
.map(existingRecord -> syncCandidateWithExisting(externalReferenceCandidate, existingRecord))
.orElse(externalReferenceCandidate);

externalReferenceRepository.save(externalReferenceToUpsert);
}

@NonNull
private ExternalReference mapJsonToExternalReference(@NonNull final JsonRequestExternalReferenceUpsert request, @NonNull final OrgId orgId)
{
Check.assumeNotNull(request.getExternalReferenceItem().getMetasfreshId(), "MetasfreshId cannot be null at this stage!");

final IExternalReferenceType externalReferenceType = externalReferenceTypes.ofCode(request.getExternalReferenceItem().getLookupItem().getType())
.orElseThrow(() -> new InvalidIdentifierException("type", request.getExternalReferenceItem().getLookupItem().getType()));

final IExternalSystem externalSystem = externalSystems.ofCode(request.getSystemName().getName())
.orElseThrow(() -> new InvalidIdentifierException("externalSystem", request.getSystemName().getName()));

return ExternalReference.builder()
.orgId(orgId)
.externalSystem(externalSystem)
.externalReferenceType(externalReferenceType)
.externalReference(request.getExternalReferenceItem().getLookupItem().getId())
.recordId(request.getExternalReferenceItem().getMetasfreshId().getValue())
.version(request.getExternalReferenceItem().getVersion())
.build();
}

@NonNull
private ExternalReference syncCandidateWithExisting(
@NonNull final ExternalReference candidate,
@NonNull final ExternalReference existingReference)
{
return ExternalReference.builder()
//existing
.externalReferenceId(existingReference.getExternalReferenceId())
//candidate
.orgId(candidate.getOrgId())
.externalSystem(candidate.getExternalSystem())
.externalReferenceType(candidate.getExternalReferenceType())
.externalReference(candidate.getExternalReference())
.recordId(candidate.getRecordId())
.version(candidate.getVersion())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package de.metas.externalsystem.model;

import javax.annotation.Nullable;
import org.adempiere.model.ModelColumn;

import javax.annotation.Nullable;

/** Generated Interface for ExternalSystem_Config
* @author metasfresh (generated)
*/
Expand Down Expand Up @@ -57,7 +58,7 @@ public interface I_ExternalSystem_Config
* <br>Mandatory: true
* <br>Virtual Column: false
*/
void setCamelURL (java.lang.String CamelURL);
void setCamelURL (String CamelURL);

/**
* Get Camel-URL.
Expand All @@ -66,7 +67,7 @@ public interface I_ExternalSystem_Config
* <br>Mandatory: true
* <br>Virtual Column: false
*/
java.lang.String getCamelURL();
String getCamelURL();

ModelColumn<I_ExternalSystem_Config, Object> COLUMN_CamelURL = new ModelColumn<>(I_ExternalSystem_Config.class, "CamelURL", null);
String COLUMNNAME_CamelURL = "CamelURL";
Expand Down Expand Up @@ -103,7 +104,7 @@ public interface I_ExternalSystem_Config
* <br>Mandatory: false
* <br>Virtual Column: false
*/
void setDescription (@Nullable java.lang.String Description);
void setDescription (@Nullable String Description);

/**
* Get Description.
Expand All @@ -112,7 +113,7 @@ public interface I_ExternalSystem_Config
* <br>Mandatory: false
* <br>Virtual Column: false
*/
@Nullable java.lang.String getDescription();
@Nullable String getDescription();

ModelColumn<I_ExternalSystem_Config, Object> COLUMN_Description = new ModelColumn<>(I_ExternalSystem_Config.class, "Description", null);
String COLUMNNAME_Description = "Description";
Expand Down Expand Up @@ -168,7 +169,7 @@ public interface I_ExternalSystem_Config
* <br>Mandatory: true
* <br>Virtual Column: false
*/
void setName (java.lang.String Name);
void setName (String Name);

/**
* Get Name.
Expand All @@ -177,7 +178,7 @@ public interface I_ExternalSystem_Config
* <br>Mandatory: true
* <br>Virtual Column: false
*/
java.lang.String getName();
String getName();

ModelColumn<I_ExternalSystem_Config, Object> COLUMN_Name = new ModelColumn<>(I_ExternalSystem_Config.class, "Name", null);
String COLUMNNAME_Name = "Name";
Expand All @@ -190,7 +191,7 @@ public interface I_ExternalSystem_Config
* <br>Mandatory: true
* <br>Virtual Column: false
*/
void setType (java.lang.String Type);
void setType (String Type);

/**
* Get Type.
Expand All @@ -200,7 +201,7 @@ public interface I_ExternalSystem_Config
* <br>Mandatory: true
* <br>Virtual Column: false
*/
java.lang.String getType();
String getType();

ModelColumn<I_ExternalSystem_Config, Object> COLUMN_Type = new ModelColumn<>(I_ExternalSystem_Config.class, "Type", null);
String COLUMNNAME_Type = "Type";
Expand Down

0 comments on commit 7cb44f5

Please sign in to comment.