Adjustments

This commit is contained in:
Uwe Klinger
2021-06-11 10:40:17 +02:00
parent 870f8d063d
commit 097b74db03
5 changed files with 24 additions and 94 deletions

View File

@@ -3,7 +3,7 @@
*/ */
using from './admin/fiori-service'; using from './admin/fiori-service';
using from './browse/fiori-service'; //using from './browse/fiori-service';
using from './common'; using from './common';
using from '@capire/common'; using from '@capire/common';

View File

@@ -1,5 +0,0 @@
ID;name
ACME;A Company Making Everything
B4U;Books for You
S&C;Shakespeare & Co.
WSL;Waterstones
1 ID name
2 ACME A Company Making Everything
3 B4U Books for You
4 S&C Shakespeare & Co.
5 WSL Waterstones

View File

@@ -1,6 +1,3 @@
ID;name ID;name
ACME;A Company Making Everything (local) ACME;A Company Making Everything (replicated)
B4U;Books for You
S&C;Shakespeare & Co.
WSL;Waterstones
1 ID name
2 ACME A Company Making Everything (local) A Company Making Everything (replicated)
B4U Books for You
S&C Shakespeare & Co.
WSL Waterstones
3

View File

@@ -10,44 +10,18 @@ using { API_BUSINESS_PARTNER as S4 } from './external/API_BUSINESS_PARTNER.csn';
@cds.persistence: {table,skip:false} // add persistency @cds.persistence: {table,skip:false} // add persistency
@readonly @readonly
entity sap.capire.bookshop.Suppliers as projection on S4.A_BusinessPartner { entity sap.capire.bookshop.Suppliers as projection on S4.A_BusinessPartner {
// TODO: Aliases not supported in Java, yet?
key BusinessPartner as ID, key BusinessPartner as ID,
BusinessPartnerFullName as name, BusinessPartnerFullName as name
// REVISIT: following is not supported so far in cds compiler...
// to_BusinessPartnerAddress as city {
// CityCode as code,
// CityName as name
// }
// to_BusinessPartnerAddress
// REVISIT: following is not supported so far in cqn2odata...
// to_BusinessPartnerAddress.CityCode as city,
// to_BusinessPartnerAddress.CityName as city_name,
//// REVISIT: Should this be here or in the service, when it is only used for Fiori?
//// Compositions should work as well
//// The version with "virtual" is prefered, as this makes clear that the association is "added" here
// virtual books: Association to Books on book.supplier = $self,
// books2: Association to Books on book.supplier = $self,
//// Add virtual field, that does'nt exisits in the persistence or the underlying service
// virtual saveEnabled: Boolean
} excluding {
OrganizationBPName1, OrganizationBPName2,OrganizationBPName3, OrganizationBPName4, to_BuPaIdentification, to_BuPaIndustry, to_BusinessPartnerAddress, to_BusinessPartnerBank, to_BusinessPartnerContact, to_BusinessPartnerRole, to_BusinessPartnerTax, to_Customer, to_Supplier
} }
/*
// REVISIT: Alternative idea to use a specific replication view, but request data from You can also expose external entities through your own services
// a different view and manual map values. For this to work, you need to delegate the respective calls
// entity ReplicatedSuppliers as projection on Suppliers { addressed to your services into calls to the external service.
// ID, */
// name, extend service AdminService with {
// to_BusinessPartnerAddress.CityCode as city, entity Suppliers as projection on bookshop.Suppliers;
// to_BusinessPartnerAddress.CityName as city_name }
// }
/* /*
@@ -59,16 +33,6 @@ extend Books with {
supplier : Association to bookshop.Suppliers; supplier : Association to bookshop.Suppliers;
} }
/*
You can also expose external entities through your own services
For this to work, you need to delegate the respective calls
addressed to your services into calls to the external service.
*/
extend service AdminService with {
entity Suppliers as projection on bookshop.Suppliers;
}
/** /**
Having locally cached replicas also allows us to display supplier Having locally cached replicas also allows us to display supplier
data in lists of books, which otherwise would generate unwanted data in lists of books, which otherwise would generate unwanted

View File

@@ -31,10 +31,11 @@ module.exports = async()=>{ // called by server.js
if (!replicated) await replicate (supplierId, 'initial'); if (!replicated) await replicate (supplierId, 'initial');
}; };
if (supplierId) if (supplierId) {
return (await Promise.all ([ next(), replicateIfNotExists() ]))[0] const [result, _] = await Promise.all([next(), replicateIfNotExists()]);
else return result;
return next() //> don't forget to pass down the interceptor stack } else
return next(); //> don't forget to pass down the interceptor stack
}) })
}) })
@@ -42,46 +43,19 @@ module.exports = async()=>{ // called by server.js
// Subscribe to changes in the S4 origin of Suppliers data // Subscribe to changes in the S4 origin of Suppliers data
S4bupa.on ('BusinessPartner.Changed', async msg => { //> would be great if we had batch events from S/4 S4bupa.on ('BusinessPartner.Changed', async msg => { //> would be great if we had batch events from S/4
console.log(">>", msg.event) console.log(">>", msg.event)
const id = msg.data.BusinessPartner; const ID = msg.data.BusinessPartner;
let replica = await SELECT.one('ID').from (Suppliers) .where ('ID =', id); let replica = await SELECT.one('ID').from(Suppliers).where({ID});
if (replica) await replicate(id); if (replica) await replicate(ID);
}) })
/** /**
* Helper function to replicate Suppliers data. * Helper function to replicate Suppliers data.
* @param {string} a single ID * @param {string} ID a single ID
* @param {truthy|falsy} _initial indicates whether an insert or an update is required * @param {truthy|falsy} _initial indicates whether an insert or an update is required
*/ */
async function replicate (id,_initial) { async function replicate (ID,_initial) {
// TODO: Doesn't work when running in same process with mocked API_BUSINESS_PARTNER let supplier = await S4bupa.run(SELECT.one(Suppliers).where({ID}));
if (_initial) return db.insert(supplier).into(Suppliers);
// TODO: Doesn't work because fields are not mapped back! else return db.update(Suppliers).with(supplier).where({ID});
//let supplier = await S4bupa.run(SELECT.one('*').from(Suppliers).where('ID =',id));
let suppliers = await S4bupa.read(Suppliers).where('ID =',id);
if (_initial) return db.insert (suppliers) .into (Suppliers) //> using bulk insert
else return db.update(Suppliers,id) .with (suppliers[0]);
} }
// TODO: remove test code
{
// one server: returns AdminService.Suppliers
// two servers: returns API_BUSINESS_PARTNER.A_BusinessPartner
const tx = S4bupa.tx({});
let result = await tx.run(SELECT('*').from ('AdminService.Suppliers') .where ('ID =', 'ACME'));
tx.commit();
console.log(result);
}
// TODO: remove test code
{
// one server: returns AdminService.Suppliers
// two servers: returns AdminService.Suppliers
const tx = db.tx({});
let result = await db.run(SELECT('*').from ('AdminService.Suppliers') .where ('ID =', 'ACME'));
tx.commit();
console.log(result);
}
} }