src/org/sonews/storage/Group.java
author František Kučera <franta-hg@frantovo.cz>
Wed Dec 31 12:07:40 2014 +0100 (2014-12-31)
changeset 120 bb1c8a7b774c
parent 47 e118b4d60029
permissions -rwxr-xr-x
XSLT: licence – GNU GPLv3
chris@3
     1
/*
chris@3
     2
 *   SONEWS News Server
chris@3
     3
 *   see AUTHORS for the list of contributors
chris@3
     4
 *
chris@3
     5
 *   This program is free software: you can redistribute it and/or modify
chris@3
     6
 *   it under the terms of the GNU General Public License as published by
chris@3
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@3
     8
 *   (at your option) any later version.
chris@3
     9
 *
chris@3
    10
 *   This program is distributed in the hope that it will be useful,
chris@3
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@3
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@3
    13
 *   GNU General Public License for more details.
chris@3
    14
 *
chris@3
    15
 *   You should have received a copy of the GNU General Public License
chris@3
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@3
    17
 */
chris@3
    18
package org.sonews.storage;
chris@3
    19
chris@3
    20
import java.sql.SQLException;
chris@3
    21
import java.util.List;
chris@3
    22
import org.sonews.util.Log;
chris@3
    23
import org.sonews.util.Pair;
chris@3
    24
chris@3
    25
/**
chris@3
    26
 * Represents a logical Group within this newsserver.
chris@3
    27
 * @author Christian Lins
chris@3
    28
 * @since sonews/0.5.0
chris@3
    29
 */
cli@48
    30
