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 2006-2008 Sun Microsystems, Inc.
015 * Portions copyright 2015-2016 ForgeRock AS.
016 */
017package org.forgerock.opendj.config.server;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.forgerock.i18n.LocalizableMessage;
023import org.forgerock.opendj.ldap.ResultCode;
024import org.forgerock.util.Utils;
025
026/**
027 * This class defines a data structure that can be used to hold information
028 * about the result of processing a configuration change.
029 */
030public final class ConfigChangeResult {
031    /**
032     * A set of messages describing the changes that were made, any
033     * action that may be required, or any problems that were encountered.
034     */
035    private final List<LocalizableMessage> messages = new ArrayList<>();
036
037    /**
038     * Indicates whether one or more of the changes requires
039     * administrative action in order to take effect.
040     */
041    private boolean adminActionRequired;
042
043    /** The result code to return to the client from this configuration change. */
044    private ResultCode resultCode = ResultCode.SUCCESS;
045
046    /** Creates a new config change result object with the provided information. */
047    public ConfigChangeResult() {
048        // nothing more to do
049    }
050
051    /**
052     * Retrieves the result code for this config change result.
053     *
054     * @return The result code for this config change result.
055     */
056    public ResultCode getResultCode() {
057        return resultCode;
058    }
059
060    /**
061     * Specifies the result code for this config change result.
062     *
063     * @param resultCode
064     *            The result code for this config change result.
065     */
066    public void setResultCode(ResultCode resultCode) {
067        this.resultCode = resultCode;
068    }
069
070    /**
071     * Sets the provided result code for this config change result
072     * if the current result code is success.
073     *
074     * @param newResultCode
075     *          The new result code for this config change result.
076     */
077    public void setResultCodeIfSuccess(ResultCode newResultCode) {
078        if (getResultCode() == ResultCode.SUCCESS) {
079            setResultCode(newResultCode);
080        }
081    }
082
083    /**
084     * Aggregates the results from the provided config change result.
085     *
086     * @param other
087     *          The config change result to aggregate
088     */
089    public void aggregate(ConfigChangeResult other) {
090        if (other.getResultCode() != ResultCode.SUCCESS) {
091            setResultCodeIfSuccess(other.getResultCode());
092            messages.addAll(other.getMessages());
093        }
094    }
095
096    /**
097     * Indicates whether administrative action is required before one or more of
098     * the changes will take effect.
099     *
100     * @return <CODE>true</CODE> if one or more of the configuration changes
101     *         require administrative action to take effect, or
102     *         <CODE>false</CODE> if not.
103     */
104    public boolean adminActionRequired() {
105        return adminActionRequired;
106    }
107
108    /**
109     * Specifies whether administrative action is required before one or more of
110     * the changes will take effect.
111     *
112     * @param adminActionRequired
113     *            Specifies whether administrative action is required before one
114     *            or more of the changes will take effect.
115     */
116    public void setAdminActionRequired(boolean adminActionRequired) {
117        this.adminActionRequired = adminActionRequired;
118    }
119
120    /**
121     * Retrieves the set of messages that provide explanation for the processing
122     * of the configuration changes. This list may be modified by the caller.
123     *
124     * @return The set of messages that provide explanation for the processing
125     *         of the configuration changes.
126     */
127    public List<LocalizableMessage> getMessages() {
128        return messages;
129    }
130
131    /**
132     * Adds the provided message to the set of messages for this config change
133     * result.
134     *
135     * @param message
136     *            The message to add to the set of messages for this config
137     *            change result.
138     */
139    public void addMessage(LocalizableMessage message) {
140        messages.add(message);
141    }
142
143    /**
144     * Retrieves a string representation of this config change result.
145     *
146     * @return A string representation of this config change result.
147     */
148    @Override
149    public String toString() {
150        StringBuilder buffer = new StringBuilder();
151        toString(buffer);
152        return buffer.toString();
153    }
154
155    /**
156     * Appends a string representation of this config change result to the
157     * provided buffer.
158     *
159     * @param buffer
160     *            The buffer to which the information should be appended.
161     */
162    public void toString(StringBuilder buffer) {
163        buffer.append("ConfigChangeResult(result=");
164        buffer.append(resultCode);
165        buffer.append(", adminActionRequired=");
166        buffer.append(adminActionRequired);
167        buffer.append(", messages={");
168        Utils.joinAsString(buffer, ",", messages);
169        buffer.append("})");
170    }
171}