001/*-
002 * #%L
003 * Smile CDR - CDR
004 * %%
005 * Copyright (C) 2016 - 2026 Smile CDR, Inc.
006 * %%
007 * All rights reserved.
008 * #L%
009 */
010package ca.cdr.api.camel;
011
012import ca.cdr.api.model.enm.TransactionLogBodyTypeEnum;
013import ca.cdr.api.model.enm.TransactionLogOutcomeEnum;
014import ca.cdr.api.model.enm.TransactionLogStepTypeEnum;
015import ca.cdr.api.model.json.TransactionLogStepJson;
016import ca.cdr.api.transactionlog.ITransactionLogCommonSettings;
017import org.apache.camel.Exchange;
018import org.apache.commons.lang3.tuple.Pair;
019
020import java.util.List;
021import java.util.Optional;
022
023public interface ICamelProcessorTxLogHelper {
024
025        /**
026         * Requests to include message body in the transaction log step
027         * Default value is true
028         */
029        String TX_LOG_PARAM_SHOW_BODY = "showMsgBody";
030
031        /**
032         * If transaction logging was initiated (smile:txLogStart procedure, present before current procedure in
033         * route), adds a log step to the transaction log
034         * @param theExchange           the camel exchange
035         * @param theProcedureName  the log-producing procedure name
036         */
037        void addStepIfTxLogActive(Exchange theExchange, String theProcedureName);
038
039        /**
040         * If transaction logging was initiated (smile:txLogStart procedure, present before current procedure in
041         * route), adds step from provider to the transaction log
042         * @param theExchange           the camel exchange
043         * @param theStepsProvider  the provider of the corresponding transaction log steps
044         */
045        void addStepIfTxLogActive(Exchange theExchange, ITxLogStepsProvider theStepsProvider);
046
047        /**
048         * If transaction logging was initiated (smile:txLogStart procedure, present before current procedure in
049         * route), adds step from provider to the transaction log, adds the list of steps to the transaction log
050         * @param theExchange  the camel exchange
051         * @param theLogSteps  the log steps to be added to the transaction log
052         */
053        void addStepIfTxLogActive(Exchange theExchange, List<TransactionLogStepJson> theLogSteps);
054
055        /**
056         * Builds a new transaction log step from provided exchange properties and parameters.
057         * If theExchange.isFailed(), the step outcome is set to FAIL, otherwise SUCCESS.
058         * @param theExchange           the camel exchange
059         * @param theLogStepType        the transaction log step type
060         * @param theBodyAndType        A Pair containing the body and the body type or null
061         * @return the built transaction log step
062         */
063        TransactionLogStepJson buildTxLogStep(
064                        Exchange theExchange,
065                        TransactionLogStepTypeEnum theLogStepType,
066                        Pair<String, TransactionLogBodyTypeEnum> theBodyAndType);
067
068        /**
069         * @deprecated use {@link ICamelProcessorTxLogHelper#buildTxLogStep(Exchange, TransactionLogStepTypeEnum, Pair)}
070         * Builds a new transaction log step considering received exchange properties and parameters.
071         * @param theExchange           the camel exchange
072         * @param theLogStepType        the transaction log step type
073         * @param theBodyAndType        A Pair containing the body and the body type or null
074         * @param theOutcome            Ignored.
075         * @return the built transaction log step
076         */
077        @Deprecated
078        default TransactionLogStepJson buildTxLogStep(
079                        Exchange theExchange,
080                        TransactionLogStepTypeEnum theLogStepType,
081                        Pair<String, TransactionLogBodyTypeEnum> theBodyAndType,
082                        @SuppressWarnings("unused") TransactionLogOutcomeEnum theOutcome) {
083                return buildTxLogStep(theExchange, theLogStepType, theBodyAndType);
084        }
085
086        /**
087         * Builds a string containing the received procedure name, including the module id if present in the exchange
088         * @param theExchange           the camel exchange
089         * @param theProcedureName  the log-producing procedure name
090         * @return a string with the built executing procedure URI
091         */
092        String getRequestUrl(Exchange theExchange, String theProcedureName);
093
094        /**
095         * Informs if 'showMsgBody' parameter is present
096         * @param theExchange           the camel exchange
097         * @return boolean indicating if 'showMsgBody' parameter is present
098         */
099        boolean isTxLogShowBody(Exchange theExchange);
100
101        /**
102         * informs if a transaction log is active for the route
103         * @param theExchange the camel exchange
104         * @return boolean indicating if transaction log is active for the route
105         */
106        boolean isTxLogStarted(Exchange theExchange);
107
108        /**
109         * Requests transaction log initiation for the route
110         * @param theExchange the camel exchange
111         */
112        void startTxLog(Exchange theExchange);
113
114        /**
115         * Requests transaction log initiation for the route. This initiates the log before the route is invoked, allowing
116         * the caller to add entries to the log before passing control to Camel. The transaction log wil not auto-commit when
117         * the route finishes, allowing the caller to continue adding entries after the Camel invocation returns.
118         * The log will only commit once **both** the route is complete and the caller has called
119         * {@link #manualCommitTxLog(Exchange)}
120         * @param theExchange the camel exchange
121         */
122        void startTxLogForManualCommit(Exchange theExchange);
123
124        /**
125         * Commits the transaction log to configured writers
126         * @param theExchange the camel exchange
127         */
128        void commitTxLog(Exchange theExchange);
129
130        /**
131         * Allows a transaction log that was initiated with {@link #startTxLogForManualCommit(Exchange)} to commit.
132         * The log may not commit immediately if the route is still processing (async mode).
133         * @param theExchange the camel exchange
134         */
135        void manualCommitTxLog(Exchange theExchange);
136
137        /**
138         * Discards current transaction logging and marks logging not initiated for the route
139         * @param theExchange the camel exchange
140         */
141        void clearTxLog(Exchange theExchange);
142
143        /**
144         * Returns the settings associated with this writer. Event sources can use this to optimize whether to
145         * include specific elements in their log entries.
146         */
147        ITransactionLogCommonSettings getSettings();
148
149        /**
150         * Obtain a value stored in the exchange request details map
151         *
152         * @param theKey the key for referencing the object to extract from the request details map
153         * @param theExchange the camel exchange encapsulating the request details map
154         * @return the value stored in the exchange request details map, or null if not found
155         */
156        <T> T getPropertyFromRequestDetails(String theKey, Exchange theExchange);
157
158        /**
159         * Store a key/value in the exchange request details map
160         *
161         * @param theKey the key that will reference the object being stored in request details map
162         * @param theValue the object being stored in request details map
163         * @param theExchange the camel exchange encapsulating the request details map
164         */
165        void addPropertyToRequestDetails(String theKey, Object theValue, Exchange theExchange);
166
167        /**
168         * Returns the current transaction log guid for the given exchange.
169         *
170         * NB: it's not enough to start a transaction; a step must be added
171         * before a tx guid is generated.
172         * If no step has been added, an empty optional will be returned.
173         *
174         * @param theExchange - the exchange
175         * @return - the txlog guid (if present), or null (if no steps have been
176         *                              added yet).
177         */
178        Optional<String> getCurrentTxLogGuid(Exchange theExchange);
179}