001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2015 ForgeRock AS.
015 */
016package org.forgerock.services.context;
017
018import org.forgerock.services.TransactionId;
019import org.forgerock.json.JsonValue;
020
021/**
022 * This context aims to hold the {@link TransactionId}.
023 */
024public class TransactionIdContext extends AbstractContext {
025
026    private static final String ATTR_TRANSACTION_ID = "transactionId";
027
028    private final TransactionId transactionId;
029
030    /**
031     * Constructs a new TransactionIdContext.
032     *
033     * @param parent The parent context
034     * @param transactionId The transaction id to use in this context
035     */
036    public TransactionIdContext(Context parent, TransactionId transactionId) {
037        super(transactionId.getValue(), "transactionId", parent);
038        this.transactionId = transactionId;
039    }
040
041    /**
042     * Restores a saved context.
043     * @param savedContext The saved state.
044     * @param classLoader The {@code ClassLoader} to use.
045     */
046    public TransactionIdContext(JsonValue savedContext, ClassLoader classLoader) {
047        super(savedContext, classLoader);
048        this.transactionId = TransactionId.valueOf(data.get(ATTR_TRANSACTION_ID));
049    }
050
051    /**
052     * Returns the transaction id.
053     * @return the transaction id
054     */
055    public TransactionId getTransactionId() {
056        return transactionId;
057    }
058
059    /**
060     * Updates the data object to have the current transactionId state.
061     * {@inheritDoc}
062     */
063    @Override
064    public JsonValue toJsonValue() {
065        data.put(ATTR_TRANSACTION_ID, transactionId.toJson().getObject());
066        return super.toJsonValue();
067    }
068}