src/org/sonews/storage/Group.java
author cli
Sun, 29 Aug 2010 23:23:15 +0200
changeset 38 fdfc7225f799
parent 37 74139325d305
child 42 7f84f4de2893
permissions -rw-r--r--
Implement JDBCDatabase.update(Article) method to fix issue #7.
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
chris@3
    19
package org.sonews.storage;
chris@3
    20
chris@3
    21
import java.sql.SQLException;
chris@3
    22
import java.util.List;
chris@3
    23
import org.sonews.util.Log;
chris@3
    24
import org.sonews.util.Pair;
chris@3
    25
chris@3
    26
/**
chris@3
    27
 * Represents a logical Group within this newsserver.
chris@3
    28
 * @author Christian Lins
chris@3
    29
 * @since sonews/0.5.0
chris@3
    30
 */
chris@3
    31
// TODO: This class should not be public!
chris@3
    32
public class Group extends Channel
chris@3
    33
{
chris@3
    34
cli@37
    35
	private long id = 0;
cli@37
    36
	private int flags = -1;
cli@37
    37
	private String name = null;
chris@3
    38
cli@37
    39
	/**
cli@37
    40
	 * @return List of all groups this server handles.
cli@37
    41
	 */
cli@37
    42
	public static List<Channel> getAll()
cli@37
    43
	{
cli@37
    44
		try {
cli@37
    45
			return StorageManager.current().getGroups();
cli@37
    46
		} catch (StorageBackendException ex) {
cli@37
    47
			Log.get().severe(ex.getMessage());
cli@37
    48
			return null;
cli@37
    49
		}
cli@37
    50
	}
chris@3
    51
cli@37
    52
	/**
cli@37
    53
	 * @param name
cli@37
    54
	 * @param id
cli@37
    55
	 */
cli@37
    56
	public Group(final String name, final long id, final int flags)
cli@37
    57
	{
cli@37
    58
		this.id = id;
cli@37
    59
		this.flags = flags;
cli@37
    60
		this.name = name;
cli@37
    61
	}
chris@3
    62
cli@37
    63
	@Override
cli@37
    64
	public boolean equals(Object obj)
cli@37
    65
	{
cli@37
    66
		if (obj instanceof Group) {
cli@37
    67
			return ((Group) obj).id == this.id;
cli@37
    68
		} else {
cli@37
    69
			return false;
cli@37
    70
		}
cli@37
    71
	}
chris@3
    72
cli@37
    73
	public Article getArticle(long idx)
cli@37
    74
		throws StorageBackendException
cli@37
    75
	{
cli@37
    76
		return StorageManager.current().getArticle(idx, this.id);
cli@37
    77
	}
chris@3
    78
cli@37
    79
	public List<Pair<Long, ArticleHead>> getArticleHeads(final long first, final long last)
cli@37
    80
		throws StorageBackendException
cli@37
    81
	{
cli@37
    82
		return StorageManager.current().getArticleHeads(this, first, last);
cli@37
    83
	}
chris@3
    84
cli@37
    85
	public List<Long> getArticleNumbers()
cli@37
    86
		throws StorageBackendException
cli@37
    87
	{
cli@37
    88
		return StorageManager.current().getArticleNumbers(id);
cli@37
    89
	}
chris@3
    90
cli@37
    91
	public long getFirstArticleNumber()
cli@37
    92
		throws StorageBackendException
cli@37
    93
	{
cli@37
    94
		return StorageManager.current().getFirstArticleNumber(this);
cli@37
    95
	}
chris@3
    96
cli@37
    97
	public int getFlags()
cli@37
    98
	{
cli@37
    99
		return this.flags;
cli@37
   100
	}
chris@3
   101
cli@37
   102
	public long getIndexOf(Article art)
cli@37
   103
		throws StorageBackendException
cli@37
   104
	{
cli@37
   105
		return StorageManager.current().getArticleIndex(art, this);
cli@37
   106
	}
chris@3
   107
cli@37
   108
	/**
cli@37
   109
	 * Returns the group id.
cli@37
   110
	 */
cli@37
   111
	public long getInternalID()
cli@37
   112
	{
cli@37
   113
		assert id > 0;
chris@3
   114
cli@37
   115
		return id;
cli@37
   116
	}
chris@3
   117
cli@37
   118
	public boolean isDeleted()
cli@37
   119
	{
cli@37
   120
		return (this.flags & DELETED) != 0;
cli@37
   121
	}
chris@3
   122
cli@37
   123
	public boolean isMailingList()
cli@37
   124
	{
cli@37
   125
		return (this.flags & MAILINGLIST) != 0;
cli@37
   126
	}
chris@3
   127
cli@37
   128
	public boolean isWriteable()
cli@37
   129
	{
cli@37
   130
		return true;
cli@37
   131
	}
chris@3
   132
cli@37
   133
	public long getLastArticleNumber()
cli@37
   134
		throws StorageBackendException
cli@37
   135
	{
cli@37
   136
		return StorageManager.current().getLastArticleNumber(this);
cli@37
   137
	}
chris@3
   138
cli@37
   139
	public String getName()
cli@37
   140
	{
cli@37
   141
		return name;
cli@37
   142
	}
chris@3
   143
cli@37
   144
	/**
cli@37
   145
	 * Performs this.flags |= flag to set a specified flag and updates the data
cli@37
   146
	 * in the JDBCDatabase.
cli@37
   147
	 * @param flag
cli@37
   148
	 */
cli@37
   149
	public void setFlag(final int flag)
cli@37
   150
	{
cli@37
   151
		this.flags |= flag;
cli@37
   152
	}
chris@3
   153
cli@37
   154
	public void setName(final String name)
cli@37
   155
	{
cli@37
   156
		this.name = name;
cli@37
   157
	}
cli@37
   158
cli@37
   159
	/**
cli@37
   160
	 * @return Number of posted articles in this group.
cli@37
   161
	 * @throws java.sql.SQLException
cli@37
   162
	 */
cli@37
   163
	public long getPostingsCount()
cli@37
   164
		throws StorageBackendException
cli@37
   165
	{
cli@37
   166
		return StorageManager.current().getPostingsCount(this.name);
cli@37
   167
	}
cli@37
   168
cli@37
   169
	/**
cli@37
   170
	 * Updates flags and name in the backend.
cli@37
   171
	 */
cli@37
   172
	public void update()
cli@37
   173
		throws StorageBackendException
cli@37
   174
	{
cli@37
   175
		StorageManager.current().update(this);
cli@37
   176
	}
chris@3
   177
}