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