001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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;
029
030import org.opencms.db.log.CmsLogEntry;
031import org.opencms.db.userpublishlist.CmsUserPublishListEntry;
032import org.opencms.main.OpenCms;
033import org.opencms.publish.CmsPublishManager;
034import org.opencms.util.CmsPair;
035import org.opencms.util.CmsUUID;
036
037import java.util.ArrayList;
038import java.util.HashMap;
039import java.util.List;
040import java.util.Map;
041
042/**
043 * This class can be used to convert a series of log entry objects to a set of changes which should be applied to the user
044 * publish list.<p>
045 */
046public class CmsLogToPublishListChangeConverter {
047
048    /**
049     * The possible actions for a publish list entry.<p>
050     */
051    private enum Action {
052        /** Delete an entry. */
053        delete,
054
055        /** Update an entry. */
056        update
057    }
058
059    /**
060     * The state consists of a map from (user id, structure id) pairs to time stamps. Negative time stamps are used when
061     * the user publish list entry for the given key should be deleted.<p>
062     */
063    private Map<CmsPair<CmsUUID, CmsUUID>, Long> m_state = new HashMap<CmsPair<CmsUUID, CmsUUID>, Long>();
064
065    /**
066     * Feeds a log entry to the converter.<p>
067     *
068     * @param entry the log entry to process
069     */
070    public void add(CmsLogEntry entry) {
071
072        CmsUUID userId = entry.getUserId();
073        CmsUUID structureId = entry.getStructureId();
074        if ((userId == null) || (structureId == null)) {
075            return;
076        }
077        CmsPair<CmsUUID, CmsUUID> key = CmsPair.create(userId, structureId);
078        if (isDeleting(entry)) {
079            m_state.put(key, Long.valueOf(-1));
080        } else if (isChanging(entry)) {
081            m_state.put(key, Long.valueOf(entry.getDate()));
082        }
083    }
084
085    /**
086     * Gets the list of publish list entries which should be updated in the database.<p>
087     *
088     * @return the list of publish list entries to update
089     */
090    public List<CmsUserPublishListEntry> getPublishListAdditions() {
091
092        return filterEntries(Action.update);
093    }
094
095    /**
096     * Gets the list of user publish list entries to delete.<p>
097     *
098     * In the objects returned, only the structure id and user id fields are meaningful.<p>
099     *
100     * @return the list of user publish list entries to delete
101     */
102    public List<CmsUserPublishListEntry> getPublishListDeletions() {
103
104        return filterEntries(Action.delete);
105    }
106
107    /**
108     * Gets all CmsUserPublishListEntry values from the internal state map whose action matches the parameter given.<p>
109     *
110     * @param action an action constant
111     * @return all CmsUserPublishListEntry values from the internal state map whose action matches the parameter given
112     */
113    protected List<CmsUserPublishListEntry> filterEntries(Action action) {
114
115        boolean isDeleteAll = (action == Action.delete)
116            && (OpenCms.getPublishManager().getPublishListRemoveMode() == CmsPublishManager.PublishListRemoveMode.allUsers);
117        List<CmsUserPublishListEntry> result = new ArrayList<CmsUserPublishListEntry>();
118        for (Map.Entry<CmsPair<CmsUUID, CmsUUID>, Long> entry : m_state.entrySet()) {
119            CmsPair<CmsUUID, CmsUUID> key = entry.getKey();
120            Long value = entry.getValue();
121            Action valueAction = getAction(value.longValue());
122            if (valueAction.equals(action)) {
123                result.add(
124                    new CmsUserPublishListEntry(
125                        isDeleteAll ? null : key.getFirst(),
126                        key.getSecond(),
127                        value.longValue()));
128            }
129        }
130        return result;
131    }
132
133    /**
134     * Checks whether the given log entry should update an entry in the publish list.<p>
135     *
136     * @param entry the log entry
137     *
138     * @return true if the corresponding publish list entry should be removed
139     */
140    protected boolean isChanging(CmsLogEntry entry) {
141
142        return entry.getType().getId() > 20;
143    }
144
145    /**
146     * Checks whether the given log entry should remove an entry from the publish list.<p>
147     *
148     * @param entry the log entry
149     *
150     * @return true if the corresponding publish list entry should be removed
151     */
152    protected boolean isDeleting(CmsLogEntry entry) {
153
154        return entry.getType().getId() <= 20;
155    }
156
157    /**
158     * Gets the publish list action corresponding to a given timestamp value.<p>
159     *
160     * @param value the timestamp value
161     *
162     * @return the action belonging to execute for this value
163     */
164    private Action getAction(long value) {
165
166        if (value == -1) {
167            return Action.delete;
168        } else {
169            return Action.update;
170        }
171    }
172
173}