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.containerpage.client;
029
030import org.opencms.gwt.client.rpc.CmsRpcAction;
031import org.opencms.util.CmsUUID;
032
033import java.util.HashSet;
034import java.util.Iterator;
035import java.util.Set;
036import java.util.stream.Collectors;
037
038import com.google.gwt.user.client.Timer;
039
040/**
041 * Helper class for periodically checking a set of elements for publish locks and then
042 * reloading the corresponding elements when they are no longer locked.
043 */
044public class CmsPublishLockChecker {
045
046    /** The delay between to publish lock checks. */
047    public static final int DELAY = 500;
048
049    /** True if we are currently checking for locks. */
050    protected boolean m_active = false;
051
052    /** The ids to check .*/
053    protected Set<CmsUUID> m_toCheck = new HashSet<>();
054
055    /** The container page controller. */
056    protected CmsContainerpageController m_controller;
057
058    /**
059     * Creates a new instance.
060     *
061     * @param controller the container page controller
062     */
063    public CmsPublishLockChecker(CmsContainerpageController controller) {
064
065        m_controller = controller;
066    }
067
068    /**
069     * Adds a set of element ids which should be checked for publish locks.
070     *
071     * @param ids the ids which should be checked
072     */
073    public void addIdsToCheck(Set<CmsUUID> ids) {
074
075        m_toCheck.addAll(ids);
076        if (!m_active) {
077            m_active = true;
078            scheduleNextCheck();
079        }
080    }
081
082    /**
083     * Processes the results of the publish lock check RPC call.
084     *
085     * @param locked the remaining ids of elements with a publish lock
086     */
087    protected void processCheckResult(Set<CmsUUID> locked) {
088
089        Iterator<CmsUUID> iterator = m_toCheck.iterator();
090        Set<CmsUUID> toUpdate = new HashSet<>();
091        while (iterator.hasNext()) {
092            CmsUUID id = iterator.next();
093            if (!locked.contains(id)) {
094                toUpdate.add(id);
095                iterator.remove();
096            }
097        }
098        if (toUpdate.size() > 0) {
099            m_controller.reloadElements(
100                toUpdate.stream().map(id -> "" + id).collect(Collectors.toList()),
101                () -> {/*do nothing*/});
102        }
103        if (m_toCheck.size() > 0) {
104            scheduleNextCheck();
105        } else {
106            m_active = false;
107        }
108    }
109
110    /**
111     * Schedules the next check.
112     */
113    private void scheduleNextCheck() {
114
115        Timer timer = new Timer() {
116
117            @SuppressWarnings("synthetic-access")
118            @Override
119            public void run() {
120
121                startCheck();
122            }
123        };
124        timer.schedule(DELAY);
125    }
126
127    /**
128     * Starts the RPC call to check for publish locks.
129     */
130    private void startCheck() {
131
132        CmsRpcAction<Set<CmsUUID>> action = new CmsRpcAction<Set<CmsUUID>>() {
133
134            @Override
135            public void execute() {
136
137                start(0, false);
138                m_controller.getContainerpageService().getElementsLockedForPublishing(m_toCheck, this);
139            }
140
141            @Override
142            protected void onResponse(Set<CmsUUID> locked) {
143
144                stop(false);
145
146                processCheckResult(locked);
147            }
148
149        };
150        action.execute();
151    }
152}