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.db.CmsDbContext;
031import org.opencms.db.I_CmsProjectDriver;
032import org.opencms.db.log.CmsLogEntry;
033import org.opencms.file.CmsDataAccessException;
034import org.opencms.util.CmsUUID;
035
036import java.util.ArrayList;
037import java.util.HashMap;
038import java.util.List;
039import java.util.Map;
040
041/**
042 * Abstract base class used for converting from CmsLogEntry instances to the necessary changes in the user's publish list.
043 *
044 * <p>Multiple log entries can be  added via the add() method, and then all changes to the database are performed
045 * when the writeToDatabase() method is called.
046 */
047public abstract class A_CmsLogPublishListConverter {
048
049    /** Map from structure ids to publish list state objects. */
050    protected Map<CmsUUID, CmsPublishListResourceState> m_entries = new HashMap<>();
051
052    /**
053     * Processes a log entry.
054     *
055     * @param entry the entry to process
056     * */
057    public abstract void add(CmsLogEntry entry);
058
059    /**
060     * Gets the state entry for the given structure id, creating it if it doesn't already exist.
061     *
062     * @param key the structure id of a resource
063     * @return the state object for the structure id
064     */
065    public CmsPublishListResourceState getEntry(CmsUUID key) {
066
067        CmsPublishListResourceState result = m_entries.get(key);
068        if (result == null) {
069            result = new CmsPublishListResourceState();
070            m_entries.put(key, result);
071        }
072        return result;
073    }
074
075    /**
076     * Writes the collected changes to the database.
077     *
078     * @param dbc the database context
079     * @param projectDriver the project driver
080     *
081     * @throws CmsDataAccessException if something goes wrong
082     */
083    public void writeChangesToDatabase(CmsDbContext dbc, I_CmsProjectDriver projectDriver)
084    throws CmsDataAccessException {
085
086        List<CmsUserPublishListEntry> entriesToWrite = new ArrayList<>();
087        for (CmsUUID structureId : m_entries.keySet()) {
088            if (m_entries.get(structureId).isRemoveAll()) {
089                entriesToWrite.add(new CmsUserPublishListEntry(null, structureId, 0));
090            } else {
091                for (CmsUUID userId : m_entries.get(structureId).getRemoveUsers()) {
092                    entriesToWrite.add(new CmsUserPublishListEntry(userId, structureId, 0));
093                }
094            }
095        }
096        if (entriesToWrite.size() > 0) {
097            projectDriver.deleteUserPublishListEntries(dbc, entriesToWrite);
098        }
099        entriesToWrite.clear();
100        for (CmsUUID structureId : m_entries.keySet()) {
101            CmsPublishListResourceState state = m_entries.get(structureId);
102            for (CmsUUID userId : state.getUpdateUsers()) {
103                entriesToWrite.add(new CmsUserPublishListEntry(userId, structureId, state.getTimestamp(userId)));
104            }
105        }
106        if (entriesToWrite.size() > 0) {
107            projectDriver.writeUserPublishListEntries(dbc, entriesToWrite);
108        }
109    }
110
111}