001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 *      Portions copyright 2015 ForgeRock AS
026 */
027package org.forgerock.opendj.config.server;
028
029import java.util.ArrayList;
030import java.util.Iterator;
031import java.util.List;
032
033import org.forgerock.i18n.LocalizableMessage;
034import org.forgerock.opendj.ldap.ResultCode;
035
036/**
037 * This class defines a data structure that can be used to hold information
038 * about the result of processing a configuration change.
039 */
040public final class ConfigChangeResult {
041    /**
042     * A set of messages describing the changes that were made, any
043     * action that may be required, or any problems that were encountered.
044     */
045    private final List<LocalizableMessage> messages = new ArrayList<>();
046
047    /**
048     * Indicates whether one or more of the changes requires
049     * administrative action in order to take effect.
050     */
051    private boolean adminActionRequired;
052
053    /**
054     * The result code to return to the client from this configuration change.
055     */
056    private ResultCode resultCode = ResultCode.SUCCESS;
057
058    /**
059     * Creates a new config change result object with the provided information.
060     */
061    public ConfigChangeResult() {
062        // nothing more to do
063    }
064
065    /**
066     * Retrieves the result code for this config change result.
067     *
068     * @return The result code for this config change result.
069     */
070    public ResultCode getResultCode() {
071        return resultCode;
072    }
073
074    /**
075     * Specifies the result code for this config change result.
076     *
077     * @param resultCode
078     *            The result code for this config change result.
079     */
080    public void setResultCode(ResultCode resultCode) {
081        this.resultCode = resultCode;
082    }
083
084    /**
085     * Sets the provided result code for this config change result
086     * if the current result code is success.
087     *
088     * @param newResultCode
089     *          The new result code for this config change result.
090     */
091    public void setResultCodeIfSuccess(ResultCode newResultCode) {
092        if (getResultCode() == ResultCode.SUCCESS) {
093            setResultCode(newResultCode);
094        }
095    }
096
097    /**
098     * Indicates whether administrative action is required before one or more of
099     * the changes will take effect.
100     *
101     * @return <CODE>true</CODE> if one or more of the configuration changes
102     *         require administrative action to take effect, or
103     *         <CODE>false</CODE> if not.
104     */
105    public boolean adminActionRequired() {
106        return adminActionRequired;
107    }
108
109    /**
110     * Specifies whether administrative action is required before one or more of
111     * the changes will take effect.
112     *
113     * @param adminActionRequired
114     *            Specifies whether administrative action is required before one
115     *            or more of the changes will take effect.
116     */
117    public void setAdminActionRequired(boolean adminActionRequired) {
118        this.adminActionRequired = adminActionRequired;
119    }
120
121    /**
122     * Retrieves the set of messages that provide explanation for the processing
123     * of the configuration changes. This list may be modified by the caller.
124     *
125     * @return The set of messages that provide explanation for the processing
126     *         of the configuration changes.
127     */
128    public List<LocalizableMessage> getMessages() {
129        return messages;
130    }
131
132    /**
133     * Adds the provided message to the set of messages for this config change
134     * result.
135     *
136     * @param message
137     *            The message to add to the set of messages for this config
138     *            change result.
139     */
140    public void addMessage(LocalizableMessage message) {
141        messages.add(message);
142    }
143
144    /**
145     * Retrieves a string representation of this config change result.
146     *
147     * @return A string representation of this config change result.
148     */
149    @Override
150    public String toString() {
151        StringBuilder buffer = new StringBuilder();
152        toString(buffer);
153        return buffer.toString();
154    }
155
156    /**
157     * Appends a string representation of this config change result to the
158     * provided buffer.
159     *
160     * @param buffer
161     *            The buffer to which the information should be appended.
162     */
163    public void toString(StringBuilder buffer) {
164        buffer.append("ConfigChangeResult(result=");
165        buffer.append(resultCode);
166        buffer.append(", adminActionRequired=");
167        buffer.append(adminActionRequired);
168        buffer.append(", messages={");
169
170        if (!messages.isEmpty()) {
171            final Iterator<LocalizableMessage> iterator = messages.iterator();
172            buffer.append(iterator.next());
173            while (iterator.hasNext()) {
174                buffer.append(",");
175                buffer.append(iterator.next());
176            }
177        }
178
179        buffer.append("})");
180    }
181}