001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ade.publish.client;
029
030import org.opencms.util.CmsUUID;
031
032/**
033 * This class encapsulates the possible states of a publish item.<p>
034 *
035 * An item can be enabled or disabled (because of an error). If it is enabled,
036 * it can change between the states "normal", "publish", and "remove", but if it is
037 * disabled, it can only change between "normal" and "remove".<p>
038 *
039 * The state will be changed depending on various signals which are passed as
040 * parameters to the handleSignal() method.<p>
041 *
042 * If the item is enabled, the only possible state transitions are as follows:
043 *
044 * publish to normal
045 * publish to remove
046 * normal to publish
047 * normal to remove
048 * remove to normal
049 *
050 * @since 8.0.0
051 */
052public class CmsPublishItemStatus {
053
054    /**
055     * The enum for the publish item state.<p>
056     */
057    public enum State {
058        /** Normal state. */
059        normal, /** State for items which should be published. */
060        publish,
061
062        /** State for items which should be removed. */
063        remove;
064    }
065
066    /**
067     * The enum for the type of signals which can change the item state.<p>
068     */
069    enum Signal {
070        /** User selected publish. */
071        publish, /** User selected remove. */
072        remove, /** User deselected publish. */
073        unpublish, /** User deselected remove. */
074        unremove;
075    }
076
077    /** The status update handler which should be notified of changes to the state. */
078    I_CmsPublishItemStatusUpdateHandler m_handler;
079
080    /** Flag which indicates if this item is disabled. */
081    private boolean m_disabled;
082
083    /** The id of the item. */
084    private CmsUUID m_id;
085
086    /** The current state of the item. */
087    private State m_state;
088
089    /**
090     * Creates a new publish item status bean.<p>
091     *
092     * @param id the publish item id
093     * @param state the publish item state
094     * @param disabled true if this item is disabled
095     * @param handler the handler which should be notified of state changes
096     */
097    public CmsPublishItemStatus(
098        CmsUUID id,
099        State state,
100        boolean disabled,
101        I_CmsPublishItemStatusUpdateHandler handler) {
102
103        m_id = id;
104        m_state = state;
105        m_disabled = disabled;
106        m_handler = handler;
107        assert m_disabled ? m_state != State.publish : true;
108    }
109
110    /**
111     * Gets the current state of the publish item.<p>
112     *
113     * @return the current state
114     */
115    public State getState() {
116
117        return m_state;
118    }
119
120    /**
121     * Handles a signal which may change the current state.<p>
122     *
123     * @param signal the signal
124     */
125    public void handleSignal(Signal signal) {
126
127        switch (signal) {
128            case publish:
129                signalPublish();
130                break;
131            case unpublish:
132                signalUnpublish();
133                break;
134            case remove:
135                signalRemove();
136                break;
137            case unremove:
138                signalUnremove();
139                break;
140            default:
141                break;
142        }
143        m_handler.update(m_id, this);
144    }
145
146    /**
147     * Checks whether this publish item is disabled.<p>
148     *
149     * @return true if the publish item is disabled
150     */
151    public boolean isDisabled() {
152
153        return m_disabled;
154    }
155
156    /**
157     * Executes a publish signal.<p>
158     */
159    protected void signalPublish() {
160
161        if (!m_disabled) {
162            if (m_state != State.remove) {
163                m_state = State.publish;
164            }
165        }
166    }
167
168    /**
169     * Executes a remove signal.<p>
170     */
171    protected void signalRemove() {
172
173        m_state = State.remove;
174    }
175
176    /**
177     * Executes an unpublish signal.<p>
178     */
179    protected void signalUnpublish() {
180
181        if (m_state == State.publish) {
182            m_state = State.normal;
183        }
184    }
185
186    /**
187     * Executes an unremove signal.<p>
188     */
189    protected void signalUnremove() {
190
191        if (m_disabled) {
192            m_state = State.normal;
193        } else {
194            m_state = State.publish;
195        }
196    }
197
198}