src/org/sonews/storage/Group.java
author cli
Tue, 07 Jun 2011 09:23:34 +0200
changeset 43 7d0e65712a95
parent 42 7f84f4de2893
child 47 e118b4d60029
permissions -rwxr-xr-x
Adapt config sample to use hsqldb
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
 */
chris@3
    30
// TODO: This class should not be public!
cli@42
    31
public class Group extends Channel {
chris@3
    32
cli@37
    33
	private long id = 0;
cli@37
    34
	private int flags = -1;
cli@37
    35
	private String name = null;
chris@3
    36
cli@37
    37
	/**
cli@37
    38
	 * @return List of all groups this server handles.
cli@37
    39
	 */
cli@42
    40
	public static List<Channel> getAll() {
cli@37
    41
		try {
cli@37
    42
			return StorageManager.current().getGroups();
cli@37
    43
		} catch (StorageBackendException ex) {
cli@37
    44
			Log.get().severe(ex.getMessage());
cli@37
    45
			return null;
cli@37
    46
		}
cli@37
    47
	}
chris@3
    48
cli@37
    49
	/**
cli@37
    50
	 * @param name
cli@37
    51
	 * @param id
cli@37
    52
	 */
cli@42
    53
	public Group(final String name, final long id, final int flags) {
cli@37
    54
		this.id = id;
cli@37
    55
		this.flags = flags;
cli@37
    56
		this.name = name;
cli@37
    57
	}
chris@3
    58
cli@37
    59
	@Override
cli@42
    60
	public boolean equals(Object obj) {
cli@37
    61
		if (obj instanceof Group) {
cli@37
    62
			return ((Group) obj).id == this.id;
cli@37
    63
		} else {
cli@37
    64
			return false;
cli@37
    65
		}
cli@37
    66
	}
chris@3
    67
cli@37
    68
	public Article getArticle(long idx)
cli@42
    69
			throws StorageBackendException {
cli@37
    70
		return StorageManager.current().getArticle(idx, this.id);
cli@37
    71
	}
chris@3
    72
cli@37
    73
	public List<Pair<Long, ArticleHead>> getArticleHeads(final long first, final long last)
cli@42
    74
			throws StorageBackendException {
cli@37
    75
		return StorageManager.current().getArticleHeads(this, first, last);
cli@37
    76
	}
chris@3
    77
cli@37
    78
	public List<Long> getArticleNumbers()
cli@42
    79
			throws StorageBackendException {
cli@37
    80
		return StorageManager.current().getArticleNumbers(id);
cli@37
    81
	}
chris@3
    82
cli@37
    83
	public long getFirstArticleNumber()
cli@42
    84
			throws StorageBackendException {
cli@37
    85
		return StorageManager.current().getFirstArticleNumber(this);
cli@37
    86
	}
chris@3
    87
cli@42
    88
	public int getFlags() {
cli@37
    89
		return this.flags;
cli@37
    90
	}
chris@3
    91
cli@37
    92
	public long getIndexOf(Article art)
cli@42
    93
			throws StorageBackendException {
cli@37
    94
		return StorageManager.current().getArticleIndex(art, this);
cli@37
    95
	}
chris@3
    96
cli@37
    97
	/**
cli@37
    98
	 * Returns the group id.
cli@37
    99
	 */
cli@42
   100
	public long getInternalID() {
cli@37
   101
		assert id > 0;
chris@3
   102
cli@37
   103
		return id;
cli@37
   104
	}
chris@3
   105
cli@42
   106
	public boolean isDeleted() {
cli@37
   107
		return (this.flags & DELETED) != 0;
cli@37
   108
	}
chris@3
   109
cli@42
   110
	public boolean isMailingList() {
cli@37
   111
		return (this.flags & MAILINGLIST) != 0;
cli@37
   112
	}
chris@3
   113
cli@42
   114
	public boolean isWriteable() {
cli@37
   115
		return true;
cli@37
   116
	}
chris@3
   117
cli@37
   118
	public long getLastArticleNumber()
cli@42
   119
			throws StorageBackendException {
cli@37
   120
		return StorageManager.current().getLastArticleNumber(this);
cli@37
   121
	}
chris@3
   122
cli@42
   123
	public String getName() {
cli@37
   124
		return name;
cli@37
   125
	}
chris@3
   126
cli@37
   127
	/**
cli@37
   128
	 * Performs this.flags |= flag to set a specified flag and updates the data
cli@37
   129
	 * in the JDBCDatabase.
cli@37
   130
	 * @param flag
cli@37
   131
	 */
cli@42
   132
	public void setFlag(final int flag) {
cli@37
   133
		this.flags |= flag;
cli@37
   134
	}
chris@3
   135
cli@42
   136
	public void setName(final String name) {
cli@37
   137
		this.name = name;
cli@37
   138
	}
cli@37
   139
cli@37
   140
	/**
cli@37
   141
	 * @return Number of posted articles in this group.
cli@37
   142
	 * @throws java.sql.SQLException
cli@37
   143
	 */
cli@37
   144
	public long getPostingsCount()
cli@42
   145
			throws StorageBackendException {
cli@37
   146
		return StorageManager.current().getPostingsCount(this.name);
cli@37
   147
	}
cli@37
   148
cli@37
   149
	/**
cli@37
   150
	 * Updates flags and name in the backend.
cli@37
   151
	 */
cli@37
   152
	public void update()
cli@42
   153
			throws StorageBackendException {
cli@37
   154
		StorageManager.current().update(this);
cli@37
   155
	}
chris@3
   156
}