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;
029
030import org.opencms.file.CmsGroup;
031import org.opencms.file.CmsObject;
032import org.opencms.main.CmsException;
033import org.opencms.util.CmsStringUtil;
034
035import java.util.ArrayList;
036import java.util.List;
037
038/**
039 * Provides filters for getting resources subscribed by a user or group in a specified time range.<p>
040 *
041 * @since 8.0
042 */
043public class CmsSubscriptionFilter extends CmsVisitedByFilter {
044
045    /** The groups to check subscribed resources for. */
046    private List<CmsGroup> m_groups;
047
048    /** The mode to read subscribed resources. */
049    private CmsSubscriptionReadMode m_mode;
050
051    /**
052     * Constructor, without parameters.<p>
053     *
054     * It is required to set the user or group manually if using this constructor.<p>
055     */
056    public CmsSubscriptionFilter() {
057
058        super();
059        m_groups = new ArrayList<CmsGroup>();
060        m_mode = CmsSubscriptionReadMode.UNVISITED;
061    }
062
063    /**
064     * Constructor, setting the user to the current user from the context.<p>
065     *
066     * @param cms the current users context
067     */
068    public CmsSubscriptionFilter(CmsObject cms) {
069
070        this(cms, false);
071    }
072
073    /**
074     * Constructor, setting the user to the current user from the context.<p>
075     *
076     * @param cms the current users context
077     * @param addUserGroups determines if the groups of the current user should be added to the list of groups of the filter
078     */
079    public CmsSubscriptionFilter(CmsObject cms, boolean addUserGroups) {
080
081        super(cms);
082        m_groups = new ArrayList<CmsGroup>();
083        if (addUserGroups) {
084            try {
085                m_groups = cms.getGroupsOfUser(getUser().getName(), false);
086            } catch (CmsException e) {
087                // failed to set user groups
088            }
089        }
090        m_mode = CmsSubscriptionReadMode.UNVISITED;
091    }
092
093    /**
094     * Adds a group to the list of groups to check subscribed resources for.<p>
095     *
096     * @param group the group to add
097     */
098    public void addGroup(CmsGroup group) {
099
100        m_groups.add(group);
101    }
102
103    /**
104     * @see java.lang.Object#clone()
105     */
106    @Override
107    public Object clone() {
108
109        CmsSubscriptionFilter filter = new CmsSubscriptionFilter();
110        filter.setFromDate(getFromDate());
111        filter.setParentPath(getParentPath());
112        filter.setToDate(getToDate());
113        if (getUser() != null) {
114            filter.setUser(getUser());
115        }
116        filter.setGroups(getGroups());
117        filter.setMode(getMode());
118        return filter;
119    }
120
121    /**
122     * Returns the groups to check subscribed resources for.<p>
123     *
124     * @return the groups to check subscribed resources for
125     */
126    public List<CmsGroup> getGroups() {
127
128        return m_groups;
129    }
130
131    /**
132     * Returns the mode to read subscribed resources.<p>
133     *
134     * @return the mode to read subscribed resources
135     */
136    public CmsSubscriptionReadMode getMode() {
137
138        return m_mode;
139    }
140
141    /**
142     * Sets the groups to check subscribed resources for.<p>
143     *
144     * @param groups the groups to check subscribed resources for
145     */
146    public void setGroups(List<CmsGroup> groups) {
147
148        m_groups = groups;
149    }
150
151    /**
152     * Sets the mode to read subscribed resources.<p>
153     *
154     * @param mode the mode to read subscribed resources
155     */
156    public void setMode(CmsSubscriptionReadMode mode) {
157
158        m_mode = mode;
159    }
160
161    /**
162     * Sets the groups of the user currently set in this filter as the list of groups to check subscribed resources for.<p>
163     *
164     * @param cms the current users context
165     */
166    public void setUserGroups(CmsObject cms) {
167
168        if (getUser() != null) {
169            try {
170                m_groups = cms.getGroupsOfUser(getUser().getName(), false);
171            } catch (CmsException e) {
172                // failed to set user groups
173            }
174        }
175    }
176
177    /**
178     * Returns a user readable representation of the filter.<p>
179     *
180     * @see java.lang.Object#toString()
181     */
182    @Override
183    public String toString() {
184
185        StringBuffer result = new StringBuffer(256);
186        result.append(super.toString());
187        if (!getGroups().isEmpty()) {
188            result.append(", Groups: ").append(CmsStringUtil.collectionAsString(getGroups(), ";"));
189        }
190        result.append(", Mode: ").append(getMode().toString());
191        return result.toString();
192    }
193}