001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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,
060        /** State for items which should be published. */
061        publish,
062
063        /** State for items which should be removed. */
064        remove;
065    }
066
067    /**
068     * The enum for the type of signals which can change the item state.<p>
069     */
070    enum Signal {
071        /** User selected publish. */
072        publish,
073        /** User selected remove. */
074        remove,
075        /** User deselected publish. */
076        unpublish,
077        /** User deselected remove. */
078        unremove;
079    }
080
081    /** The status update handler which should be notified of changes to the state. */
082    I_CmsPublishItemStatusUpdateHandler m_handler;
083
084    /** Flag which indicates if this item is disabled. */
085    private boolean m_disabled;
086
087    /** The id of the item. */
088    private CmsUUID m_id;
089
090    /** The current state of the item. */
091    private State m_state;
092
093    /**
094     * Creates a new publish item status bean.<p>
095     *
096     * @param id the publish item id
097     * @param state the publish item state
098     * @param disabled true if this item is disabled
099     * @param handler the handler which should be notified of state changes
100     */
101    public CmsPublishItemStatus(
102        CmsUUID id,
103        State state,
104        boolean disabled,
105        I_CmsPublishItemStatusUpdateHandler handler) {
106
107        m_id = id;
108        m_state = state;
109        m_disabled = disabled;
110        m_handler = handler;
111        assert m_disabled ? m_state != State.publish : true;
112    }
113
114    /**
115     * Gets the current state of the publish item.<p>
116     *
117     * @return the current state
118     */
119    public State getState() {
120
121        return m_state;
122    }
123
124    /**
125     * Handles a signal which may change the current state.<p>
126     *
127     * @param signal the signal
128     */
129    public void handleSignal(Signal signal) {
130
131        switch (signal) {
132            case publish:
133                signalPublish();
134                break;
135            case unpublish:
136                signalUnpublish();
137                break;
138            case remove:
139                signalRemove();
140                break;
141            case unremove:
142                signalUnremove();
143                break;
144            default:
145                break;
146        }
147        m_handler.update(m_id, this);
148    }
149
150    /**
151     * Checks whether this publish item is disabled.<p>
152     *
153     * @return true if the publish item is disabled
154     */
155    public boolean isDisabled() {
156
157        return m_disabled;
158    }
159
160    /**
161     * Executes a publish signal.<p>
162     */
163    protected void signalPublish() {
164
165        if (!m_disabled) {
166            if (m_state != State.remove) {
167                m_state = State.publish;
168            }
169        }
170    }
171
172    /**
173     * Executes a remove signal.<p>
174     */
175    protected void signalRemove() {
176
177        m_state = State.remove;
178    }
179
180    /**
181     * Executes an unpublish signal.<p>
182     */
183    protected void signalUnpublish() {
184
185        if (m_state == State.publish) {
186            m_state = State.normal;
187        }
188    }
189
190    /**
191     * Executes an unremove signal.<p>
192     */
193    protected void signalUnremove() {
194
195        if (m_disabled) {
196            m_state = State.normal;
197        } else {
198            m_state = State.publish;
199        }
200    }
201
202}