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.db.userpublishlist;
029
030import org.opencms.util.CmsUUID;
031
032import java.util.ArrayList;
033import java.util.HashMap;
034import java.util.List;
035import java.util.Map;
036
037/**
038 * Keeps track of publish list changes for a single resource.
039 */
040public class CmsPublishListResourceState {
041
042    /** Enum representing a state change. */
043    enum StateChange {
044        /** Resource should be removed from publish list. */
045        delete,
046        /** Resource should be added to publish list. */
047        update;
048    }
049
050    /** Keeps track of whether all publish list entries for the resource should be removed. */
051    private boolean m_removeAll;
052
053    /** Map of time stamps of changes. */
054    private Map<CmsUUID, Long> m_timestamps = new HashMap<>();
055
056    /** Map from user id to state changes. */
057    private Map<CmsUUID, StateChange> m_userChanges = new HashMap<>();
058
059    /**
060     * Adds a removal from the publish list for the given user id.
061     *
062     * @param userId a user id
063     */
064    public void addRemove(CmsUUID userId) {
065
066        m_userChanges.put(userId, StateChange.delete);
067
068    }
069
070    /**
071     * Adds an update for the publish list for the given user id.
072     *
073     * @param userId a user id
074     * @param timestamp the change timestamp
075     */
076    public void addUpdate(CmsUUID userId, long timestamp) {
077
078        m_userChanges.put(userId, StateChange.update);
079        m_timestamps.put(userId, Long.valueOf(timestamp));
080
081    }
082
083    /**
084     * Gets the users from whose publish list the resource should be removed.
085     *
086     * @return list of user ids
087     */
088    public List<CmsUUID> getRemoveUsers() {
089
090        List<CmsUUID> result = new ArrayList<>();
091        for (Map.Entry<CmsUUID, StateChange> entry : m_userChanges.entrySet()) {
092            if (entry.getValue() == StateChange.delete) {
093                result.add(entry.getKey());
094            }
095        }
096        return result;
097    }
098
099    /**
100     * Gets the log timestamp for the given user id.
101     *
102     * @param userId the user id
103     * @return the timestamp for the change with the given user id
104     */
105    public long getTimestamp(CmsUUID userId) {
106
107        Long timestamp = m_timestamps.get(userId);
108        if (timestamp == null) {
109            return 0;
110        }
111        return timestamp.longValue();
112    }
113
114    /**
115     * Gets the users to whose publish list the resource should be added
116     *
117     * @return list of user ids
118     */
119    public List<CmsUUID> getUpdateUsers() {
120
121        List<CmsUUID> result = new ArrayList<>();
122        for (Map.Entry<CmsUUID, StateChange> entry : m_userChanges.entrySet()) {
123            if (entry.getValue() == StateChange.update) {
124                result.add(entry.getKey());
125            }
126        }
127        return result;
128    }
129
130    /**
131     * Returns true if the structure id should be removed for all users.
132     *
133     * @return true if resource should be removed from all publish lists
134     */
135    public boolean isRemoveAll() {
136
137        return m_removeAll;
138    }
139
140    /**
141     * Sets the 'remove all' status to 'true'.
142     *
143     * <p>All collected changes/removals done before this will be discarded.
144     * Changes/removals done after this will not clear the removeAll status.
145     */
146    public void setRemoveAll() {
147
148        m_removeAll = true;
149        m_userChanges.clear();
150    }
151
152}