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