public class Group {
chris@3
    31
cli@47
    32
	/**
cli@47
    33
	 * If this flag is set the Group is no real newsgroup but a mailing list
cli@47
    34
	 * mirror. In that case every posting and receiving mails must go through
cli@47
    35
	 * the mailing list gateway.
cli@47
    36
	 */
cli@47
    37
	public static final int MAILINGLIST = 0x1;
cli@47
    38
cli@47
    39
	/**
cli@47
    40
	 * If this flag is set the Group is marked as readonly and the posting
cli@47
    41
	 * is prohibited. This can be useful for groups that are synced only in
cli@47
    42
	 * one direction.
cli@47
    43
	 */
cli@47
    44
	public static final int READONLY = 0x2;
cli@47
    45
cli@47
    46
	/**
cli@47
    47
	 * If this flag is set the Group is marked as deleted and must not occur
cli@47
    48
	 * in any output. The deletion is done lazily by a low priority daemon.
cli@47
    49
	 */
cli@47
    50
	public static final int DELETED = 0x80;
cli@47
    51
cli@37
    52
	private long id = 0;
cli@37
    53
	private int flags = -1;
cli@37
    54
	private String name = null;
chris@3
    55
cli@37
    56
	/**
cli@37
    57
	 * @return List of all groups this server handles.
cli@37
    58
	 */
cli@48
    59
	public static List<Group> getAll() {
cli@37
    60
		try {
cli@37
    61
			return StorageManager.current().getGroups();
cli@37
    62
		} catch (StorageBackendException ex) {
cli@37
    63
			Log.get().severe(ex.getMessage());
cli@37
    64
			return null;
cli@37
    65
		}
cli@37
    66
	}
chris@3
    67
cli@37
    68
	/**
cli@47
    69
	 * Constructor.
cli@37
    70
	 * @param name
cli@37
    71
	 * @param id
cli@47
    72
	 * @param flags
cli@37
    73
	 */
cli@42
    74
	public Group(final String name, final long id, final int flags) {
cli@37
    75
		this.id = id;
cli@37
    76
		this.flags = flags;
cli@37
    77
		this.name = name;
cli@37
    78
	}
chris@3
    79
cli@37
    80
	@Override
cli@42
    81
	public boolean equals(Object obj) {
cli@37
    82
		if (obj instanceof Group) {
cli@37
    83
			return ((Group) obj).id == this.id;
cli@37
    84
		} else {
cli@37
    85
			return false;
cli@37
    86
		}
cli@37
    87
	}
chris@3
    88
cli@37
    89
	public Article getArticle(long idx)
cli@42
    90
			throws StorageBackendException {
cli@37
    91
		return StorageManager.current().getArticle(idx, this.id);
cli@37
    92
	}
chris@3
    93
cli@37
    94
	public List<Pair<Long, ArticleHead>> getArticleHeads(final long first, final long last)
cli@42
    95
			throws StorageBackendException {
cli@37
    96
		return StorageManager.current().getArticleHeads(this, first, last);
cli@37
    97
	}
chris@3
    98
cli@37
    99
	public List<Long> getArticleNumbers()
cli@42
   100
			throws StorageBackendException {
cli@37
   101
		return StorageManager.current().getArticleNumbers(id);
cli@37
   102
	}
chris@3
   103
cli@37
   104
	public long getFirstArticleNumber()
cli@42
   105
			throws StorageBackendException {
cli@37
   106
		return StorageManager.current().getFirstArticleNumber(this);
cli@37
   107
	}
chris@3
   108
cli@42
   109
	public int getFlags() {
cli@37
   110
		return this.flags;
cli@37
   111
	}
chris@3
   112
cli@37
   113
	public long getIndexOf(Article art)
cli@42
   114
			throws StorageBackendException {
cli@37
   115
		return StorageManager.current().getArticleIndex(art, this);
cli@37
   116
	}
chris@3
   117
cli@37
   118
	/**
cli@37
   119
	 * Returns the group id.
cli@37
   120
	 */
cli@42
   121
	public long getInternalID() {
cli@37
   122
		assert id > 0;
cli@37
   123
		return id;
cli@37
   124
	}
chris@3
   125
cli@42
   126
	public boolean isDeleted() {
cli@37
   127
		return (this.flags & DELETED) != 0;
cli@37
   128
	}
chris@3
   129
cli@42
   130
	public boolean isMailingList() {
cli@37
   131
		return (this.flags & MAILINGLIST) != 0;
cli@37
   132
	}
chris@3
   133
cli@42
   134
	public boolean isWriteable() {
cli@37
   135
		return true;
cli@37
   136
	}
chris@3
   137
cli@37
   138
	public long getLastArticleNumber()
cli@42
   139
			throws StorageBackendException {
cli@37
   140
		return StorageManager.current().getLastArticleNumber(this);
cli@37
   141
	}
chris@3
   142
cli@42
   143
	public String getName() {
cli@37
   144
		return name;
cli@37
   145
	}
chris@3
   146
cli@37
   147
	/**
cli@37
   148
	 * Performs this.flags |= flag to set a specified flag and updates the data
cli@37
   149
	 * in the JDBCDatabase.
cli@37
   150
	 * @param flag
cli@37
   151
	 */
cli@42
   152
	public void setFlag(final int flag) {
cli@37
   153
		this.flags |= flag;
cli@37
   154
	}
chris@3
   155
cli@47
   156
	public void unsetFlag(final int flag) {
cli@47
   157
		this.flags &= ~flag;
cli@47
   158
	}
cli@47
   159
cli@42
   160
	public void setName(final String name) {
cli@37
   161
		this.name = name;
cli@37
   162
	}
cli@37
   163
cli@37
   164
	/**
cli@37
   165
	 * @return Number of posted articles in this group.
cli@37
   166
	 * @throws java.sql.SQLException
cli@37
   167
	 */
cli@37
   168
	public long getPostingsCount()
cli@42
   169
			throws StorageBackendException {
cli@37
   170
		return StorageManager.current().getPostingsCount(this.name);
cli@37
   171
	}
cli@37
   172
cli@37
   173
	/**
cli@37
   174
	 * Updates flags and name in the backend.
cli@37
   175
	 */
cli@37
   176
	public void update()
cli@42
   177
			throws StorageBackendException {
cli@37
   178
		StorageManager.current().update(this);
cli@37
   179
	}
chris@3
   180
}