Skip to content

Commit ced1f6b

Browse files
committed
update cds-plugin to simpler annotations
1 parent 0a78b30 commit ced1f6b

File tree

3 files changed

+66
-48
lines changed

3 files changed

+66
-48
lines changed

cds-plugin.js

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
const {
22
getFieldsHoldingPrintConfig,
3-
getAnnotatedParamsOfAction
3+
getAnnotatedParamsOfAction,
4+
getPrintParamsAttributeFromAction
45
} = require('./lib/annotation-helper');
56

67
const cds = require('@sap/cds');
8+
const LOG = cds.log('print');
9+
10+
const PRINT = "@print";
711

812
cds.once("served", async () => {
913
// Iterate over all services
@@ -24,35 +28,43 @@ cds.once("served", async () => {
2428
});
2529
}
2630

27-
// Track the fields holding print configurations
28-
await getFieldsHoldingPrintConfig(entity);
31+
if(!entity.actions) continue
2932

30-
// Check if the entity has actions
31-
if (entity.actions) {
32-
let actionsArray;
33+
for(const action of entity.actions) {
34+
if(action[PRINT]) {
3335

34-
// Convert actions to an array if it's an object
35-
if (Array.isArray(entity.actions)) {
36-
actionsArray = entity.actions;
37-
} else if (typeof entity.actions === 'object') {
38-
actionsArray = Object.values(entity.actions);
39-
}
36+
const printer = await cds.connect.to("print");
37+
38+
const { numberOfCopiesAttribute, queueIDAttribute, fileNameAttribute, contentAttribute } = getPrintParamsAttributeFromAction(entity, action);
39+
40+
srv.on(action.name, entity, async (req) => {
41+
42+
43+
const numberOfCopies = req.data[numberOfCopiesAttribute];
44+
const queueID = req.data[queueIDAttribute];
4045

41-
// Iterate over all bound actions
42-
for (let boundAction of actionsArray) {
43-
if(boundAction['@print']) {
44-
45-
// Track the action parameters holding print configurations
46-
getAnnotatedParamsOfAction(boundAction);
47-
48-
const actionName = boundAction.name.split('.').pop();
49-
50-
// Register for print related handling
51-
const printer = await cds.connect.to("print");
52-
srv.after(actionName, async (results, req) => {
53-
return printer.print(req);
54-
});
55-
}
46+
const object = await SELECT.one.from(req.subject).columns([fileNameAttribute, contentAttribute]);
47+
48+
try {
49+
50+
await printer.print({
51+
qname: queueID,
52+
numberOfCopies: numberOfCopies,
53+
docsToPrint: [{
54+
fileName: object[fileNameAttribute],
55+
content: object[contentAttribute].toString('base64'),
56+
isMainDocument: true
57+
}]
58+
})
59+
60+
}
61+
catch (error) {
62+
LOG.error(error)
63+
req.reject(500, `Printing failed: ${error.message ?? "Unknown error"}`);
64+
}
65+
66+
67+
});
5668
}
5769
}
5870
}

lib/annotation-helper.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
const fieldsHoldingPrintConfig = new Map();
22
const annotatedParametersOfAction = new Map();
3-
4-
/**
5-
* This method captures annotated fields of an entity.
6-
* Fields of entity which are annotated to be used in specific action are stored in actionSpecificConfigFields.
7-
* Rest of the annotated fields are stored in genericConfigFields.
8-
* @param {Object} entity - The entity object containing elements to be processed.
9-
* @returns {Map} - A map containing the fields holding print configuration.
10-
*/
11-
const getFieldsHoldingPrintConfig = async function(entity) {
12-
const {genericConfigFields, actionSpecificConfigFields} = extractFieldsHoldingPrintConfig(entity.elements);
13-
if (Object.keys(genericConfigFields).length > 0 || Object.keys(actionSpecificConfigFields).length > 0) {
14-
fieldsHoldingPrintConfig.set(entity.name, {
15-
genericConfigFields,
16-
actionSpecificConfigFields
17-
});
18-
}
19-
return fieldsHoldingPrintConfig;
20-
}
3+
const cds = require('@sap/cds');
214

225
/**
236
* This method captures annotated parameters of an action.
@@ -293,9 +276,31 @@ function getQueueValueHelpEntity(entity) {
293276
}
294277
}
295278

279+
function getPrintParamsAttributeFromAction(entity, action) {
280+
281+
const copiesElement = Object.values(action.params).find(el => el['@print.numberOfCopies']);
282+
const queueElement = Object.values(action.params).find(el => el['@print.queue']);
283+
284+
const fileName = Object.values(entity.elements).find(el => el['@print.fileName']);
285+
const content = Object.values(entity.elements).find(el => el['@print.fileContent']);
286+
287+
288+
if(!copiesElement || !queueElement, !fileName || !content) {
289+
cds.error(`Print action ${action.name} is missing required annotations. Make sure @print.numberOfCopies, @print.queue are present in the action and @print.fileName and @print.fileContent are present in the entity.`);
290+
}
291+
292+
return {
293+
numberOfCopiesAttribute: copiesElement.name,
294+
queueIDAttribute: queueElement.name,
295+
fileNameAttribute: fileName.name,
296+
contentAttribute: content.name
297+
};
298+
}
299+
300+
296301
module.exports = {
297-
getFieldsHoldingPrintConfig,
298302
getAnnotatedParamsOfAction,
299303
getPrintConfigFromActionOrEntity,
300-
getQueueValueHelpEntity
304+
getQueueValueHelpEntity,
305+
getPrintParamsAttributeFromAction
301306
}

srv/printToConsole.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ module.exports = class PrintToConsole extends PrintService {
5050
LOG.info(`\nDocument ${index + 1}: ${doc.fileName}`);
5151
LOG.info('-------------------------------');
5252
// Decode base64 content and display
53+
// Add following lines to show Base64
5354
// const content = Buffer.from(doc.content, 'base64').toString('utf-8');
54-
console.log(doc.content);
55+
// console.log({ content });
5556

5657
LOG.info('-------------------------------\n');
5758
});

0 commit comments

Comments
 (0)